undo_redo.cpp 16 KB

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