terrain-editor/modules/terrain_editor/edit_history.cpp

38 lines
1 KiB
C++

#include "edit_history.h"
EditHistory *EditHistory::singleton_instance{ nullptr };
void EditHistory::_bind_methods() {
ClassDB::bind_method(D_METHOD("push_action", "do_action", "undo_action"), &self_type::push_action);
ClassDB::bind_method(D_METHOD("undo_last"), &self_type::undo);
ClassDB::bind_method(D_METHOD("redo_last"), &self_type::redo);
}
void EditHistory::push_action(Callable do_action, Callable undo_action) {
if (this->undo_count != 0) {
this->history.resize(this->history.size() - this->undo_count);
this->undo_count = 0;
}
do_action.call();
this->history.push_back({ do_action, undo_action });
}
void EditHistory::undo() {
if (this->undo_count < this->history.size()) {
this->undo_count++;
this->history[this->history.size() - this->undo_count].undo_action.call();
}
}
void EditHistory::redo() {
if (this->undo_count > 0) {
this->history[this->history.size() - this->undo_count].do_action.call();
this->undo_count--;
}
}
void EditHistory::clear_history() {
this->history.clear();
this->undo_count = 0;
}