| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020 Rerrah |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "swap_instruments_command.hpp" |
| 7 |
|
| 8 |
SwapInstrumentsCommand::SwapInstrumentsCommand(std::weak_ptr<InstrumentsManager> manager, |
| 9 |
std::weak_ptr<Module> mod, int inst1, int inst2, |
| 10 |
int song, bool patternChange) |
| 11 |
: AbstractCommand(CommandId::SwapInstruments), |
| 12 |
manager_(manager), |
| 13 |
mod_(mod), |
| 14 |
inst1Num_(inst1), |
| 15 |
inst2Num_(inst2), |
| 16 |
songNum_(song), |
| 17 |
patternChange_(patternChange) |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
bool SwapInstrumentsCommand::redo() |
| 22 |
{ |
| 23 |
manager_.lock()->swapInstruments(inst1Num_, inst2Num_); |
| 24 |
if (patternChange_) swapInstrumentsInPatterns(); |
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
bool SwapInstrumentsCommand::undo() |
| 29 |
{ |
| 30 |
manager_.lock()->swapInstruments(inst1Num_, inst2Num_); |
| 31 |
if (patternChange_) swapInstrumentsInPatterns(); |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
void SwapInstrumentsCommand::swapInstrumentsInPatterns() |
| 36 |
{ |
| 37 |
// OPTIMIZE: It is too slow! |
| 38 |
for (size_t n = 0; n < mod_.lock()->getSongCount(); ++n) { |
| 39 |
Song& song = mod_.lock()->getSong(static_cast<int>(n)); |
| 40 |
for (const auto& attrib : song.getStyle().trackAttribs) { |
| 41 |
Track& track = song.getTrack(attrib.number); |
| 42 |
for (int i = 0; i < 256; ++i) { // Used track size |
| 43 |
Pattern& pat = track.getPattern(i); |
| 44 |
for (size_t j = 0; j < pat.getSize(); ++j) { |
| 45 |
Step& step = pat.getStep(static_cast<int>(j)); |
| 46 |
if (step.getInstrumentNumber() == inst1Num_) step.setInstrumentNumber(inst2Num_); |
| 47 |
else if (step.getInstrumentNumber() == inst2Num_) step.setInstrumentNumber(inst1Num_); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |