undo_redo.h 4.8 KB

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