undo_redo.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*************************************************************************/
  2. /* undo_redo.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "undo_redo.h"
  30. void UndoRedo::_discard_redo() {
  31. if (current_action==actions.size()-1)
  32. return;
  33. for(int i=current_action+1;i<actions.size();i++) {
  34. for (List<Operation>::Element *E=actions[i].do_ops.front();E;E=E->next()) {
  35. if (E->get().type==Operation::TYPE_REFERENCE) {
  36. Object *obj = ObjectDB::get_instance(E->get().object);
  37. if (obj)
  38. memdelete(obj);
  39. }
  40. }
  41. //ERASE do data
  42. }
  43. actions.resize(current_action+1);
  44. }
  45. void UndoRedo::create_action(const String& p_name,bool p_mergeable) {
  46. if (action_level==0) {
  47. _discard_redo();
  48. if (p_mergeable && actions.size() && actions[actions.size()-1].name==p_name) {
  49. //old will replace new (it's mergeable after all)
  50. // should check references though!
  51. current_action=actions.size()-2;
  52. actions[current_action+1].do_ops.clear();
  53. //actions[current_action+1].undo_ops.clear(); - no, this is kept
  54. merging=true;
  55. } else {
  56. Action new_action;
  57. new_action.name=p_name;
  58. actions.push_back(new_action);
  59. merging=false;
  60. }
  61. }
  62. action_level++;
  63. }
  64. void UndoRedo::add_do_method(Object *p_object,const String& p_method,VARIANT_ARG_DECLARE) {
  65. VARIANT_ARGPTRS
  66. ERR_FAIL_COND(action_level<=0);
  67. ERR_FAIL_COND((current_action+1)>=actions.size());
  68. Operation do_op;
  69. do_op.object=p_object->get_instance_ID();
  70. if (p_object->cast_to<Resource>())
  71. do_op.resref=Ref<Resource>(p_object->cast_to<Resource>());
  72. do_op.type=Operation::TYPE_METHOD;
  73. do_op.name=p_method;
  74. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  75. do_op.args[i]=*argptr[i];
  76. }
  77. actions[current_action+1].do_ops.push_back(do_op);
  78. }
  79. void UndoRedo::add_undo_method(Object *p_object,const String& p_method,VARIANT_ARG_DECLARE) {
  80. VARIANT_ARGPTRS
  81. ERR_FAIL_COND(action_level<=0);
  82. ERR_FAIL_COND((current_action+1)>=actions.size());
  83. if (merging)
  84. return; //- no undo if merging
  85. Operation undo_op;
  86. undo_op.object=p_object->get_instance_ID();
  87. if (p_object->cast_to<Resource>())
  88. undo_op.resref=Ref<Resource>(p_object->cast_to<Resource>());
  89. undo_op.type=Operation::TYPE_METHOD;
  90. undo_op.name=p_method;
  91. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  92. undo_op.args[i]=*argptr[i];
  93. }
  94. actions[current_action+1].undo_ops.push_back(undo_op);
  95. }
  96. void UndoRedo::add_do_property(Object *p_object,const String& p_property,const Variant& p_value) {
  97. ERR_FAIL_COND(action_level<=0);
  98. ERR_FAIL_COND((current_action+1)>=actions.size());
  99. Operation do_op;
  100. do_op.object=p_object->get_instance_ID();
  101. if (p_object->cast_to<Resource>())
  102. do_op.resref=Ref<Resource>(p_object->cast_to<Resource>());
  103. do_op.type=Operation::TYPE_PROPERTY;
  104. do_op.name=p_property;
  105. do_op.args[0]=p_value;
  106. actions[current_action+1].do_ops.push_back(do_op);
  107. }
  108. void UndoRedo::add_undo_property(Object *p_object,const String& p_property,const Variant& p_value) {
  109. ERR_FAIL_COND(action_level<=0);
  110. ERR_FAIL_COND((current_action+1)>=actions.size());
  111. Operation undo_op;
  112. undo_op.object=p_object->get_instance_ID();
  113. if (p_object->cast_to<Resource>())
  114. undo_op.resref=Ref<Resource>(p_object->cast_to<Resource>());
  115. undo_op.type=Operation::TYPE_PROPERTY;
  116. undo_op.name=p_property;
  117. undo_op.args[0]=p_value;
  118. actions[current_action+1].undo_ops.push_back(undo_op);
  119. }
  120. void UndoRedo::add_do_reference(Object *p_object) {
  121. ERR_FAIL_COND(action_level<=0);
  122. ERR_FAIL_COND((current_action+1)>=actions.size());
  123. Operation do_op;
  124. do_op.object=p_object->get_instance_ID();
  125. if (p_object->cast_to<Resource>())
  126. do_op.resref=Ref<Resource>(p_object->cast_to<Resource>());
  127. do_op.type=Operation::TYPE_REFERENCE;
  128. actions[current_action+1].do_ops.push_back(do_op);
  129. }
  130. void UndoRedo::add_undo_reference(Object *p_object) {
  131. ERR_FAIL_COND(action_level<=0);
  132. ERR_FAIL_COND((current_action+1)>=actions.size());
  133. Operation undo_op;
  134. undo_op.object=p_object->get_instance_ID();
  135. if (p_object->cast_to<Resource>())
  136. undo_op.resref=Ref<Resource>(p_object->cast_to<Resource>());
  137. undo_op.type=Operation::TYPE_REFERENCE;
  138. actions[current_action+1].undo_ops.push_back(undo_op);
  139. }
  140. void UndoRedo::_pop_history_tail() {
  141. _discard_redo();
  142. if (!actions.size())
  143. return;
  144. for (List<Operation>::Element *E=actions[0].undo_ops.front();E;E=E->next()) {
  145. if (E->get().type==Operation::TYPE_REFERENCE) {
  146. Object *obj = ObjectDB::get_instance(E->get().object);
  147. if (obj)
  148. memdelete(obj);
  149. }
  150. }
  151. actions.remove(0);
  152. current_action--;
  153. }
  154. void UndoRedo::commit_action() {
  155. ERR_FAIL_COND(action_level<=0);
  156. action_level--;
  157. if (action_level>0)
  158. return; //still nested
  159. redo(); // perform action
  160. if (max_steps>0 && actions.size()>max_steps) {
  161. //clear early steps
  162. while(actions.size() > max_steps)
  163. _pop_history_tail();
  164. }
  165. if (callback && actions.size()>0) {
  166. callback(callback_ud,actions[actions.size()-1].name);
  167. }
  168. }
  169. void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
  170. for (;E;E=E->next()) {
  171. Operation &op=E->get();
  172. Object *obj = ObjectDB::get_instance(op.object);
  173. if (!obj) {
  174. //corruption
  175. clear_history();
  176. ERR_FAIL_COND(!obj);
  177. }
  178. switch(op.type) {
  179. case Operation::TYPE_METHOD: {
  180. obj->call(op.name,VARIANT_ARGS_FROM_ARRAY(op.args));
  181. #ifdef TOOLS_ENABLED
  182. Resource* res = obj->cast_to<Resource>();
  183. if (res)
  184. res->set_edited(true);
  185. #endif
  186. } break;
  187. case Operation::TYPE_PROPERTY: {
  188. obj->set(op.name,op.args[0]);
  189. #ifdef TOOLS_ENABLED
  190. Resource* res = obj->cast_to<Resource>();
  191. if (res)
  192. res->set_edited(true);
  193. #endif
  194. } break;
  195. case Operation::TYPE_REFERENCE: {
  196. //do nothing
  197. } break;
  198. }
  199. }
  200. }
  201. void UndoRedo::redo() {
  202. ERR_FAIL_COND(action_level>0);
  203. if ((current_action+1)>=actions.size())
  204. return; //nothing to redo
  205. current_action++;
  206. _process_operation_list(actions[current_action].do_ops.front());
  207. version++;
  208. }
  209. void UndoRedo::undo() {
  210. ERR_FAIL_COND(action_level>0);
  211. if (current_action<0)
  212. return; //nothing to redo
  213. _process_operation_list(actions[current_action].undo_ops.front());
  214. current_action--;
  215. }
  216. void UndoRedo::clear_history() {
  217. ERR_FAIL_COND(action_level>0);
  218. _discard_redo();
  219. while(actions.size())
  220. _pop_history_tail();
  221. version++;
  222. }
  223. String UndoRedo::get_current_action_name() const {
  224. ERR_FAIL_COND_V(action_level>0,"");
  225. if (current_action<0)
  226. return ""; //nothing to redo
  227. return actions[current_action].name;
  228. }
  229. void UndoRedo::set_max_steps(int p_max_steps) {
  230. max_steps=p_max_steps;
  231. }
  232. int UndoRedo::get_max_steps() const {
  233. return max_steps;
  234. }
  235. uint64_t UndoRedo::get_version() const {
  236. return version;
  237. }
  238. void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback,void* p_ud) {
  239. callback=p_callback;
  240. callback_ud=p_ud;
  241. }
  242. UndoRedo::UndoRedo() {
  243. version=0;
  244. action_level=0;
  245. current_action=-1;
  246. max_steps=-1;
  247. merging=true;
  248. callback=NULL;
  249. callback_ud=NULL;
  250. }
  251. UndoRedo::~UndoRedo() {
  252. clear_history();
  253. }