Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The UndoRedo class has been extended to make it more useful for its own use in addition to internal use by the engine itself. #41193

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/undo_redo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ void UndoRedo::commit_action() {
committing++;
redo(); // perform action
committing--;

if (max_actions > 0 && actions.size() > max_actions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking:
Shouldn't this occur in create_action instead? Note that this is where _discard_redo() is used currently, and that it is where the action itself is created.

Also, note that the if does not need to check for actions.size() > max_actions as it is going to be checked by the while anyway.

//clear early steps
while (actions.size() > max_actions)
_pop_history_tail();
}

if (callback && actions.size() > 0) {
callback(callback_ud, actions[actions.size() - 1].name);
}
Expand Down Expand Up @@ -372,6 +379,22 @@ String UndoRedo::get_current_action_name() const {
return actions[current_action].name;
}

int UndoRedo::get_action_count() const {
return actions.size();
}

int UndoRedo::get_current_action() const {
return current_action;
}

void UndoRedo::set_max_actions(int p_max_actions) {
max_actions = p_max_actions;
}

int UndoRedo::get_max_actions() const {
return max_actions;
}

bool UndoRedo::has_undo() {
return current_action >= 0;
}
Expand Down Expand Up @@ -506,6 +529,10 @@ void UndoRedo::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &UndoRedo::add_undo_reference);
ClassDB::bind_method(D_METHOD("clear_history", "increase_version"), &UndoRedo::clear_history, DEFVAL(true));
ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name);
ClassDB::bind_method(D_METHOD("set_max_actions", "max_actions"), &UndoRedo::set_max_actions);
ClassDB::bind_method(D_METHOD("get_max_actions"), &UndoRedo::get_max_actions);
ClassDB::bind_method(D_METHOD("get_action_count"), &UndoRedo::get_action_count);
ClassDB::bind_method(D_METHOD("get_current_action"), &UndoRedo::get_current_action);
ClassDB::bind_method(D_METHOD("has_undo"), &UndoRedo::has_undo);
ClassDB::bind_method(D_METHOD("has_redo"), &UndoRedo::has_redo);
ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version);
Expand Down
5 changes: 5 additions & 0 deletions core/undo_redo.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class UndoRedo : public Object {
Vector<Action> actions;
int current_action = -1;
int action_level = 0;
int max_actions = -1;
MergeMode merge_mode = MERGE_DISABLE;
bool merging = false;
uint64_t version = 1;
Expand Down Expand Up @@ -114,6 +115,10 @@ class UndoRedo : public Object {
bool redo();
bool undo();
String get_current_action_name() const;
int get_action_count() const;
int get_current_action() const;
void set_max_actions(int p_max_actions);
int get_max_actions() const;
void clear_history(bool p_increase_version = true);

bool has_undo();
Expand Down
30 changes: 30 additions & 0 deletions doc/classes/UndoRedo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@
Gets the name of the current action.
</description>
</method>
<method name="get_action_count" qualifiers="const">
<return type="int">
</return>
<description>
Gets the ammount of actions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "amount"

Might be nice to clarify what this means exactly, i.e. that it represents the amount of actions currently stored in the undo/redo.

</description>
</method>
<method name="get_current_action" qualifiers="const">
<return type="int">
</return>
<description>
Gets the index of the current action.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a link between the "current action" and the amount of undo steps available? If so, the current documentation does not mention it.

</description>
</method>
<method name="set_max_actions">
<return type="void">
</return>
<argument index="0" name="max_actions" type="int">
</argument>
<description>
Sets the max ammount of actions. If [code]max_actions[/code] is [code]-1[/code], there is no limit of actions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, "amount"

There should be an explanation of what happens after the limit is reached, likely that the oldest action (index 0) is forgotten by the UndoRedo. Also, would be ideal if what happens to action indices after the limit is reached is also clarified.

</description>
</method>
<method name="get_max_actions" qualifiers="const">
<return type="int">
</return>
<description>
Returns the max ammount of actions. Returns [code]-1[/code], when there is no limit of actions.
</description>
</method>
<method name="get_version" qualifiers="const">
<return type="int">
</return>
Expand Down