Documentation
¶
Overview ¶
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 Instance.Undo method is used, the undo_* methods will run. If the Instance.Redo method is used, once again, all of the do_* methods will run.
Here's an example on how to add an action:
package main import ( "graphics.gd/classdb/Node2D" "graphics.gd/classdb/UndoRedo" "graphics.gd/variant/Vector2" ) var undoRedo = UndoRedo.New() func ExampleUndoRedo(node Node2D.Instance) { undoRedo.CreateAction("Move the node") undoRedo.AddDoMethod(func() {}) undoRedo.AddUndoMethod(func() {}) undoRedo.AddDoProperty(node.AsObject(), "position", Vector2.New(100.0, 100.0)) undoRedo.AddUndoProperty(node.AsObject(), "position", node.Position()) undoRedo.CommitAction() }
Before calling any of the add_(un)do_* methods, you need to first call Instance.CreateAction. Afterwards you need to call Instance.CommitAction.
If you don't need to register a method, you can leave Instance.AddDoMethod and Instance.AddUndoMethod out; the same goes for properties. You can also register more than one method/property.
If you are making an graphics.gd/classdb/EditorPlugin and want to integrate into the editor's undo history, use graphics.gd/classdb/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.
package main func ExamplesUndoRedo() { undoRedo.CreateAction("Add object") // DO undoRedo.AddDoMethod(func() {}) // _create_object undoRedo.AddDoMethod(func() {}) // _add_object_to_singleton // UNDO undoRedo.AddUndoMethod(func() {}) // _remove_object_from_singleton undoRedo.AddUndoMethod(func() {}) // _destroy_that_object undoRedo.CommitAction() }
Index ¶
- type Advanced
- type Any
- type Expanded
- type Extension
- type ID
- type Instance
- func (self Instance) AddDoMethod(callable func())
- func (self Instance) AddDoProperty(obj Object.Instance, property string, value any)
- func (self Instance) AddDoReference(obj Object.Instance)
- func (self Instance) AddUndoMethod(callable func())
- func (self Instance) AddUndoProperty(obj Object.Instance, property string, value any)
- func (self Instance) AddUndoReference(obj Object.Instance)
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsUndoRedo() Instance
- func (self Instance) ClearHistory()
- func (self Instance) CommitAction()
- func (self Instance) CreateAction(name string)
- func (self Instance) EndForceKeepInMergeEnds()
- func (self Instance) GetActionName(id int) string
- func (self Instance) GetCurrentAction() int
- func (self Instance) GetCurrentActionName() string
- func (self Instance) GetHistoryCount() int
- func (self Instance) GetVersion() int
- func (self Instance) HasRedo() bool
- func (self Instance) HasUndo() bool
- func (self Instance) ID() ID
- func (self Instance) IsCommittingAction() bool
- func (self Instance) MaxSteps() int
- func (self Instance) OnVersionChanged(cb func(), flags ...Signal.Flags)
- func (self Instance) Redo() bool
- func (self Instance) SetMaxSteps(value int)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) StartForceKeepInMergeEnds()
- func (self Instance) Undo() bool
- func (self Instance) Virtual(name string) reflect.Value
- type MergeMode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type Expanded ¶
func (Expanded) ClearHistory ¶
Clear the undo/redo history and associated references.
Passing false to 'increase_version' will prevent the version number from increasing when the history is cleared.
func (Expanded) CommitAction ¶
Commit the action. If 'execute' is true (which it is by default), all "do" methods/properties are called/set when this function is called.
func (Expanded) CreateAction ¶
Create a new action. After this is called, do all your calls to Instance.AddDoMethod, Instance.AddUndoMethod, Instance.AddDoProperty, and Instance.AddUndoProperty, then commit the action with Instance.CommitAction.
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.
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension
func (*Extension[T]) AsUndoRedo ¶
type ID ¶
ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.
type Instance ¶
Instance of the class with convieniently typed arguments and results.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AddDoMethod ¶
func (self Instance) AddDoMethod(callable func())
Register a func that will be called when the action is committed.
func (Instance) AddDoProperty ¶
Register a 'property' that would change its value to 'value' when the action is committed.
func (Instance) AddDoReference ¶
Register a reference to an object that will be erased if the "do" history is deleted. This is useful for objects added by the "do" action and removed by the "undo" action.
When the "do" history is deleted, if the object is a graphics.gd/classdb/RefCounted, it will be unreferenced. Otherwise, it will be freed. Do not use for resources.
func (Instance) AddUndoMethod ¶
func (self Instance) AddUndoMethod(callable func())
Register a func that will be called when the action is undone.
func (Instance) AddUndoProperty ¶
Register a 'property' that would change its value to 'value' when the action is undone.
func (Instance) AddUndoReference ¶
Register a reference to an object that will be erased if the "undo" history is deleted. This is useful for objects added by the "undo" action and removed by the "do" action.
When the "undo" history is deleted, if the object is a graphics.gd/classdb/RefCounted, it will be unreferenced. Otherwise, it will be freed. Do not use for resources.
func (Instance) AsUndoRedo ¶
func (Instance) ClearHistory ¶
func (self Instance) ClearHistory()
Clear the undo/redo history and associated references.
Passing false to 'increase_version' will prevent the version number from increasing when the history is cleared.
func (Instance) CommitAction ¶
func (self Instance) CommitAction()
Commit the action. If 'execute' is true (which it is by default), all "do" methods/properties are called/set when this function is called.
func (Instance) CreateAction ¶
Create a new action. After this is called, do all your calls to Instance.AddDoMethod, Instance.AddUndoMethod, Instance.AddDoProperty, and Instance.AddUndoProperty, then commit the action with Instance.CommitAction.
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.
func (Instance) EndForceKeepInMergeEnds ¶
func (self Instance) EndForceKeepInMergeEnds()
Stops marking operations as to be processed even if the action gets merged with another in the MergeEnds mode. See Instance.StartForceKeepInMergeEnds.
func (Instance) GetActionName ¶
Gets the action name from its index.
func (Instance) GetCurrentAction ¶
Gets the index of the current action.
func (Instance) GetCurrentActionName ¶
Gets the name of the current action, equivalent to get_action_name(get_current_action()).
func (Instance) GetHistoryCount ¶
Returns how many elements are in the history.
func (Instance) GetVersion ¶
Gets the version. Every time a new action is committed, the graphics.gd/classdb/UndoRedo's version number is increased automatically.
This is useful mostly to check if something changed from a saved version.
func (Instance) IsCommittingAction ¶
Returns true if the graphics.gd/classdb/UndoRedo is currently committing the action, i.e. running its "do" method or property change (see Instance.CommitAction).
func (Instance) OnVersionChanged ¶
func (Instance) SetMaxSteps ¶
func (Instance) StartForceKeepInMergeEnds ¶
func (self Instance) 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 Instance.EndForceKeepInMergeEnds.
type MergeMode ¶
type MergeMode int //gd:UndoRedo.MergeMode
const ( // Makes "do"/"undo" operations stay in separate actions. MergeDisable MergeMode = 0 // 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. MergeEnds MergeMode = 1 // Merges this action with the previous one if they have the same name. MergeAll MergeMode = 2 )