editor_undo_redo_manager.cpp 19 KB

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