1/**************************************************************************/
2/* editor_undo_redo_manager.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef EDITOR_UNDO_REDO_MANAGER_H
32#define EDITOR_UNDO_REDO_MANAGER_H
33
34#include "core/object/class_db.h"
35#include "core/object/object.h"
36#include "core/object/undo_redo.h"
37
38class EditorUndoRedoManager : public Object {
39 GDCLASS(EditorUndoRedoManager, Object);
40
41 static EditorUndoRedoManager *singleton;
42
43public:
44 enum SpecialHistory {
45 GLOBAL_HISTORY = 0,
46 REMOTE_HISTORY = -9,
47 INVALID_HISTORY = -99,
48 };
49
50 struct Action {
51 int history_id = INVALID_HISTORY;
52 double timestamp = 0;
53 String action_name;
54 UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE;
55 bool backward_undo_ops = false;
56 };
57
58 struct History {
59 int id = INVALID_HISTORY;
60 UndoRedo *undo_redo = nullptr;
61 uint64_t saved_version = 1;
62 List<Action> undo_stack;
63 List<Action> redo_stack;
64 };
65
66private:
67 HashMap<int, History> history_map;
68 Action pending_action;
69
70 bool is_committing = false;
71
72 History *_get_newest_undo();
73
74protected:
75 static void _bind_methods();
76
77public:
78 History &get_or_create_history(int p_idx);
79 UndoRedo *get_history_undo_redo(int p_idx) const;
80 int get_history_id_for_object(Object *p_object) const;
81 History &get_history_for_object(Object *p_object);
82
83 void create_action_for_history(const String &p_name, int p_history_id, UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE, bool p_backward_undo_ops = false);
84 void create_action(const String &p_name = "", UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE, Object *p_custom_context = nullptr, bool p_backward_undo_ops = false);
85
86 void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
87 void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
88
89 template <typename... VarArgs>
90 void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
91 Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
92 const Variant *argptrs[sizeof...(p_args) + 1];
93 for (uint32_t i = 0; i < sizeof...(p_args); i++) {
94 argptrs[i] = &args[i];
95 }
96
97 add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
98 }
99
100 template <typename... VarArgs>
101 void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
102 Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
103 const Variant *argptrs[sizeof...(p_args) + 1];
104 for (uint32_t i = 0; i < sizeof...(p_args); i++) {
105 argptrs[i] = &args[i];
106 }
107
108 add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
109 }
110
111 void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
112 void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
113
114 void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value);
115 void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value);
116 void add_do_reference(Object *p_object);
117 void add_undo_reference(Object *p_object);
118
119 void commit_action(bool p_execute = true);
120 bool is_committing_action() const;
121
122 bool undo();
123 bool undo_history(int p_id);
124 bool redo();
125 bool redo_history(int p_id);
126 void clear_history(bool p_increase_version = true, int p_idx = INVALID_HISTORY);
127
128 void set_history_as_saved(int p_idx);
129 void set_history_as_unsaved(int p_idx);
130 bool is_history_unsaved(int p_idx);
131 bool has_undo();
132 bool has_redo();
133
134 String get_current_action_name();
135 int get_current_action_history_id();
136
137 void discard_history(int p_idx, bool p_erase_from_map = true);
138
139 static EditorUndoRedoManager *get_singleton();
140 EditorUndoRedoManager();
141 ~EditorUndoRedoManager();
142};
143
144VARIANT_ENUM_CAST(EditorUndoRedoManager::SpecialHistory);
145
146#endif // EDITOR_UNDO_REDO_MANAGER_H
147