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