editor_undo_redo_manager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /**************************************************************************/
  2. /* editor_undo_redo_manager.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 "editor_undo_redo_manager.h"
  31. #include "core/io/resource.h"
  32. #include "core/os/os.h"
  33. #include "core/templates/local_vector.h"
  34. #include "editor/debugger/editor_debugger_inspector.h"
  35. #include "editor/debugger/editor_debugger_node.h"
  36. #include "editor/editor_log.h"
  37. #include "editor/editor_node.h"
  38. #include "scene/main/node.h"
  39. EditorUndoRedoManager *EditorUndoRedoManager::singleton = nullptr;
  40. EditorUndoRedoManager::History &EditorUndoRedoManager::get_or_create_history(int p_idx) {
  41. if (!history_map.has(p_idx)) {
  42. History history;
  43. history.undo_redo = memnew(UndoRedo);
  44. history.id = p_idx;
  45. history_map[p_idx] = history;
  46. EditorNode::get_singleton()->get_log()->register_undo_redo(history.undo_redo);
  47. EditorDebuggerNode::get_singleton()->register_undo_redo(history.undo_redo);
  48. }
  49. return history_map[p_idx];
  50. }
  51. UndoRedo *EditorUndoRedoManager::get_history_undo_redo(int p_idx) const {
  52. ERR_FAIL_COND_V(!history_map.has(p_idx), nullptr);
  53. return history_map[p_idx].undo_redo;
  54. }
  55. int EditorUndoRedoManager::get_history_id_for_object(Object *p_object) const {
  56. int history_id = INVALID_HISTORY;
  57. if (Object::cast_to<EditorDebuggerRemoteObject>(p_object)) {
  58. return REMOTE_HISTORY;
  59. }
  60. if (Node *node = Object::cast_to<Node>(p_object)) {
  61. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  62. if (edited_scene && (node == edited_scene || edited_scene->is_ancestor_of(node))) {
  63. int idx = EditorNode::get_singleton()->get_editor_data().get_current_edited_scene_history_id();
  64. if (idx > 0) {
  65. history_id = idx;
  66. }
  67. }
  68. }
  69. if (Resource *res = Object::cast_to<Resource>(p_object)) {
  70. if (res->is_built_in()) {
  71. if (res->get_path().is_empty()) {
  72. int idx = EditorNode::get_singleton()->get_editor_data().get_current_edited_scene_history_id();
  73. if (idx > 0) {
  74. history_id = idx;
  75. }
  76. } else {
  77. int idx = EditorNode::get_singleton()->get_editor_data().get_scene_history_id_from_path(res->get_path().get_slice("::", 0));
  78. if (idx > 0) {
  79. history_id = idx;
  80. }
  81. }
  82. }
  83. }
  84. if (history_id == INVALID_HISTORY) {
  85. if (pending_action.history_id != INVALID_HISTORY) {
  86. history_id = pending_action.history_id;
  87. } else {
  88. history_id = GLOBAL_HISTORY;
  89. }
  90. }
  91. return history_id;
  92. }
  93. EditorUndoRedoManager::History &EditorUndoRedoManager::get_history_for_object(Object *p_object) {
  94. int history_id = get_history_id_for_object(p_object);
  95. ERR_FAIL_COND_V_MSG(pending_action.history_id != INVALID_HISTORY && history_id != pending_action.history_id, get_or_create_history(pending_action.history_id), vformat("UndoRedo history mismatch: expected %d, got %d.", pending_action.history_id, history_id));
  96. History &history = get_or_create_history(history_id);
  97. if (pending_action.history_id == INVALID_HISTORY) {
  98. pending_action.history_id = history_id;
  99. history.undo_redo->create_action(pending_action.action_name, pending_action.merge_mode);
  100. }
  101. return history;
  102. }
  103. void EditorUndoRedoManager::create_action_for_history(const String &p_name, int p_history_id, UndoRedo::MergeMode p_mode) {
  104. if (pending_action.history_id != INVALID_HISTORY) {
  105. // Nested action.
  106. p_history_id = pending_action.history_id;
  107. } else {
  108. pending_action.action_name = p_name;
  109. pending_action.timestamp = OS::get_singleton()->get_unix_time();
  110. pending_action.merge_mode = p_mode;
  111. }
  112. if (p_history_id != INVALID_HISTORY) {
  113. pending_action.history_id = p_history_id;
  114. History &history = get_or_create_history(p_history_id);
  115. history.undo_redo->create_action(pending_action.action_name, pending_action.merge_mode);
  116. }
  117. }
  118. void EditorUndoRedoManager::create_action(const String &p_name, UndoRedo::MergeMode p_mode, Object *p_custom_context) {
  119. create_action_for_history(p_name, INVALID_HISTORY, p_mode);
  120. if (p_custom_context) {
  121. // This assigns history to pending action.
  122. get_history_for_object(p_custom_context);
  123. }
  124. }
  125. void EditorUndoRedoManager::add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
  126. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  127. undo_redo->add_do_method(Callable(p_object, p_method).bindp(p_args, p_argcount));
  128. }
  129. void EditorUndoRedoManager::add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
  130. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  131. undo_redo->add_undo_method(Callable(p_object, p_method).bindp(p_args, p_argcount));
  132. }
  133. void EditorUndoRedoManager::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  134. if (p_argcount < 2) {
  135. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  136. r_error.argument = 0;
  137. return;
  138. }
  139. if (p_args[0]->get_type() != Variant::OBJECT) {
  140. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  141. r_error.argument = 0;
  142. r_error.expected = Variant::OBJECT;
  143. return;
  144. }
  145. if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
  146. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  147. r_error.argument = 1;
  148. r_error.expected = Variant::STRING_NAME;
  149. return;
  150. }
  151. r_error.error = Callable::CallError::CALL_OK;
  152. Object *object = *p_args[0];
  153. StringName method = *p_args[1];
  154. add_do_methodp(object, method, p_args + 2, p_argcount - 2);
  155. }
  156. void EditorUndoRedoManager::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  157. if (p_argcount < 2) {
  158. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  159. r_error.argument = 0;
  160. return;
  161. }
  162. if (p_args[0]->get_type() != Variant::OBJECT) {
  163. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  164. r_error.argument = 0;
  165. r_error.expected = Variant::OBJECT;
  166. return;
  167. }
  168. if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
  169. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  170. r_error.argument = 1;
  171. r_error.expected = Variant::STRING_NAME;
  172. return;
  173. }
  174. r_error.error = Callable::CallError::CALL_OK;
  175. Object *object = *p_args[0];
  176. StringName method = *p_args[1];
  177. add_undo_methodp(object, method, p_args + 2, p_argcount - 2);
  178. }
  179. void EditorUndoRedoManager::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  180. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  181. undo_redo->add_do_property(p_object, p_property, p_value);
  182. }
  183. void EditorUndoRedoManager::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  184. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  185. undo_redo->add_undo_property(p_object, p_property, p_value);
  186. }
  187. void EditorUndoRedoManager::add_do_reference(Object *p_object) {
  188. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  189. undo_redo->add_do_reference(p_object);
  190. }
  191. void EditorUndoRedoManager::add_undo_reference(Object *p_object) {
  192. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  193. undo_redo->add_undo_reference(p_object);
  194. }
  195. void EditorUndoRedoManager::commit_action(bool p_execute) {
  196. if (pending_action.history_id == INVALID_HISTORY) {
  197. return; // Empty action, do nothing.
  198. }
  199. is_committing = true;
  200. History &history = get_or_create_history(pending_action.history_id);
  201. history.undo_redo->commit_action(p_execute);
  202. history.redo_stack.clear();
  203. if (history.undo_redo->get_action_level() > 0) {
  204. // Nested action.
  205. is_committing = false;
  206. return;
  207. }
  208. if (!history.undo_stack.is_empty()) {
  209. const Action &prev_action = history.undo_stack.back()->get();
  210. if (pending_action.merge_mode != UndoRedo::MERGE_DISABLE && pending_action.merge_mode == prev_action.merge_mode && pending_action.action_name == prev_action.action_name) {
  211. // Discard action if it should be merged (UndoRedo handles merging internally).
  212. pending_action = Action();
  213. is_committing = false;
  214. return;
  215. }
  216. }
  217. history.undo_stack.push_back(pending_action);
  218. pending_action = Action();
  219. is_committing = false;
  220. emit_signal(SNAME("history_changed"));
  221. }
  222. bool EditorUndoRedoManager::is_committing_action() const {
  223. return is_committing;
  224. }
  225. bool EditorUndoRedoManager::undo() {
  226. if (!has_undo()) {
  227. return false;
  228. }
  229. History *selected_history = _get_newest_undo();
  230. if (selected_history) {
  231. return undo_history(selected_history->id);
  232. }
  233. return false;
  234. }
  235. bool EditorUndoRedoManager::undo_history(int p_id) {
  236. ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false);
  237. History &history = get_or_create_history(p_id);
  238. Action action = history.undo_stack.back()->get();
  239. history.undo_stack.pop_back();
  240. history.redo_stack.push_back(action);
  241. bool success = history.undo_redo->undo();
  242. if (success) {
  243. emit_signal(SNAME("version_changed"));
  244. }
  245. return success;
  246. }
  247. bool EditorUndoRedoManager::redo() {
  248. if (!has_redo()) {
  249. return false;
  250. }
  251. int selected_history = INVALID_HISTORY;
  252. double global_timestamp = INFINITY;
  253. // Pick the history with lowest last action timestamp (either global or current scene).
  254. {
  255. History &history = get_or_create_history(GLOBAL_HISTORY);
  256. if (!history.redo_stack.is_empty()) {
  257. selected_history = history.id;
  258. global_timestamp = history.redo_stack.back()->get().timestamp;
  259. }
  260. }
  261. {
  262. History &history = get_or_create_history(REMOTE_HISTORY);
  263. if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) {
  264. selected_history = history.id;
  265. global_timestamp = history.redo_stack.back()->get().timestamp;
  266. }
  267. }
  268. {
  269. History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
  270. if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) {
  271. selected_history = history.id;
  272. }
  273. }
  274. if (selected_history != INVALID_HISTORY) {
  275. return redo_history(selected_history);
  276. }
  277. return false;
  278. }
  279. bool EditorUndoRedoManager::redo_history(int p_id) {
  280. ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false);
  281. History &history = get_or_create_history(p_id);
  282. Action action = history.redo_stack.back()->get();
  283. history.redo_stack.pop_back();
  284. history.undo_stack.push_back(action);
  285. bool success = history.undo_redo->redo();
  286. if (success) {
  287. emit_signal(SNAME("version_changed"));
  288. }
  289. return success;
  290. }
  291. void EditorUndoRedoManager::set_history_as_saved(int p_id) {
  292. History &history = get_or_create_history(p_id);
  293. history.saved_version = history.undo_redo->get_version();
  294. }
  295. void EditorUndoRedoManager::set_history_as_unsaved(int p_id) {
  296. History &history = get_or_create_history(p_id);
  297. history.saved_version = -1;
  298. }
  299. bool EditorUndoRedoManager::is_history_unsaved(int p_id) {
  300. History &history = get_or_create_history(p_id);
  301. return history.undo_redo->get_version() != history.saved_version;
  302. }
  303. bool EditorUndoRedoManager::has_undo() {
  304. for (const KeyValue<int, History> &E : history_map) {
  305. if ((E.key == GLOBAL_HISTORY || E.key == REMOTE_HISTORY || E.key == EditorNode::get_editor_data().get_current_edited_scene_history_id()) && !E.value.undo_stack.is_empty()) {
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311. bool EditorUndoRedoManager::has_redo() {
  312. for (const KeyValue<int, History> &E : history_map) {
  313. if ((E.key == GLOBAL_HISTORY || E.key == REMOTE_HISTORY || E.key == EditorNode::get_editor_data().get_current_edited_scene_history_id()) && !E.value.redo_stack.is_empty()) {
  314. return true;
  315. }
  316. }
  317. return false;
  318. }
  319. void EditorUndoRedoManager::clear_history(bool p_increase_version, int p_idx) {
  320. if (p_idx != INVALID_HISTORY) {
  321. History &history = get_or_create_history(p_idx);
  322. history.undo_redo->clear_history(p_increase_version);
  323. history.undo_stack.clear();
  324. history.redo_stack.clear();
  325. if (!p_increase_version) {
  326. set_history_as_saved(p_idx);
  327. }
  328. emit_signal(SNAME("history_changed"));
  329. return;
  330. }
  331. for (const KeyValue<int, History> &E : history_map) {
  332. E.value.undo_redo->clear_history(p_increase_version);
  333. set_history_as_saved(E.key);
  334. }
  335. emit_signal(SNAME("history_changed"));
  336. }
  337. String EditorUndoRedoManager::get_current_action_name() {
  338. if (has_undo()) {
  339. History *selected_history = _get_newest_undo();
  340. if (selected_history) {
  341. return selected_history->undo_redo->get_current_action_name();
  342. }
  343. }
  344. return "";
  345. }
  346. int EditorUndoRedoManager::get_current_action_history_id() {
  347. if (has_undo()) {
  348. History *selected_history = _get_newest_undo();
  349. if (selected_history) {
  350. return selected_history->id;
  351. }
  352. }
  353. return INVALID_HISTORY;
  354. }
  355. void EditorUndoRedoManager::discard_history(int p_idx, bool p_erase_from_map) {
  356. ERR_FAIL_COND(!history_map.has(p_idx));
  357. History &history = history_map[p_idx];
  358. if (history.undo_redo) {
  359. memdelete(history.undo_redo);
  360. history.undo_redo = nullptr;
  361. }
  362. if (p_erase_from_map) {
  363. history_map.erase(p_idx);
  364. }
  365. }
  366. EditorUndoRedoManager::History *EditorUndoRedoManager::_get_newest_undo() {
  367. History *selected_history = nullptr;
  368. double global_timestamp = 0;
  369. // Pick the history with greatest last action timestamp (either global or current scene).
  370. {
  371. History &history = get_or_create_history(GLOBAL_HISTORY);
  372. if (!history.undo_stack.is_empty()) {
  373. selected_history = &history;
  374. global_timestamp = history.undo_stack.back()->get().timestamp;
  375. }
  376. }
  377. {
  378. History &history = get_or_create_history(REMOTE_HISTORY);
  379. if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
  380. selected_history = &history;
  381. global_timestamp = history.undo_stack.back()->get().timestamp;
  382. }
  383. }
  384. {
  385. History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
  386. if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
  387. selected_history = &history;
  388. }
  389. }
  390. return selected_history;
  391. }
  392. void EditorUndoRedoManager::_bind_methods() {
  393. ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode", "custom_context"), &EditorUndoRedoManager::create_action, DEFVAL(UndoRedo::MERGE_DISABLE), DEFVAL((Object *)nullptr));
  394. ClassDB::bind_method(D_METHOD("commit_action", "execute"), &EditorUndoRedoManager::commit_action, DEFVAL(true));
  395. ClassDB::bind_method(D_METHOD("is_committing_action"), &EditorUndoRedoManager::is_committing_action);
  396. {
  397. MethodInfo mi;
  398. mi.name = "add_do_method";
  399. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  400. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  401. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &EditorUndoRedoManager::_add_do_method, mi, varray(), false);
  402. }
  403. {
  404. MethodInfo mi;
  405. mi.name = "add_undo_method";
  406. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  407. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  408. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &EditorUndoRedoManager::_add_undo_method, mi, varray(), false);
  409. }
  410. ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &EditorUndoRedoManager::add_do_property);
  411. ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &EditorUndoRedoManager::add_undo_property);
  412. ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &EditorUndoRedoManager::add_do_reference);
  413. ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &EditorUndoRedoManager::add_undo_reference);
  414. ClassDB::bind_method(D_METHOD("get_object_history_id", "object"), &EditorUndoRedoManager::get_history_id_for_object);
  415. ClassDB::bind_method(D_METHOD("get_history_undo_redo", "id"), &EditorUndoRedoManager::get_history_undo_redo);
  416. ADD_SIGNAL(MethodInfo("history_changed"));
  417. ADD_SIGNAL(MethodInfo("version_changed"));
  418. BIND_ENUM_CONSTANT(GLOBAL_HISTORY);
  419. BIND_ENUM_CONSTANT(REMOTE_HISTORY);
  420. BIND_ENUM_CONSTANT(INVALID_HISTORY);
  421. }
  422. EditorUndoRedoManager *EditorUndoRedoManager::get_singleton() {
  423. return singleton;
  424. }
  425. EditorUndoRedoManager::EditorUndoRedoManager() {
  426. if (!singleton) {
  427. singleton = this;
  428. }
  429. }
  430. EditorUndoRedoManager::~EditorUndoRedoManager() {
  431. for (const KeyValue<int, History> &E : history_map) {
  432. discard_history(E.key, false);
  433. }
  434. }