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()
private UndoRedo _undoRedo;
public override void _Ready()
{
_undoRedo = new UndoRedo();
}
public void DoSomething()
{
// Put your code here.
}
public void UndoSomething()
{
// Put here the code that reverts what's done by "DoSomething()".
}
private void OnMyButtonPressed()
{
var node = GetNode<Node2D>("MyNode2D");
_undoRedo.CreateAction("Move the node");
_undoRedo.AddDoMethod(new Callable(this, MethodName.DoSomething));
_undoRedo.AddUndoMethod(new Callable(this, MethodName.UndoSomething));
_undoRedo.AddDoProperty(node, "position", new Vector2(100, 100));
_undoRedo.AddUndoProperty(node, "position", node.Position);
_undoRedo.CommitAction();
}
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()
_undo_redo.CreateAction("Add object");
// DO
_undo_redo.AddDoMethod(new Callable(this, MethodName.CreateObject));
_undo_redo.AddDoMethod(new Callable(this, MethodName.AddObjectToSingleton));
// UNDO
_undo_redo.AddUndoMethod(new Callable(this, MethodName.RemoveObjectFromSingleton));
_undo_redo.AddUndoMethod(new Callable(this, MethodName.DestroyThatObject));
_undo_redo.CommitAction();
Propiedades
|
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 |
|
get_action_name(id: int) |
|
get_current_action_name() const |
|
get_version() const |
|
has_redo() const |
|
has_undo() const |
|
is_committing_action() const |
|
redo() |
|
void |
|
undo() |
Señales
version_changed() 🔗
Enumeraciones
enum MergeMode: 🔗
MergeMode MERGE_DISABLE = 0
Hace que las operaciones de "hacer"/"deshacer" se mantengan en acciones separadas.
MergeMode MERGE_ENDS = 1
Merges this action with the previous one if they have the same name. Keeps only the first action's "undo" operations and the last action's "do" operations. Useful for sequential changes to a single value.
MergeMode MERGE_ALL = 2
Merges this action with the previous one if they have the same name.
Descripciones de Propiedades
The maximum number of steps that can be stored in the undo/redo history. If the number of stored steps exceeds this limit, older steps are removed from history and can no longer be reached by calling undo(). A value of 0 or lower means no limit.
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.
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()).
Devuelve cuántos elementos hay en el historial.
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.
Devuelve true si una acción de "redo" está disponible.
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()).
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().
Deshace la última acción.