undo_redo.cpp 16 KB

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