From b3997191d8b750b845294b49b08f327e7679d509 Mon Sep 17 00:00:00 2001 From: kobewi Date: Sun, 18 Sep 2022 23:27:00 +0200 Subject: [PATCH] Change UndoRedo to use Callables --- core/object/undo_redo.cpp | 160 +++++++++------------------- core/object/undo_redo.h | 34 +----- doc/classes/UndoRedo.xml | 14 ++- editor/editor_undo_redo_manager.cpp | 4 +- 4 files changed, 61 insertions(+), 151 deletions(-) diff --git a/core/object/undo_redo.cpp b/core/object/undo_redo.cpp index d3c48853f134..aa66e86bc0bc 100644 --- a/core/object/undo_redo.cpp +++ b/core/object/undo_redo.cpp @@ -126,27 +126,28 @@ void UndoRedo::create_action(const String &p_name, MergeMode p_mode) { force_keep_in_merge_ends = false; } -void UndoRedo::add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) { - ERR_FAIL_COND(p_object == nullptr); +void UndoRedo::add_do_method(const Callable &p_callable) { + ERR_FAIL_COND(p_callable.is_null()); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); + + Object *object = p_callable.get_object(); + ERR_FAIL_NULL(object); + Operation do_op; - do_op.object = p_object->get_instance_id(); - if (Object::cast_to(p_object)) { - do_op.ref = Ref(Object::cast_to(p_object)); + do_op.callable = p_callable; + do_op.object = p_callable.get_object_id(); + if (Object::cast_to(object)) { + do_op.ref = Ref(Object::cast_to(object)); } - do_op.type = Operation::TYPE_METHOD; - do_op.name = p_method; + do_op.name = p_callable.get_method(); - for (int i = 0; i < p_argcount; i++) { - do_op.args.push_back(*p_args[i]); - } actions.write[current_action + 1].do_ops.push_back(do_op); } -void UndoRedo::add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) { - ERR_FAIL_COND(p_object == nullptr); +void UndoRedo::add_undo_method(const Callable &p_callable) { + ERR_FAIL_COND(p_callable.is_null()); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); @@ -155,19 +156,19 @@ void UndoRedo::add_undo_methodp(Object *p_object, const StringName &p_method, co return; } + Object *object = p_callable.get_object(); + ERR_FAIL_NULL(object); + Operation undo_op; - undo_op.object = p_object->get_instance_id(); - if (Object::cast_to(p_object)) { - undo_op.ref = Ref(Object::cast_to(p_object)); + undo_op.callable = p_callable; + undo_op.object = p_callable.get_object_id(); + if (Object::cast_to(object)) { + undo_op.ref = Ref(Object::cast_to(object)); } - undo_op.type = Operation::TYPE_METHOD; undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends; - undo_op.name = p_method; + undo_op.name = p_callable.get_method(); - for (int i = 0; i < p_argcount; i++) { - undo_op.args.push_back(*p_args[i]); - } actions.write[current_action + 1].undo_ops.push_back(undo_op); } @@ -183,7 +184,7 @@ void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, c do_op.type = Operation::TYPE_PROPERTY; do_op.name = p_property; - do_op.args.push_back(p_value); + do_op.value = p_value; actions.write[current_action + 1].do_ops.push_back(do_op); } @@ -206,7 +207,7 @@ void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, undo_op.type = Operation::TYPE_PROPERTY; undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends; undo_op.name = p_property; - undo_op.args.push_back(p_value); + undo_op.value = p_value; actions.write[current_action + 1].undo_ops.push_back(undo_op); } @@ -312,33 +313,42 @@ void UndoRedo::_process_operation_list(List::Element *E) { switch (op.type) { case Operation::TYPE_METHOD: { - int argc = op.args.size(); - Vector argptrs; - argptrs.resize(argc); - - for (int i = 0; i < argc; i++) { - argptrs.write[i] = &op.args[i]; - } - Callable::CallError ce; - obj->callp(op.name, (const Variant **)argptrs.ptr(), argc, ce); + Variant ret; + op.callable.callp(nullptr, 0, ret, ce); if (ce.error != Callable::CallError::CALL_OK) { - ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, (const Variant **)argptrs.ptr(), argc, ce)); + ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, nullptr, 0, ce)); } #ifdef TOOLS_ENABLED Resource *res = Object::cast_to(obj); if (res) { res->set_edited(true); } - #endif if (method_callback) { - method_callback(method_callback_ud, obj, op.name, (const Variant **)argptrs.ptr(), argc); + Vector binds; + if (op.callable.is_custom()) { + CallableCustomBind *ccb = dynamic_cast(op.callable.get_custom()); + if (ccb) { + binds = ccb->get_binds(); + } + } + + if (binds.is_empty()) { + method_callback(method_callback_ud, obj, op.name, nullptr, 0); + } else { + const Variant **args = (const Variant **)alloca(sizeof(const Variant **) * binds.size()); + for (int i = 0; i < binds.size(); i++) { + args[i] = (const Variant *)&binds[i]; + } + + method_callback(method_callback_ud, obj, op.name, args, binds.size()); + } } } break; case Operation::TYPE_PROPERTY: { - obj->set(op.name, op.args[0]); + obj->set(op.name, op.value); #ifdef TOOLS_ENABLED Resource *res = Object::cast_to(obj); if (res) { @@ -346,7 +356,7 @@ void UndoRedo::_process_operation_list(List::Element *E) { } #endif if (property_callback) { - property_callback(prop_callback_ud, obj, op.name, op.args[0]); + property_callback(prop_callback_ud, obj, op.name, op.value); } } break; case Operation::TYPE_REFERENCE: { @@ -444,87 +454,13 @@ UndoRedo::~UndoRedo() { clear_history(); } -void 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; - return; - } - - if (p_args[0]->get_type() != Variant::OBJECT) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::OBJECT; - return; - } - - if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 1; - r_error.expected = Variant::STRING_NAME; - return; - } - - r_error.error = Callable::CallError::CALL_OK; - - Object *object = *p_args[0]; - StringName method = *p_args[1]; - - add_do_methodp(object, method, p_args + 2, p_argcount - 2); -} - -void 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; - return; - } - - if (p_args[0]->get_type() != Variant::OBJECT) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::OBJECT; - return; - } - - if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 1; - r_error.expected = Variant::STRING_NAME; - return; - } - - r_error.error = Callable::CallError::CALL_OK; - - Object *object = *p_args[0]; - StringName method = *p_args[1]; - - add_undo_methodp(object, method, p_args + 2, p_argcount - 2); -} - 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", "execute"), &UndoRedo::commit_action, DEFVAL(true)); ClassDB::bind_method(D_METHOD("is_committing_action"), &UndoRedo::is_committing_action); - { - MethodInfo mi; - mi.name = "add_do_method"; - mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object")); - mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method")); - - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &UndoRedo::_add_do_method, mi, varray(), false); - } - - { - MethodInfo mi; - mi.name = "add_undo_method"; - mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object")); - mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method")); - - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &UndoRedo::_add_undo_method, mi, varray(), false); - } - + ClassDB::bind_method(D_METHOD("add_do_method", "callable"), &UndoRedo::add_do_method); + ClassDB::bind_method(D_METHOD("add_undo_method", "callable"), &UndoRedo::add_undo_method); ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &UndoRedo::add_do_property); ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &UndoRedo::add_undo_property); ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &UndoRedo::add_do_reference); diff --git a/core/object/undo_redo.h b/core/object/undo_redo.h index 63cf3e5cbe64..c7c58697c346 100644 --- a/core/object/undo_redo.h +++ b/core/object/undo_redo.h @@ -46,8 +46,6 @@ class UndoRedo : public Object { }; typedef void (*CommitNotifyCallback)(void *p_ud, const String &p_name); - void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error); - void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error); typedef void (*MethodNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount); typedef void (*PropertyNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value); @@ -58,14 +56,14 @@ class UndoRedo : public Object { TYPE_METHOD, TYPE_PROPERTY, TYPE_REFERENCE - }; + } type; - Type type; bool force_keep_in_merge_ends; Ref ref; ObjectID object; StringName name; - Vector args; + Callable callable; + Variant value; void delete_reference(); }; @@ -106,30 +104,8 @@ class UndoRedo : public Object { public: void create_action(const String &p_name = "", MergeMode p_mode = MERGE_DISABLE); - void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount); - void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount); - - template - void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) { - Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. - const Variant *argptrs[sizeof...(p_args) + 1]; - for (uint32_t i = 0; i < sizeof...(p_args); i++) { - argptrs[i] = &args[i]; - } - - add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args)); - } - template - void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) { - Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. - const Variant *argptrs[sizeof...(p_args) + 1]; - for (uint32_t i = 0; i < sizeof...(p_args); i++) { - argptrs[i] = &args[i]; - } - - add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args)); - } - + void add_do_method(const Callable &p_callable); + void add_undo_method(const Callable &p_callable); void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value); void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value); void add_do_reference(Object *p_object); diff --git a/doc/classes/UndoRedo.xml b/doc/classes/UndoRedo.xml index 3ef59b1c39d4..7258efbddafc 100644 --- a/doc/classes/UndoRedo.xml +++ b/doc/classes/UndoRedo.xml @@ -62,12 +62,11 @@ - + - - + - Register a [param method] that will be called when the action is committed. + Register a [Callable] that will be called when the action is committed. @@ -86,12 +85,11 @@ Register a reference for "do" that will be erased if the "do" history is lost. This is useful mostly for new nodes created for the "do" call. Do not use for resources. - + - - + - Register a [param method] that will be called when the action is undone. + Register a [Callable] that will be called when the action is undone. diff --git a/editor/editor_undo_redo_manager.cpp b/editor/editor_undo_redo_manager.cpp index 8c04a4d595e0..064448fd961d 100644 --- a/editor/editor_undo_redo_manager.cpp +++ b/editor/editor_undo_redo_manager.cpp @@ -131,12 +131,12 @@ void EditorUndoRedoManager::create_action(const String &p_name, UndoRedo::MergeM void EditorUndoRedoManager::add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) { UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo; - undo_redo->add_do_methodp(p_object, p_method, p_args, p_argcount); + undo_redo->add_do_method(Callable(p_object, p_method).bindp(p_args, p_argcount)); } void EditorUndoRedoManager::add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) { UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo; - undo_redo->add_undo_methodp(p_object, p_method, p_args, p_argcount); + undo_redo->add_undo_method(Callable(p_object, p_method).bindp(p_args, p_argcount)); } void EditorUndoRedoManager::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {