undo_redo.cpp 13 KB

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