Add scope prefix to undo actions

This commit is contained in:
kobewi 2022-12-08 14:04:48 +01:00
parent 6318320c04
commit ff994585b3
3 changed files with 73 additions and 59 deletions

View File

@ -2754,11 +2754,20 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
log->add_message(TTR("Can't undo while mouse buttons are pressed."), EditorLog::MSG_TYPE_EDITOR);
} else {
String action = editor_data.get_undo_redo()->get_current_action_name();
int id = editor_data.get_undo_redo()->get_current_action_history_id();
if (!editor_data.get_undo_redo()->undo()) {
log->add_message(TTR("Nothing to undo."), EditorLog::MSG_TYPE_EDITOR);
} else if (!action.is_empty()) {
log->add_message(vformat(TTR("Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
switch (id) {
case EditorUndoRedoManager::GLOBAL_HISTORY:
log->add_message(vformat(TTR("Global Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
break;
case EditorUndoRedoManager::REMOTE_HISTORY:
log->add_message(vformat(TTR("Remote Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
break;
default:
log->add_message(vformat(TTR("Scene Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
}
}
}
} break;
@ -2770,7 +2779,20 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
log->add_message(TTR("Nothing to redo."), EditorLog::MSG_TYPE_EDITOR);
} else {
String action = editor_data.get_undo_redo()->get_current_action_name();
log->add_message(vformat(TTR("Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
if (action.is_empty()) {
break;
}
switch (editor_data.get_undo_redo()->get_current_action_history_id()) {
case EditorUndoRedoManager::GLOBAL_HISTORY:
log->add_message(vformat(TTR("Global Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
break;
case EditorUndoRedoManager::REMOTE_HISTORY:
log->add_message(vformat(TTR("Remote Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
break;
default:
log->add_message(vformat(TTR("Scene Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
}
}
}
} break;

View File

@ -270,35 +270,9 @@ bool EditorUndoRedoManager::undo() {
return false;
}
int selected_history = INVALID_HISTORY;
double global_timestamp = 0;
// Pick the history with greatest last action timestamp (either global or current scene).
{
History &history = get_or_create_history(GLOBAL_HISTORY);
if (!history.undo_stack.is_empty()) {
selected_history = history.id;
global_timestamp = history.undo_stack.back()->get().timestamp;
}
}
{
History &history = get_or_create_history(REMOTE_HISTORY);
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
selected_history = history.id;
global_timestamp = history.undo_stack.back()->get().timestamp;
}
}
{
History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
selected_history = history.id;
}
}
if (selected_history != INVALID_HISTORY) {
return undo_history(selected_history);
History *selected_history = _get_newest_undo();
if (selected_history) {
return undo_history(selected_history->id);
}
return false;
}
@ -427,33 +401,7 @@ void EditorUndoRedoManager::clear_history(bool p_increase_version, int p_idx) {
String EditorUndoRedoManager::get_current_action_name() {
if (has_undo()) {
History *selected_history = nullptr;
double global_timestamp = 0;
// Pick the history with greatest last action timestamp (either global or current scene).
{
History &history = get_or_create_history(GLOBAL_HISTORY);
if (!history.undo_stack.is_empty()) {
selected_history = &history;
global_timestamp = history.undo_stack.back()->get().timestamp;
}
}
{
History &history = get_or_create_history(REMOTE_HISTORY);
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
selected_history = &history;
global_timestamp = history.undo_stack.back()->get().timestamp;
}
}
{
History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
selected_history = &history;
}
}
History *selected_history = _get_newest_undo();
if (selected_history) {
return selected_history->undo_redo->get_current_action_name();
}
@ -461,6 +409,16 @@ String EditorUndoRedoManager::get_current_action_name() {
return "";
}
int EditorUndoRedoManager::get_current_action_history_id() {
if (has_undo()) {
History *selected_history = _get_newest_undo();
if (selected_history) {
return selected_history->id;
}
}
return INVALID_HISTORY;
}
void EditorUndoRedoManager::discard_history(int p_idx, bool p_erase_from_map) {
ERR_FAIL_COND(!history_map.has(p_idx));
History &history = history_map[p_idx];
@ -475,6 +433,37 @@ void EditorUndoRedoManager::discard_history(int p_idx, bool p_erase_from_map) {
}
}
EditorUndoRedoManager::History *EditorUndoRedoManager::_get_newest_undo() {
History *selected_history = nullptr;
double global_timestamp = 0;
// Pick the history with greatest last action timestamp (either global or current scene).
{
History &history = get_or_create_history(GLOBAL_HISTORY);
if (!history.undo_stack.is_empty()) {
selected_history = &history;
global_timestamp = history.undo_stack.back()->get().timestamp;
}
}
{
History &history = get_or_create_history(REMOTE_HISTORY);
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
selected_history = &history;
global_timestamp = history.undo_stack.back()->get().timestamp;
}
}
{
History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
selected_history = &history;
}
}
return selected_history;
}
void EditorUndoRedoManager::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode", "custom_context"), &EditorUndoRedoManager::create_action, DEFVAL(UndoRedo::MERGE_DISABLE), DEFVAL((Object *)nullptr));
ClassDB::bind_method(D_METHOD("commit_action", "execute"), &EditorUndoRedoManager::commit_action, DEFVAL(true));

View File

@ -66,6 +66,8 @@ private:
bool is_committing = false;
History *_get_newest_undo();
protected:
static void _bind_methods();
@ -127,6 +129,7 @@ public:
bool has_redo();
String get_current_action_name();
int get_current_action_history_id();
void discard_history(int p_idx, bool p_erase_from_map = true);
~EditorUndoRedoManager();