undo_redo.cpp 16 KB

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