UndoRedo

Hereda: Object

Proporciona una interfaz de alto nivel para implementar operaciones de deshacer y rehacer.

Descripción

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.

When an action is committed, all of the do_* methods will run. If the undo() method is used, the undo_* methods will run. If the redo() method is used, once again, all of the do_* methods will run.

Here's an example on how to add an action:

var undo_redo = UndoRedo.new()

func do_something():
    pass # Put your code here.

func undo_something():
    pass # Put here the code that reverts what's done by "do_something()".

func _on_my_button_pressed():
    var node = get_node("MyNode2D")
    undo_redo.create_action("Move the node")
    undo_redo.add_do_method(do_something)
    undo_redo.add_undo_method(undo_something)
    undo_redo.add_do_property(node, "position", Vector2(100, 100))
    undo_redo.add_undo_property(node, "position", node.position)
    undo_redo.commit_action()

Before calling any of the add_(un)do_* methods, you need to first call create_action(). Afterwards you need to call commit_action().

If you don't need to register a method, you can leave add_do_method() and add_undo_method() out; the same goes for properties. You can also register more than one method/property.

If you are making an EditorPlugin and want to integrate into the editor's undo history, use EditorUndoRedoManager instead.

If you are registering multiple properties/method which depend on one another, be aware that by default undo operation are called in the same order they have been added. Therefore instead of grouping do operation with their undo operations it is better to group do on one side and undo on the other as shown below.

undo_redo.create_action("Add object")

# DO
undo_redo.add_do_method(_create_object)
undo_redo.add_do_method(_add_object_to_singleton)

# UNDO
undo_redo.add_undo_method(_remove_object_from_singleton)
undo_redo.add_undo_method(_destroy_that_object)

undo_redo.commit_action()

Propiedades

int

max_steps

0

Métodos

void

add_do_method(callable: Callable)

void

add_do_property(object: Object, property: StringName, value: Variant)

void

add_do_reference(object: Object)

void

add_undo_method(callable: Callable)

void

add_undo_property(object: Object, property: StringName, value: Variant)

void

add_undo_reference(object: Object)

void

clear_history(increase_version: bool = true)

void

commit_action(execute: bool = true)

void

create_action(name: String, merge_mode: MergeMode = 0, backward_undo_ops: bool = false)

void

end_force_keep_in_merge_ends()

String

get_action_name(id: int)

int

get_current_action()

String

get_current_action_name() const

int

get_history_count()

int

get_version() const

bool

has_redo() const

bool

has_undo() const

bool

is_committing_action() const

bool

redo()

void

start_force_keep_in_merge_ends()

bool

undo()


Señales

version_changed() 🔗

Llamado cuando undo() o redo() fue llamado.


Enumeraciones

enum MergeMode: 🔗

MergeMode MERGE_DISABLE = 0

Hace que las operaciones de "hacer"/"deshacer" se mantengan en acciones separadas.

MergeMode MERGE_ENDS = 1

Fusiona esta acción con la anterior si tienen el mismo nombre. Mantiene solo las operaciones "deshacer" de la primera acción y las operaciones "rehacer" de la última acción. Útil para cambios secuenciales a un único valor.

MergeMode MERGE_ALL = 2

Fusiona esta acción con la anterior si tienen el mismo nombre.


Descripciones de Propiedades

int max_steps = 0 🔗

  • void set_max_steps(value: int)

  • int get_max_steps()

El número máximo de pasos que se pueden almacenar en el historial de deshacer/rehacer. Si el número de pasos almacenados excede este límite, los pasos más antiguos se eliminan del historial y ya no se pueden alcanzar llamando a undo(). Un valor de 0 o inferior significa que no hay límite.


Descripciones de Métodos

void add_do_method(callable: Callable) 🔗

Registra una Callable que se llamará cuando se confirme la acción.


void add_do_property(object: Object, property: StringName, value: Variant) 🔗

Registra una property que cambiará su valor a value cuando se confirme la acción.


