UndoRedo

package
v0.0.0-...-ff35923 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2025 License: MIT Imports: 22 Imported by: 0

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

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 Any

type Any interface {
	gd.IsClass
	AsUndoRedo() Instance
}

type Expanded

type Expanded [1]gdclass.UndoRedo

func (Expanded) ClearHistory

func (self Expanded) ClearHistory(increase_version bool)

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

func (self Expanded) CommitAction(execute bool)

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

func (self Expanded) CreateAction(name string, merge_mode MergeMode, backward_undo_ops bool)

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

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

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]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsUndoRedo

func (self *Extension[T]) AsUndoRedo() Instance

type ID

type ID Object.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.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.UndoRedo

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 New

func New() Instance

func (Instance) AddDoMethod

func (self Instance) AddDoMethod(callable func())

Register a func that will be called when the action is committed.

func (Instance) AddDoProperty

func (self Instance) AddDoProperty(obj Object.Instance, property string, value any)

Register a 'property' that would change its value to 'value' when the action is committed.

func (Instance) AddDoReference

func (self Instance) AddDoReference(obj Object.Instance)

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

func (self Instance) AddUndoProperty(obj Object.Instance, property string, value any)

Register a 'property' that would change its value to 'value' when the action is undone.

func (Instance) AddUndoReference

func (self Instance) AddUndoReference(obj Object.Instance)

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) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsUndoRedo

func (self Instance) AsUndoRedo() Instance

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

func (self Instance) CreateAction(name string)

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

func (self Instance) GetActionName(id int) string

Gets the action name from its index.

func (Instance) GetCurrentAction

func (self Instance) GetCurrentAction() int

Gets the index of the current action.

func (Instance) GetCurrentActionName

func (self Instance) GetCurrentActionName() string

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

func (Instance) GetHistoryCount

func (self Instance) GetHistoryCount() int

Returns how many elements are in the history.

func (Instance) GetVersion

func (self Instance) GetVersion() int

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) HasRedo

func (self Instance) HasRedo() bool

Returns true if a "redo" action is available.

func (Instance) HasUndo

func (self Instance) HasUndo() bool

Returns true if an "undo" action is available.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsCommittingAction

func (self Instance) IsCommittingAction() bool

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) MaxSteps

func (self Instance) MaxSteps() int

func (Instance) OnVersionChanged

func (self Instance) OnVersionChanged(cb func(), flags ...Signal.Flags)

func (Instance) Redo

func (self Instance) Redo() bool

Redo the last action.

func (Instance) SetMaxSteps

func (self Instance) SetMaxSteps(value int)

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

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.

func (Instance) Undo

func (self Instance) Undo() bool

Undo the last action.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

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
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL