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