Merge pull request #69761 from KoBeWi/where_undo

Add scope prefix to undo actions
This commit is contained in:
Rémi Verschelde 2022-12-14 23:28:40 +01:00
commit 1c5cfc4675
No known key found for this signature in database
GPG Key ID: C3336907360768E1
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); log->add_message(TTR("Can't undo while mouse buttons are pressed."), EditorLog::MSG_TYPE_EDITOR);
} else { } else {
String action = editor_data.get_undo_redo()->get_current_action_name(); 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()) { if (!editor_data.get_undo_redo()->undo()) {
log->add_message(TTR("Nothing to undo."), EditorLog::MSG_TYPE_EDITOR); log->add_message(TTR("Nothing to undo."), EditorLog::MSG_TYPE_EDITOR);
} else if (!action.is_empty()) { } 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; } 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); log->add_message(TTR("Nothing to redo."), EditorLog::MSG_TYPE_EDITOR);
} else { } else {
String action = editor_data.get_undo_redo()->get_current_action_name(); 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; } break;

View File

@ -270,35 +270,9 @@ bool EditorUndoRedoManager::undo() {
return false; return false;
} }
int selected_history = INVALID_HISTORY; History *selected_history = _get_newest_undo();
double global_timestamp = 0; if (selected_history) {
return undo_history(selected_history->id);
// 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);
} }
return false; return false;
} }
@ -427,6 +401,39 @@ void EditorUndoRedoManager::clear_history(bool p_increase_version, int p_idx) {
String EditorUndoRedoManager::get_current_action_name() { String EditorUndoRedoManager::get_current_action_name() {
if (has_undo()) { if (has_undo()) {
History *selected_history = _get_newest_undo();
if (selected_history) {
return selected_history->undo_redo->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];
if (history.undo_redo) {
memdelete(history.undo_redo);
history.undo_redo = nullptr;
}
if (p_erase_from_map) {
history_map.erase(p_idx);
}
}
EditorUndoRedoManager::History *EditorUndoRedoManager::_get_newest_undo() {
History *selected_history = nullptr; History *selected_history = nullptr;
double global_timestamp = 0; double global_timestamp = 0;
@ -454,25 +461,7 @@ String EditorUndoRedoManager::get_current_action_name() {
} }
} }
if (selected_history) { return selected_history;
return selected_history->undo_redo->get_current_action_name();
}
}
return "";
}
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];
if (history.undo_redo) {
memdelete(history.undo_redo);
history.undo_redo = nullptr;
}
if (p_erase_from_map) {
history_map.erase(p_idx);
}
} }
void EditorUndoRedoManager::_bind_methods() { void EditorUndoRedoManager::_bind_methods() {

View File

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