editor_undo_redo_manager.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 "editor_undo_redo_manager.compat.inc"
  32. #include "core/io/resource.h"
  33. #include "core/os/os.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<EditorDebuggerRemoteObjects>(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_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_editor_data().get_current_edited_scene_history_id();
  73. if (idx > 0) {
  74. history_id = idx;
  75. }
  76. } else {
  77. int idx = EditorNode::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;
  95. if (!forced_history) {
  96. history_id = get_history_id_for_object(p_object);
  97. 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));
  98. } else {
  99. history_id = pending_action.history_id;
  100. }
  101. History &history = get_or_create_history(history_id);
  102. if (pending_action.history_id == INVALID_HISTORY) {
  103. pending_action.history_id = history_id;
  104. history.undo_redo->create_action(pending_action.action_name, pending_action.merge_mode, pending_action.backward_undo_ops);
  105. }
  106. return history;
  107. }
  108. void EditorUndoRedoManager::force_fixed_history() {
  109. ERR_FAIL_COND_MSG(pending_action.history_id == INVALID_HISTORY, "The current action has no valid history assigned.");
  110. forced_history = true;
  111. }
  112. void EditorUndoRedoManager::create_action_for_history(const String &p_name, int p_history_id, UndoRedo::MergeMode p_mode, bool p_backward_undo_ops, bool p_mark_unsaved) {
  113. if (pending_action.history_id != INVALID_HISTORY) {
  114. // Nested action.
  115. p_history_id = pending_action.history_id;
  116. } else {
  117. pending_action.action_name = p_name;
  118. pending_action.timestamp = OS::get_singleton()->get_unix_time();
  119. pending_action.merge_mode = p_mode;
  120. pending_action.backward_undo_ops = p_backward_undo_ops;
  121. pending_action.mark_unsaved = p_mark_unsaved;
  122. }
  123. if (p_history_id != INVALID_HISTORY) {
  124. pending_action.history_id = p_history_id;
  125. History &history = get_or_create_history(p_history_id);
  126. history.undo_redo->create_action(pending_action.action_name, pending_action.merge_mode, pending_action.backward_undo_ops);
  127. }
  128. }
  129. void EditorUndoRedoManager::create_action(const String &p_name, UndoRedo::MergeMode p_mode, Object *p_custom_context, bool p_backward_undo_ops, bool p_mark_unsaved) {
  130. create_action_for_history(p_name, INVALID_HISTORY, p_mode, p_backward_undo_ops, p_mark_unsaved);
  131. if (p_custom_context) {
  132. // This assigns history to pending action.
  133. get_history_for_object(p_custom_context);
  134. }
  135. }
  136. void EditorUndoRedoManager::add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
  137. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  138. undo_redo->add_do_method(Callable(p_object, p_method).bindp(p_args, p_argcount));
  139. }
  140. void EditorUndoRedoManager::add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
  141. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  142. undo_redo->add_undo_method(Callable(p_object, p_method).bindp(p_args, p_argcount));
  143. }
  144. void EditorUndoRedoManager::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  145. if (p_argcount < 2) {
  146. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  147. r_error.expected = 2;
  148. return;
  149. }
  150. if (p_args[0]->get_type() != Variant::OBJECT) {
  151. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  152. r_error.argument = 0;
  153. r_error.expected = Variant::OBJECT;
  154. return;
  155. }
  156. if (!p_args[1]->is_string()) {
  157. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  158. r_error.argument = 1;
  159. r_error.expected = Variant::STRING_NAME;
  160. return;
  161. }
  162. r_error.error = Callable::CallError::CALL_OK;
  163. Object *object = *p_args[0];
  164. StringName method = *p_args[1];
  165. add_do_methodp(object, method, p_args + 2, p_argcount - 2);
  166. }
  167. void EditorUndoRedoManager::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  168. if (p_argcount < 2) {
  169. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  170. r_error.expected = 2;
  171. return;
  172. }
  173. if (p_args[0]->get_type() != Variant::OBJECT) {
  174. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  175. r_error.argument = 0;
  176. r_error.expected = Variant::OBJECT;
  177. return;
  178. }
  179. if (!p_args[1]->is_string()) {
  180. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  181. r_error.argument = 1;
  182. r_error.expected = Variant::STRING_NAME;
  183. return;
  184. }
  185. r_error.error = Callable::CallError::CALL_OK;
  186. Object *object = *p_args[0];
  187. StringName method = *p_args[1];
  188. add_undo_methodp(object, method, p_args + 2, p_argcount - 2);
  189. }
  190. void EditorUndoRedoManager::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  191. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  192. undo_redo->add_do_property(p_object, p_property, p_value);
  193. }
  194. void EditorUndoRedoManager::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  195. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  196. undo_redo->add_undo_property(p_object, p_property, p_value);
  197. }
  198. void EditorUndoRedoManager::add_do_reference(Object *p_object) {
  199. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  200. undo_redo->add_do_reference(p_object);
  201. }
  202. void EditorUndoRedoManager::add_undo_reference(Object *p_object) {
  203. UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
  204. undo_redo->add_undo_reference(p_object);
  205. }
  206. void EditorUndoRedoManager::commit_action(bool p_execute) {
  207. if (pending_action.history_id == INVALID_HISTORY) {
  208. return; // Empty action, do nothing.
  209. }
  210. forced_history = false;
  211. is_committing = true;
  212. History &history = get_or_create_history(pending_action.history_id);
  213. bool merging = history.undo_redo->is_merging();
  214. history.undo_redo->commit_action(p_execute);
  215. if (history.undo_redo->get_action_level() > 0) {
  216. // Nested action.
  217. is_committing = false;
  218. return;
  219. }
  220. history.redo_stack.clear();
  221. // If you undo history beyond saved version and modify it, the saved version can no longer be restored.
  222. if (history.saved_version >= history.undo_redo->get_version()) {
  223. history.saved_version = UNSAVED_VERSION;
  224. }
  225. if (!merging) {
  226. history.undo_stack.push_back(pending_action);
  227. }
  228. if (history.id != GLOBAL_HISTORY) {
  229. // Clear global redo, to avoid unexpected actions when redoing.
  230. History &global = get_or_create_history(GLOBAL_HISTORY);
  231. global.redo_stack.clear();
  232. global.undo_redo->discard_redo();
  233. if (global.saved_version > global.undo_redo->get_version()) {
  234. global.saved_version = UNSAVED_VERSION;
  235. }
  236. } else {
  237. // On global actions, clear redo of all scenes instead.
  238. for (KeyValue<int, History> &E : history_map) {
  239. if (E.key == GLOBAL_HISTORY) {
  240. continue;
  241. }
  242. E.value.redo_stack.clear();
  243. E.value.undo_redo->discard_redo();
  244. if (E.value.saved_version > E.value.undo_redo->get_version()) {
  245. E.value.saved_version = UNSAVED_VERSION;
  246. }
  247. }
  248. }
  249. pending_action = Action();
  250. is_committing = false;
  251. emit_signal(SNAME("history_changed"));
  252. }
  253. bool EditorUndoRedoManager::is_committing_action() const {
  254. return is_committing;
  255. }
  256. bool EditorUndoRedoManager::undo() {
  257. if (!has_undo()) {
  258. return false;
  259. }
  260. History *selected_history = _get_newest_undo();
  261. if (selected_history) {
  262. return undo_history(selected_history->id);
  263. }
  264. return false;
  265. }
  266. bool EditorUndoRedoManager::undo_history(int p_id) {
  267. ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false);
  268. History &history = get_or_create_history(p_id);
  269. Action action = history.undo_stack.back()->get();
  270. history.undo_stack.pop_back();
  271. history.redo_stack.push_back(action);
  272. bool success = history.undo_redo->undo();
  273. if (success) {
  274. emit_signal(SNAME("version_changed"));
  275. }
  276. return success;
  277. }
  278. bool EditorUndoRedoManager::redo() {
  279. if (!has_redo()) {
  280. return false;
  281. }
  282. int selected_history = INVALID_HISTORY;
  283. double global_timestamp = Math::INF;
  284. // Pick the history with lowest last action timestamp (either global or current scene).
  285. {
  286. History &history = get_or_create_history(GLOBAL_HISTORY);
  287. if (!history.redo_stack.is_empty()) {
  288. selected_history = history.id;
  289. global_timestamp = history.redo_stack.back()->get().timestamp;
  290. }
  291. }
  292. {
  293. History &history = get_or_create_history(REMOTE_HISTORY);
  294. if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) {
  295. selected_history = history.id;
  296. global_timestamp = history.redo_stack.back()->get().timestamp;
  297. }
  298. }
  299. {
  300. History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
  301. if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) {
  302. selected_history = history.id;
  303. }
  304. }
  305. if (selected_history != INVALID_HISTORY) {
  306. return redo_history(selected_history);
  307. }
  308. return false;
  309. }
  310. bool EditorUndoRedoManager::redo_history(int p_id) {
  311. ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false);
  312. History &history = get_or_create_history(p_id);
  313. Action action = history.redo_stack.back()->get();
  314. history.redo_stack.pop_back();
  315. history.undo_stack.push_back(action);
  316. bool success = history.undo_redo->redo();
  317. if (success) {
  318. emit_signal(SNAME("version_changed"));
  319. }
  320. return success;
  321. }
  322. void EditorUndoRedoManager::set_history_as_saved(int p_id) {
  323. History &history = get_or_create_history(p_id);
  324. history.saved_version = history.undo_redo->get_version();
  325. }
  326. void EditorUndoRedoManager::set_history_as_unsaved(int p_id) {
  327. History &history = get_or_create_history(p_id);
  328. history.saved_version = UNSAVED_VERSION;
  329. }
  330. bool EditorUndoRedoManager::is_history_unsaved(int p_id) {
  331. History &history = get_or_create_history(p_id);
  332. if (history.saved_version == UNSAVED_VERSION) {
  333. return true;
  334. }
  335. int version_difference = history.undo_redo->get_version() - history.saved_version;
  336. if (version_difference > 0) {
  337. List<Action>::Element *E = history.undo_stack.back();
  338. for (int i = 0; i < version_difference; i++) {
  339. ERR_FAIL_NULL_V_MSG(E, false, "Inconsistent undo history.");
  340. if (E->get().mark_unsaved) {
  341. return true;
  342. }
  343. E = E->prev();
  344. }
  345. } else if (version_difference < 0) {
  346. List<Action>::Element *E = history.redo_stack.back();
  347. for (int i = 0; i > version_difference; i--) {
  348. ERR_FAIL_NULL_V_MSG(E, false, "Inconsistent redo history.");
  349. if (E->get().mark_unsaved) {
  350. return true;
  351. }
  352. E = E->prev();
  353. }
  354. }
  355. return false;
  356. }
  357. bool EditorUndoRedoManager::has_undo() {
  358. for (const KeyValue<int, History> &E : history_map) {
  359. 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()) {
  360. return true;
  361. }
  362. }
  363. return false;
  364. }
  365. bool EditorUndoRedoManager::has_redo() {
  366. for (const KeyValue<int, History> &E : history_map) {
  367. 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()) {
  368. return true;
  369. }
  370. }
  371. return false;
  372. }
  373. bool EditorUndoRedoManager::has_history(int p_idx) const {
  374. return history_map.has(p_idx);
  375. }
  376. void EditorUndoRedoManager::clear_history(int p_idx, bool p_increase_version) {
  377. if (p_idx != INVALID_HISTORY) {
  378. History &history = get_or_create_history(p_idx);
  379. history.undo_redo->clear_history(p_increase_version);
  380. history.undo_stack.clear();
  381. history.redo_stack.clear();
  382. if (p_increase_version) {
  383. history.saved_version = UNSAVED_VERSION;
  384. } else {
  385. set_history_as_saved(p_idx);
  386. }
  387. emit_signal(SNAME("history_changed"));
  388. return;
  389. }
  390. for (KeyValue<int, History> &E : history_map) {
  391. if (E.key == REMOTE_HISTORY) {
  392. continue;
  393. }
  394. E.value.undo_redo->clear_history(p_increase_version);
  395. E.value.undo_stack.clear();
  396. E.value.redo_stack.clear();
  397. set_history_as_saved(E.key);
  398. }
  399. emit_signal(SNAME("history_changed"));
  400. }
  401. String EditorUndoRedoManager::get_current_action_name() {
  402. if (has_undo()) {
  403. History *selected_history = _get_newest_undo();
  404. if (selected_history) {
  405. return selected_history->undo_redo->get_current_action_name();
  406. }
  407. }
  408. return "";
  409. }
  410. int EditorUndoRedoManager::get_current_action_history_id() {
  411. if (has_undo()) {
  412. History *selected_history = _get_newest_undo();
  413. if (selected_history) {
  414. return selected_history->id;
  415. }
  416. }
  417. return INVALID_HISTORY;
  418. }
  419. void EditorUndoRedoManager::discard_history(int p_idx, bool p_erase_from_map) {
  420. ERR_FAIL_COND(!history_map.has(p_idx));
  421. History &history = history_map[p_idx];
  422. if (history.undo_redo) {
  423. memdelete(history.undo_redo);
  424. history.undo_redo = nullptr;
  425. }
  426. if (p_erase_from_map) {
  427. history_map.erase(p_idx);
  428. }
  429. }
  430. EditorUndoRedoManager::History *EditorUndoRedoManager::_get_newest_undo() {
  431. History *selected_history = nullptr;
  432. double global_timestamp = 0;
  433. // Pick the history with greatest last action timestamp (either global or current scene).
  434. {
  435. History &history = get_or_create_history(GLOBAL_HISTORY);
  436. if (!history.undo_stack.is_empty()) {
  437. selected_history = &history;
  438. global_timestamp = history.undo_stack.back()->get().timestamp;
  439. }
  440. }
  441. {
  442. History &history = get_or_create_history(REMOTE_HISTORY);
  443. if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
  444. selected_history = &history;
  445. global_timestamp = history.undo_stack.back()->get().timestamp;
  446. }
  447. }
  448. {
  449. History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
  450. if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
  451. selected_history = &history;
  452. }
  453. }
  454. return selected_history;
  455. }
  456. void EditorUndoRedoManager::_bind_methods() {
  457. ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode", "custom_context", "backward_undo_ops", "mark_unsaved"), &EditorUndoRedoManager::create_action, DEFVAL(UndoRedo::MERGE_DISABLE), DEFVAL((Object *)nullptr), DEFVAL(false), DEFVAL(true));
  458. ClassDB::bind_method(D_METHOD("commit_action", "execute"), &EditorUndoRedoManager::commit_action, DEFVAL(true));
  459. ClassDB::bind_method(D_METHOD("is_committing_action"), &EditorUndoRedoManager::is_committing_action);
  460. ClassDB::bind_method(D_METHOD("force_fixed_history"), &EditorUndoRedoManager::force_fixed_history);
  461. {
  462. MethodInfo mi;
  463. mi.name = "add_do_method";
  464. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  465. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  466. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &EditorUndoRedoManager::_add_do_method, mi, varray(), false);
  467. }
  468. {
  469. MethodInfo mi;
  470. mi.name = "add_undo_method";
  471. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  472. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  473. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &EditorUndoRedoManager::_add_undo_method, mi, varray(), false);
  474. }
  475. ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &EditorUndoRedoManager::add_do_property);
  476. ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &EditorUndoRedoManager::add_undo_property);
  477. ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &EditorUndoRedoManager::add_do_reference);
  478. ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &EditorUndoRedoManager::add_undo_reference);
  479. ClassDB::bind_method(D_METHOD("get_object_history_id", "object"), &EditorUndoRedoManager::get_history_id_for_object);
  480. ClassDB::bind_method(D_METHOD("get_history_undo_redo", "id"), &EditorUndoRedoManager::get_history_undo_redo);
  481. ClassDB::bind_method(D_METHOD("clear_history", "id", "increase_version"), &EditorUndoRedoManager::clear_history, DEFVAL(INVALID_HISTORY), DEFVAL(true));
  482. ADD_SIGNAL(MethodInfo("history_changed"));
  483. ADD_SIGNAL(MethodInfo("version_changed"));
  484. BIND_ENUM_CONSTANT(GLOBAL_HISTORY);
  485. BIND_ENUM_CONSTANT(REMOTE_HISTORY);
  486. BIND_ENUM_CONSTANT(INVALID_HISTORY);
  487. }
  488. EditorUndoRedoManager *EditorUndoRedoManager::get_singleton() {
  489. return singleton;
  490. }
  491. EditorUndoRedoManager::EditorUndoRedoManager() {
  492. if (!singleton) {
  493. singleton = this;
  494. }
  495. }
  496. EditorUndoRedoManager::~EditorUndoRedoManager() {
  497. for (const KeyValue<int, History> &E : history_map) {
  498. discard_history(E.key, false);
  499. }
  500. }