history_dock.cpp 9.7 KB

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