Debian Codegraph @

BambooTracker/command/pattern/paste_mix_copied_data_to_pattern_command.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2018 Rerrah
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "paste_mix_copied_data_to_pattern_command.hpp"
7 #include "pattern_command_utils.hpp"
8
9 PasteMixCopiedDataToPatternCommand::PasteMixCopiedDataToPatternCommand(
10 std::weak_ptr<Module> mod, int songNum, int beginTrack, int beginColumn,
11 int beginOrder, int beginStep, const Vector2d<std::string>& cells)
12 : AbstractCommand(CommandId::PasteMixCopiedDataToPattern),
13 mod_(mod),
14 song_(songNum),
15 track_(beginTrack),
16 col_(beginColumn),
17 order_(beginOrder),
18 step_(beginStep),
19 cells_(cells)
20 {
21 auto& song = mod.lock()->getSong(songNum);
22 prevCells_ = command_utils::getPreviousCells(song, cells.columnSize(), cells.rowSize(),
23 beginTrack, beginColumn, beginOrder, beginStep);
24 }
25
26 bool PasteMixCopiedDataToPatternCommand::redo()
27 {
28 try {
29 auto& song = mod_.lock()->getSong(song_);
30
31 int stepIndex = step_;
32 int orderIndex = order_;
33
34 for (const auto& row : cells_) {
35 int trackIndex = track_;
36 int columnIndex = col_;
37
38 for (const std::string& cell : row) {
39 if (static_cast<std::size_t>(stepIndex) >= song.getTrack(trackIndex).getPatternFromOrderNumber(orderIndex).getSize()) {
40 if (static_cast<std::size_t>(++orderIndex) < song.getTrack(trackIndex).getOrderSize()) {
41 stepIndex = 0;
42 }
43 else {
44 return true;
45 }
46 }
47
48 Step& step = command_utils::getStep(song, trackIndex, orderIndex, stepIndex);
49 switch (columnIndex) {
50 case 0: {
51 int n = std::stoi(cell);
52 if (!Step::testEmptyNote(n) && step.isEmptyNote()) {
53 step.setNoteNumber(n);
54 }
55 break;
56 }
57
58 case 1: {
59 int n = std::stoi(cell);
60 if (!Step::testEmptyInstrument(n) && !step.hasInstrument()) {
61 step.setInstrumentNumber(n);
62 }
63 break;
64 }
65
66 case 2: {
67 int volume = std::stoi(cell);
68 if (!Step::testEmptyVolume(volume) && !step.hasVolume()) {
69 step.setVolume(volume);
70 }
71 break;
72 }
73
74 default: {
75 int effectColumnIndex = columnIndex - 3;
76 int effectNumber = effectColumnIndex / 2;
77 if (effectColumnIndex % 2) {
78 // Effect value column.
79 int value = std::stoi(cell);
80 if (!Step::testEmptyEffectValue(value) && !step.hasEffectValue(effectNumber)) {
81 step.setEffectValue(effectNumber, value);
82 }
83 }
84 else {
85 // Effect ID column.
86 if (!Step::testEmptyEffectId(cell) && !step.hasEffectId(effectNumber)) {
87 step.setEffectId(effectNumber, cell);
88 }
89 }
90 break;
91 }
92 }
93
94 ++columnIndex;
95 trackIndex += (columnIndex / Step::N_COLUMN);
96 columnIndex %= Step::N_COLUMN;
97 }
98
99 ++stepIndex;
100 }
101
102 return true;
103 }
104 catch (...) {
105 return false;
106 }
107 }
108
109 bool PasteMixCopiedDataToPatternCommand::undo()
110 {
111 try {
112 command_utils::restorePattern(mod_.lock()->getSong(song_), prevCells_, track_, col_, order_, step_);
113 return true;
114 }
115 catch (...) {
116 return false;
117 }
118 }