history_dock.cpp 9.9 KB

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