history_dock.cpp 8.9 KB

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