undo_redo.cpp 17 KB

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