Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/editor_undo_redo_manager.h
20951 views
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
#pragma once
32
33
#include "core/object/object.h"
34
#include "core/object/undo_redo.h"
35
36
class EditorUndoRedoManager : public Object {
37
GDCLASS(EditorUndoRedoManager, Object);
38
39
static EditorUndoRedoManager *singleton;
40
41
static constexpr uint64_t UNSAVED_VERSION = 0;
42
43
public:
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
bool mark_unsaved = true;
57
};
58
59
struct History {
60
int id = INVALID_HISTORY;
61
UndoRedo *undo_redo = nullptr;
62
uint64_t saved_version = 1;
63
List<Action> undo_stack;
64
List<Action> redo_stack;
65
};
66
67
private:
68
HashMap<int, History> history_map;
69
Action pending_action;
70
71
bool forced_history = false;
72
bool is_committing = false;
73
74
History *_get_newest_undo();
75
76
protected:
77
static void _bind_methods();
78
79
#ifndef DISABLE_DEPRECATED
80
void _create_action_bind_compat_106121(const String &p_name = "", UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE, Object *p_custom_context = nullptr, bool p_backward_undo_ops = false);
81
static void _bind_compatibility_methods();
82
#endif
83
84
public:
85
History &get_or_create_history(int p_idx);
86
UndoRedo *get_history_undo_redo(int p_idx) const;
87
int get_history_id_for_object(Object *p_object) const;
88
History &get_history_for_object(Object *p_object);
89
void force_fixed_history();
90
91
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, bool p_mark_unsaved = true);
92
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, bool p_mark_unsaved = true);
93
94
void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
95
void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
96
97
template <typename... VarArgs>
98
void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
99
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
100
const Variant *argptrs[sizeof...(p_args) + 1];
101
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
102
argptrs[i] = &args[i];
103
}
104
105
add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
106
}
107
108
template <typename... VarArgs>
109
void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
110
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
111
const Variant *argptrs[sizeof...(p_args) + 1];
112
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
113
argptrs[i] = &args[i];
114
}
115
116
add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
117
}
118
119
void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
120
void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
121
122
void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value);
123
void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value);
124
void add_do_reference(Object *p_object);
125
void add_undo_reference(Object *p_object);
126
127
void commit_action(bool p_execute = true);
128
bool is_committing_action() const;
129
130
bool undo();
131
bool undo_history(int p_id);
132
bool redo();
133
bool redo_history(int p_id);
134
void clear_history(int p_idx = INVALID_HISTORY, bool p_increase_version = true);
135
136
void set_history_as_saved(int p_idx);
137
void set_history_as_unsaved(int p_idx);
138
bool is_history_unsaved(int p_idx);
139
bool has_undo();
140
bool has_redo();
141
bool has_history(int p_idx) const;
142
143
String get_current_action_name();
144
int get_current_action_history_id();
145
146
void discard_history(int p_idx, bool p_erase_from_map = true);
147
148
static EditorUndoRedoManager *get_singleton();
149
EditorUndoRedoManager();
150
~EditorUndoRedoManager();
151
};
152
153
VARIANT_ENUM_CAST(EditorUndoRedoManager::SpecialHistory);
154
155