history_dock.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**************************************************************************/
  2. /* history_dock.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "history_dock.h"
  31. #include "core/io/config_file.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/settings/editor_command_palette.h"
  36. #include "editor/settings/editor_settings.h"
  37. #include "scene/gui/check_box.h"
  38. #include "scene/gui/item_list.h"
  39. struct SortActionsByTimestamp {
  40. bool operator()(const EditorUndoRedoManager::Action &l, const EditorUndoRedoManager::Action &r) const {
  41. return l.timestamp > r.timestamp;
  42. }
  43. };
  44. void HistoryDock::on_history_changed() {
  45. if (is_visible_in_tree()) {
  46. refresh_history();
  47. } else {
  48. need_refresh = true;
  49. }
  50. }
  51. void HistoryDock::refresh_history() {
  52. action_list->clear();
  53. bool include_scene = current_scene_checkbox->is_pressed();
  54. bool include_global = global_history_checkbox->is_pressed();
  55. if (!include_scene && !include_global) {
  56. action_list->add_item(TTRC("The Beginning"));
  57. action_list->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
  58. return;
  59. }
  60. const EditorUndoRedoManager::History &current_scene_history = ur_manager->get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
  61. const EditorUndoRedoManager::History &global_history = ur_manager->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY);
  62. Vector<EditorUndoRedoManager::Action> full_history;
  63. {
  64. int full_size = 0;
  65. if (include_scene) {
  66. full_size += current_scene_history.redo_stack.size() + current_scene_history.undo_stack.size();
  67. }
  68. if (include_global) {
  69. full_size += global_history.redo_stack.size() + global_history.undo_stack.size();
  70. }
  71. full_history.resize(full_size);
  72. }
  73. int i = 0;
  74. if (include_scene) {
  75. for (const EditorUndoRedoManager::Action &E : current_scene_history.redo_stack) {
  76. full_history.write[i] = E;
  77. i++;
  78. }
  79. for (const EditorUndoRedoManager::Action &E : current_scene_history.undo_stack) {
  80. full_history.write[i] = E;
  81. i++;
  82. }
  83. }
  84. if (include_global) {
  85. for (const EditorUndoRedoManager::Action &E : global_history.redo_stack) {
  86. full_history.write[i] = E;
  87. i++;
  88. }
  89. for (const EditorUndoRedoManager::Action &E : global_history.undo_stack) {
  90. full_history.write[i] = E;
  91. i++;
  92. }
  93. }
  94. full_history.sort_custom<SortActionsByTimestamp>();
  95. for (const EditorUndoRedoManager::Action &E : full_history) {
  96. action_list->add_item(E.action_name);
  97. if (E.history_id == EditorUndoRedoManager::GLOBAL_HISTORY) {
  98. action_list->set_item_custom_fg_color(-1, get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
  99. }
  100. }
  101. action_list->add_item(TTRC("The Beginning"));
  102. action_list->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
  103. refresh_version();
  104. }
  105. void HistoryDock::on_version_changed() {
  106. if (is_visible_in_tree()) {
  107. refresh_version();
  108. } else {
  109. need_refresh = true;
  110. }
  111. }
  112. void HistoryDock::refresh_version() {
  113. int idx = 0;
  114. bool include_scene = current_scene_checkbox->is_pressed();
  115. bool include_global = global_history_checkbox->is_pressed();
  116. if (!include_scene && !include_global) {
  117. current_version = idx;
  118. action_list->set_current(idx);
  119. return;
  120. }
  121. const EditorUndoRedoManager::History &current_scene_history = ur_manager->get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
  122. const EditorUndoRedoManager::History &global_history = ur_manager->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY);
  123. double newest_undo_timestamp = 0;
  124. if (include_scene && !current_scene_history.undo_stack.is_empty()) {
  125. newest_undo_timestamp = current_scene_history.undo_stack.front()->get().timestamp;
  126. }
  127. if (include_global && !global_history.undo_stack.is_empty()) {
  128. double global_undo_timestamp = global_history.undo_stack.front()->get().timestamp;
  129. if (global_undo_timestamp > newest_undo_timestamp) {
  130. newest_undo_timestamp = global_undo_timestamp;
  131. }
  132. }
  133. if (include_scene) {
  134. int skip = 0;
  135. for (const EditorUndoRedoManager::Action &E : current_scene_history.redo_stack) {
  136. if (E.timestamp < newest_undo_timestamp) {
  137. skip++;
  138. } else {
  139. break;
  140. }
  141. }
  142. idx += current_scene_history.redo_stack.size() - skip;
  143. }
  144. if (include_global) {
  145. int skip = 0;
  146. for (const EditorUndoRedoManager::Action &E : global_history.redo_stack) {
  147. if (E.timestamp < newest_undo_timestamp) {
  148. skip++;
  149. } else {
  150. break;
  151. }
  152. }
  153. idx += global_history.redo_stack.size() - skip;
  154. }
  155. current_version = idx;
  156. action_list->set_current(idx);
  157. }
  158. void HistoryDock::save_layout_to_config(Ref<ConfigFile> &p_layout, const String &p_section) const {
  159. p_layout->set_value(p_section, "include_scene", current_scene_checkbox->is_pressed());
  160. p_layout->set_value(p_section, "include_global", global_history_checkbox->is_pressed());
  161. }
  162. void HistoryDock::load_layout_from_config(const Ref<ConfigFile> &p_layout, const String &p_section) {
  163. current_scene_checkbox->set_pressed_no_signal(p_layout->get_value(p_section, "include_scene", true));
  164. global_history_checkbox->set_pressed_no_signal(p_layout->get_value(p_section, "include_global", true));
  165. refresh_history();
  166. }
  167. void HistoryDock::seek_history(int p_index) {
  168. bool include_scene = current_scene_checkbox->is_pressed();
  169. bool include_global = global_history_checkbox->is_pressed();
  170. if (!include_scene && !include_global) {
  171. return;
  172. }
  173. int current_scene_id = EditorNode::get_editor_data().get_current_edited_scene_history_id();
  174. while (current_version < p_index) {
  175. if (include_scene) {
  176. if (include_global) {
  177. ur_manager->undo();
  178. } else {
  179. ur_manager->undo_history(current_scene_id);
  180. }
  181. } else {
  182. ur_manager->undo_history(EditorUndoRedoManager::GLOBAL_HISTORY);
  183. }
  184. }
  185. while (current_version > p_index) {
  186. if (include_scene) {
  187. if (include_global) {
  188. ur_manager->redo();
  189. } else {
  190. ur_manager->redo_history(current_scene_id);
  191. }
  192. } else {
  193. ur_manager->redo_history(EditorUndoRedoManager::GLOBAL_HISTORY);
  194. }
  195. }
  196. }
  197. void HistoryDock::_notification(int p_notification) {
  198. switch (p_notification) {
  199. case NOTIFICATION_READY: {
  200. EditorNode::get_singleton()->connect("scene_changed", callable_mp(this, &HistoryDock::on_history_changed));
  201. } break;
  202. case NOTIFICATION_VISIBILITY_CHANGED: {
  203. if (is_visible_in_tree() && need_refresh) {
  204. refresh_history();
  205. }
  206. } break;
  207. }
  208. }
  209. HistoryDock::HistoryDock() {
  210. set_name(TTRC("History"));
  211. set_icon_name("History");
  212. set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("docks/open_history", TTRC("Open History Dock")));
  213. set_default_slot(EditorDock::DOCK_SLOT_LEFT_BR);
  214. ur_manager = EditorUndoRedoManager::get_singleton();
  215. ur_manager->connect("history_changed", callable_mp(this, &HistoryDock::on_history_changed));
  216. ur_manager->connect("version_changed", callable_mp(this, &HistoryDock::on_version_changed));
  217. VBoxContainer *main_vb = memnew(VBoxContainer);
  218. add_child(main_vb);
  219. HBoxContainer *mode_hb = memnew(HBoxContainer);
  220. main_vb->add_child(mode_hb);
  221. current_scene_checkbox = memnew(CheckBox);
  222. mode_hb->add_child(current_scene_checkbox);
  223. current_scene_checkbox->set_flat(true);
  224. current_scene_checkbox->set_text(TTRC("Scene"));
  225. current_scene_checkbox->set_h_size_flags(SIZE_EXPAND_FILL);
  226. current_scene_checkbox->set_clip_text(true);
  227. current_scene_checkbox->set_pressed(true);
  228. current_scene_checkbox->connect(SceneStringName(toggled), callable_mp(this, &HistoryDock::refresh_history).unbind(1));
  229. global_history_checkbox = memnew(CheckBox);
  230. mode_hb->add_child(global_history_checkbox);
  231. global_history_checkbox->set_flat(true);
  232. global_history_checkbox->set_text(TTRC("Global"));
  233. global_history_checkbox->set_h_size_flags(SIZE_EXPAND_FILL);
  234. global_history_checkbox->set_clip_text(true);
  235. global_history_checkbox->set_pressed(true);
  236. global_history_checkbox->connect(SceneStringName(toggled), callable_mp(this, &HistoryDock::refresh_history).unbind(1));
  237. MarginContainer *mc = memnew(MarginContainer);
  238. mc->set_theme_type_variation("NoBorderHorizontalBottom");
  239. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  240. main_vb->add_child(mc);
  241. action_list = memnew(ItemList);
  242. action_list->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_TOP);
  243. action_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  244. mc->add_child(action_list);
  245. action_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  246. action_list->connect(SceneStringName(item_selected), callable_mp(this, &HistoryDock::seek_history));
  247. }