diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2020-05-14 23:09:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-14 23:09:03 +0200 |
commit | 00949f0c5fcc6a4f8382a4a97d5591fd9ec380f8 (patch) | |
tree | 2b1c31f45add24085b64425ce440f577424c16a1 /core/undo_redo.cpp | |
parent | 5046f666a1181675b39f156c38346525dc1c444e (diff) | |
parent | 0ee0fa42e6639b6fa474b7cf6afc6b1a78142185 (diff) |
Merge pull request #38738 from akien-mga/cause-we-never-go-out-of-style
Style: Remove new line at block start, enforce line between functions, enforce braces in if and loop blocks
Diffstat (limited to 'core/undo_redo.cpp')
-rw-r--r-- | core/undo_redo.cpp | 112 |
1 files changed, 48 insertions, 64 deletions
diff --git a/core/undo_redo.cpp b/core/undo_redo.cpp index 9324dfb573..90750f2c6e 100644 --- a/core/undo_redo.cpp +++ b/core/undo_redo.cpp @@ -33,19 +33,17 @@ #include "core/os/os.h" void UndoRedo::_discard_redo() { - - if (current_action == actions.size() - 1) + 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) { - Object *obj = ObjectDB::get_instance(E->get().object); - if (obj) + if (obj) { memdelete(obj); + } } } //ERASE do data @@ -55,31 +53,26 @@ void UndoRedo::_discard_redo() { } void UndoRedo::create_action(const String &p_name, MergeMode p_mode) { - uint32_t ticks = OS::get_singleton()->get_ticks_msec(); if (action_level == 0) { - _discard_redo(); // Check if the merge operation is valid if (p_mode != MERGE_DISABLE && actions.size() && actions[actions.size() - 1].name == p_name && actions[actions.size() - 1].last_tick + 800 > ticks) { - 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) + if (obj) { memdelete(obj); + } } E = E->next(); @@ -92,7 +85,6 @@ void UndoRedo::create_action(const String &p_name, MergeMode p_mode) { merge_mode = p_mode; merging = true; } else { - Action new_action; new_action.name = p_name; new_action.last_tick = ticks; @@ -106,15 +98,15 @@ void UndoRedo::create_action(const String &p_name, MergeMode p_mode) { } void UndoRedo::add_do_method(Object *p_object, const StringName &p_method, VARIANT_ARG_DECLARE) { - VARIANT_ARGPTRS ERR_FAIL_COND(p_object == nullptr); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); Operation do_op; do_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) + if (Object::cast_to<Resource>(p_object)) { do_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + } do_op.type = Operation::TYPE_METHOD; do_op.name = p_method; @@ -126,20 +118,21 @@ void UndoRedo::add_do_method(Object *p_object, const StringName &p_method, VARIA } void UndoRedo::add_undo_method(Object *p_object, const StringName &p_method, VARIANT_ARG_DECLARE) { - VARIANT_ARGPTRS ERR_FAIL_COND(p_object == nullptr); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); // No undo if the merge mode is MERGE_ENDS - if (merge_mode == MERGE_ENDS) + if (merge_mode == MERGE_ENDS) { return; + } Operation undo_op; undo_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) + if (Object::cast_to<Resource>(p_object)) { undo_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + } undo_op.type = Operation::TYPE_METHOD; undo_op.name = p_method; @@ -149,87 +142,92 @@ void UndoRedo::add_undo_method(Object *p_object, const StringName &p_method, VAR } actions.write[current_action + 1].undo_ops.push_back(undo_op); } -void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) { +void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) { ERR_FAIL_COND(p_object == nullptr); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); Operation do_op; do_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) + if (Object::cast_to<Resource>(p_object)) { do_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + } do_op.type = Operation::TYPE_PROPERTY; do_op.name = p_property; do_op.args[0] = p_value; actions.write[current_action + 1].do_ops.push_back(do_op); } -void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) { +void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) { ERR_FAIL_COND(p_object == nullptr); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); // No undo if the merge mode is MERGE_ENDS - if (merge_mode == MERGE_ENDS) + if (merge_mode == MERGE_ENDS) { return; + } Operation undo_op; undo_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) + if (Object::cast_to<Resource>(p_object)) { undo_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + } undo_op.type = Operation::TYPE_PROPERTY; undo_op.name = p_property; undo_op.args[0] = p_value; actions.write[current_action + 1].undo_ops.push_back(undo_op); } -void UndoRedo::add_do_reference(Object *p_object) { +void UndoRedo::add_do_reference(Object *p_object) { ERR_FAIL_COND(p_object == nullptr); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); Operation do_op; do_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) + if (Object::cast_to<Resource>(p_object)) { do_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + } do_op.type = Operation::TYPE_REFERENCE; actions.write[current_action + 1].do_ops.push_back(do_op); } -void UndoRedo::add_undo_reference(Object *p_object) { +void UndoRedo::add_undo_reference(Object *p_object) { ERR_FAIL_COND(p_object == nullptr); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); // No undo if the merge mode is MERGE_ENDS - if (merge_mode == MERGE_ENDS) + if (merge_mode == MERGE_ENDS) { return; + } Operation undo_op; undo_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) + if (Object::cast_to<Resource>(p_object)) { undo_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + } undo_op.type = Operation::TYPE_REFERENCE; actions.write[current_action + 1].undo_ops.push_back(undo_op); } void UndoRedo::_pop_history_tail() { - _discard_redo(); - if (!actions.size()) + if (!actions.size()) { return; + } for (List<Operation>::Element *E = actions.write[0].undo_ops.front(); E; E = E->next()) { - if (E->get().type == Operation::TYPE_REFERENCE) { - Object *obj = ObjectDB::get_instance(E->get().object); - if (obj) + if (obj) { memdelete(obj); + } } } @@ -244,11 +242,11 @@ bool UndoRedo::is_committing_action() const { } void UndoRedo::commit_action() { - ERR_FAIL_COND(action_level <= 0); action_level--; - if (action_level > 0) + if (action_level > 0) { return; //still nested + } if (merging) { version--; @@ -264,19 +262,16 @@ void UndoRedo::commit_action() { } void UndoRedo::_process_operation_list(List<Operation>::Element *E) { - for (; E; E = E->next()) { - 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; + } switch (op.type) { - case Operation::TYPE_METHOD: { - Vector<const Variant *> argptrs; argptrs.resize(VARIANT_ARG_MAX); int argc = 0; @@ -297,8 +292,9 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) { } #ifdef TOOLS_ENABLED Resource *res = Object::cast_to<Resource>(obj); - if (res) + if (res) { res->set_edited(true); + } #endif @@ -307,12 +303,12 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) { } } break; case Operation::TYPE_PROPERTY: { - obj->set(op.name, op.args[0]); #ifdef TOOLS_ENABLED Resource *res = Object::cast_to<Resource>(obj); - if (res) + if (res) { res->set_edited(true); + } #endif if (property_callback) { property_callback(prop_callback_ud, obj, op.name, op.args[0]); @@ -326,11 +322,11 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) { } bool UndoRedo::redo() { - ERR_FAIL_COND_V(action_level > 0, false); - if ((current_action + 1) >= actions.size()) + if ((current_action + 1) >= actions.size()) { return false; //nothing to redo + } current_action++; @@ -342,10 +338,10 @@ bool UndoRedo::redo() { } bool UndoRedo::undo() { - ERR_FAIL_COND_V(action_level > 0, false); - if (current_action < 0) + if (current_action < 0) { return false; //nothing to redo + } _process_operation_list(actions.write[current_action].undo_ops.front()); current_action--; version--; @@ -355,12 +351,12 @@ bool UndoRedo::undo() { } void UndoRedo::clear_history(bool p_increase_version) { - ERR_FAIL_COND(action_level > 0); _discard_redo(); - while (actions.size()) + while (actions.size()) { _pop_history_tail(); + } if (p_increase_version) { version++; @@ -369,53 +365,45 @@ void UndoRedo::clear_history(bool p_increase_version) { } String UndoRedo::get_current_action_name() const { - ERR_FAIL_COND_V(action_level > 0, ""); - if (current_action < 0) + if (current_action < 0) { return ""; + } return actions[current_action].name; } bool UndoRedo::has_undo() { - return current_action >= 0; } bool UndoRedo::has_redo() { - return (current_action + 1) < actions.size(); } uint64_t UndoRedo::get_version() const { - return version; } void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) { - callback = p_callback; callback_ud = p_ud; } void UndoRedo::set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud) { - method_callback = p_method_callback; method_callbck_ud = p_ud; } void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud) { - property_callback = p_property_callback; prop_callback_ud = p_ud; } UndoRedo::~UndoRedo() { - clear_history(); } Variant UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 2) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 0; @@ -444,7 +432,6 @@ Variant UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callabl Variant v[VARIANT_ARG_MAX]; for (int i = 0; i < MIN(VARIANT_ARG_MAX, p_argcount - 2); ++i) { - v[i] = *p_args[i + 2]; } @@ -453,7 +440,6 @@ Variant UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callabl } Variant UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 2) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 0; @@ -482,7 +468,6 @@ Variant UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Calla Variant v[VARIANT_ARG_MAX]; for (int i = 0; i < MIN(VARIANT_ARG_MAX, p_argcount - 2); ++i) { - v[i] = *p_args[i + 2]; } @@ -491,7 +476,6 @@ Variant UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Calla } void UndoRedo::_bind_methods() { - ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE)); ClassDB::bind_method(D_METHOD("commit_action"), &UndoRedo::commit_action); ClassDB::bind_method(D_METHOD("is_committing_action"), &UndoRedo::is_committing_action); |