undo_redo.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /**************************************************************************/
  2. /* undo_redo.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 "undo_redo.h"
  31. #include "core/io/resource.h"
  32. #include "core/os/os.h"
  33. #include "core/templates/local_vector.h"
  34. void UndoRedo::Operation::delete_reference() {
  35. if (type != Operation::TYPE_REFERENCE) {
  36. return;
  37. }
  38. if (ref.is_valid()) {
  39. ref.unref();
  40. } else {
  41. Object *obj = ObjectDB::get_instance(object);
  42. if (obj) {
  43. memdelete(obj);
  44. }
  45. }
  46. }
  47. void UndoRedo::_discard_redo() {
  48. if (current_action == actions.size() - 1) {
  49. return;
  50. }
  51. for (int i = current_action + 1; i < actions.size(); i++) {
  52. for (Operation &E : actions.write[i].do_ops) {
  53. E.delete_reference();
  54. }
  55. //ERASE do data
  56. }
  57. actions.resize(current_action + 1);
  58. }
  59. bool UndoRedo::_redo(bool p_execute) {
  60. ERR_FAIL_COND_V(action_level > 0, false);
  61. if ((current_action + 1) >= actions.size()) {
  62. return false; //nothing to redo
  63. }
  64. current_action++;
  65. if (p_execute) {
  66. _process_operation_list(actions.write[current_action].do_ops.front());
  67. }
  68. version++;
  69. emit_signal(SNAME("version_changed"));
  70. return true;
  71. }
  72. void UndoRedo::create_action(const String &p_name, MergeMode p_mode, bool p_backward_undo_ops) {
  73. uint64_t ticks = OS::get_singleton()->get_ticks_msec();
  74. if (action_level == 0) {
  75. _discard_redo();
  76. // Check if the merge operation is valid
  77. if (p_mode != MERGE_DISABLE && actions.size() && actions[actions.size() - 1].name == p_name && actions[actions.size() - 1].backward_undo_ops == p_backward_undo_ops && actions[actions.size() - 1].last_tick + 800 > ticks) {
  78. current_action = actions.size() - 2;
  79. if (p_mode == MERGE_ENDS) {
  80. // Clear all do ops from last action if they are not forced kept
  81. LocalVector<List<Operation>::Element *> to_remove;
  82. for (List<Operation>::Element *E = actions.write[current_action + 1].do_ops.front(); E; E = E->next()) {
  83. if (!E->get().force_keep_in_merge_ends) {
  84. to_remove.push_back(E);
  85. }
  86. }
  87. for (List<Operation>::Element *E : to_remove) {
  88. // Delete all object references
  89. E->get().delete_reference();
  90. E->erase();
  91. }
  92. }
  93. actions.write[actions.size() - 1].last_tick = ticks;
  94. // Revert reverse from previous commit.
  95. if (actions[actions.size() - 1].backward_undo_ops) {
  96. actions.write[actions.size() - 1].undo_ops.reverse();
  97. }
  98. merge_mode = p_mode;
  99. merging = true;
  100. } else {
  101. Action new_action;
  102. new_action.name = p_name;
  103. new_action.last_tick = ticks;
  104. new_action.backward_undo_ops = p_backward_undo_ops;
  105. actions.push_back(new_action);
  106. merge_mode = MERGE_DISABLE;
  107. }
  108. }
  109. action_level++;
  110. force_keep_in_merge_ends = false;
  111. }
  112. void UndoRedo::add_do_method(const Callable &p_callable) {
  113. ERR_FAIL_COND(p_callable.is_null());
  114. ERR_FAIL_COND(action_level <= 0);
  115. ERR_FAIL_COND((current_action + 1) >= actions.size());
  116. Object *object = p_callable.get_object();
  117. ERR_FAIL_NULL(object);
  118. Operation do_op;
  119. do_op.callable = p_callable;
  120. do_op.object = p_callable.get_object_id();
  121. if (Object::cast_to<RefCounted>(object)) {
  122. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(object));
  123. }
  124. do_op.type = Operation::TYPE_METHOD;
  125. do_op.name = p_callable.get_method();
  126. actions.write[current_action + 1].do_ops.push_back(do_op);
  127. }
  128. void UndoRedo::add_undo_method(const Callable &p_callable) {
  129. ERR_FAIL_COND(p_callable.is_null());
  130. ERR_FAIL_COND(action_level <= 0);
  131. ERR_FAIL_COND((current_action + 1) >= actions.size());
  132. // No undo if the merge mode is MERGE_ENDS
  133. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  134. return;
  135. }
  136. Object *object = p_callable.get_object();
  137. ERR_FAIL_NULL(object);
  138. Operation undo_op;
  139. undo_op.callable = p_callable;
  140. undo_op.object = p_callable.get_object_id();
  141. if (Object::cast_to<RefCounted>(object)) {
  142. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(object));
  143. }
  144. undo_op.type = Operation::TYPE_METHOD;
  145. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  146. undo_op.name = p_callable.get_method();
  147. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  148. }
  149. void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  150. ERR_FAIL_COND(p_object == nullptr);
  151. ERR_FAIL_COND(action_level <= 0);
  152. ERR_FAIL_COND((current_action + 1) >= actions.size());
  153. Operation do_op;
  154. do_op.object = p_object->get_instance_id();
  155. if (Object::cast_to<RefCounted>(p_object)) {
  156. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  157. }
  158. do_op.type = Operation::TYPE_PROPERTY;
  159. do_op.name = p_property;
  160. do_op.value = p_value;
  161. actions.write[current_action + 1].do_ops.push_back(do_op);
  162. }
  163. void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  164. ERR_FAIL_COND(p_object == nullptr);
  165. ERR_FAIL_COND(action_level <= 0);
  166. ERR_FAIL_COND((current_action + 1) >= actions.size());
  167. // No undo if the merge mode is MERGE_ENDS
  168. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  169. return;
  170. }
  171. Operation undo_op;
  172. undo_op.object = p_object->get_instance_id();
  173. if (Object::cast_to<RefCounted>(p_object)) {
  174. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  175. }
  176. undo_op.type = Operation::TYPE_PROPERTY;
  177. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  178. undo_op.name = p_property;
  179. undo_op.value = p_value;
  180. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  181. }
  182. void UndoRedo::add_do_reference(Object *p_object) {
  183. ERR_FAIL_COND(p_object == nullptr);
  184. ERR_FAIL_COND(action_level <= 0);
  185. ERR_FAIL_COND((current_action + 1) >= actions.size());
  186. Operation do_op;
  187. do_op.object = p_object->get_instance_id();
  188. if (Object::cast_to<RefCounted>(p_object)) {
  189. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  190. }
  191. do_op.type = Operation::TYPE_REFERENCE;
  192. actions.write[current_action + 1].do_ops.push_back(do_op);
  193. }
  194. void UndoRedo::add_undo_reference(Object *p_object) {
  195. ERR_FAIL_COND(p_object == nullptr);
  196. ERR_FAIL_COND(action_level <= 0);
  197. ERR_FAIL_COND((current_action + 1) >= actions.size());
  198. // No undo if the merge mode is MERGE_ENDS
  199. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  200. return;
  201. }
  202. Operation undo_op;
  203. undo_op.object = p_object->get_instance_id();
  204. if (Object::cast_to<RefCounted>(p_object)) {
  205. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  206. }
  207. undo_op.type = Operation::TYPE_REFERENCE;
  208. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  209. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  210. }
  211. void UndoRedo::start_force_keep_in_merge_ends() {
  212. ERR_FAIL_COND(action_level <= 0);
  213. ERR_FAIL_COND((current_action + 1) >= actions.size());
  214. force_keep_in_merge_ends = true;
  215. }
  216. void UndoRedo::end_force_keep_in_merge_ends() {
  217. ERR_FAIL_COND(action_level <= 0);
  218. ERR_FAIL_COND((current_action + 1) >= actions.size());
  219. force_keep_in_merge_ends = false;
  220. }
  221. void UndoRedo::_pop_history_tail() {
  222. _discard_redo();
  223. if (!actions.size()) {
  224. return;
  225. }
  226. for (Operation &E : actions.write[0].undo_ops) {
  227. E.delete_reference();
  228. }
  229. actions.remove_at(0);
  230. if (current_action >= 0) {
  231. current_action--;
  232. }
  233. }
  234. bool UndoRedo::is_committing_action() const {
  235. return committing > 0;
  236. }
  237. void UndoRedo::commit_action(bool p_execute) {
  238. ERR_FAIL_COND(action_level <= 0);
  239. action_level--;
  240. if (action_level > 0) {
  241. return; //still nested
  242. }
  243. if (merging) {
  244. version--;
  245. merging = false;
  246. }
  247. if (actions[actions.size() - 1].backward_undo_ops) {
  248. actions.write[actions.size() - 1].undo_ops.reverse();
  249. }
  250. committing++;
  251. _redo(p_execute); // perform action
  252. committing--;
  253. if (callback && actions.size() > 0) {
  254. callback(callback_ud, actions[actions.size() - 1].name);
  255. }
  256. }
  257. void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
  258. const int PREALLOCATE_ARGS_COUNT = 16;
  259. LocalVector<const Variant *> args;
  260. args.reserve(PREALLOCATE_ARGS_COUNT);
  261. for (; E; E = E->next()) {
  262. Operation &op = E->get();
  263. Object *obj = ObjectDB::get_instance(op.object);
  264. if (!obj) { //may have been deleted and this is fine
  265. continue;
  266. }
  267. switch (op.type) {
  268. case Operation::TYPE_METHOD: {
  269. Callable::CallError ce;
  270. Variant ret;
  271. op.callable.callp(nullptr, 0, ret, ce);
  272. if (ce.error != Callable::CallError::CALL_OK) {
  273. ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, nullptr, 0, ce));
  274. }
  275. #ifdef TOOLS_ENABLED
  276. Resource *res = Object::cast_to<Resource>(obj);
  277. if (res) {
  278. res->set_edited(true);
  279. }
  280. #endif
  281. if (method_callback) {
  282. Vector<Variant> binds;
  283. if (op.callable.is_custom()) {
  284. CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(op.callable.get_custom());
  285. if (ccb) {
  286. binds = ccb->get_binds();
  287. }
  288. }
  289. if (binds.is_empty()) {
  290. method_callback(method_callback_ud, obj, op.name, nullptr, 0);
  291. } else {
  292. args.clear();
  293. for (int i = 0; i < binds.size(); i++) {
  294. args.push_back(&binds[i]);
  295. }
  296. method_callback(method_callback_ud, obj, op.name, args.ptr(), binds.size());
  297. }
  298. }
  299. } break;
  300. case Operation::TYPE_PROPERTY: {
  301. obj->set(op.name, op.value);
  302. #ifdef TOOLS_ENABLED
  303. Resource *res = Object::cast_to<Resource>(obj);
  304. if (res) {
  305. res->set_edited(true);
  306. }
  307. #endif
  308. if (property_callback) {
  309. property_callback(prop_callback_ud, obj, op.name, op.value);
  310. }
  311. } break;
  312. case Operation::TYPE_REFERENCE: {
  313. //do nothing
  314. } break;
  315. }
  316. }
  317. }
  318. bool UndoRedo::redo() {
  319. return _redo(true);
  320. }
  321. bool UndoRedo::undo() {
  322. ERR_FAIL_COND_V(action_level > 0, false);
  323. if (current_action < 0) {
  324. return false; //nothing to redo
  325. }
  326. _process_operation_list(actions.write[current_action].undo_ops.front());
  327. current_action--;
  328. version--;
  329. emit_signal(SNAME("version_changed"));
  330. return true;
  331. }
  332. int UndoRedo::get_history_count() {
  333. ERR_FAIL_COND_V(action_level > 0, -1);
  334. return actions.size();
  335. }
  336. int UndoRedo::get_current_action() {
  337. ERR_FAIL_COND_V(action_level > 0, -1);
  338. return current_action;
  339. }
  340. String UndoRedo::get_action_name(int p_id) {
  341. ERR_FAIL_INDEX_V(p_id, actions.size(), "");
  342. return actions[p_id].name;
  343. }
  344. void UndoRedo::clear_history(bool p_increase_version) {
  345. ERR_FAIL_COND(action_level > 0);
  346. _discard_redo();
  347. while (actions.size()) {
  348. _pop_history_tail();
  349. }
  350. if (p_increase_version) {
  351. version++;
  352. emit_signal(SNAME("version_changed"));
  353. }
  354. }
  355. String UndoRedo::get_current_action_name() const {
  356. ERR_FAIL_COND_V(action_level > 0, "");
  357. if (current_action < 0) {
  358. return "";
  359. }
  360. return actions[current_action].name;
  361. }
  362. int UndoRedo::get_action_level() const {
  363. return action_level;
  364. }
  365. bool UndoRedo::has_undo() const {
  366. return current_action >= 0;
  367. }
  368. bool UndoRedo::has_redo() const {
  369. return (current_action + 1) < actions.size();
  370. }
  371. uint64_t UndoRedo::get_version() const {
  372. return version;
  373. }
  374. void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) {
  375. callback = p_callback;
  376. callback_ud = p_ud;
  377. }
  378. void UndoRedo::set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud) {
  379. method_callback = p_method_callback;
  380. method_callback_ud = p_ud;
  381. }
  382. void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud) {
  383. property_callback = p_property_callback;
  384. prop_callback_ud = p_ud;
  385. }
  386. UndoRedo::~UndoRedo() {
  387. clear_history();
  388. }
  389. void UndoRedo::_bind_methods() {
  390. ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode", "backward_undo_ops"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE), DEFVAL(false));
  391. ClassDB::bind_method(D_METHOD("commit_action", "execute"), &UndoRedo::commit_action, DEFVAL(true));
  392. ClassDB::bind_method(D_METHOD("is_committing_action"), &UndoRedo::is_committing_action);
  393. ClassDB::bind_method(D_METHOD("add_do_method", "callable"), &UndoRedo::add_do_method);
  394. ClassDB::bind_method(D_METHOD("add_undo_method", "callable"), &UndoRedo::add_undo_method);
  395. ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &UndoRedo::add_do_property);
  396. ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &UndoRedo::add_undo_property);
  397. ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &UndoRedo::add_do_reference);
  398. ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &UndoRedo::add_undo_reference);
  399. ClassDB::bind_method(D_METHOD("start_force_keep_in_merge_ends"), &UndoRedo::start_force_keep_in_merge_ends);
  400. ClassDB::bind_method(D_METHOD("end_force_keep_in_merge_ends"), &UndoRedo::end_force_keep_in_merge_ends);
  401. ClassDB::bind_method(D_METHOD("get_history_count"), &UndoRedo::get_history_count);
  402. ClassDB::bind_method(D_METHOD("get_current_action"), &UndoRedo::get_current_action);
  403. ClassDB::bind_method(D_METHOD("get_action_name", "id"), &UndoRedo::get_action_name);
  404. ClassDB::bind_method(D_METHOD("clear_history", "increase_version"), &UndoRedo::clear_history, DEFVAL(true));
  405. ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name);
  406. ClassDB::bind_method(D_METHOD("has_undo"), &UndoRedo::has_undo);
  407. ClassDB::bind_method(D_METHOD("has_redo"), &UndoRedo::has_redo);
  408. ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version);
  409. ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo);
  410. ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo);
  411. ADD_SIGNAL(MethodInfo("version_changed"));
  412. BIND_ENUM_CONSTANT(MERGE_DISABLE);
  413. BIND_ENUM_CONSTANT(MERGE_ENDS);
  414. BIND_ENUM_CONSTANT(MERGE_ALL);
  415. }