undo_redo.cpp 16 KB

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