class_undoredo.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  2. .. DO NOT EDIT THIS FILE, but the UndoRedo.xml source instead.
  3. .. The source is found in doc/classes or modules/<name>/doc_classes.
  4. .. _class_UndoRedo:
  5. UndoRedo
  6. ========
  7. **Inherits:** :ref:`Object<class_Object>`
  8. **Category:** Core
  9. Brief Description
  10. -----------------
  11. Helper to manage UndoRedo in the editor or custom tools.
  12. Methods
  13. -------
  14. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  15. | :ref:`Variant<class_Variant>` | :ref:`add_do_method<class_UndoRedo_add_do_method>` **(** :ref:`Object<class_Object>` object, :ref:`String<class_String>` method **)** vararg |
  16. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | void | :ref:`add_do_property<class_UndoRedo_add_do_property>` **(** :ref:`Object<class_Object>` object, :ref:`String<class_String>` property, :ref:`Variant<class_Variant>` value **)** |
  18. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. | void | :ref:`add_do_reference<class_UndoRedo_add_do_reference>` **(** :ref:`Object<class_Object>` object **)** |
  20. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  21. | :ref:`Variant<class_Variant>` | :ref:`add_undo_method<class_UndoRedo_add_undo_method>` **(** :ref:`Object<class_Object>` object, :ref:`String<class_String>` method **)** vararg |
  22. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | void | :ref:`add_undo_property<class_UndoRedo_add_undo_property>` **(** :ref:`Object<class_Object>` object, :ref:`String<class_String>` property, :ref:`Variant<class_Variant>` value **)** |
  24. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | void | :ref:`add_undo_reference<class_UndoRedo_add_undo_reference>` **(** :ref:`Object<class_Object>` object **)** |
  26. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | void | :ref:`clear_history<class_UndoRedo_clear_history>` **(** :ref:`bool<class_bool>` increase_version=true **)** |
  28. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | void | :ref:`commit_action<class_UndoRedo_commit_action>` **(** **)** |
  30. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | void | :ref:`create_action<class_UndoRedo_create_action>` **(** :ref:`String<class_String>` name, :ref:`MergeMode<enum_UndoRedo_MergeMode>` merge_mode=0 **)** |
  32. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`String<class_String>` | :ref:`get_current_action_name<class_UndoRedo_get_current_action_name>` **(** **)** const |
  34. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`int<class_int>` | :ref:`get_version<class_UndoRedo_get_version>` **(** **)** const |
  36. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`bool<class_bool>` | :ref:`redo<class_UndoRedo_redo>` **(** **)** |
  38. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`bool<class_bool>` | :ref:`undo<class_UndoRedo_undo>` **(** **)** |
  40. +--------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. Enumerations
  42. ------------
  43. .. _enum_UndoRedo_MergeMode:
  44. enum **MergeMode**:
  45. - **MERGE_DISABLE** = **0**
  46. - **MERGE_ENDS** = **1**
  47. - **MERGE_ALL** = **2**
  48. Description
  49. -----------
  50. Helper to manage UndoRedo in the editor or custom tools. It works by registering methods and property changes inside 'actions'.
  51. Common behavior is to create an action, then add do/undo calls to functions or property changes, then committing the action.
  52. Here's an example on how to add an action to Godot editor's own 'undoredo':
  53. ::
  54. var undoredo = get_undo_redo() # method of EditorPlugin
  55. func do_something():
  56. pass # put your code here
  57. func undo_something():
  58. pass # put here the code that reverts what's done by "do_something()"
  59. func _on_MyButton_pressed():
  60. var node = get_node("MyNode2D")
  61. undoredo.create_action("Move the node")
  62. undoredo.add_do_method(self, "do_something")
  63. undoredo.add_undo_method(self, "undo_something")
  64. undoredo.add_do_property(node, "position", Vector2(100,100))
  65. undoredo.add_undo_property(node, "position", node.position)
  66. undoredo.commit_action()
  67. :ref:`create_action<class_UndoRedo_create_action>`, :ref:`add_do_method<class_UndoRedo_add_do_method>`, :ref:`add_undo_method<class_UndoRedo_add_undo_method>`, :ref:`add_do_property<class_UndoRedo_add_do_property>`, :ref:`add_undo_property<class_UndoRedo_add_undo_property>`, and :ref:`commit_action<class_UndoRedo_commit_action>` should be called one after the other, like in the example. Not doing so could lead to crashes.
  68. If you don't need to register a method you can leave :ref:`add_do_method<class_UndoRedo_add_do_method>` and :ref:`add_undo_method<class_UndoRedo_add_undo_method>` out, and so it goes for properties. You can register more than one method/property.
  69. Method Descriptions
  70. -------------------
  71. .. _class_UndoRedo_add_do_method:
  72. - :ref:`Variant<class_Variant>` **add_do_method** **(** :ref:`Object<class_Object>` object, :ref:`String<class_String>` method **)** vararg
  73. Register a method that will be called when the action is committed.
  74. .. _class_UndoRedo_add_do_property:
  75. - void **add_do_property** **(** :ref:`Object<class_Object>` object, :ref:`String<class_String>` property, :ref:`Variant<class_Variant>` value **)**
  76. Register a property value change for 'do'.
  77. .. _class_UndoRedo_add_do_reference:
  78. - void **add_do_reference** **(** :ref:`Object<class_Object>` object **)**
  79. Register a reference for 'do' that will be erased if the 'do' history is lost. This is useful mostly for new nodes created for the 'do' call. Do not use for resources.
  80. .. _class_UndoRedo_add_undo_method:
  81. - :ref:`Variant<class_Variant>` **add_undo_method** **(** :ref:`Object<class_Object>` object, :ref:`String<class_String>` method **)** vararg
  82. Register a method that will be called when the action is undone.
  83. .. _class_UndoRedo_add_undo_property:
  84. - void **add_undo_property** **(** :ref:`Object<class_Object>` object, :ref:`String<class_String>` property, :ref:`Variant<class_Variant>` value **)**
  85. Register a property value change for 'undo'.
  86. .. _class_UndoRedo_add_undo_reference:
  87. - void **add_undo_reference** **(** :ref:`Object<class_Object>` object **)**
  88. Register a reference for 'undo' that will be erased if the 'undo' history is lost. This is useful mostly for nodes removed with the 'do' call (not the 'undo' call!).
  89. .. _class_UndoRedo_clear_history:
  90. - void **clear_history** **(** :ref:`bool<class_bool>` increase_version=true **)**
  91. Clear the undo/redo history and associated references.
  92. Passing ``false`` to ``increase_version`` will prevent the version number to be increased from this.
  93. .. _class_UndoRedo_commit_action:
  94. - void **commit_action** **(** **)**
  95. Commit the action. All 'do' methods/properties are called/set when this function is called.
  96. .. _class_UndoRedo_create_action:
  97. - void **create_action** **(** :ref:`String<class_String>` name, :ref:`MergeMode<enum_UndoRedo_MergeMode>` merge_mode=0 **)**
  98. Create a new action. After this is called, do all your calls to :ref:`add_do_method<class_UndoRedo_add_do_method>`, :ref:`add_undo_method<class_UndoRedo_add_undo_method>`, :ref:`add_do_property<class_UndoRedo_add_do_property>`, and :ref:`add_undo_property<class_UndoRedo_add_undo_property>`, then commit the action with :ref:`commit_action<class_UndoRedo_commit_action>`.
  99. .. _class_UndoRedo_get_current_action_name:
  100. - :ref:`String<class_String>` **get_current_action_name** **(** **)** const
  101. Get the name of the current action.
  102. .. _class_UndoRedo_get_version:
  103. - :ref:`int<class_int>` **get_version** **(** **)** const
  104. Get the version, each time a new action is committed, the version number of the UndoRedo is increased automatically.
  105. This is useful mostly to check if something changed from a saved version.
  106. .. _class_UndoRedo_redo:
  107. - :ref:`bool<class_bool>` **redo** **(** **)**
  108. Redo last action.
  109. .. _class_UndoRedo_undo:
  110. - :ref:`bool<class_bool>` **undo** **(** **)**
  111. Undo last action.