undo_redo.cpp 15 KB

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