void add_do_reference(object: Object) 🔗

Registra una referencia a un objeto que se borrará si se elimina el historial de "hacer". Esto es útil para los objetos añadidos por la acción "hacer" y eliminados por la acción "deshacer".

Cuando se elimina el historial de "hacer", si el objeto es un RefCounted, se liberará la referencia. De lo contrario, se liberará. No lo utilices para los recursos.

var node = Node2D.new()
undo_redo.create_action("Añadir nodo")
undo_redo.add_do_method(add_child.bind(node))
undo_redo.add_do_reference(node)
undo_redo.add_undo_method(remove_child.bind(node))
undo_redo.commit_action()

void add_undo_method(callable: Callable) 🔗

Registra un Callable que se llamará cuando se deshaga la acción.


void add_undo_property(object: Object, property: StringName, value: Variant) 🔗

Registra una property que cambiaría su valor a value cuando se deshaga la acción.


void add_undo_reference(object: Object) 🔗

Registra una referencia a un objeto que se borrará si se elimina el historial de "deshacer". Esto es útil para los objetos añadidos por la acción "deshacer" y eliminados por la acción "hacer".

Cuando se elimina el historial de "deshacer", si el objeto es un RefCounted, se liberará la referencia. De lo contrario, se liberará. No lo utilices para los recursos.

var node = $Node2D
undo_redo.create_action("Eliminar nodo")
undo_redo.add_do_method(remove_child.bind(node))
undo_redo.add_undo_method(add_child.bind(node))
undo_redo.add_undo_reference(node)
undo_redo.commit_action()

void clear_history(increase_version: bool = true) 🔗

Borra el historial de deshacer/rehacer y las referencias asociadas.

Pasar false a increase_version evitará que el número de versión se incremente cuando se borre el historial.


void commit_action(execute: bool = true) 🔗

Confirma la acción. Si execute es true (que lo es por defecto), todos los métodos/propiedades "hacer" son llamados/establecidos cuando se llama a esta función.


void create_action(name: String, merge_mode: MergeMode = 0, backward_undo_ops: bool = false) 🔗

Create a new action. After this is called, do all your calls to add_do_method(), add_undo_method(), add_do_property(), and add_undo_property(), then commit the action with commit_action().

The way actions are merged is dictated by merge_mode.

The way undo operation are ordered in actions is dictated by backward_undo_ops. When backward_undo_ops is false undo option are ordered in the same order they were added. Which means the first operation to be added will be the first to be undone.


void end_force_keep_in_merge_ends() 🔗

Stops marking operations as to be processed even if the action gets merged with another in the MERGE_ENDS mode. See start_force_keep_in_merge_ends().


String get_action_name(id: int) 🔗

Obtiene el nombre de la acción de su índice.


int get_current_action() 🔗

Obtiene el índice de la acción actual.


String get_current_action_name() const 🔗

Gets the name of the current action, equivalent to get_action_name(get_current_action()).


int get_history_count() 🔗

Devuelve cuántos elementos hay en el historial.


int get_version() const 🔗

Consigue la versión. Cada vez que se comete una nueva acción, el número de versión de UndoRedos se incrementa automáticamente.

Esto es útil sobre todo para comprobar si algo cambió de una versión guardada.


bool has_redo() const 🔗

Devuelve true si una acción de "redo" está disponible.


bool has_undo() const 🔗

Devuelve true si se dispone de una acción de "deshacer".


bool is_committing_action() const 🔗

Devuelve true si el UndoRedo está actualmente cometiendo la acción, es decir, ejecutando su método "hacer" o cambio de propiedad (ver commit_action()).


bool redo() 🔗

Rehacer la última acción.


void start_force_keep_in_merge_ends() 🔗

Marks the next "do" and "undo" operations to be processed even if the action gets merged with another in the MERGE_ENDS mode. Return to normal operation using end_force_keep_in_merge_ends().


bool undo() 🔗

Deshace la última acción.