undo_redo.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/io/resource.h"
  32. #include "core/os/os.h"
  33. #include "core/templates/local_vector.h"
  34. void UndoRedo::Operation::delete_reference() {
  35. if (type != Operation::TYPE_REFERENCE) {
  36. return;
  37. }
  38. if (ref.is_valid()) {
  39. ref.unref();
  40. } else {
  41. Object *obj = ObjectDB::get_instance(object);
  42. if (obj) {
  43. memdelete(obj);
  44. }
  45. }
  46. }
  47. void UndoRedo::_discard_redo() {
  48. if (current_action == actions.size() - 1) {
  49. return;
  50. }
  51. for (int i = current_action + 1; i < actions.size(); i++) {
  52. for (Operation &E : actions.write[i].do_ops) {
  53. E.delete_reference();
  54. }
  55. //ERASE do data
  56. }
  57. actions.resize(current_action + 1);
  58. }
  59. bool UndoRedo::_redo(bool p_execute) {
  60. ERR_FAIL_COND_V(action_level > 0, false);
  61. if ((current_action + 1) >= actions.size()) {
  62. return false; //nothing to redo
  63. }
  64. current_action++;
  65. if (p_execute) {
  66. _process_operation_list(actions.write[current_action].do_ops.front());
  67. }
  68. version++;
  69. emit_signal(SNAME("version_changed"));
  70. return true;
  71. }
  72. void UndoRedo::create_action(const String &p_name, MergeMode p_mode) {
  73. uint64_t ticks = OS::get_singleton()->get_ticks_msec();
  74. if (action_level == 0) {
  75. _discard_redo();
  76. // Check if the merge operation is valid
  77. if (p_mode != MERGE_DISABLE && actions.size() && actions[actions.size() - 1].name == p_name && actions[actions.size() - 1].last_tick + 800 > ticks) {
  78. current_action = actions.size() - 2;
  79. if (p_mode == MERGE_ENDS) {
  80. // Clear all do ops from last action if they are not forced kept
  81. LocalVector<List<Operation>::Element *> to_remove;
  82. for (List<Operation>::Element *E = actions.write[current_action + 1].do_ops.front(); E; E = E->next()) {
  83. if (!E->get().force_keep_in_merge_ends) {
  84. to_remove.push_back(E);
  85. }
  86. }
  87. for (List<Operation>::Element *E : to_remove) {
  88. // Delete all object references
  89. E->get().delete_reference();
  90. E->erase();
  91. }
  92. }
  93. actions.write[actions.size() - 1].last_tick = ticks;
  94. merge_mode = p_mode;
  95. merging = true;
  96. } else {
  97. Action new_action;
  98. new_action.name = p_name;
  99. new_action.last_tick = ticks;
  100. actions.push_back(new_action);
  101. merge_mode = MERGE_DISABLE;
  102. }
  103. }
  104. action_level++;
  105. force_keep_in_merge_ends = false;
  106. }
  107. void UndoRedo::add_do_method(const Callable &p_callable) {
  108. ERR_FAIL_COND(p_callable.is_null());
  109. ERR_FAIL_COND(action_level <= 0);
  110. ERR_FAIL_COND((current_action + 1) >= actions.size());
  111. Object *object = p_callable.get_object();
  112. ERR_FAIL_NULL(object);
  113. Operation do_op;
  114. do_op.callable = p_callable;
  115. do_op.object = p_callable.get_object_id();
  116. if (Object::cast_to<RefCounted>(object)) {
  117. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(object));
  118. }
  119. do_op.type = Operation::TYPE_METHOD;
  120. do_op.name = p_callable.get_method();
  121. actions.write[current_action + 1].do_ops.push_back(do_op);
  122. }
  123. void UndoRedo::add_undo_method(const Callable &p_callable) {
  124. ERR_FAIL_COND(p_callable.is_null());
  125. ERR_FAIL_COND(action_level <= 0);
  126. ERR_FAIL_COND((current_action + 1) >= actions.size());
  127. // No undo if the merge mode is MERGE_ENDS
  128. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  129. return;
  130. }
  131. Object *object = p_callable.get_object();
  132. ERR_FAIL_NULL(object);
  133. Operation undo_op;
  134. undo_op.callable = p_callable;
  135. undo_op.object = p_callable.get_object_id();
  136. if (Object::cast_to<RefCounted>(object)) {
  137. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(object));
  138. }
  139. undo_op.type = Operation::TYPE_METHOD;
  140. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  141. undo_op.name = p_callable.get_method();
  142. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  143. }
  144. void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  145. ERR_FAIL_COND(p_object == nullptr);
  146. ERR_FAIL_COND(action_level <= 0);
  147. ERR_FAIL_COND((current_action + 1) >= actions.size());
  148. Operation do_op;
  149. do_op.object = p_object->get_instance_id();
  150. if (Object::cast_to<RefCounted>(p_object)) {
  151. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  152. }
  153. do_op.type = Operation::TYPE_PROPERTY;
  154. do_op.name = p_property;
  155. do_op.value = p_value;
  156. actions.write[current_action + 1].do_ops.push_back(do_op);
  157. }
  158. void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
  159. ERR_FAIL_COND(p_object == nullptr);
  160. ERR_FAIL_COND(action_level <= 0);
  161. ERR_FAIL_COND((current_action + 1) >= actions.size());
  162. // No undo if the merge mode is MERGE_ENDS
  163. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  164. return;
  165. }
  166. Operation undo_op;
  167. undo_op.object = p_object->get_instance_id();
  168. if (Object::cast_to<RefCounted>(p_object)) {
  169. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  170. }
  171. undo_op.type = Operation::TYPE_PROPERTY;
  172. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  173. undo_op.name = p_property;
  174. undo_op.value = p_value;
  175. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  176. }
  177. void UndoRedo::add_do_reference(Object *p_object) {
  178. ERR_FAIL_COND(p_object == nullptr);
  179. ERR_FAIL_COND(action_level <= 0);
  180. ERR_FAIL_COND((current_action + 1) >= actions.size());
  181. Operation do_op;
  182. do_op.object = p_object->get_instance_id();
  183. if (Object::cast_to<RefCounted>(p_object)) {
  184. do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  185. }
  186. do_op.type = Operation::TYPE_REFERENCE;
  187. actions.write[current_action + 1].do_ops.push_back(do_op);
  188. }
  189. void UndoRedo::add_undo_reference(Object *p_object) {
  190. ERR_FAIL_COND(p_object == nullptr);
  191. ERR_FAIL_COND(action_level <= 0);
  192. ERR_FAIL_COND((current_action + 1) >= actions.size());
  193. // No undo if the merge mode is MERGE_ENDS
  194. if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
  195. return;
  196. }
  197. Operation undo_op;
  198. undo_op.object = p_object->get_instance_id();
  199. if (Object::cast_to<RefCounted>(p_object)) {
  200. undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
  201. }
  202. undo_op.type = Operation::TYPE_REFERENCE;
  203. undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
  204. actions.write[current_action + 1].undo_ops.push_back(undo_op);
  205. }
  206. void UndoRedo::start_force_keep_in_merge_ends() {
  207. ERR_FAIL_COND(action_level <= 0);
  208. ERR_FAIL_COND((current_action + 1) >= actions.size());
  209. force_keep_in_merge_ends = true;
  210. }
  211. void UndoRedo::end_force_keep_in_merge_ends() {
  212. ERR_FAIL_COND(action_level <= 0);
  213. ERR_FAIL_COND((current_action + 1) >= actions.size());
  214. force_keep_in_merge_ends = false;
  215. }
  216. void UndoRedo::_pop_history_tail() {
  217. _discard_redo();
  218. if (!actions.size()) {
  219. return;
  220. }
  221. for (Operation &E : actions.write[0].undo_ops) {
  222. E.delete_reference();
  223. }
  224. actions.remove_at(0);
  225. if (current_action >= 0) {
  226. current_action--;
  227. }
  228. }
  229. bool UndoRedo::is_committing_action() const {
  230. return committing > 0;
  231. }
  232. void UndoRedo::commit_action(bool p_execute) {
  233. ERR_FAIL_COND(action_level <= 0);
  234. action_level--;
  235. if (action_level > 0) {
  236. return; //still nested
  237. }
  238. if (merging) {
  239. version--;
  240. merging = false;
  241. }
  242. committing++;
  243. _redo(p_execute); // perform action
  244. committing--;
  245. if (callback && actions.size() > 0) {
  246. callback(callback_ud, actions[actions.size() - 1].name);
  247. }
  248. }
  249. void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
  250. for (; E; E = E->next()) {
  251. Operation &op = E->get();
  252. Object *obj = ObjectDB::get_instance(op.object);
  253. if (!obj) { //may have been deleted and this is fine
  254. continue;
  255. }
  256. switch (op.type) {
  257. case Operation::TYPE_METHOD: {
  258. Callable::CallError ce;
  259. Variant ret;
  260. op.callable.callp(nullptr, 0, ret, ce);
  261. if (ce.error != Callable::CallError::CALL_OK) {
  262. ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, nullptr, 0, ce));
  263. }
  264. #ifdef TOOLS_ENABLED
  265. Resource *res = Object::cast_to<Resource>(obj);
  266. if (res) {
  267. res->set_edited(true);
  268. }
  269. #endif
  270. if (method_callback) {
  271. Vector<Variant> binds;
  272. if (op.callable.is_custom()) {
  273. CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(op.callable.get_custom());
  274. if (ccb) {
  275. binds = ccb->get_binds();
  276. }
  277. }
  278. if (binds.is_empty()) {
  279. method_callback(method_callback_ud, obj, op.name, nullptr, 0);
  280. } else {
  281. const Variant **args = (const Variant **)alloca(sizeof(const Variant **) * binds.size());
  282. for (int i = 0; i < binds.size(); i++) {
  283. args[i] = (const Variant *)&binds[i];
  284. }
  285. method_callback(method_callback_ud, obj, op.name, args, binds.size());
  286. }
  287. }
  288. } break;
  289. case Operation::TYPE_PROPERTY: {
  290. obj->set(op.name, op.value);
  291. #ifdef TOOLS_ENABLED
  292. Resource *res = Object::cast_to<Resource>(obj);
  293. if (res) {
  294. res->set_edited(true);
  295. }
  296. #endif
  297. if (property_callback) {
  298. property_callback(prop_callback_ud, obj, op.name, op.value);
  299. }
  300. } break;
  301. case Operation::TYPE_REFERENCE: {
  302. //do nothing
  303. } break;
  304. }
  305. }
  306. }
  307. bool UndoRedo::redo() {
  308. return _redo(true);
  309. }
  310. bool UndoRedo::undo() {
  311. ERR_FAIL_COND_V(action_level > 0, false);
  312. if (current_action < 0) {
  313. return false; //nothing to redo
  314. }
  315. _process_operation_list(actions.write[current_action].undo_ops.front());
  316. current_action--;
  317. version--;
  318. emit_signal(SNAME("version_changed"));
  319. return true;
  320. }
  321. int UndoRedo::get_history_count() {
  322. ERR_FAIL_COND_V(action_level > 0, -1);
  323. return actions.size();
  324. }
  325. int UndoRedo::get_current_action() {
  326. ERR_FAIL_COND_V(action_level > 0, -1);
  327. return current_action;
  328. }
  329. String UndoRedo::get_action_name(int p_id) {
  330. ERR_FAIL_INDEX_V(p_id, actions.size(), "");
  331. return actions[p_id].name;
  332. }
  333. void UndoRedo::clear_history(bool p_increase_version) {
  334. ERR_FAIL_COND(action_level > 0);
  335. _discard_redo();
  336. while (actions.size()) {
  337. _pop_history_tail();
  338. }
  339. if (p_increase_version) {
  340. version++;
  341. emit_signal(SNAME("version_changed"));
  342. }
  343. }
  344. String UndoRedo::get_current_action_name() const {
  345. ERR_FAIL_COND_V(action_level > 0, "");
  346. if (current_action < 0) {
  347. return "";
  348. }
  349. return actions[current_action].name;
  350. }
  351. int UndoRedo::get_action_level() const {
  352. return action_level;
  353. }
  354. bool UndoRedo::has_undo() const {
  355. return current_action >= 0;
  356. }
  357. bool UndoRedo::has_redo() const {
  358. return (current_action + 1) < actions.size();
  359. }
  360. uint64_t UndoRedo::get_version() const {
  361. return version;
  362. }
  363. void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) {
  364. callback = p_callback;
  365. callback_ud = p_ud;
  366. }
  367. void UndoRedo::set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud) {
  368. method_callback = p_method_callback;
  369. method_callback_ud = p_ud;
  370. }
  371. void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud) {
  372. property_callback = p_property_callback;
  373. prop_callback_ud = p_ud;
  374. }
  375. UndoRedo::~UndoRedo() {
  376. clear_history();
  377. }
  378. void UndoRedo::_bind_methods() {
  379. ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE));
  380. ClassDB::bind_method(D_METHOD("commit_action", "execute"), &UndoRedo::commit_action, DEFVAL(true));
  381. ClassDB::bind_method(D_METHOD("is_committing_action"), &UndoRedo::is_committing_action);
  382. ClassDB::bind_method(D_METHOD("add_do_method", "callable"), &UndoRedo::add_do_method);
  383. ClassDB::bind_method(D_METHOD("add_undo_method", "callable"), &UndoRedo::add_undo_method);
  384. ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &UndoRedo::add_do_property);
  385. ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &UndoRedo::add_undo_property);
  386. ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &UndoRedo::add_do_reference);
  387. ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &UndoRedo::add_undo_reference);
  388. ClassDB::bind_method(D_METHOD("start_force_keep_in_merge_ends"), &UndoRedo::start_force_keep_in_merge_ends);
  389. ClassDB::bind_method(D_METHOD("end_force_keep_in_merge_ends"), &UndoRedo::end_force_keep_in_merge_ends);
  390. ClassDB::bind_method(D_METHOD("get_history_count"), &UndoRedo::get_history_count);
  391. ClassDB::bind_method(D_METHOD("get_current_action"), &UndoRedo::get_current_action);
  392. ClassDB::bind_method(D_METHOD("get_action_name", "id"), &UndoRedo::get_action_name);
  393. ClassDB::bind_method(D_METHOD("clear_history", "increase_version"), &UndoRedo::clear_history, DEFVAL(true));
  394. ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name);
  395. ClassDB::bind_method(D_METHOD("has_undo"), &UndoRedo::has_undo);
  396. ClassDB::bind_method(D_METHOD("has_redo"), &UndoRedo::has_redo);
  397. ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version);
  398. ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo);
  399. ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo);
  400. ADD_SIGNAL(MethodInfo("version_changed"));
  401. BIND_ENUM_CONSTANT(MERGE_DISABLE);
  402. BIND_ENUM_CONSTANT(MERGE_ENDS);
  403. BIND_ENUM_CONSTANT(MERGE_ALL);
  404. }