UndoRedo
Provides a high-level interface for implementing undo and redo operations.
UndoRedo.swift:29class UndoRedo
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:
Before calling any of the add_(un)do_*
methods, you need to first call createAction(name:mergeMode:backwardUndoOps:)
. Afterwards you need to call commitAction(execute:)
.
If you don’t need to register a method, you can leave addDoMethod(callable:)
and addUndoMethod(callable:)
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.
This object emits the following signals:
Superclasses
class Object
Base class for all other classes in the engine.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Types
Type members
Instance members
var versionChanged: SimpleSignal
func addDoMethod(callable: Callable
) Register a
Callable
that will be called when the action is committed.func addDoProperty(object: Object?, property: StringName, value: Variant
) Register a
property
that would change its value tovalue
when the action is committed.func addDoReference(object: Object?
) 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.
func addUndoMethod(callable: Callable
) Register a
Callable
that will be called when the action is undone.func addUndoProperty(object: Object?, property: StringName, value: Variant
) Register a
property
that would change its value tovalue
when the action is undone.func addUndoReference(object: Object?
) 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!).
func clearHistory(increaseVersion: Bool
) Clear the undo/redo history and associated references.
func commitAction(execute: Bool
) Commit the action. If
execute
istrue
(which it is by default), all “do” methods/properties are called/set when this function is called.func createAction(name: String, mergeMode: UndoRedo.MergeMode, backwardUndoOps: Bool
) Create a new action. After this is called, do all your calls to
addDoMethod(callable:)
,addUndoMethod(callable:)
,addDoProperty(object:property:value:)
, andaddUndoProperty(object:property:value:)
, then commit the action withcommitAction(execute:)
.func endForceKeepInMergeEnds(
) Stops marking operations as to be processed even if the action gets merged with another in the .mergeEnds mode. See
startForceKeepInMergeEnds
.func getActionName(id: Int32
) -> String Gets the action name from its index.
func getCurrentAction(
) -> Int32 Gets the index of the current action.
func getCurrentActionName(
) -> String Gets the name of the current action, equivalent to
get_action_name(get_current_action())
.func getHistoryCount(
) -> Int32 Returns how many elements are in the history.
func getVersion(
) -> UInt Gets the version. Every time a new action is committed, the
UndoRedo
’s version number is increased automatically.func hasRedo(
) -> Bool Returns
true
if a “redo” action is available.func hasUndo(
) -> Bool Returns
true
if an “undo” action is available.func isCommittingAction(
) -> Bool Returns
true
if theUndoRedo
is currently committing the action, i.e. running its “do” method or property change (seecommitAction(execute:)
).func redo(
) -> Bool Redo the last action.
func startForceKeepInMergeEnds(
) Marks the next “do” and “undo” operations to be processed even if the action gets merged with another in the .mergeEnds mode. Return to normal operation using
endForceKeepInMergeEnds
.func undo(
) -> Bool Undo the last action.