| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020 Rerrah |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "paste_insert_copied_data_to_pattern_command.hpp" |
| 7 |
#include <algorithm> |
| 8 |
#include <iterator> |
| 9 |
#include "pattern_command_utils.hpp" |
| 10 |
|
| 11 |
PasteInsertCopiedDataToPatternCommand::PasteInsertCopiedDataToPatternCommand( |
| 12 |
std::weak_ptr<Module> mod, int songNum, int beginTrack, int beginColumn, |
| 13 |
int beginOrder, int beginStep, const Vector2d<std::string>& cells) |
| 14 |
: AbstractCommand(CommandId::PasteInsertCopiedDataToPattern), |
| 15 |
mod_(mod), |
| 16 |
song_(songNum), |
| 17 |
track_(beginTrack), |
| 18 |
col_(beginColumn), |
| 19 |
order_(beginOrder), |
| 20 |
step_(beginStep) |
| 21 |
{ |
| 22 |
auto& song = mod.lock()->getSong(songNum); |
| 23 |
size_t newStepSize = song.getTrack(track_).getPatternFromOrderNumber(order_).getSize() - step_; |
| 24 |
prevCells_ = command_utils::getPreviousCells(song, cells.columnSize(), newStepSize, |
| 25 |
beginTrack, beginColumn, beginOrder, beginStep); |
| 26 |
|
| 27 |
Vector2d<std::string> newCells(newStepSize, cells.columnSize()); |
| 28 |
std::size_t shiftedRowIndex = std::min(cells.rowSize(), newStepSize); |
| 29 |
std::copy_n(cells.begin(), shiftedRowIndex, newCells.begin()); |
| 30 |
std::copy_n(prevCells_.begin(), newStepSize - shiftedRowIndex, newCells.begin() + shiftedRowIndex); |
| 31 |
cells_ = newCells; |
| 32 |
} |
| 33 |
|
| 34 |
bool PasteInsertCopiedDataToPatternCommand::redo() |
| 35 |
{ |
| 36 |
try { |
| 37 |
command_utils::restorePattern(mod_.lock()->getSong(song_), cells_, track_, col_, order_, step_); |
| 38 |
return true; |
| 39 |
} |
| 40 |
catch (...) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
bool PasteInsertCopiedDataToPatternCommand::undo() |
| 47 |
{ |
| 48 |
try { |
| 49 |
command_utils::restorePattern(mod_.lock()->getSong(song_), prevCells_, track_, col_, order_, step_); |
| 50 |
return true; |
| 51 |
} |
| 52 |
catch (...) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
} |