undo_redo.h
1 /*************************************************************************/
2 /* undo_redo.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef UNDO_REDO_H
30 #define UNDO_REDO_H
31 
32 #include "object.h"
33 #include "resource.h"
34 
35 
36 
37 
38 class UndoRedo : public Object {
39 
40  OBJ_TYPE(UndoRedo,Object);
41  OBJ_SAVE_TYPE( UndoRedo );
42 public:
43 
44  typedef void (*CommitNotifyCallback)(void *p_ud,const String& p_name);
45  Variant _add_do_method(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
46  Variant _add_undo_method(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
47 
48  typedef void (*MethodNotifyCallback)(void *p_ud,Object*p_base,const StringName& p_name,VARIANT_ARG_DECLARE);
49  typedef void (*PropertyNotifyCallback)(void *p_ud,Object*p_base,const StringName& p_property,const Variant& p_value);
50 
51 private:
52  struct Operation {
53 
54  enum Type {
55  TYPE_METHOD,
56  TYPE_PROPERTY,
57  TYPE_REFERENCE
58  };
59 
60  Type type;
61  Ref<Resource> resref;
62  ObjectID object;
63  String name;
64  Variant args[VARIANT_ARG_MAX];
65 
66  };
67 
68 
69  struct Action {
70  String name;
71  List<Operation> do_ops;
72  List<Operation> undo_ops;
73  };
74 
75  Vector<Action> actions;
76  int current_action;
77  int action_level;
78  int max_steps;
79  bool merging;
80  uint64_t version;
81 
82  void _pop_history_tail();
83  void _process_operation_list(List<Operation>::Element *E);
84  void _discard_redo();
85 
86 
87  CommitNotifyCallback callback;
88  void* callback_ud;
89  void* method_callbck_ud;
90  void* prop_callback_ud;
91 
92  MethodNotifyCallback method_callback;
93  PropertyNotifyCallback property_callback;
94 
95 
96 protected:
97  static void _bind_methods();
98 
99 public:
100 
101  void create_action(const String& p_name="",bool p_mergeable=false);
102 
103  void add_do_method(Object *p_object,const String& p_method,VARIANT_ARG_LIST);
104  void add_undo_method(Object *p_object,const String& p_method,VARIANT_ARG_LIST);
105  void add_do_property(Object *p_object,const String& p_property,const Variant& p_value);
106  void add_undo_property(Object *p_object,const String& p_property,const Variant& p_value);
107  void add_do_reference(Object *p_object);
108  void add_undo_reference(Object *p_object);
109 
110  void commit_action();
111 
112  void redo();
113  void undo();
114  String get_current_action_name() const;
115  void clear_history();
116 
117  void set_max_steps(int p_max_steps);
118  int get_max_steps() const;
119 
120  uint64_t get_version() const;
121 
122  void set_commit_notify_callback(CommitNotifyCallback p_callback,void* p_ud);
123 
124  void set_method_notify_callback(MethodNotifyCallback p_method_callback,void* p_ud);
125  void set_property_notify_callback(PropertyNotifyCallback p_property_callback,void* p_ud);
126 
127  UndoRedo();
128  ~UndoRedo();
129 };
130 
131 #endif // UNDO_REDO_H
Definition: variant.h:74
Definition: string_db.h:48
Definition: ustring.h:64
Definition: object.h:317
Definition: variant.h:379
Definition: undo_redo.h:38