history_dock.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_settings.h"
  36. #include "scene/gui/check_box.h"
  37. #include "scene/gui/item_list.h"
  38. struct SortActionsByTimestamp {
  39. bool operator()(const EditorUndoRedoManager::Action &l, const EditorUndoRedoManager::Action &r) const {
  40. return l.timestamp > r.timestamp;
  41. }
  42. };
  43. void HistoryDock::on_history_changed() {
  44. if (is_visible_in_tree()) {
  45. refresh_history();
  46. } else {
  47. need_refresh = true;
  48. }
  49. }
  50. void HistoryDock::refresh_history() {
  51. action_list->clear();
  52. bool include_scene = current_scene_checkbox->is_pressed();
  53. bool include_global = global_history_checkbox->is_pressed();
  54. if (!include_scene && !include_global) {
  55. action_list->add_item(TTRC("The Beginning"));
  56. action_list->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
  57. return;
  58. }
  59. const EditorUndoRedoManager::History &current_scene_history = ur_manager->get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
  60. const EditorUndoRedoManager::History &global_history = ur_manager->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY);
  61. Vector<EditorUndoRedoManager::Action> full_history;
  62. {
  63. int full_size = 0;
  64. if (include_scene) {
  65. full_size += current_scene_history.redo_stack.size() + current_scene_history.undo_stack.size();
  66. }
  67. if (include_global) {
  68. full_size += global_history.redo_stack.size() + global_history.undo_stack.size();
  69. }
  70. full_history.resize(full_size);
  71. }
  72. int i = 0;
  73. if (include_scene) {
  74. for (const EditorUndoRedoManager::Action &E : current_scene_history.redo_stack) {
  75. full_history.write[i] = E;
  76. i++;
  77. }
  78. for (const EditorUndoRedoManager::Action &E : current_scene_history.undo_stack) {
  79. full_history.write[i] = E;
  80. i++;
  81. }
  82. }
  83. if (include_global) {
  84. for (const EditorUndoRedoManager::Action &E : global_history.redo_stack) {
  85. full_history.write[i] = E;
  86. i++;
  87. }
  88. for (const EditorUndoRedoManager::Action &E : global_history.undo_stack) {
  89. full_history.write[i] = E;
  90. i++;
  91. }
  92. }
  93. full_history.sort_custom<SortActionsByTimestamp>();
  94. for (const EditorUndoRedoManager::Action &E : full_history) {
  95. action_list->add_item(E.action_name);
  96. if (E.history_id == EditorUndoRedoManager::GLOBAL_HISTORY) {
  97. action_list->set_item_custom_fg_color(-1, get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
  98. }
  99. }
  100. action_list->add_item(TTRC("The Beginning"));
  101. action_list->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
  102. refresh_version();
  103. }
  104. void HistoryDock::on_version_changed() {
  105. if (is_visible_in_tree()) {
  106. refresh_version();
  107. } else {
  108. need_refresh = true;
  109. }
  110. }
  111. void HistoryDock::refresh_version() {
  112. int idx = 0;
  113. bool include_scene = current_scene_checkbox->is_pressed();
  114. bool include_global = global_history_checkbox->is_pressed();
  115. if (!include_scene && !include_global) {
  116. current_version = idx;
  117. action_list->set_current(idx);
  118. return;
  119. }
  120. const EditorUndoRedoManager::History &current_scene_history = ur_manager->get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
  121. const EditorUndoRedoManager::History &global_history = ur_manager->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY);
  122. double newest_undo_timestamp = 0;
  123. if (include_scene && !current_scene_history.undo_stack.is_empty()) {
  124. newest_undo_timestamp = current_scene_history.undo_stack.front()->get().timestamp;
  125. }
  126. if (include_global && !global_history.undo_stack.is_empty()) {
  127. double global_undo_timestamp = global_history.undo_stack.front()->get().timestamp;
  128. if (global_undo_timestamp > newest_undo_timestamp) {
  129. newest_undo_timestamp = global_undo_timestamp;
  130. }
  131. }
  132. if (include_scene) {
  133. int skip = 0;
  134. for (const EditorUndoRedoManager::Action &E : current_scene_history.redo_stack) {
  135. if (E.timestamp < newest_undo_timestamp) {
  136. skip++;
  137. } else {
  138. break;
  139. }
  140. }
  141. idx += current_scene_history.redo_stack.size() - skip;
  142. }
  143. if (include_global) {
  144. int skip = 0;
  145. for (const EditorUndoRedoManager::Action &E : global_history.redo_stack) {
  146. if (E.timestamp < newest_undo_timestamp) {
  147. skip++;
  148. } else {
  149. break;
  150. }
  151. }
  152. idx += global_history.redo_stack.size() - skip;
  153. }
  154. current_version = idx;
  155. action_list->set_current(idx);
  156. }
  157. void HistoryDock::_save_layout_to_config(Ref<ConfigFile> p_layout, const String &p_section) const {
  158. p_layout->set_value(p_section, "dock_history_include_scene", current_scene_checkbox->is_pressed());
  159. p_layout->set_value(p_section, "dock_history_include_global", global_history_checkbox->is_pressed());
  160. }
  161. void HistoryDock::_load_layout_from_config(Ref<ConfigFile> p_layout, const String &p_section) {
  162. current_scene_checkbox->set_pressed_no_signal(p_layout->get_value(p_section, "dock_history_include_scene", true));
  163. global_history_checkbox->set_pressed_no_signal(p_layout->get_value(p_section, "dock_history_include_global", true));
  164. refresh_history();
  165. }
  166. void HistoryDock::seek_history(int p_index) {
  167. bool include_scene = current_scene_checkbox->is_pressed();
  168. bool include_global = global_history_checkbox->is_pressed();
  169. if (!include_scene && !include_global) {
  170. return;
  171. }
  172. int current_scene_id = EditorNode::get_editor_data().get_current_edited_scene_history_id();
  173. while (current_version < p_index) {
  174. if (include_scene) {
  175. if (include_global) {
  176. ur_manager->undo();
  177. } else {
  178. ur_manager->undo_history(current_scene_id);
  179. }
  180. } else {
  181. ur_manager->undo_history(EditorUndoRedoManager::GLOBAL_HISTORY);
  182. }
  183. }
  184. while (current_version > p_index) {
  185. if (include_scene) {
  186. if (include_global) {
  187. ur_manager->redo();
  188. } else {
  189. ur_manager->redo_history(current_scene_id);
  190. }
  191. } else {
  192. ur_manager->redo_history(EditorUndoRedoManager::GLOBAL_HISTORY);
  193. }
  194. }
  195. }
  196. void HistoryDock::_notification(int p_notification) {
  197. switch (p_notification) {
  198. case NOTIFICATION_READY: {
  199. EditorNode::get_singleton()->connect("scene_changed", callable_mp(this, &HistoryDock::on_history_changed));
  200. } break;
  201. case NOTIFICATION_VISIBILITY_CHANGED: {
  202. if (is_visible_in_tree() && need_refresh) {
  203. refresh_history();
  204. }
  205. } break;
  206. }
  207. }
  208. void HistoryDock::_bind_methods() {
  209. ClassDB::bind_method(D_METHOD("_save_layout_to_config"), &HistoryDock::_save_layout_to_config);
  210. ClassDB::bind_method(D_METHOD("_load_layout_from_config"), &HistoryDock::_load_layout_from_config);
  211. }
  212. HistoryDock::HistoryDock() {
  213. set_name("History");
  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. HBoxContainer *mode_hb = memnew(HBoxContainer);
  218. add_child(mode_hb);
  219. current_scene_checkbox = memnew(CheckBox);
  220. mode_hb->add_child(current_scene_checkbox);
  221. current_scene_checkbox->set_flat(true);
  222. current_scene_checkbox->set_text(TTRC("Scene"));
  223. current_scene_checkbox->set_h_size_flags(SIZE_EXPAND_FILL);
  224. current_scene_checkbox->set_clip_text(true);
  225. current_scene_checkbox->set_pressed(true);
  226. current_scene_checkbox->connect(SceneStringName(toggled), callable_mp(this, &HistoryDock::refresh_history).unbind(1));
  227. global_history_checkbox = memnew(CheckBox);
  228. mode_hb->add_child(global_history_checkbox);
  229. global_history_checkbox->set_flat(true);
  230. global_history_checkbox->set_text(TTRC("Global"));
  231. global_history_checkbox->set_h_size_flags(SIZE_EXPAND_FILL);
  232. global_history_checkbox->set_clip_text(true);
  233. global_history_checkbox->set_pressed(true);
  234. global_history_checkbox->connect(SceneStringName(toggled), callable_mp(this, &HistoryDock::refresh_history).unbind(1));
  235. action_list = memnew(ItemList);
  236. action_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  237. add_child(action_list);
  238. action_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  239. action_list->connect(SceneStringName(item_selected), callable_mp(this, &HistoryDock::seek_history));
  240. }