undo_redo.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 "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[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[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[current_action + 1].do_ops.pop_front();
  65. }
  66. }
  67. actions[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[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[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[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[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[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[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[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. obj->call(op.name, VARIANT_ARGS_FROM_ARRAY(op.args));
  204. #ifdef TOOLS_ENABLED
  205. Resource *res = Object::cast_to<Resource>(obj);
  206. if (res)
  207. res->set_edited(true);
  208. #endif
  209. if (method_callback) {
  210. method_callback(method_callbck_ud, obj, op.name, VARIANT_ARGS_FROM_ARRAY(op.args));
  211. }
  212. } break;
  213. case Operation::TYPE_PROPERTY: {
  214. obj->set(op.name, op.args[0]);
  215. #ifdef TOOLS_ENABLED
  216. Resource *res = Object::cast_to<Resource>(obj);
  217. if (res)
  218. res->set_edited(true);
  219. #endif
  220. if (property_callback) {
  221. property_callback(prop_callback_ud, obj, op.name, op.args[0]);
  222. }
  223. } break;
  224. case Operation::TYPE_REFERENCE: {
  225. //do nothing
  226. } break;
  227. }
  228. }
  229. }
  230. void UndoRedo::redo() {
  231. ERR_FAIL_COND(action_level > 0);
  232. if ((current_action + 1) >= actions.size())
  233. return; //nothing to redo
  234. current_action++;
  235. _process_operation_list(actions[current_action].do_ops.front());
  236. version++;
  237. }
  238. void UndoRedo::undo() {
  239. ERR_FAIL_COND(action_level > 0);
  240. if (current_action < 0)
  241. return; //nothing to redo
  242. _process_operation_list(actions[current_action].undo_ops.front());
  243. current_action--;
  244. version--;
  245. }
  246. void UndoRedo::clear_history() {
  247. ERR_FAIL_COND(action_level > 0);
  248. _discard_redo();
  249. while (actions.size())
  250. _pop_history_tail();
  251. //version++;
  252. }
  253. String UndoRedo::get_current_action_name() const {
  254. ERR_FAIL_COND_V(action_level > 0, "");
  255. if (current_action < 0)
  256. return ""; //nothing to redo
  257. return actions[current_action].name;
  258. }
  259. uint64_t UndoRedo::get_version() const {
  260. return version;
  261. }
  262. void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) {
  263. callback = p_callback;
  264. callback_ud = p_ud;
  265. }
  266. void UndoRedo::set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud) {
  267. method_callback = p_method_callback;
  268. method_callbck_ud = p_ud;
  269. }
  270. void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud) {
  271. property_callback = p_property_callback;
  272. prop_callback_ud = p_ud;
  273. }
  274. UndoRedo::UndoRedo() {
  275. version = 1;
  276. action_level = 0;
  277. current_action = -1;
  278. merge_mode = MERGE_DISABLE;
  279. callback = NULL;
  280. callback_ud = NULL;
  281. method_callbck_ud = NULL;
  282. prop_callback_ud = NULL;
  283. method_callback = NULL;
  284. property_callback = NULL;
  285. }
  286. UndoRedo::~UndoRedo() {
  287. clear_history();
  288. }
  289. Variant UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  290. if (p_argcount < 2) {
  291. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  292. r_error.argument = 0;
  293. return Variant();
  294. }
  295. if (p_args[0]->get_type() != Variant::OBJECT) {
  296. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  297. r_error.argument = 0;
  298. r_error.expected = Variant::OBJECT;
  299. return Variant();
  300. }
  301. if (p_args[1]->get_type() != Variant::STRING) {
  302. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  303. r_error.argument = 1;
  304. r_error.expected = Variant::STRING;
  305. return Variant();
  306. }
  307. r_error.error = Variant::CallError::CALL_OK;
  308. Object *object = *p_args[0];
  309. String method = *p_args[1];
  310. Variant v[VARIANT_ARG_MAX];
  311. for (int i = 0; i < MIN(VARIANT_ARG_MAX, p_argcount - 2); ++i) {
  312. v[i] = *p_args[i + 2];
  313. }
  314. add_do_method(object, method, v[0], v[1], v[2], v[3], v[4]);
  315. return Variant();
  316. }
  317. Variant UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  318. if (p_argcount < 2) {
  319. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  320. r_error.argument = 0;
  321. return Variant();
  322. }
  323. if (p_args[0]->get_type() != Variant::OBJECT) {
  324. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  325. r_error.argument = 0;
  326. r_error.expected = Variant::OBJECT;
  327. return Variant();
  328. }
  329. if (p_args[1]->get_type() != Variant::STRING) {
  330. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  331. r_error.argument = 1;
  332. r_error.expected = Variant::STRING;
  333. return Variant();
  334. }
  335. r_error.error = Variant::CallError::CALL_OK;
  336. Object *object = *p_args[0];
  337. String method = *p_args[1];
  338. Variant v[VARIANT_ARG_MAX];
  339. for (int i = 0; i < MIN(VARIANT_ARG_MAX, p_argcount - 2); ++i) {
  340. v[i] = *p_args[i + 2];
  341. }
  342. add_undo_method(object, method, v[0], v[1], v[2], v[3], v[4]);
  343. return Variant();
  344. }
  345. void UndoRedo::_bind_methods() {
  346. ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE));
  347. ClassDB::bind_method(D_METHOD("commit_action"), &UndoRedo::commit_action);
  348. //ClassDB::bind_method(D_METHOD("add_do_method","p_object", "p_method", "VARIANT_ARG_LIST"),&UndoRedo::add_do_method);
  349. //ClassDB::bind_method(D_METHOD("add_undo_method","p_object", "p_method", "VARIANT_ARG_LIST"),&UndoRedo::add_undo_method);
  350. {
  351. MethodInfo mi;
  352. mi.name = "add_do_method";
  353. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  354. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  355. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &UndoRedo::_add_do_method, mi);
  356. }
  357. {
  358. MethodInfo mi;
  359. mi.name = "add_undo_method";
  360. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  361. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  362. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &UndoRedo::_add_undo_method, mi);
  363. }
  364. ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &UndoRedo::add_do_property);
  365. ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &UndoRedo::add_undo_property);
  366. ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &UndoRedo::add_do_reference);
  367. ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &UndoRedo::add_undo_reference);
  368. ClassDB::bind_method(D_METHOD("clear_history"), &UndoRedo::clear_history);
  369. ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name);
  370. ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version);
  371. ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo);
  372. ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo);
  373. BIND_ENUM_CONSTANT(MERGE_DISABLE);
  374. BIND_ENUM_CONSTANT(MERGE_ENDS);
  375. BIND_ENUM_CONSTANT(MERGE_ALL);
  376. }