undo_redo.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "object.h"
  32. #include "resource.h"
  33. class UndoRedo : public Object {
  34. OBJ_TYPE(UndoRedo,Object);
  35. OBJ_SAVE_TYPE( UndoRedo );
  36. public:
  37. typedef void (*CommitNotifyCallback)(void *p_ud,const String& p_name);
  38. Variant _add_do_method(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  39. Variant _add_undo_method(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  40. typedef void (*MethodNotifyCallback)(void *p_ud,Object*p_base,const StringName& p_name,VARIANT_ARG_DECLARE);
  41. typedef void (*PropertyNotifyCallback)(void *p_ud,Object*p_base,const StringName& p_property,const Variant& p_value);
  42. private:
  43. struct Operation {
  44. enum Type {
  45. TYPE_METHOD,
  46. TYPE_PROPERTY,
  47. TYPE_REFERENCE
  48. };
  49. Type type;
  50. Ref<Resource> resref;
  51. ObjectID object;
  52. String name;
  53. Variant args[VARIANT_ARG_MAX];
  54. };
  55. struct Action {
  56. String name;
  57. List<Operation> do_ops;
  58. List<Operation> undo_ops;
  59. };
  60. Vector<Action> actions;
  61. int current_action;
  62. int action_level;
  63. int max_steps;
  64. bool merging;
  65. uint64_t version;
  66. void _pop_history_tail();
  67. void _process_operation_list(List<Operation>::Element *E);
  68. void _discard_redo();
  69. CommitNotifyCallback callback;
  70. void* callback_ud;
  71. void* method_callbck_ud;
  72. void* prop_callback_ud;
  73. MethodNotifyCallback method_callback;
  74. PropertyNotifyCallback property_callback;
  75. protected:
  76. static void _bind_methods();
  77. public:
  78. void create_action(const String& p_name="",bool p_mergeable=false);
  79. void add_do_method(Object *p_object,const String& p_method,VARIANT_ARG_LIST);
  80. void add_undo_method(Object *p_object,const String& p_method,VARIANT_ARG_LIST);
  81. void add_do_property(Object *p_object,const String& p_property,const Variant& p_value);
  82. void add_undo_property(Object *p_object,const String& p_property,const Variant& p_value);
  83. void add_do_reference(Object *p_object);
  84. void add_undo_reference(Object *p_object);
  85. void commit_action();
  86. void redo();
  87. void undo();
  88. String get_current_action_name() const;
  89. void clear_history();
  90. void set_max_steps(int p_max_steps);
  91. int get_max_steps() const;
  92. uint64_t get_version() const;
  93. void set_commit_notify_callback(CommitNotifyCallback p_callback,void* p_ud);
  94. void set_method_notify_callback(MethodNotifyCallback p_method_callback,void* p_ud);
  95. void set_property_notify_callback(PropertyNotifyCallback p_property_callback,void* p_ud);
  96. UndoRedo();
  97. ~UndoRedo();
  98. };
  99. #endif // UNDO_REDO_H