2014-02-10 09:10:30 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* undo_redo.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#ifndef UNDO_REDO_H
|
|
|
|
#define UNDO_REDO_H
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/object/class_db.h"
|
2021-06-05 00:03:15 +08:00
|
|
|
#include "core/object/ref_counted.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
class UndoRedo : public Object {
|
2017-01-03 10:03:46 +08:00
|
|
|
GDCLASS(UndoRedo, Object);
|
2015-06-22 03:55:47 +08:00
|
|
|
OBJ_SAVE_TYPE(UndoRedo);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
public:
|
2016-08-18 04:14:51 +08:00
|
|
|
enum MergeMode {
|
|
|
|
MERGE_DISABLE,
|
|
|
|
MERGE_ENDS,
|
|
|
|
MERGE_ALL
|
|
|
|
};
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
typedef void (*CommitNotifyCallback)(void *p_ud, const String &p_name);
|
|
|
|
|
2022-03-09 21:58:40 +08:00
|
|
|
typedef void (*MethodNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount);
|
2015-08-02 23:29:37 +08:00
|
|
|
typedef void (*PropertyNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value);
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
private:
|
|
|
|
struct Operation {
|
|
|
|
enum Type {
|
|
|
|
TYPE_METHOD,
|
|
|
|
TYPE_PROPERTY,
|
|
|
|
TYPE_REFERENCE
|
2022-09-19 05:27:00 +08:00
|
|
|
} type;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2023-06-29 02:57:47 +08:00
|
|
|
bool force_keep_in_merge_ends = false;
|
2021-06-05 00:03:15 +08:00
|
|
|
Ref<RefCounted> ref;
|
2014-02-10 09:10:30 +08:00
|
|
|
ObjectID object;
|
2020-02-21 05:58:05 +08:00
|
|
|
StringName name;
|
2022-09-19 05:27:00 +08:00
|
|
|
Callable callable;
|
|
|
|
Variant value;
|
2022-03-01 17:59:37 +08:00
|
|
|
|
|
|
|
void delete_reference();
|
2014-02-10 09:10:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Action {
|
|
|
|
String name;
|
|
|
|
List<Operation> do_ops;
|
|
|
|
List<Operation> undo_ops;
|
2023-06-03 05:03:39 +08:00
|
|
|
uint64_t last_tick = 0;
|
|
|
|
bool backward_undo_ops = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Action> actions;
|
2020-05-12 23:01:17 +08:00
|
|
|
int current_action = -1;
|
2021-10-12 20:03:05 +08:00
|
|
|
bool force_keep_in_merge_ends = false;
|
2020-05-12 23:01:17 +08:00
|
|
|
int action_level = 0;
|
2024-01-10 09:20:11 +08:00
|
|
|
int max_steps = 0;
|
2020-05-12 23:01:17 +08:00
|
|
|
MergeMode merge_mode = MERGE_DISABLE;
|
|
|
|
bool merging = false;
|
|
|
|
uint64_t version = 1;
|
2023-11-27 01:13:35 +08:00
|
|
|
int merge_total = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
void _pop_history_tail();
|
2023-10-11 22:01:55 +08:00
|
|
|
void _process_operation_list(List<Operation>::Element *E, bool p_execute);
|
2014-02-10 09:10:30 +08:00
|
|
|
void _discard_redo();
|
2021-01-12 19:48:41 +08:00
|
|
|
bool _redo(bool p_execute);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
CommitNotifyCallback callback = nullptr;
|
|
|
|
void *callback_ud = nullptr;
|
2022-03-10 05:20:39 +08:00
|
|
|
void *method_callback_ud = nullptr;
|
2020-05-12 23:01:17 +08:00
|
|
|
void *prop_callback_ud = nullptr;
|
2015-08-02 23:29:37 +08:00
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
MethodNotifyCallback method_callback = nullptr;
|
|
|
|
PropertyNotifyCallback property_callback = nullptr;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
int committing = 0;
|
2019-03-04 04:53:59 +08:00
|
|
|
|
2015-06-22 03:55:47 +08:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
public:
|
2023-06-03 05:03:39 +08:00
|
|
|
void create_action(const String &p_name = "", MergeMode p_mode = MERGE_DISABLE, bool p_backward_undo_ops = false);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2022-09-19 05:27:00 +08:00
|
|
|
void add_do_method(const Callable &p_callable);
|
|
|
|
void add_undo_method(const Callable &p_callable);
|
2020-02-21 05:58:05 +08:00
|
|
|
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);
|
2014-02-10 09:10:30 +08:00
|
|
|
void add_do_reference(Object *p_object);
|
|
|
|
void add_undo_reference(Object *p_object);
|
|
|
|
|
2021-10-12 20:03:05 +08:00
|
|
|
void start_force_keep_in_merge_ends();
|
|
|
|
void end_force_keep_in_merge_ends();
|
|
|
|
|
2019-05-19 18:34:40 +08:00
|
|
|
bool is_committing_action() const;
|
2021-01-12 19:48:41 +08:00
|
|
|
void commit_action(bool p_execute = true);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-05-27 10:41:19 +08:00
|
|
|
bool redo();
|
|
|
|
bool undo();
|
2014-02-10 09:10:30 +08:00
|
|
|
String get_current_action_name() const;
|
2022-10-28 08:54:55 +08:00
|
|
|
int get_action_level() const;
|
2021-01-12 19:48:41 +08:00
|
|
|
|
|
|
|
int get_history_count();
|
|
|
|
int get_current_action();
|
|
|
|
String get_action_name(int p_id);
|
2018-11-19 04:07:38 +08:00
|
|
|
void clear_history(bool p_increase_version = true);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2021-08-17 11:41:46 +08:00
|
|
|
bool has_undo() const;
|
|
|
|
bool has_redo() const;
|
2019-02-14 07:52:51 +08:00
|
|
|
|
2023-11-07 07:55:14 +08:00
|
|
|
bool is_merging() const;
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
uint64_t get_version() const;
|
|
|
|
|
2024-01-10 09:20:11 +08:00
|
|
|
void set_max_steps(int p_max_steps);
|
|
|
|
int get_max_steps() const;
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud);
|
|
|
|
|
2015-08-02 23:29:37 +08:00
|
|
|
void set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud);
|
|
|
|
void set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud);
|
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
UndoRedo() {}
|
2014-02-10 09:10:30 +08:00
|
|
|
~UndoRedo();
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:14:51 +08:00
|
|
|
VARIANT_ENUM_CAST(UndoRedo::MergeMode);
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#endif // UNDO_REDO_H
|