This commit is contained in:
Erik Selecký 2021-11-10 16:46:21 +01:00 committed by GitHub
commit b6e16f1e40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 50 deletions

View file

@ -33,27 +33,21 @@
#include "core/os/os.h"
#include "core/resource.h"
UndoRedo::Operation::~Operation() {
if (type == Operation::TYPE_REFERENCE) {
if (!ref.is_valid()) {
if (Object *obj = ObjectDB::get_instance(object)) {
memdelete(obj);
}
}
}
}
void UndoRedo::_discard_redo() {
if (current_action == actions.size() - 1) {
return;
}
for (int i = current_action + 1; i < actions.size(); i++) {
for (List<Operation>::Element *E = actions.write[i].do_ops.front(); E; E = E->next()) {
if (E->get().type == Operation::TYPE_REFERENCE) {
if (E->get().ref.is_valid()) {
E->get().ref.unref();
} else {
Object *obj = ObjectDB::get_instance(E->get().object);
if (obj) {
memdelete(obj);
}
}
}
}
//ERASE do data
}
actions.resize(current_action + 1);
}
@ -68,21 +62,7 @@ void UndoRedo::create_action(const String &p_name, MergeMode p_mode) {
current_action = actions.size() - 2;
if (p_mode == MERGE_ENDS) {
// Clear all do ops from last action, and delete all object references
List<Operation>::Element *E = actions.write[current_action + 1].do_ops.front();
while (E) {
if (E->get().type == Operation::TYPE_REFERENCE) {
Object *obj = ObjectDB::get_instance(E->get().object);
if (obj) {
memdelete(obj);
}
}
E = E->next();
actions.write[current_action + 1].do_ops.pop_front();
}
actions.write[current_action + 1].do_ops.clear();
}
actions.write[actions.size() - 1].last_tick = ticks;
@ -223,19 +203,6 @@ void UndoRedo::_pop_history_tail() {
return;
}
for (List<Operation>::Element *E = actions.write[0].undo_ops.front(); E; E = E->next()) {
if (E->get().type == Operation::TYPE_REFERENCE) {
if (E->get().ref.is_valid()) {
E->get().ref.unref();
} else {
Object *obj = ObjectDB::get_instance(E->get().object);
if (obj) {
memdelete(obj);
}
}
}
}
actions.remove(0);
if (current_action >= 0) {
current_action--;
@ -250,7 +217,7 @@ void UndoRedo::commit_action() {
ERR_FAIL_COND(action_level <= 0);
action_level--;
if (action_level > 0) {
return; //still nested
return; // still nested
}
if (merging) {
@ -271,7 +238,7 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
Operation &op = E->get();
Object *obj = ObjectDB::get_instance(op.object);
if (!obj) { //may have been deleted and this is fine
if (!obj) { // may have been deleted and this is fine
continue;
}
@ -320,8 +287,12 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
}
} break;
case Operation::TYPE_REFERENCE: {
//do nothing
// do nothing
} break;
default: {
ERR_PRINT("Invalid enum value of type UndoRedo::Operation::Type. Please report this as bug.");
}
}
}
}
@ -330,7 +301,7 @@ bool UndoRedo::redo() {
ERR_FAIL_COND_V(action_level > 0, false);
if ((current_action + 1) >= actions.size()) {
return false; //nothing to redo
return false; // nothing to redo
}
current_action++;
@ -345,7 +316,7 @@ bool UndoRedo::redo() {
bool UndoRedo::undo() {
ERR_FAIL_COND_V(action_level > 0, false);
if (current_action < 0) {
return false; //nothing to redo
return false; // nothing to redo
}
_process_operation_list(actions.write[current_action].undo_ops.front());
current_action--;

View file

@ -53,17 +53,21 @@ public:
private:
struct Operation {
public:
enum Type {
TYPE_INVALID,
TYPE_METHOD,
TYPE_PROPERTY,
TYPE_REFERENCE
};
Type type;
Type type = TYPE_INVALID;
Ref<Reference> ref;
ObjectID object;
String name;
Variant args[VARIANT_ARG_MAX];
~Operation();
};
struct Action {