1 /*************************************************************************/
2 /*  undo_redo.h                                                          */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 UNDO_REDO_H
32 #define UNDO_REDO_H
33 
34 #include "core/object.h"
35 #include "core/resource.h"
36 
37 class UndoRedo : public Object {
38 
39 	GDCLASS(UndoRedo, Object);
40 	OBJ_SAVE_TYPE(UndoRedo);
41 
42 public:
43 	enum MergeMode {
44 		MERGE_DISABLE,
45 		MERGE_ENDS,
46 		MERGE_ALL
47 	};
48 
49 	typedef void (*CommitNotifyCallback)(void *p_ud, const String &p_name);
50 	Variant _add_do_method(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
51 	Variant _add_undo_method(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
52 
53 	typedef void (*MethodNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE);
54 	typedef void (*PropertyNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value);
55 
56 private:
57 	struct Operation {
58 
59 		enum Type {
60 			TYPE_METHOD,
61 			TYPE_PROPERTY,
62 			TYPE_REFERENCE
63 		};
64 
65 		Type type;
66 		Ref<Resource> resref;
67 		ObjectID object;
68 		String name;
69 		Variant args[VARIANT_ARG_MAX];
70 	};
71 
72 	struct Action {
73 		String name;
74 		List<Operation> do_ops;
75 		List<Operation> undo_ops;
76 		uint64_t last_tick;
77 	};
78 
79 	Vector<Action> actions;
80 	int current_action;
81 	int action_level;
82 	MergeMode merge_mode;
83 	bool merging;
84 	uint64_t version;
85 
86 	void _pop_history_tail();
87 	void _process_operation_list(List<Operation>::Element *E);
88 	void _discard_redo();
89 
90 	CommitNotifyCallback callback;
91 	void *callback_ud;
92 	void *method_callbck_ud;
93 	void *prop_callback_ud;
94 
95 	MethodNotifyCallback method_callback;
96 	PropertyNotifyCallback property_callback;
97 
98 	int committing;
99 
100 protected:
101 	static void _bind_methods();
102 
103 public:
104 	void create_action(const String &p_name = "", MergeMode p_mode = MERGE_DISABLE);
105 
106 	void add_do_method(Object *p_object, const String &p_method, VARIANT_ARG_LIST);
107 	void add_undo_method(Object *p_object, const String &p_method, VARIANT_ARG_LIST);
108 	void add_do_property(Object *p_object, const String &p_property, const Variant &p_value);
109 	void add_undo_property(Object *p_object, const String &p_property, const Variant &p_value);
110 	void add_do_reference(Object *p_object);
111 	void add_undo_reference(Object *p_object);
112 
113 	bool is_committing_action() const;
114 	void commit_action();
115 
116 	bool redo();
117 	bool undo();
118 	String get_current_action_name() const;
119 	void clear_history(bool p_increase_version = true);
120 
121 	bool has_undo();
122 	bool has_redo();
123 
124 	uint64_t get_version() const;
125 
126 	void set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud);
127 
128 	void set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud);
129 	void set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud);
130 
131 	UndoRedo();
132 	~UndoRedo();
133 };
134 
135 VARIANT_ENUM_CAST(UndoRedo::MergeMode);
136 
137 #endif // UNDO_REDO_H
138