undo_redo.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*************************************************************************/
  2. /* undo_redo.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. void UndoRedo::_discard_redo() {
  34. if (current_action == actions.size() - 1) {
  35. return;
  36. }
  37. for (int i = current_action + 1; i < actions.size(); i++) {
  38. for (List<Operation>::Element *E = actions.write[i].do_ops.front(); E; E = E->next()) {
  39. if (E->get().type == Operation::TYPE_REFERENCE) {
  40. Object *obj = ObjectDB::get_instance(E->get().object);
  41. if (obj) {
  42. memdelete(obj);
  43. }
  44. }
  45. }
  46. //ERASE do data
  47. }
  48. actions.resize(current_action + 1);
  49. }
  50. bool UndoRedo::_redo(bool p_execute) {
  51. ERR_FAIL_COND_V(action_level > 0, false);
  52. if ((current_action + 1) >= actions.size()) {
  53. return false; //nothing to redo
  54. }
  55. current_action++;
  56. if (p_execute) {
  57. _process_operation_list(actions.write[current_action].do_ops.front());
  58. }
  59. version++;
  60. emit_signal("version_changed");
  61. return true;
  62. }
  63. void UndoRedo::create_action(const String &p_name, MergeMode p_mode) {
  64. uint32_t ticks = OS::get_singleton()->get_ticks_msec();
  65. if (action_level == 0) {
  66. _discard_redo();
  67. // Check if the merge operation is valid
  68. if (p_mode != MERGE_DISABLE && actions.size() && actions[actions.size() - 1].name == p_name && actions[actions.size() - 1].last_tick + 800 > ticks) {
  69. current_action = actions.size() - 2;
  70. if (p_mode == MERGE_ENDS) {
  71. // Clear all do ops from last action, and delete all object references
  72. List<Operation>::Element *E = actions.write[current_action + 1].do_ops.front();
  73. while (E) {
  74. if (E->get().type == Operation::TYPE_REFERENCE) {
  75. Object *obj = ObjectDB::get_instance(E->get().object);
  76. if (obj) {
  77. memdelete(obj);
  78. }
  79. }
  80. E = E->next();
  81. actions.write[current_action + 1].do_ops.pop_front();
  82. }
  83. }
  84. actions.write[actions.size() - 1].last_tick = ticks;
  85. merge_mode = p_mode;
  86. merging = true;
  87. } else {
  88. Action new_action;
  89. new_action.name = p_name;
  90. new_action.last_tick = ticks;
  91. actions.push_back(new_action);
  92. merge_mode = MERGE_DISABLE;
  93. }
  94. }
  95. action_level++;
  96. }
  97. void UndoRedo::add_do_method(Object *p_object, const StringName &p_method, VARIANT_ARG_DECLARE) {
  98. VARIANT_ARGPTRS
  99. ERR_FAIL_COND(p_object == nullptr);
  100. ERR_FAIL_COND(action_level <= 0);
  101. ERR_FAIL_COND((current_action + 1) >= actions.size());
  102. Operation do_op;
  103. do_op.object = p_object->get_instance_id();
  104. if (Object::cast_to<Reference>(p_object)) {
  105. do_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object));
  106. }
  107. do_op.type = Operation::TYPE_METHOD;
  108. do_op.name = p_method;
  109. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  110. do_op.args[i] = *argptr[i];
  111. }
  112. actions.write[current_action + 1].do_ops.push_back(do_op);
  113. }
  114. void UndoRedo::add_undo_method(Object *p_object, const StringName &p_method, VARIANT_ARG_DECLARE) {
  115. VARIANT_ARGPTRS
  116. ERR_FAIL_COND(p_object == nullptr);
  117. ERR_FAIL_COND(action_level <= 0);
  118. ERR_FAIL_COND((current_action + 1) >= actions.size());
  119. // No undo if the merge mode is MERGE_ENDS
  120. if (merge_mode == MERGE_ENDS) {
  121. return;
  122. }
  123. Operation undo_op;
  124. undo_op.object = p_object->get_instance_id();
  125. if (Object::cast_to<Reference>(p_object)) {
  126. undo_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object));
  127. }
  128. undo_op.type = Operation::TYPE_METHOD;
  129. undo_op.name = p_method;
  130. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  131. undo_op.args[i] = *argptr[i];
  132. }
  133. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  134. }
  135. void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  136. ERR_FAIL_COND(p_object == nullptr);
  137. ERR_FAIL_COND(action_level <= 0);
  138. ERR_FAIL_COND((current_action + 1) >= actions.size());
  139. Operation do_op;
  140. do_op.object = p_object->get_instance_id();
  141. if (Object::cast_to<Reference>(p_object)) {
  142. do_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object));
  143. }
  144. do_op.type = Operation::TYPE_PROPERTY;
  145. do_op.name = p_property;
  146. do_op.args[0] = p_value;
  147. actions.write[current_action + 1].do_ops.push_back(do_op);
  148. }
  149. void UndoRedo::add_undo_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. // No undo if the merge mode is MERGE_ENDS
  154. if (merge_mode == MERGE_ENDS) {
  155. return;
  156. }
  157. Operation undo_op;
  158. undo_op.object = p_object->get_instance_id();
  159. if (Object::cast_to<Reference>(p_object)) {
  160. undo_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object));
  161. }
  162. undo_op.type = Operation::TYPE_PROPERTY;
  163. undo_op.name = p_property;
  164. undo_op.args[0] = p_value;
  165. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  166. }
  167. void UndoRedo::add_do_reference(Object *p_object) {
  168. ERR_FAIL_COND(p_object == nullptr);
  169. ERR_FAIL_COND(action_level <= 0);
  170. ERR_FAIL_COND((current_action + 1) >= actions.size());
  171. Operation do_op;
  172. do_op.object = p_object->get_instance_id();
  173. if (Object::cast_to<Reference>(p_object)) {
  174. do_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object));
  175. }
  176. do_op.type = Operation::TYPE_REFERENCE;
  177. actions.write[current_action + 1].do_ops.push_back(do_op);
  178. }
  179. void UndoRedo::add_undo_reference(Object *p_object) {
  180. ERR_FAIL_COND(p_object == nullptr);
  181. ERR_FAIL_COND(action_level <= 0);
  182. ERR_FAIL_COND((current_action + 1) >= actions.size());
  183. // No undo if the merge mode is MERGE_ENDS
  184. if (merge_mode == MERGE_ENDS) {
  185. return;
  186. }
  187. Operation undo_op;
  188. undo_op.object = p_object->get_instance_id();
  189. if (Object::cast_to<Reference>(p_object)) {
  190. undo_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object));
  191. }
  192. undo_op.type = Operation::TYPE_REFERENCE;
  193. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  194. }
  195. void UndoRedo::_pop_history_tail() {
  196. _discard_redo();
  197. if (!actions.size()) {
  198. return;
  199. }
  200. for (List<Operation>::Element *E = actions.write[0].undo_ops.front(); E; E = E->next()) {
  201. if (E->get().type == Operation::TYPE_REFERENCE) {
  202. Object *obj = ObjectDB::get_instance(E->get().object);
  203. if (obj) {
  204. memdelete(obj);
  205. }
  206. }
  207. }
  208. actions.remove(0);
  209. if (current_action >= 0) {
  210. current_action--;
  211. }
  212. }
  213. bool UndoRedo::is_committing_action() const {
  214. return committing > 0;
  215. }
  216. void UndoRedo::commit_action(bool p_execute) {
  217. ERR_FAIL_COND(action_level <= 0);
  218. action_level--;
  219. if (action_level > 0) {
  220. return; //still nested
  221. }
  222. if (merging) {
  223. version--;
  224. merging = false;
  225. }
  226. committing++;
  227. _redo(p_execute); // perform action
  228. committing--;
  229. if (callback && actions.size() > 0) {
  230. callback(callback_ud, actions[actions.size() - 1].name);
  231. }
  232. }
  233. void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
  234. for (; E; E = E->next()) {
  235. Operation &op = E->get();
  236. Object *obj = ObjectDB::get_instance(op.object);
  237. if (!obj) { //may have been deleted and this is fine
  238. continue;
  239. }
  240. switch (op.type) {
  241. case Operation::TYPE_METHOD: {
  242. Vector<const Variant *> argptrs;
  243. argptrs.resize(VARIANT_ARG_MAX);
  244. int argc = 0;
  245. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  246. if (op.args[i].get_type() == Variant::NIL) {
  247. break;
  248. }
  249. argptrs.write[i] = &op.args[i];
  250. argc++;
  251. }
  252. argptrs.resize(argc);
  253. Callable::CallError ce;
  254. obj->call(op.name, (const Variant **)argptrs.ptr(), argc, ce);
  255. if (ce.error != Callable::CallError::CALL_OK) {
  256. ERR_PRINT("Error calling method from signal '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, (const Variant **)argptrs.ptr(), argc, ce));
  257. }
  258. #ifdef TOOLS_ENABLED
  259. Resource *res = Object::cast_to<Resource>(obj);
  260. if (res) {
  261. res->set_edited(true);
  262. }
  263. #endif
  264. if (method_callback) {
  265. method_callback(method_callbck_ud, obj, op.name, VARIANT_ARGS_FROM_ARRAY(op.args));
  266. }
  267. } break;
  268. case Operation::TYPE_PROPERTY: {
  269. obj->set(op.name, op.args[0]);
  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 (property_callback) {
  277. property_callback(prop_callback_ud, obj, op.name, op.args[0]);
  278. }
  279. } break;
  280. case Operation::TYPE_REFERENCE: {
  281. //do nothing
  282. } break;
  283. }
  284. }
  285. }
  286. bool UndoRedo::redo() {
  287. return _redo(true);
  288. }
  289. bool UndoRedo::undo() {
  290. ERR_FAIL_COND_V(action_level > 0, false);
  291. if (current_action < 0) {
  292. return false; //nothing to redo
  293. }
  294. _process_operation_list(actions.write[current_action].undo_ops.front());
  295. current_action--;
  296. version--;
  297. emit_signal("version_changed");
  298. return true;
  299. }
  300. int UndoRedo::get_history_count() {
  301. ERR_FAIL_COND_V(action_level > 0, -1);
  302. return actions.size();
  303. }
  304. int UndoRedo::get_current_action() {
  305. ERR_FAIL_COND_V(action_level > 0, -1);
  306. return current_action;
  307. }
  308. String UndoRedo::get_action_name(int p_id) {
  309. ERR_FAIL_INDEX_V(p_id, actions.size(), "");
  310. return actions[p_id].name;
  311. }
  312. void UndoRedo::clear_history(bool p_increase_version) {
  313. ERR_FAIL_COND(action_level > 0);
  314. _discard_redo();
  315. while (actions.size()) {
  316. _pop_history_tail();
  317. }
  318. if (p_increase_version) {
  319. version++;
  320. emit_signal("version_changed");
  321. }
  322. }
  323. String UndoRedo::get_current_action_name() const {
  324. ERR_FAIL_COND_V(action_level > 0, "");
  325. if (current_action < 0) {
  326. return "";
  327. }
  328. return actions[current_action].name;
  329. }
  330. bool UndoRedo::has_undo() {
  331. return current_action >= 0;
  332. }
  333. bool UndoRedo::has_redo() {
  334. return (current_action + 1) < actions.size();
  335. }
  336. uint64_t UndoRedo::get_version() const {
  337. return version;
  338. }
  339. void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) {
  340. callback = p_callback;
  341. callback_ud = p_ud;
  342. }
  343. void UndoRedo::set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud) {
  344. method_callback = p_method_callback;
  345. method_callbck_ud = p_ud;
  346. }
  347. void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud) {
  348. property_callback = p_property_callback;
  349. prop_callback_ud = p_ud;
  350. }
  351. UndoRedo::~UndoRedo() {
  352. clear_history();
  353. }
  354. Variant UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  355. if (p_argcount < 2) {
  356. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  357. r_error.argument = 0;
  358. return Variant();
  359. }
  360. if (p_args[0]->get_type() != Variant::OBJECT) {
  361. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  362. r_error.argument = 0;
  363. r_error.expected = Variant::OBJECT;
  364. return Variant();
  365. }
  366. if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
  367. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  368. r_error.argument = 1;
  369. r_error.expected = Variant::STRING_NAME;
  370. return Variant();
  371. }
  372. r_error.error = Callable::CallError::CALL_OK;
  373. Object *object = *p_args[0];
  374. StringName method = *p_args[1];
  375. Variant v[VARIANT_ARG_MAX];
  376. for (int i = 0; i < MIN(VARIANT_ARG_MAX, p_argcount - 2); ++i) {
  377. v[i] = *p_args[i + 2];
  378. }
  379. static_assert(VARIANT_ARG_MAX == 5, "This code needs to be updated if VARIANT_ARG_MAX != 5");
  380. add_do_method(object, method, v[0], v[1], v[2], v[3], v[4]);
  381. return Variant();
  382. }
  383. Variant UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  384. if (p_argcount < 2) {
  385. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  386. r_error.argument = 0;
  387. return Variant();
  388. }
  389. if (p_args[0]->get_type() != Variant::OBJECT) {
  390. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  391. r_error.argument = 0;
  392. r_error.expected = Variant::OBJECT;
  393. return Variant();
  394. }
  395. if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
  396. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  397. r_error.argument = 1;
  398. r_error.expected = Variant::STRING_NAME;
  399. return Variant();
  400. }
  401. r_error.error = Callable::CallError::CALL_OK;
  402. Object *object = *p_args[0];
  403. StringName method = *p_args[1];
  404. Variant v[VARIANT_ARG_MAX];
  405. for (int i = 0; i < MIN(VARIANT_ARG_MAX, p_argcount - 2); ++i) {
  406. v[i] = *p_args[i + 2];
  407. }
  408. static_assert(VARIANT_ARG_MAX == 5, "This code needs to be updated if VARIANT_ARG_MAX != 5");
  409. add_undo_method(object, method, v[0], v[1], v[2], v[3], v[4]);
  410. return Variant();
  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("get_history_count"), &UndoRedo::get_history_count);
  435. ClassDB::bind_method(D_METHOD("get_current_action"), &UndoRedo::get_current_action);
  436. ClassDB::bind_method(D_METHOD("get_action_name", "id"), &UndoRedo::get_action_name);
  437. ClassDB::bind_method(D_METHOD("clear_history", "increase_version"), &UndoRedo::clear_history, DEFVAL(true));
  438. ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name);
  439. ClassDB::bind_method(D_METHOD("has_undo"), &UndoRedo::has_undo);
  440. ClassDB::bind_method(D_METHOD("has_redo"), &UndoRedo::has_redo);
  441. ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version);
  442. ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo);
  443. ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo);
  444. ADD_SIGNAL(MethodInfo("version_changed"));
  445. BIND_ENUM_CONSTANT(MERGE_DISABLE);
  446. BIND_ENUM_CONSTANT(MERGE_ENDS);
  447. BIND_ENUM_CONSTANT(MERGE_ALL);
  448. }