undo_redo.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*************************************************************************/
  2. /* undo_redo.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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) {
  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].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 (unsigned int i = 0; i < to_remove.size(); i++) {
  88. List<Operation>::Element *E = to_remove[i];
  89. // Delete all object references
  90. E->get().delete_reference();
  91. E->erase();
  92. }
  93. }
  94. actions.write[actions.size() - 1].last_tick = ticks;
  95. merge_mode = p_mode;
  96. merging = true;
  97. } else {
  98. Action new_action;
  99. new_action.name = p_name;
  100. new_action.last_tick = ticks;
  101. actions.push_back(new_action);
  102. merge_mode = MERGE_DISABLE;
  103. }
  104. }
  105. action_level++;
  106. force_keep_in_merge_ends = false;
  107. }
  108. void UndoRedo::add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
  109. ERR_FAIL_COND(p_object == nullptr);
  110. ERR_FAIL_COND(action_level <= 0);
  111. ERR_FAIL_COND((current_action + 1) >= actions.size());
  112. Operation do_op;
  113. do_op.object = p_object->get_instance_id();
  114. if (Object::cast_to<RefCounted>(p_object)) {
  115. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  116. }
  117. do_op.type = Operation::TYPE_METHOD;
  118. do_op.name = p_method;
  119. for (int i = 0; i < p_argcount; i++) {
  120. do_op.args.push_back(*p_args[i]);
  121. }
  122. actions.write[current_action + 1].do_ops.push_back(do_op);
  123. }
  124. void UndoRedo::add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
  125. ERR_FAIL_COND(p_object == nullptr);
  126. ERR_FAIL_COND(action_level <= 0);
  127. ERR_FAIL_COND((current_action + 1) >= actions.size());
  128. // No undo if the merge mode is MERGE_ENDS
  129. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  130. return;
  131. }
  132. Operation undo_op;
  133. undo_op.object = p_object->get_instance_id();
  134. if (Object::cast_to<RefCounted>(p_object)) {
  135. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  136. }
  137. undo_op.type = Operation::TYPE_METHOD;
  138. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  139. undo_op.name = p_method;
  140. for (int i = 0; i < p_argcount; i++) {
  141. undo_op.args.push_back(*p_args[i]);
  142. }
  143. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  144. }
  145. void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  146. ERR_FAIL_COND(p_object == nullptr);
  147. ERR_FAIL_COND(action_level <= 0);
  148. ERR_FAIL_COND((current_action + 1) >= actions.size());
  149. Operation do_op;
  150. do_op.object = p_object->get_instance_id();
  151. if (Object::cast_to<RefCounted>(p_object)) {
  152. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  153. }
  154. do_op.type = Operation::TYPE_PROPERTY;
  155. do_op.name = p_property;
  156. do_op.args.push_back(p_value);
  157. actions.write[current_action + 1].do_ops.push_back(do_op);
  158. }
  159. void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  160. ERR_FAIL_COND(p_object == nullptr);
  161. ERR_FAIL_COND(action_level <= 0);
  162. ERR_FAIL_COND((current_action + 1) >= actions.size());
  163. // No undo if the merge mode is MERGE_ENDS
  164. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  165. return;
  166. }
  167. Operation undo_op;
  168. undo_op.object = p_object->get_instance_id();
  169. if (Object::cast_to<RefCounted>(p_object)) {
  170. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  171. }
  172. undo_op.type = Operation::TYPE_PROPERTY;
  173. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  174. undo_op.name = p_property;
  175. undo_op.args.push_back(p_value);
  176. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  177. }
  178. void UndoRedo::add_do_reference(Object *p_object) {
  179. ERR_FAIL_COND(p_object == nullptr);
  180. ERR_FAIL_COND(action_level <= 0);
  181. ERR_FAIL_COND((current_action + 1) >= actions.size());
  182. Operation do_op;
  183. do_op.object = p_object->get_instance_id();
  184. if (Object::cast_to<RefCounted>(p_object)) {
  185. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  186. }
  187. do_op.type = Operation::TYPE_REFERENCE;
  188. actions.write[current_action + 1].do_ops.push_back(do_op);
  189. }
  190. void UndoRedo::add_undo_reference(Object *p_object) {
  191. ERR_FAIL_COND(p_object == nullptr);
  192. ERR_FAIL_COND(action_level <= 0);
  193. ERR_FAIL_COND((current_action + 1) >= actions.size());
  194. // No undo if the merge mode is MERGE_ENDS
  195. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  196. return;
  197. }
  198. Operation undo_op;
  199. undo_op.object = p_object->get_instance_id();
  200. if (Object::cast_to<RefCounted>(p_object)) {
  201. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  202. }
  203. undo_op.type = Operation::TYPE_REFERENCE;
  204. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  205. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  206. }
  207. void UndoRedo::start_force_keep_in_merge_ends() {
  208. ERR_FAIL_COND(action_level <= 0);
  209. ERR_FAIL_COND((current_action + 1) >= actions.size());
  210. force_keep_in_merge_ends = true;
  211. }
  212. void UndoRedo::end_force_keep_in_merge_ends() {
  213. ERR_FAIL_COND(action_level <= 0);
  214. ERR_FAIL_COND((current_action + 1) >= actions.size());
  215. force_keep_in_merge_ends = false;
  216. }
  217. void UndoRedo::_pop_history_tail() {
  218. _discard_redo();
  219. if (!actions.size()) {
  220. return;
  221. }
  222. for (Operation &E : actions.write[0].undo_ops) {
  223. E.delete_reference();
  224. }
  225. actions.remove_at(0);
  226. if (current_action >= 0) {
  227. current_action--;
  228. }
  229. }
  230. bool UndoRedo::is_committing_action() const {
  231. return committing > 0;
  232. }
  233. void UndoRedo::commit_action(bool p_execute) {
  234. ERR_FAIL_COND(action_level <= 0);
  235. action_level--;
  236. if (action_level > 0) {
  237. return; //still nested
  238. }
  239. if (merging) {
  240. version--;
  241. merging = false;
  242. }
  243. committing++;
  244. _redo(p_execute); // perform action
  245. committing--;
  246. if (callback && actions.size() > 0) {
  247. callback(callback_ud, actions[actions.size() - 1].name);
  248. }
  249. }
  250. void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
  251. for (; E; E = E->next()) {
  252. Operation &op = E->get();
  253. Object *obj = ObjectDB::get_instance(op.object);
  254. if (!obj) { //may have been deleted and this is fine
  255. continue;
  256. }
  257. switch (op.type) {
  258. case Operation::TYPE_METHOD: {
  259. int argc = op.args.size();
  260. Vector<const Variant *> argptrs;
  261. argptrs.resize(argc);
  262. for (int i = 0; i < argc; i++) {
  263. argptrs.write[i] = &op.args[i];
  264. }
  265. Callable::CallError ce;
  266. obj->callp(op.name, (const Variant **)argptrs.ptr(), argc, ce);
  267. if (ce.error != Callable::CallError::CALL_OK) {
  268. ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, (const Variant **)argptrs.ptr(), argc, ce));
  269. }
  270. #ifdef TOOLS_ENABLED
  271. Resource *res = Object::cast_to<Resource>(obj);
  272. if (res) {
  273. res->set_edited(true);
  274. }
  275. #endif
  276. if (method_callback) {
  277. method_callback(method_callback_ud, obj, op.name, (const Variant **)argptrs.ptr(), argc);
  278. }
  279. } break;
  280. case Operation::TYPE_PROPERTY: {
  281. obj->set(op.name, op.args[0]);
  282. #ifdef TOOLS_ENABLED
  283. Resource *res = Object::cast_to<Resource>(obj);
  284. if (res) {
  285. res->set_edited(true);
  286. }
  287. #endif
  288. if (property_callback) {
  289. property_callback(prop_callback_ud, obj, op.name, op.args[0]);
  290. }
  291. } break;
  292. case Operation::TYPE_REFERENCE: {
  293. //do nothing
  294. } break;
  295. }
  296. }
  297. }
  298. bool UndoRedo::redo() {
  299. return _redo(true);
  300. }
  301. bool UndoRedo::undo() {
  302. ERR_FAIL_COND_V(action_level > 0, false);
  303. if (current_action < 0) {
  304. return false; //nothing to redo
  305. }
  306. _process_operation_list(actions.write[current_action].undo_ops.front());
  307. current_action--;
  308. version--;
  309. emit_signal(SNAME("version_changed"));
  310. return true;
  311. }
  312. int UndoRedo::get_history_count() {
  313. ERR_FAIL_COND_V(action_level > 0, -1);
  314. return actions.size();
  315. }
  316. int UndoRedo::get_current_action() {
  317. ERR_FAIL_COND_V(action_level > 0, -1);
  318. return current_action;
  319. }
  320. String UndoRedo::get_action_name(int p_id) {
  321. ERR_FAIL_INDEX_V(p_id, actions.size(), "");
  322. return actions[p_id].name;
  323. }
  324. void UndoRedo::clear_history(bool p_increase_version) {
  325. ERR_FAIL_COND(action_level > 0);
  326. _discard_redo();
  327. while (actions.size()) {
  328. _pop_history_tail();
  329. }
  330. if (p_increase_version) {
  331. version++;
  332. emit_signal(SNAME("version_changed"));
  333. }
  334. }
  335. String UndoRedo::get_current_action_name() const {
  336. ERR_FAIL_COND_V(action_level > 0, "");
  337. if (current_action < 0) {
  338. return "";
  339. }
  340. return actions[current_action].name;
  341. }
  342. bool UndoRedo::has_undo() const {
  343. return current_action >= 0;
  344. }
  345. bool UndoRedo::has_redo() const {
  346. return (current_action + 1) < actions.size();
  347. }
  348. uint64_t UndoRedo::get_version() const {
  349. return version;
  350. }
  351. void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) {
  352. callback = p_callback;
  353. callback_ud = p_ud;
  354. }
  355. void UndoRedo::set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud) {
  356. method_callback = p_method_callback;
  357. method_callback_ud = p_ud;
  358. }
  359. void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud) {
  360. property_callback = p_property_callback;
  361. prop_callback_ud = p_ud;
  362. }
  363. UndoRedo::~UndoRedo() {
  364. clear_history();
  365. }
  366. void UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  367. if (p_argcount < 2) {
  368. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  369. r_error.argument = 0;
  370. return;
  371. }
  372. if (p_args[0]->get_type() != Variant::OBJECT) {
  373. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  374. r_error.argument = 0;
  375. r_error.expected = Variant::OBJECT;
  376. return;
  377. }
  378. if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
  379. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  380. r_error.argument = 1;
  381. r_error.expected = Variant::STRING_NAME;
  382. return;
  383. }
  384. r_error.error = Callable::CallError::CALL_OK;
  385. Object *object = *p_args[0];
  386. StringName method = *p_args[1];
  387. add_do_methodp(object, method, p_args + 2, p_argcount - 2);
  388. }
  389. void UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  390. if (p_argcount < 2) {
  391. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  392. r_error.argument = 0;
  393. return;
  394. }
  395. if (p_args[0]->get_type() != Variant::OBJECT) {
  396. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  397. r_error.argument = 0;
  398. r_error.expected = Variant::OBJECT;
  399. return;
  400. }
  401. if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
  402. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  403. r_error.argument = 1;
  404. r_error.expected = Variant::STRING_NAME;
  405. return;
  406. }
  407. r_error.error = Callable::CallError::CALL_OK;
  408. Object *object = *p_args[0];
  409. StringName method = *p_args[1];
  410. add_undo_methodp(object, method, p_args + 2, p_argcount - 2);
  411. }
  412. void UndoRedo::_bind_methods() {
  413. ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE));
  414. ClassDB::bind_method(D_METHOD("commit_action", "execute"), &UndoRedo::commit_action, DEFVAL(true));
  415. ClassDB::bind_method(D_METHOD("is_committing_action"), &UndoRedo::is_committing_action);
  416. {
  417. MethodInfo mi;
  418. mi.name = "add_do_method";
  419. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  420. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  421. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &UndoRedo::_add_do_method, mi, varray(), false);
  422. }
  423. {
  424. MethodInfo mi;
  425. mi.name = "add_undo_method";
  426. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  427. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  428. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &UndoRedo::_add_undo_method, mi, varray(), false);
  429. }
  430. ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &UndoRedo::add_do_property);
  431. ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &UndoRedo::add_undo_property);
  432. ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &UndoRedo::add_do_reference);
  433. ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &UndoRedo::add_undo_reference);
  434. ClassDB::bind_method(D_METHOD("start_force_keep_in_merge_ends"), &UndoRedo::start_force_keep_in_merge_ends);
  435. ClassDB::bind_method(D_METHOD("end_force_keep_in_merge_ends"), &UndoRedo::end_force_keep_in_merge_ends);
  436. ClassDB::bind_method(D_METHOD("get_history_count"), &UndoRedo::get_history_count);
  437. ClassDB::bind_method(D_METHOD("get_current_action"), &UndoRedo::get_current_action);
  438. ClassDB::bind_method(D_METHOD("get_action_name", "id"), &UndoRedo::get_action_name);
  439. ClassDB::bind_method(D_METHOD("clear_history", "increase_version"), &UndoRedo::clear_history, DEFVAL(true));
  440. ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name);
  441. ClassDB::bind_method(D_METHOD("has_undo"), &UndoRedo::has_undo);
  442. ClassDB::bind_method(D_METHOD("has_redo"), &UndoRedo::has_redo);
  443. ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version);
  444. ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo);
  445. ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo);
  446. ADD_SIGNAL(MethodInfo("version_changed"));
  447. BIND_ENUM_CONSTANT(MERGE_DISABLE);
  448. BIND_ENUM_CONSTANT(MERGE_ENDS);
  449. BIND_ENUM_CONSTANT(MERGE_ALL);
  450. }