Debian Codegraph @

BambooTracker/gui/instrument_editor/fm_operator_table.cpp

raw
1 /*
2 * Copyright (C) 2018-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 "fm_operator_table.hpp"
27 #include "ui_fm_operator_table.h"
28 #include <QString>
29 #include <QPen>
30 #include <QMenu>
31 #include <QClipboard>
32 #include "gui/event_guard.hpp"
33
34 namespace
35 {
36 constexpr int DT_SIGN_TBL_[8] = { 0, 1, 2, 3, 0, -1, -2, -3 };
37 constexpr int DT_UNSIGN_TBL_[7] = { 7, 6, 5, 0, 1, 2, 3 };
38 }
39
40 FMOperatorTable::FMOperatorTable(QWidget *parent) :
41 QFrame(parent),
42 ui(new Ui::FMOperatorTable),
43 isDTNegative_(false),
44 isIgnoreEvent_(false)
45 {
46 ui->setupUi(this);
47
48 ui->groupBox->setContextMenuPolicy(Qt::CustomContextMenu);
49
50 // Init sliders
51 ui->arSlider->setText("AR");
52 ui->arSlider->setMaximum(31);
53 ui->drSlider->setText("DR");
54 ui->drSlider->setMaximum(31);
55 ui->srSlider->setText("SR");
56 ui->srSlider->setMaximum(31);
57 ui->rrSlider->setText("RR");
58 ui->rrSlider->setMaximum(15);
59 ui->slSlider->setText("SL");
60 ui->slSlider->setMaximum(15);
61 ui->tlSlider->setText("TL");
62 ui->tlSlider->setMaximum(127);
63 ui->ksSlider->setText("KS");
64 ui->ksSlider->setMaximum(3);
65 ui->mlSlider->setText("ML");
66 ui->mlSlider->setMaximum(15);
67 ui->dtSlider->setText("DT");
68 ui->dtSlider->setMaximum(7);
69
70 sliderMap_ = {
71 { Ui::FMOperatorParameter::AR, ui->arSlider },
72 { Ui::FMOperatorParameter::DR, ui->drSlider },
73 { Ui::FMOperatorParameter::SR, ui->srSlider },
74 { Ui::FMOperatorParameter::RR, ui->rrSlider },
75 { Ui::FMOperatorParameter::SL, ui->slSlider },
76 { Ui::FMOperatorParameter::TL, ui->tlSlider },
77 { Ui::FMOperatorParameter::KS, ui->ksSlider },
78 { Ui::FMOperatorParameter::ML, ui->mlSlider },
79 { Ui::FMOperatorParameter::DT, ui->dtSlider }
80 };
81
82 for (auto& pair : sliderMap_) {
83 if (pair.second == ui->dtSlider) {
84 QObject::connect(pair.second, &LabeledVerticalSlider::valueChanged,
85 this, [&](int value) {
86 repaintGraph();
87 if (!isIgnoreEvent_) {
88 if (isDTNegative_) value = DT_UNSIGN_TBL_[value + 3];
89 emit operatorValueChanged(pair.first, value);
90 }
91 });
92 }
93 else {
94 QObject::connect(pair.second, &LabeledVerticalSlider::valueChanged,
95 this, [&](int value) {
96 repaintGraph();
97 if (!isIgnoreEvent_) emit operatorValueChanged(pair.first, value);
98 });
99 }
100 }
101
102 ui->ssgegSlider->setEnabled(false);
103 ui->ssgegSlider->setText(tr("Type"));
104 ui->ssgegSlider->setMaximum(7);
105 QObject::connect(ui->ssgegSlider, &LabeledVerticalSlider::valueChanged,
106 this, [&](int value) {
107 repaintGraph();
108 if (!isIgnoreEvent_) emit operatorValueChanged(Ui::FMOperatorParameter::SSGEG, value);
109 });
110
111 ui->envFrame->viewport()->installEventFilter(this);
112 }
113
114 FMOperatorTable::~FMOperatorTable()
115 {
116 delete ui;
117 }
118
119 void FMOperatorTable::setEnvelopeSetNames(std::vector<QString> list)
120 {
121 envelopeTypes_ = list;
122 }
123
124 void FMOperatorTable::setColorPalette(std::shared_ptr<ColorPalette> palette)
125 {
126 palette_ = palette;
127 }
128
129 void FMOperatorTable::setOperatorNumber(int n)
130 {
131 number_ = n;
132 ui->groupBox->setTitle(tr("Operator %1").arg(n + 1));
133 }
134
135 int FMOperatorTable::operatorNumber() const
136 {
137 return number_;
138 }
139
140 void FMOperatorTable::setValue(Ui::FMOperatorParameter param, int value)
141 {
142 if (param == Ui::FMOperatorParameter::SSGEG) {
143 if (value == -1) {
144 ui->ssgegCheckBox->setChecked(false);
145 }
146 else {
147 ui->ssgegCheckBox->setChecked(true);
148 ui->ssgegSlider->setValue(value);
149 }
150 }
151 else {
152 if (param == Ui::FMOperatorParameter::DT && isDTNegative_) value = DT_SIGN_TBL_[value];
153 sliderMap_.at(param)->setValue(value);
154 }
155 }
156
157 void FMOperatorTable::setGroupEnabled(bool enabled)
158 {
159 Ui::EventGuard eg(isIgnoreEvent_);
160
161 ui->groupBox->setChecked(enabled);
162 }
163
164 void FMOperatorTable::setDTDisplayType(bool useNegative)
165 {
166 if (isDTNegative_ == useNegative) return;
167
168 Ui::EventGuard eg(isIgnoreEvent_);
169 isDTNegative_ = useNegative;
170 if (useNegative) {
171 int dt = DT_SIGN_TBL_[ui->dtSlider->value()];
172 ui->dtSlider->setMinimum(-3);
173 ui->dtSlider->setMaximum(3);
174 ui->dtSlider->setSign(true);
175 ui->dtSlider->setValue(dt);
176 }
177 else {
178 int dt = DT_UNSIGN_TBL_[ui->dtSlider->value() + 3];
179 ui->dtSlider->setMinimum(0);
180 ui->dtSlider->setMaximum(7);
181 ui->dtSlider->setSign(false);
182 ui->dtSlider->setValue(dt);
183 }
184 }
185
186 QString FMOperatorTable::toString() const
187 {
188 int dt = ui->dtSlider->value();
189 if (isDTNegative_) dt = DT_UNSIGN_TBL_[dt + 3];
190
191 auto str = QString("%1,%2,%3,%4,%5,%6,%7,%8,%9,%10")
192 .arg(ui->arSlider->value())
193 .arg(ui->drSlider->value())
194 .arg(ui->srSlider->value())
195 .arg(ui->rrSlider->value())
196 .arg(ui->slSlider->value())
197 .arg(ui->tlSlider->value())
198 .arg(ui->ksSlider->value())
199 .arg(ui->mlSlider->value())
200 .arg(dt)
201 .arg(ui->ssgegCheckBox->isChecked() ? ui->ssgegSlider->value() : -1);
202 return str;
203 }
204
205 bool FMOperatorTable::eventFilter(QObject* obj, QEvent* event)
206 {
207 QWidget * viewport = ui->envFrame->viewport();
208 if (obj == viewport) {
209 if (event->type() == QEvent::Paint) {
210 QPainter painter(viewport);
211 painter.eraseRect(viewport->rect());
212 painter.drawImage(viewport->rect(), envmap_, envmap_.rect());
213
214 // Don't call event->accept(), because isAccepted() is already true.
215 // Return true to stop QAbstractScrollArea from painting(?) on the viewport.
216 return true;
217 }
218 }
219
220 return false;
221 }
222
223 void FMOperatorTable::showEvent(QShowEvent*)
224 {
225 resizeGraph();
226 repaintGraph();
227 }
228
229 void FMOperatorTable::resizeEvent(QResizeEvent*)
230 {
231 resizeGraph();
232 repaintGraph();
233 }
234
235 void FMOperatorTable::resizeGraph()
236 {
237 QWidget * viewport = ui->envFrame->viewport();
238
239 // Making envmap_ a QImage and using an ARGB pixel format ensures that when we fill
240 // it with instFMEnvBackColor (transparent), the image supports transparency.
241 // On Windows, QPixmap instead defaults to Format_RGB32 which has no alpha channel.
242
243 envmap_ = QImage(viewport->size(), QImage::Format_ARGB32_Premultiplied);
244 xr_ = (envmap_.width() - (ENV_LINE_W_ + 1) * 2.) / ENV_W_;
245 yr_ = (envmap_.height() - (ENV_LINE_W_ + 1) * 2.) / ENV_H_;
246 }
247
248 void FMOperatorTable::repaintGraph()
249 {
250 if (!palette_) return;
251
252 QPointF p0, p1, p2, p3, p4;
253 const double marginHorizon = 100;
254 const double marginAr = 50;
255 const double marginDr = 150;
256 const double marginSr = 400;
257
258 if (ui->arSlider->value() && ui->tlSlider->value() < 127) {
259 p1.setY(127 - ui->tlSlider->value());
260 if (ui->arSlider->value() < 31) {
261 double ar = 127. / (marginAr * (31 - ui->arSlider->value()) / 30.);
262 p1.setX(p1.y() / ar);
263 }
264
265 if (ui->drSlider->value()) {
266 p2.setY(p1.y() * (1. - ui->slSlider->value() / 15.));
267 if (ui->drSlider->value() == 31) {
268 p2.setX(p1.x());
269 if (ui->slSlider->value() == 15) {
270 p3 = p2;
271 }
272 else {
273 if (ui->srSlider->value()) {
274 p3.setY(p2.y() / 2.);
275 if (ui->srSlider->value() == 31) {
276 p3.setX(p2.x());
277 }
278 else {
279 double sr = 127. / (marginSr * (31 - ui->srSlider->value()) / 30.);
280 p3.setX(p2.x() + p3.y() / 2. / sr);
281 }
282 }
283 else {
284 p3 = { p2.x() + ((ui->slSlider->value() == 15) ? 0 : marginHorizon), p2.y() };
285 }
286 }
287 }
288 else {
289 double dr = 127. / (marginDr * (31 - ui->drSlider->value()) / 30.);
290 p2.setX(p1.x() + (p1.y() - p2.y()) / dr);
291 if (ui->srSlider->value()) {
292 p3.setY(p2.y() / 2.);
293 if (ui->srSlider->value() == 31) {
294 p3.setX(p2.x());
295 }
296 else {
297 double sr = 127. / (marginSr * (31 - ui->srSlider->value()) / 30.);
298 p3.setX(p2.x() + p3.y() / 2. / sr);
299 }
300 }
301 else {
302 p3 = { p2.x() + ((ui->slSlider->value() == 15) ? 0 : marginHorizon), p2.y() };
303 }
304 }
305 }
306 else {
307 if (ui->slSlider->value()) {
308 p2 = { p1.x() + marginHorizon, p1.y() };
309 p3 = p2;
310 }
311 else {
312 p2 = p1;
313 if (ui->srSlider->value()) {
314 p3.setY(p2.y() / 2.);
315 if (ui->srSlider->value() == 31) {
316 p3.setX(p2.x());
317 }
318 else {
319 double sr = 127. / (marginSr * (31 - ui->srSlider->value()) / 30.);
320 p3.setX(p2.x() + p3.y() / 2. / sr);
321 }
322 }
323 else {
324 p3 = { p2.x() + marginHorizon, p2.y() };
325 }
326 }
327 }
328
329 if (!ui->rrSlider->value()) {
330 p4 = { ENV_W_, p3.y() };
331 }
332 else if (ui->rrSlider->value() == 31) {
333 p4.setX(p3.x());
334 }
335 else {
336 double rr = 127. / (marginAr * (15 - ui->rrSlider->value()) / 14.);
337 p4.setX(p3.x() + p3.y() / rr);
338 }
339 }
340
341 double envHeight = ui->ssgegCheckBox->isChecked() ? (ENV_H_ - SSGEG_H_) : ENV_H_;
342 double envHrate = envHeight / 127.;
343 p0.setY((127. - p0.y()) * envHrate);
344 p1.setY((127. - p1.y()) * envHrate);
345 p2.setY((127. - p2.y()) * envHrate);
346 p3.setY((127. - p3.y()) * envHrate);
347 p4.setY((127. - p4.y()) * envHrate);
348
349 envmap_.fill(palette_->instFMEnvBackColor);
350 QPainter painter(&envmap_);
351
352 painter.setPen(QPen(palette_->instFMEnvGridColor, ENV_LINE_T_));
353 drawLine(painter, p1.x(), 0, p1.x(), envHeight);
354 drawLine(painter, p2.x(), 0, p2.x(), envHeight);
355 drawLine(painter, p3.x(), 0, p3.x(), envHeight);
356
357 painter.setPen(QPen(palette_->instFMEnvLine1Color, ENV_LINE_W_));
358 drawLines(painter, { p0, p1, p2, p3 });
359 painter.setPen(QPen(palette_->instFMEnvLine2Color, ENV_LINE_W_));
360 drawLine(painter, p3, p4);
361
362 if (ui->ssgegCheckBox->isChecked()) {
363 const double seph = envHeight + 1;
364 painter.setPen(QPen(palette_->instFMEnvBorderColor, ENV_LINE_T_));
365 drawLine(painter, 0, seph, ENV_W_, seph);
366
367 const double toph = seph + 2;
368 const double both = ENV_H_;
369 const double horsec = ENV_W_ / 5.;
370 const double dhorsec = horsec * 2;
371 painter.setPen(QPen(palette_->instFMEnvLine3Color, ENV_LINE_W_));
372 switch (ui->ssgegSlider->value()) {
373 case 0:
374 {
375 for (int i = 0; i < 5; ++i) {
376 drawLine(painter, horsec * i, both, horsec * i, toph);
377 drawLine(painter, horsec * i, toph, horsec * (i + 1), both);
378 }
379 }
380 break;
381 case 1:
382 {
383 drawLine(painter, 0, both, 0, toph);
384 drawLine(painter, 0, toph, horsec, both);
385 drawLine(painter, horsec, both, ENV_W_, both);
386 }
387 break;
388 case 2:
389 {
390 drawLine(painter, 0, both, 0, toph);
391 drawLine(painter, 0, toph, horsec, both);
392 for (int i = 0; i < 2; ++i) {
393 drawLine(painter, horsec + dhorsec * i, both, dhorsec + dhorsec * i, toph);
394 drawLine(painter, dhorsec + dhorsec * i, toph, horsec + dhorsec * (i + 1), both);
395 }
396 }
397 break;
398 case 3:
399 {
400 drawLine(painter, 0, both, 0, toph);
401 drawLine(painter, 0, toph, horsec, both);
402 drawLine(painter, horsec, both, horsec, toph);
403 drawLine(painter, horsec, toph, ENV_W_, toph);
404 }
405 break;
406 case 4:
407 {
408 for (int i = 0; i < 5; ++i) {
409 drawLine(painter, horsec * i, both, horsec * (i + 1), toph);
410 drawLine(painter, horsec * (i + 1), toph, horsec * (i + 1), both);
411 }
412 }
413 break;
414 case 5:
415 {
416 drawLine(painter, 0, both, horsec, toph);
417 drawLine(painter, horsec, toph, ENV_W_, toph);
418 }
419 break;
420 case 6:
421 {
422 for (int i = 0; i < 2; ++i) {
423 drawLine(painter, dhorsec * i, both, horsec + dhorsec * i, toph);
424 drawLine(painter, horsec + dhorsec * i, toph, dhorsec * (i + 1), both);
425 }
426 drawLine(painter, dhorsec * 2, both, ENV_W_, toph);
427 }
428 break;
429 case 7:
430 {
431 drawLine(painter, 0, both, horsec, toph);
432 drawLine(painter, horsec, toph, horsec, both);
433 drawLine(painter, horsec, both, ENV_W_, both);
434 }
435 break;
436 }
437 }
438
439 ui->envFrame->viewport()->repaint();
440 }
441
442 void FMOperatorTable::on_ssgegCheckBox_stateChanged(int)
443 {
444 if (ui->ssgegCheckBox->isChecked()) {
445 ui->ssgegSlider->setEnabled(true);
446 if (!isIgnoreEvent_) emit operatorValueChanged(Ui::FMOperatorParameter::SSGEG, ui->ssgegSlider->value());
447 }
448 else {
449 ui->ssgegSlider->setEnabled(false);
450 if (!isIgnoreEvent_) emit operatorValueChanged(Ui::FMOperatorParameter::SSGEG, -1);
451 }
452 repaintGraph();
453 }
454
455 void FMOperatorTable::on_groupBox_toggled(bool arg1)
456 {
457 if (!isIgnoreEvent_) emit operatorEnableChanged(arg1);
458 }
459
460 void FMOperatorTable::on_groupBox_customContextMenuRequested(const QPoint &pos)
461 {
462 QPoint globalPos = ui->groupBox->mapToGlobal(pos);
463 QMenu menu;
464 // Leave Before Qt5.7.0 style due to windows xp
465 QAction* copyEnv = menu.addAction(tr("Copy envelope"));
466 QObject::connect(copyEnv, &QAction::triggered, this, [&] { emit copyEnvelopePressed(); });
467 QAction* pasteEnv = menu.addAction(tr("Paste envelope"));
468 QObject::connect(pasteEnv, &QAction::triggered, this, [&] { emit pasteEnvelopePressed(); });
469 QMenu* pasteFrom = menu.addMenu(tr("Paste envelope From"));
470 for (size_t i = 0; i < envelopeTypes_.size(); ++i) {
471 QAction* act = pasteFrom->addAction(envelopeTypes_[i]);
472 act->setData(static_cast<int>(i));
473 }
474 QObject::connect(pasteFrom, &QMenu::triggered,
475 this, [&](QAction* action) { emit pasteEnvelopeFromPressed(action->data().toInt()); });
476 menu.addSeparator();
477 QAction* copyOp = menu.addAction(tr("Copy operator"));
478 QObject::connect(copyOp, &QAction::triggered, this, [&] { emit copyOperatorPressed(number_); });
479 QAction* pasteOp = menu.addAction(tr("Paste operator"));
480 QObject::connect(pasteOp, &QAction::triggered, this, [&] { emit pasteOperatorPressed(number_); });
481
482 QClipboard* clipboard = QApplication::clipboard();
483 pasteEnv->setEnabled(clipboard->text().startsWith("FM_ENVELOPE:"));
484 pasteOp->setEnabled(clipboard->text().startsWith("FM_OPERATOR:"));
485
486 menu.exec(globalPos);
487 }