class_undoredo.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/4.0/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.0/doc/classes/UndoRedo.xml.
  6. .. _class_UndoRedo:
  7. UndoRedo
  8. ========
  9. **Inherits:** :ref:`Object<class_Object>`
  10. Provides a high-level interface for implementing undo and redo operations.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. UndoRedo works by registering methods and property changes inside "actions". You can create an action, then provide ways to do and undo this action using function calls and property changes, then commit the action.
  15. When an action is committed, all of the ``do_*`` methods will run. If the :ref:`undo<class_UndoRedo_method_undo>` method is used, the ``undo_*`` methods will run. If the :ref:`redo<class_UndoRedo_method_redo>` method is used, once again, all of the ``do_*`` methods will run.
  16. Here's an example on how to add an action:
  17. .. tabs::
  18. .. code-tab:: gdscript
  19. var undo_redo = UndoRedo.new()
  20. func do_something():
  21. pass # Put your code here.
  22. func undo_something():
  23. pass # Put here the code that reverts what's done by "do_something()".
  24. func _on_my_button_pressed():
  25. var node = get_node("MyNode2D")
  26. undo_redo.create_action("Move the node")
  27. undo_redo.add_do_method(do_something)
  28. undo_redo.add_undo_method(undo_something)
  29. undo_redo.add_do_property(node, "position", Vector2(100,100))
  30. undo_redo.add_undo_property(node, "position", node.position)
  31. undo_redo.commit_action()
  32. .. code-tab:: csharp
  33. private UndoRedo _undoRedo;
  34. public override void _Ready()
  35. {
  36. _undoRedo = new UndoRedo();
  37. }
  38. public void DoSomething()
  39. {
  40. // Put your code here.
  41. }
  42. public void UndoSomething()
  43. {
  44. // Put here the code that reverts what's done by "DoSomething()".
  45. }
  46. private void OnMyButtonPressed()
  47. {
  48. var node = GetNode<Node2D>("MyNode2D");
  49. _undoRedo.CreateAction("Move the node");
  50. _undoRedo.AddDoMethod(new Callable(this, MethodName.DoSomething));
  51. _undoRedo.AddUndoMethod(new Callable(this, MethodName.UndoSomething));
  52. _undoRedo.AddDoProperty(node, "position", new Vector2(100, 100));
  53. _undoRedo.AddUndoProperty(node, "position", node.Position);
  54. _undoRedo.CommitAction();
  55. }
  56. \ :ref:`create_action<class_UndoRedo_method_create_action>`, :ref:`add_do_method<class_UndoRedo_method_add_do_method>`, :ref:`add_undo_method<class_UndoRedo_method_add_undo_method>`, :ref:`add_do_property<class_UndoRedo_method_add_do_property>`, :ref:`add_undo_property<class_UndoRedo_method_add_undo_property>`, and :ref:`commit_action<class_UndoRedo_method_commit_action>` should be called one after the other, like in the example. Not doing so could lead to crashes.
  57. If you don't need to register a method, you can leave :ref:`add_do_method<class_UndoRedo_method_add_do_method>` and :ref:`add_undo_method<class_UndoRedo_method_add_undo_method>` out; the same goes for properties. You can also register more than one method/property in the order they should run.
  58. If you are making an :ref:`EditorPlugin<class_EditorPlugin>` and want to integrate into the editor's undo history, use :ref:`EditorUndoRedoManager<class_EditorUndoRedoManager>` instead.
  59. .. rst-class:: classref-reftable-group
  60. Methods
  61. -------
  62. .. table::
  63. :widths: auto
  64. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | void | :ref:`add_do_method<class_UndoRedo_method_add_do_method>` **(** :ref:`Callable<class_Callable>` callable **)** |
  66. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | void | :ref:`add_do_property<class_UndoRedo_method_add_do_property>` **(** :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)** |
  68. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | void | :ref:`add_do_reference<class_UndoRedo_method_add_do_reference>` **(** :ref:`Object<class_Object>` object **)** |
  70. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | void | :ref:`add_undo_method<class_UndoRedo_method_add_undo_method>` **(** :ref:`Callable<class_Callable>` callable **)** |
  72. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | void | :ref:`add_undo_property<class_UndoRedo_method_add_undo_property>` **(** :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)** |
  74. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | void | :ref:`add_undo_reference<class_UndoRedo_method_add_undo_reference>` **(** :ref:`Object<class_Object>` object **)** |
  76. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | void | :ref:`clear_history<class_UndoRedo_method_clear_history>` **(** :ref:`bool<class_bool>` increase_version=true **)** |
  78. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | void | :ref:`commit_action<class_UndoRedo_method_commit_action>` **(** :ref:`bool<class_bool>` execute=true **)** |
  80. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | void | :ref:`create_action<class_UndoRedo_method_create_action>` **(** :ref:`String<class_String>` name, :ref:`MergeMode<enum_UndoRedo_MergeMode>` merge_mode=0 **)** |
  82. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | void | :ref:`end_force_keep_in_merge_ends<class_UndoRedo_method_end_force_keep_in_merge_ends>` **(** **)** |
  84. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`String<class_String>` | :ref:`get_action_name<class_UndoRedo_method_get_action_name>` **(** :ref:`int<class_int>` id **)** |
  86. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | :ref:`int<class_int>` | :ref:`get_current_action<class_UndoRedo_method_get_current_action>` **(** **)** |
  88. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`String<class_String>` | :ref:`get_current_action_name<class_UndoRedo_method_get_current_action_name>` **(** **)** |const| |
  90. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | :ref:`int<class_int>` | :ref:`get_history_count<class_UndoRedo_method_get_history_count>` **(** **)** |
  92. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | :ref:`int<class_int>` | :ref:`get_version<class_UndoRedo_method_get_version>` **(** **)** |const| |
  94. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | :ref:`bool<class_bool>` | :ref:`has_redo<class_UndoRedo_method_has_redo>` **(** **)** |const| |
  96. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  97. | :ref:`bool<class_bool>` | :ref:`has_undo<class_UndoRedo_method_has_undo>` **(** **)** |const| |
  98. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  99. | :ref:`bool<class_bool>` | :ref:`is_committing_action<class_UndoRedo_method_is_committing_action>` **(** **)** |const| |
  100. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  101. | :ref:`bool<class_bool>` | :ref:`redo<class_UndoRedo_method_redo>` **(** **)** |
  102. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  103. | void | :ref:`start_force_keep_in_merge_ends<class_UndoRedo_method_start_force_keep_in_merge_ends>` **(** **)** |
  104. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  105. | :ref:`bool<class_bool>` | :ref:`undo<class_UndoRedo_method_undo>` **(** **)** |
  106. +-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  107. .. rst-class:: classref-section-separator
  108. ----
  109. .. rst-class:: classref-descriptions-group
  110. Signals
  111. -------
  112. .. _class_UndoRedo_signal_version_changed:
  113. .. rst-class:: classref-signal
  114. **version_changed** **(** **)**
  115. Called when :ref:`undo<class_UndoRedo_method_undo>` or :ref:`redo<class_UndoRedo_method_redo>` was called.
  116. .. rst-class:: classref-section-separator
  117. ----
  118. .. rst-class:: classref-descriptions-group
  119. Enumerations
  120. ------------
  121. .. _enum_UndoRedo_MergeMode:
  122. .. rst-class:: classref-enumeration
  123. enum **MergeMode**:
  124. .. _class_UndoRedo_constant_MERGE_DISABLE:
  125. .. rst-class:: classref-enumeration-constant
  126. :ref:`MergeMode<enum_UndoRedo_MergeMode>` **MERGE_DISABLE** = ``0``
  127. Makes "do"/"undo" operations stay in separate actions.
  128. .. _class_UndoRedo_constant_MERGE_ENDS:
  129. .. rst-class:: classref-enumeration-constant
  130. :ref:`MergeMode<enum_UndoRedo_MergeMode>` **MERGE_ENDS** = ``1``
  131. Makes so that the action's "undo" operations are from the first action created and the "do" operations are from the last subsequent action with the same name.
  132. .. _class_UndoRedo_constant_MERGE_ALL:
  133. .. rst-class:: classref-enumeration-constant
  134. :ref:`MergeMode<enum_UndoRedo_MergeMode>` **MERGE_ALL** = ``2``
  135. Makes subsequent actions with the same name be merged into one.
  136. .. rst-class:: classref-section-separator
  137. ----
  138. .. rst-class:: classref-descriptions-group
  139. Method Descriptions
  140. -------------------
  141. .. _class_UndoRedo_method_add_do_method:
  142. .. rst-class:: classref-method
  143. void **add_do_method** **(** :ref:`Callable<class_Callable>` callable **)**
  144. Register a :ref:`Callable<class_Callable>` that will be called when the action is committed.
  145. .. rst-class:: classref-item-separator
  146. ----
  147. .. _class_UndoRedo_method_add_do_property:
  148. .. rst-class:: classref-method
  149. void **add_do_property** **(** :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)**
  150. Register a ``property`` that would change its value to ``value`` when the action is committed.
  151. .. rst-class:: classref-item-separator
  152. ----
  153. .. _class_UndoRedo_method_add_do_reference:
  154. .. rst-class:: classref-method
  155. void **add_do_reference** **(** :ref:`Object<class_Object>` object **)**
  156. 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.
  157. ::
  158. var node = Node2D.new()
  159. undo_redo.create_action("Add node")
  160. undo_redo.add_do_method(add_child.bind(node))
  161. undo_redo.add_do_reference(node)
  162. undo_redo.add_undo_method(remove_child.bind(node))
  163. undo_redo.commit_action()
  164. .. rst-class:: classref-item-separator
  165. ----
  166. .. _class_UndoRedo_method_add_undo_method:
  167. .. rst-class:: classref-method
  168. void **add_undo_method** **(** :ref:`Callable<class_Callable>` callable **)**
  169. Register a :ref:`Callable<class_Callable>` that will be called when the action is undone.
  170. .. rst-class:: classref-item-separator
  171. ----
  172. .. _class_UndoRedo_method_add_undo_property:
  173. .. rst-class:: classref-method
  174. void **add_undo_property** **(** :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)**
  175. Register a ``property`` that would change its value to ``value`` when the action is undone.
  176. .. rst-class:: classref-item-separator
  177. ----
  178. .. _class_UndoRedo_method_add_undo_reference:
  179. .. rst-class:: classref-method
  180. void **add_undo_reference** **(** :ref:`Object<class_Object>` object **)**
  181. 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!).
  182. ::
  183. var node = $Node2D
  184. undo_redo.create_action("Remove node")
  185. undo_redo.add_do_method(remove_child.bind(node))
  186. undo_redo.add_undo_method(add_child.bind(node))
  187. undo_redo.add_undo_reference(node)
  188. undo_redo.commit_action()
  189. .. rst-class:: classref-item-separator
  190. ----
  191. .. _class_UndoRedo_method_clear_history:
  192. .. rst-class:: classref-method
  193. void **clear_history** **(** :ref:`bool<class_bool>` increase_version=true **)**
  194. Clear the undo/redo history and associated references.
  195. Passing ``false`` to ``increase_version`` will prevent the version number from increasing when the history is cleared.
  196. .. rst-class:: classref-item-separator
  197. ----
  198. .. _class_UndoRedo_method_commit_action:
  199. .. rst-class:: classref-method
  200. void **commit_action** **(** :ref:`bool<class_bool>` execute=true **)**
  201. Commit the action. If ``execute`` is ``true`` (which it is by default), all "do" methods/properties are called/set when this function is called.
  202. .. rst-class:: classref-item-separator
  203. ----
  204. .. _class_UndoRedo_method_create_action:
  205. .. rst-class:: classref-method
  206. void **create_action** **(** :ref:`String<class_String>` name, :ref:`MergeMode<enum_UndoRedo_MergeMode>` merge_mode=0 **)**
  207. Create a new action. After this is called, do all your calls to :ref:`add_do_method<class_UndoRedo_method_add_do_method>`, :ref:`add_undo_method<class_UndoRedo_method_add_undo_method>`, :ref:`add_do_property<class_UndoRedo_method_add_do_property>`, and :ref:`add_undo_property<class_UndoRedo_method_add_undo_property>`, then commit the action with :ref:`commit_action<class_UndoRedo_method_commit_action>`.
  208. The way actions are merged is dictated by ``merge_mode``. See :ref:`MergeMode<enum_UndoRedo_MergeMode>` for details.
  209. .. rst-class:: classref-item-separator
  210. ----
  211. .. _class_UndoRedo_method_end_force_keep_in_merge_ends:
  212. .. rst-class:: classref-method
  213. void **end_force_keep_in_merge_ends** **(** **)**
  214. Stops marking operations as to be processed even if the action gets merged with another in the :ref:`MERGE_ENDS<class_UndoRedo_constant_MERGE_ENDS>` mode. See :ref:`start_force_keep_in_merge_ends<class_UndoRedo_method_start_force_keep_in_merge_ends>`.
  215. .. rst-class:: classref-item-separator
  216. ----
  217. .. _class_UndoRedo_method_get_action_name:
  218. .. rst-class:: classref-method
  219. :ref:`String<class_String>` **get_action_name** **(** :ref:`int<class_int>` id **)**
  220. Gets the action name from its index.
  221. .. rst-class:: classref-item-separator
  222. ----
  223. .. _class_UndoRedo_method_get_current_action:
  224. .. rst-class:: classref-method
  225. :ref:`int<class_int>` **get_current_action** **(** **)**
  226. Gets the index of the current action.
  227. .. rst-class:: classref-item-separator
  228. ----
  229. .. _class_UndoRedo_method_get_current_action_name:
  230. .. rst-class:: classref-method
  231. :ref:`String<class_String>` **get_current_action_name** **(** **)** |const|
  232. Gets the name of the current action, equivalent to ``get_action_name(get_current_action())``.
  233. .. rst-class:: classref-item-separator
  234. ----
  235. .. _class_UndoRedo_method_get_history_count:
  236. .. rst-class:: classref-method
  237. :ref:`int<class_int>` **get_history_count** **(** **)**
  238. Returns how many elements are in the history.
  239. .. rst-class:: classref-item-separator
  240. ----
  241. .. _class_UndoRedo_method_get_version:
  242. .. rst-class:: classref-method
  243. :ref:`int<class_int>` **get_version** **(** **)** |const|
  244. Gets the version. Every time a new action is committed, the **UndoRedo**'s version number is increased automatically.
  245. This is useful mostly to check if something changed from a saved version.
  246. .. rst-class:: classref-item-separator
  247. ----
  248. .. _class_UndoRedo_method_has_redo:
  249. .. rst-class:: classref-method
  250. :ref:`bool<class_bool>` **has_redo** **(** **)** |const|
  251. Returns ``true`` if a "redo" action is available.
  252. .. rst-class:: classref-item-separator
  253. ----
  254. .. _class_UndoRedo_method_has_undo:
  255. .. rst-class:: classref-method
  256. :ref:`bool<class_bool>` **has_undo** **(** **)** |const|
  257. Returns ``true`` if an "undo" action is available.
  258. .. rst-class:: classref-item-separator
  259. ----
  260. .. _class_UndoRedo_method_is_committing_action:
  261. .. rst-class:: classref-method
  262. :ref:`bool<class_bool>` **is_committing_action** **(** **)** |const|
  263. Returns ``true`` if the **UndoRedo** is currently committing the action, i.e. running its "do" method or property change (see :ref:`commit_action<class_UndoRedo_method_commit_action>`).
  264. .. rst-class:: classref-item-separator
  265. ----
  266. .. _class_UndoRedo_method_redo:
  267. .. rst-class:: classref-method
  268. :ref:`bool<class_bool>` **redo** **(** **)**
  269. Redo the last action.
  270. .. rst-class:: classref-item-separator
  271. ----
  272. .. _class_UndoRedo_method_start_force_keep_in_merge_ends:
  273. .. rst-class:: classref-method
  274. void **start_force_keep_in_merge_ends** **(** **)**
  275. Marks the next "do" and "undo" operations to be processed even if the action gets merged with another in the :ref:`MERGE_ENDS<class_UndoRedo_constant_MERGE_ENDS>` mode. Return to normal operation using :ref:`end_force_keep_in_merge_ends<class_UndoRedo_method_end_force_keep_in_merge_ends>`.
  276. .. rst-class:: classref-item-separator
  277. ----
  278. .. _class_UndoRedo_method_undo:
  279. .. rst-class:: classref-method
  280. :ref:`bool<class_bool>` **undo** **(** **)**
  281. Undo the last action.
  282. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  283. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  284. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  285. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  286. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  287. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`