history_dock.cpp 8.8 KB

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