Debian Codegraph @

BambooTracker/gui/instrument_editor/tone_noise_macro_editor.cpp

raw
1 /*
2 * Copyright (C) 2019-2021 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "tone_noise_macro_editor.hpp"
27 #include <QPainter>
28 #include <QRegularExpression>
29
30 ToneNoiseMacroEditor::ToneNoiseMacroEditor(QWidget *parent)
31 : VisualizedInstrumentMacroEditor(parent)
32 {
33 setMaximumDisplayedRowCount(16);
34 setDefaultRow(0);
35 AddRow(tr("Tone"), false);
36 AddRow(tr("Noise"), false);
37 for (int i = 0; i < 32; ++i) {
38 AddRow(QString::number(i), false);
39 }
40 autoFitLabelWidth();
41 }
42
43 ToneNoiseMacroEditor::~ToneNoiseMacroEditor() = default;
44
45 void ToneNoiseMacroEditor::drawField()
46 {
47 QPainter painter(&pixmap_);
48 painter.setFont(font_);
49
50 int dispCnt = getDisplayedRowCount();
51 int textOffset = fontAscend_ - fontHeight_ + fontLeading_ / 2;
52
53 // Backgtound
54 // Tone
55 int thi = dispCnt - 1;
56 int ty = std::accumulate(rowHeights_.begin(), rowHeights_.begin() + thi, 0);
57 painter.fillRect(0, ty, panelWidth(), rowHeights_[static_cast<size_t>(thi)], palette_->tnToneBackColor);
58 // Noise
59 int nhi = dispCnt - 2;
60 int ny = std::accumulate(rowHeights_.begin(), rowHeights_.begin() + nhi, 0);
61 painter.fillRect(0, ny, panelWidth(), rowHeights_[static_cast<size_t>(nhi)], palette_->tnNoiseBackColor);
62
63 // Row label
64 painter.setPen(palette_->instSeqTagColor);
65 if (isLabelOmitted_ && !labels_.empty()) {
66 painter.drawText(1,
67 rowHeights_.front() + textOffset,
68 labels_[static_cast<size_t>(upperRow_)]);
69 int c = dispCnt / 2;
70 painter.drawText(1,
71 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + c + 1, 0) + textOffset,
72 labels_[static_cast<size_t>(upperRow_ - c)]);
73 int l = dispCnt - 3;
74 painter.drawText(1,
75 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + l + 1, 0) + textOffset,
76 labels_[static_cast<size_t>(upperRow_ - l)]);
77 }
78 else {
79 int l = dispCnt - 2;
80 for (int i = 0; i < l; ++i) {
81 painter.drawText(1,
82 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + i + 1, 0) + textOffset,
83 labels_[static_cast<size_t>(upperRow_ - i)]);
84 }
85 }
86 // Noise
87 painter.setPen(palette_->tnNoiseTextColor);
88 int n = dispCnt - 2;
89 painter.drawText(1,
90 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + n + 1, 0) + textOffset,
91 labels_[static_cast<size_t>(1)]);
92 // Tone
93 painter.setPen(palette_->tnToneTextColor);
94 int t = dispCnt - 1;
95 painter.drawText(1,
96 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + t + 1, 0) + textOffset,
97 labels_[static_cast<size_t>(0)]);
98
99 for (size_t i = 1; i < cols_.size(); i += 2) {
100 painter.fillRect(labWidth_
101 + std::accumulate(colWidths_.begin(), colWidths_.begin() + static_cast<int>(i), 0),
102 0,
103 colWidths_[i],
104 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + dispCnt - 2, 0),
105 palette_->instSeqOddColColor);
106 }
107
108 // Sequence
109 for (size_t i = 0; i < cols_.size(); ++i) {
110 int v = cols_[i].row;
111 int x = labWidth_ + std::accumulate(colWidths_.begin(), colWidths_.begin() + static_cast<int>(i), 0);
112 if (!v || (32 < v && v < 65)) { // Tone
113 painter.fillRect(x, ty, colWidths_[i], rowHeights_[static_cast<size_t>(thi)], palette_->tnToneCellColor);
114 }
115 if (0 < v && v < 65) {
116 // Noise
117 painter.fillRect(x, ny, colWidths_[i], rowHeights_[static_cast<size_t>(nhi)], palette_->tnNoiseCellColor);
118
119 // Noise period
120 int r = (v - 1) % 32 + 2;
121 if (upperRow_ >= r && r > upperRow_ - dispCnt + 2) {
122 int hi = upperRow_ - r;
123 int y = std::accumulate(rowHeights_.begin(), rowHeights_.begin() + hi, 0);
124 painter.fillRect(x, y, colWidths_[i], rowHeights_[static_cast<size_t>(hi)], palette_->instSeqCellColor);
125 }
126 }
127 }
128
129 if (hovCol_ >= 0 && hovRow_ >= 0) {
130 painter.fillRect(labWidth_ + std::accumulate(colWidths_.begin(), colWidths_.begin() + hovCol_, 0),
131 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + hovRow_, 0),
132 colWidths_[static_cast<size_t>(hovCol_)], rowHeights_[static_cast<size_t>(hovRow_)],
133 palette_->instSeqHovColor);
134 }
135 }
136
137 int ToneNoiseMacroEditor::detectRowNumberForMouseEvent(int col, int internalRow) const
138 {
139 int r = upperRow_ - internalRow;
140 int b = upperRow_ - getDisplayedRowCount() + 1;
141 int v = cols_.at(static_cast<size_t>(col)).row;
142
143 if (r == b) { // Tone
144 if (!v) return 65; // Tone to None
145 else if (v == 65) return 0; // None to Tone
146 else if (v < 33) return v + 32; // Noise to Tone+Noise
147 else return v - 32; // Tone+Noise to Noise
148 }
149 else if (r == b + 1) { // Noise
150 if (!v) return 33; // Tone to Tone+Noise(0)
151 else if (v == 65) return 1; // None to Noise(0)
152 else if (v < 33) return 65; // Noise to None
153 else return 0; // Tone+Noise to Tone
154 }
155 else { // Noise period
156 r -= 2; // Set noise period
157 if (0 < v && v < 33) return 1 + r; // Noise
158 else return 33 + r; // Tone+Noise
159 }
160 }
161
162 int ToneNoiseMacroEditor::maxInMML() const
163 {
164 return 66;
165 }
166
167 QString ToneNoiseMacroEditor::convertSequenceDataUnitToMML(Column col)
168 {
169 if (col.row == 0) return "t";
170 else {
171 int p = (col.row - 1) % 32;
172 if (col.row == 65) return "u";
173 else if (col.row < 33) return QString("%1n").arg(p);
174 else return QString("%1tn").arg(p);
175 }
176 }
177
178 bool ToneNoiseMacroEditor::interpretDataInMML(QString &text, int &cnt, std::vector<Column> &column)
179 {
180 // Tone+Noise
181 QRegularExpressionMatch m = QRegularExpression("^(\\d+)(nt|tn)").match(text);
182 if (m.hasMatch()) {
183 int p = m.captured(1).toInt();
184 if (p < 0 || 31 < p) return false;
185 column.push_back({ 33 + p, -1, "" });
186 ++cnt;
187 text.remove(QRegularExpression("^\\d+(nt|tn)"));
188 return true;
189 }
190
191 // Noise
192 m = QRegularExpression("^(\\d+)n").match(text);
193 if (m.hasMatch()) {
194 int p = m.captured(1).toInt();
195 if (p < 0 || 31 < p) return false;
196 column.push_back({ 1 + p, -1, "" });
197 ++cnt;
198 text.remove(QRegularExpression("^\\d+n"));
199 return true;
200 }
201
202 // Tone
203 m = QRegularExpression(R"(^(\d+)?t)").match(text);
204 if (m.hasMatch()) {
205 column.push_back({ 0, -1, "" });
206 ++cnt;
207 text.remove(QRegularExpression("^(\\d+)?t"));
208 return true;
209 }
210
211 // None
212 if (text.startsWith("u")) {
213 column.push_back({ 65, -1, "" });
214 ++cnt;
215 text.remove(0, 1);
216 return true;
217 }
218
219 return false;
220 }