2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* undo_redo.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "undo_redo.h"
|
|
|
|
|
|
|
|
void UndoRedo::_discard_redo() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (current_action == actions.size() - 1)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = current_action + 1; i < actions.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Operation>::Element *E = actions[i].do_ops.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (E->get().type == Operation::TYPE_REFERENCE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Object *obj = ObjectDB::get_instance(E->get().object);
|
|
|
|
if (obj)
|
|
|
|
memdelete(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//ERASE do data
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
actions.resize(current_action + 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void UndoRedo::create_action(const String &p_name, MergeMode p_mode) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (action_level == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_discard_redo();
|
|
|
|
|
2016-08-17 22:14:51 +02:00
|
|
|
// Check if the merge operation is valid
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_mode != MERGE_DISABLE && actions.size() && actions[actions.size() - 1].name == p_name) {
|
2016-08-17 22:14:51 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
current_action = actions.size() - 2;
|
2016-08-17 22:14:51 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_mode == MERGE_ENDS) {
|
2016-08-17 22:14:51 +02:00
|
|
|
|
|
|
|
// Clear all do ops from last action, and delete all object references
|
2017-03-19 00:36:26 +01:00
|
|
|
List<Operation>::Element *E = actions[current_action + 1].do_ops.front();
|
2016-08-17 22:14:51 +02:00
|
|
|
|
|
|
|
while (E) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (E->get().type == Operation::TYPE_REFERENCE) {
|
2016-08-17 22:14:51 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Object *obj = ObjectDB::get_instance(E->get().object);
|
2016-08-17 22:14:51 +02:00
|
|
|
|
|
|
|
if (obj)
|
|
|
|
memdelete(obj);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
E = E->next();
|
|
|
|
actions[current_action + 1].do_ops.pop_front();
|
2016-08-17 22:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
merge_mode = p_mode;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else {
|
2016-08-17 22:14:51 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Action new_action;
|
2017-03-19 00:36:26 +01:00
|
|
|
new_action.name = p_name;
|
2014-02-10 02:10:30 +01:00
|
|
|
actions.push_back(new_action);
|
2016-08-17 22:14:51 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
merge_mode = MERGE_DISABLE;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
action_level++;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void UndoRedo::add_do_method(Object *p_object, const String &p_method, VARIANT_ARG_DECLARE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
VARIANT_ARGPTRS
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level <= 0);
|
|
|
|
ERR_FAIL_COND((current_action + 1) >= actions.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
Operation do_op;
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.object = p_object->get_instance_ID();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_object->cast_to<Resource>())
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.resref = Ref<Resource>(p_object->cast_to<Resource>());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.type = Operation::TYPE_METHOD;
|
|
|
|
do_op.name = p_method;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < VARIANT_ARG_MAX; i++) {
|
|
|
|
do_op.args[i] = *argptr[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
actions[current_action + 1].do_ops.push_back(do_op);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void UndoRedo::add_undo_method(Object *p_object, const String &p_method, VARIANT_ARG_DECLARE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
VARIANT_ARGPTRS
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level <= 0);
|
|
|
|
ERR_FAIL_COND((current_action + 1) >= actions.size());
|
2016-08-17 22:14:51 +02:00
|
|
|
|
|
|
|
// No undo if the merge mode is MERGE_ENDS
|
2017-03-19 00:36:26 +01:00
|
|
|
if (merge_mode == MERGE_ENDS)
|
2016-08-17 22:14:51 +02:00
|
|
|
return;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Operation undo_op;
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.object = p_object->get_instance_ID();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_object->cast_to<Resource>())
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.resref = Ref<Resource>(p_object->cast_to<Resource>());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.type = Operation::TYPE_METHOD;
|
|
|
|
undo_op.name = p_method;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < VARIANT_ARG_MAX; i++) {
|
|
|
|
undo_op.args[i] = *argptr[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
actions[current_action + 1].undo_ops.push_back(undo_op);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void UndoRedo::add_do_property(Object *p_object, const String &p_property, const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level <= 0);
|
|
|
|
ERR_FAIL_COND((current_action + 1) >= actions.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
Operation do_op;
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.object = p_object->get_instance_ID();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_object->cast_to<Resource>())
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.resref = Ref<Resource>(p_object->cast_to<Resource>());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.type = Operation::TYPE_PROPERTY;
|
|
|
|
do_op.name = p_property;
|
|
|
|
do_op.args[0] = p_value;
|
|
|
|
actions[current_action + 1].do_ops.push_back(do_op);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void UndoRedo::add_undo_property(Object *p_object, const String &p_property, const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level <= 0);
|
|
|
|
ERR_FAIL_COND((current_action + 1) >= actions.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-08-17 22:14:51 +02:00
|
|
|
// No undo if the merge mode is MERGE_ENDS
|
2017-03-19 00:36:26 +01:00
|
|
|
if (merge_mode == MERGE_ENDS)
|
2016-08-17 22:14:51 +02:00
|
|
|
return;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Operation undo_op;
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.object = p_object->get_instance_ID();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_object->cast_to<Resource>())
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.resref = Ref<Resource>(p_object->cast_to<Resource>());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.type = Operation::TYPE_PROPERTY;
|
|
|
|
undo_op.name = p_property;
|
|
|
|
undo_op.args[0] = p_value;
|
|
|
|
actions[current_action + 1].undo_ops.push_back(undo_op);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
void UndoRedo::add_do_reference(Object *p_object) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level <= 0);
|
|
|
|
ERR_FAIL_COND((current_action + 1) >= actions.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
Operation do_op;
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.object = p_object->get_instance_ID();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_object->cast_to<Resource>())
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.resref = Ref<Resource>(p_object->cast_to<Resource>());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
do_op.type = Operation::TYPE_REFERENCE;
|
|
|
|
actions[current_action + 1].do_ops.push_back(do_op);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
void UndoRedo::add_undo_reference(Object *p_object) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level <= 0);
|
|
|
|
ERR_FAIL_COND((current_action + 1) >= actions.size());
|
2016-08-17 22:14:51 +02:00
|
|
|
|
|
|
|
// No undo if the merge mode is MERGE_ENDS
|
2017-03-19 00:36:26 +01:00
|
|
|
if (merge_mode == MERGE_ENDS)
|
2016-08-17 22:14:51 +02:00
|
|
|
return;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Operation undo_op;
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.object = p_object->get_instance_ID();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_object->cast_to<Resource>())
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.resref = Ref<Resource>(p_object->cast_to<Resource>());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
undo_op.type = Operation::TYPE_REFERENCE;
|
|
|
|
actions[current_action + 1].undo_ops.push_back(undo_op);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRedo::_pop_history_tail() {
|
|
|
|
|
|
|
|
_discard_redo();
|
|
|
|
|
|
|
|
if (!actions.size())
|
|
|
|
return;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Operation>::Element *E = actions[0].undo_ops.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (E->get().type == Operation::TYPE_REFERENCE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Object *obj = ObjectDB::get_instance(E->get().object);
|
|
|
|
if (obj)
|
|
|
|
memdelete(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actions.remove(0);
|
|
|
|
current_action--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRedo::commit_action() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level <= 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
action_level--;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (action_level > 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return; //still nested
|
|
|
|
|
|
|
|
redo(); // perform action
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (max_steps > 0 && actions.size() > max_steps) {
|
2014-02-10 02:10:30 +01:00
|
|
|
//clear early steps
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
while (actions.size() > max_steps)
|
2014-02-10 02:10:30 +01:00
|
|
|
_pop_history_tail();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (callback && actions.size() > 0) {
|
|
|
|
callback(callback_ud, actions[actions.size() - 1].name);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (; E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Operation &op = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Object *obj = ObjectDB::get_instance(op.object);
|
|
|
|
if (!obj) {
|
|
|
|
//corruption
|
|
|
|
clear_history();
|
|
|
|
ERR_FAIL_COND(!obj);
|
|
|
|
}
|
2015-05-12 02:36:29 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (op.type) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case Operation::TYPE_METHOD: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
obj->call(op.name, VARIANT_ARGS_FROM_ARRAY(op.args));
|
2015-05-12 02:36:29 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-03-19 00:36:26 +01:00
|
|
|
Resource *res = obj->cast_to<Resource>();
|
2015-05-12 02:36:29 +02:00
|
|
|
if (res)
|
|
|
|
res->set_edited(true);
|
2015-08-02 17:29:37 +02:00
|
|
|
|
2015-05-12 02:36:29 +02:00
|
|
|
#endif
|
2015-08-02 17:29:37 +02:00
|
|
|
|
|
|
|
if (method_callback) {
|
2017-03-19 00:36:26 +01:00
|
|
|
method_callback(method_callbck_ud, obj, op.name, VARIANT_ARGS_FROM_ARRAY(op.args));
|
2015-08-02 17:29:37 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Operation::TYPE_PROPERTY: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
obj->set(op.name, op.args[0]);
|
2014-11-02 15:31:01 +01:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-03-19 00:36:26 +01:00
|
|
|
Resource *res = obj->cast_to<Resource>();
|
2014-11-02 15:31:01 +01:00
|
|
|
if (res)
|
|
|
|
res->set_edited(true);
|
|
|
|
#endif
|
2015-08-02 17:29:37 +02:00
|
|
|
if (property_callback) {
|
2017-03-19 00:36:26 +01:00
|
|
|
property_callback(prop_callback_ud, obj, op.name, op.args[0]);
|
2015-08-02 17:29:37 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Operation::TYPE_REFERENCE: {
|
|
|
|
//do nothing
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRedo::redo() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level > 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if ((current_action + 1) >= actions.size())
|
2014-02-10 02:10:30 +01:00
|
|
|
return; //nothing to redo
|
|
|
|
current_action++;
|
|
|
|
|
|
|
|
_process_operation_list(actions[current_action].do_ops.front());
|
|
|
|
version++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRedo::undo() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level > 0);
|
|
|
|
if (current_action < 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return; //nothing to redo
|
|
|
|
_process_operation_list(actions[current_action].undo_ops.front());
|
|
|
|
current_action--;
|
2015-06-22 05:03:19 +02:00
|
|
|
version--;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRedo::clear_history() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(action_level > 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
_discard_redo();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
while (actions.size())
|
2014-02-10 02:10:30 +01:00
|
|
|
_pop_history_tail();
|
|
|
|
|
2015-06-22 05:03:19 +02:00
|
|
|
//version++;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String UndoRedo::get_current_action_name() const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND_V(action_level > 0, "");
|
|
|
|
if (current_action < 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return ""; //nothing to redo
|
|
|
|
return actions[current_action].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRedo::set_max_steps(int p_max_steps) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
max_steps = p_max_steps;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int UndoRedo::get_max_steps() const {
|
|
|
|
|
|
|
|
return max_steps;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t UndoRedo::get_version() const {
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
callback = p_callback;
|
|
|
|
callback_ud = p_ud;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void UndoRedo::set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud) {
|
2015-08-02 17:29:37 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
method_callback = p_method_callback;
|
|
|
|
method_callbck_ud = p_ud;
|
2015-08-02 17:29:37 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud) {
|
2015-08-02 17:29:37 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
property_callback = p_property_callback;
|
|
|
|
prop_callback_ud = p_ud;
|
2015-08-02 17:29:37 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
UndoRedo::UndoRedo() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
version = 1;
|
|
|
|
action_level = 0;
|
|
|
|
current_action = -1;
|
|
|
|
max_steps = -1;
|
|
|
|
merge_mode = MERGE_DISABLE;
|
|
|
|
callback = NULL;
|
|
|
|
callback_ud = NULL;
|
|
|
|
|
|
|
|
method_callbck_ud = NULL;
|
|
|
|
prop_callback_ud = NULL;
|
|
|
|
method_callback = NULL;
|
|
|
|
property_callback = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
UndoRedo::~UndoRedo() {
|
|
|
|
|
|
|
|
clear_history();
|
|
|
|
}
|
2015-06-21 21:55:47 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Variant UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
|
2015-06-21 21:55:47 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_argcount < 2) {
|
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
|
|
|
r_error.argument = 0;
|
2015-06-21 21:55:47 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_args[0]->get_type() != Variant::OBJECT) {
|
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 0;
|
|
|
|
r_error.expected = Variant::OBJECT;
|
2015-06-21 21:55:47 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_args[1]->get_type() != Variant::STRING) {
|
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 1;
|
|
|
|
r_error.expected = Variant::STRING;
|
2015-06-21 21:55:47 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
r_error.error = Variant::CallError::CALL_OK;
|
2015-06-21 21:55:47 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Object *object = *p_args[0];
|
2015-06-21 21:55:47 +02:00
|
|
|
String method = *p_args[1];
|
|
|
|
|
|
|
|
Variant v[VARIANT_ARG_MAX];
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < MIN(VARIANT_ARG_MAX, p_argcount - 2); ++i) {
|
2015-06-21 21:55:47 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
v[i] = *p_args[i + 2];
|
2015-06-21 21:55:47 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
add_do_method(object, method, v[0], v[1], v[2], v[3], v[4]);
|
2015-06-21 21:55:47 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Variant UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
|
2015-06-21 21:55:47 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_argcount < 2) {
|
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
|
|
|
r_error.argument = 0;
|
2015-06-21 21:55:47 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_args[0]->get_type() != Variant::OBJECT) {
|
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 0;
|
|
|
|
r_error.expected = Variant::OBJECT;
|
2015-06-21 21:55:47 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_args[1]->get_type() != Variant::STRING) {
|
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 1;
|
|
|
|
r_error.expected = Variant::STRING;
|
2015-06-21 21:55:47 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
r_error.error = Variant::CallError::CALL_OK;
|
2015-06-21 21:55:47 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Object *object = *p_args[0];
|
2015-06-21 21:55:47 +02:00
|
|
|
String method = *p_args[1];
|
|
|
|
|
|
|
|
Variant v[VARIANT_ARG_MAX];
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < MIN(VARIANT_ARG_MAX, p_argcount - 2); ++i) {
|
2015-06-21 21:55:47 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
v[i] = *p_args[i + 2];
|
2015-06-21 21:55:47 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
add_undo_method(object, method, v[0], v[1], v[2], v[3], v[4]);
|
2015-06-21 21:55:47 +02:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRedo::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("create_action", "name", "merge_mode"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE));
|
|
|
|
ObjectTypeDB::bind_method(_MD("commit_action"), &UndoRedo::commit_action);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2015-06-21 21:55:47 +02:00
|
|
|
//ObjectTypeDB::bind_method(_MD("add_do_method","p_object", "p_method", "VARIANT_ARG_LIST"),&UndoRedo::add_do_method);
|
|
|
|
//ObjectTypeDB::bind_method(_MD("add_undo_method","p_object", "p_method", "VARIANT_ARG_LIST"),&UndoRedo::add_undo_method);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2015-06-21 21:55:47 +02:00
|
|
|
{
|
|
|
|
MethodInfo mi;
|
2017-03-19 00:36:26 +01:00
|
|
|
mi.name = "add_do_method";
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
|
2015-06-21 21:55:47 +02:00
|
|
|
Vector<Variant> defargs;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < VARIANT_ARG_MAX; ++i) {
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i)));
|
2015-06-21 21:55:47 +02:00
|
|
|
defargs.push_back(Variant());
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT, "add_do_method", &UndoRedo::_add_do_method, mi, defargs);
|
2015-06-21 21:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
MethodInfo mi;
|
2017-03-19 00:36:26 +01:00
|
|
|
mi.name = "add_undo_method";
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
|
2015-06-21 21:55:47 +02:00
|
|
|
Vector<Variant> defargs;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < VARIANT_ARG_MAX; ++i) {
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i)));
|
2015-06-21 21:55:47 +02:00
|
|
|
defargs.push_back(Variant());
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &UndoRedo::_add_undo_method, mi, defargs);
|
2015-06-21 21:55:47 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_do_property", "object", "property", "value:Variant"), &UndoRedo::add_do_property);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_undo_property", "object", "property", "value:Variant"), &UndoRedo::add_undo_property);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_do_reference", "object"), &UndoRedo::add_do_reference);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_undo_reference", "object"), &UndoRedo::add_undo_reference);
|
|
|
|
ObjectTypeDB::bind_method(_MD("clear_history"), &UndoRedo::clear_history);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_current_action_name"), &UndoRedo::get_current_action_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_version"), &UndoRedo::get_version);
|
2016-08-17 22:14:51 +02:00
|
|
|
|
|
|
|
BIND_CONSTANT(MERGE_DISABLE);
|
|
|
|
BIND_CONSTANT(MERGE_ENDS);
|
|
|
|
BIND_CONSTANT(MERGE_ALL);
|
2015-06-23 04:44:13 +02:00
|
|
|
}
|