tween.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /*************************************************************************/
  2. /* tween.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "tween.h"
  31. #include "core/method_bind_ext.gen.inc"
  32. void Tween::_add_pending_command(StringName p_key, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5, const Variant &p_arg6, const Variant &p_arg7, const Variant &p_arg8, const Variant &p_arg9, const Variant &p_arg10) {
  33. // Add a new pending command and reference it
  34. pending_commands.push_back(PendingCommand());
  35. PendingCommand &cmd = pending_commands.back()->get();
  36. // Update the command with the target key
  37. cmd.key = p_key;
  38. // Determine command argument count
  39. int &count = cmd.args;
  40. if (p_arg10.get_type() != Variant::NIL)
  41. count = 10;
  42. else if (p_arg9.get_type() != Variant::NIL)
  43. count = 9;
  44. else if (p_arg8.get_type() != Variant::NIL)
  45. count = 8;
  46. else if (p_arg7.get_type() != Variant::NIL)
  47. count = 7;
  48. else if (p_arg6.get_type() != Variant::NIL)
  49. count = 6;
  50. else if (p_arg5.get_type() != Variant::NIL)
  51. count = 5;
  52. else if (p_arg4.get_type() != Variant::NIL)
  53. count = 4;
  54. else if (p_arg3.get_type() != Variant::NIL)
  55. count = 3;
  56. else if (p_arg2.get_type() != Variant::NIL)
  57. count = 2;
  58. else if (p_arg1.get_type() != Variant::NIL)
  59. count = 1;
  60. else
  61. count = 0;
  62. // Add the specified arguments to the command
  63. if (count > 0)
  64. cmd.arg[0] = p_arg1;
  65. if (count > 1)
  66. cmd.arg[1] = p_arg2;
  67. if (count > 2)
  68. cmd.arg[2] = p_arg3;
  69. if (count > 3)
  70. cmd.arg[3] = p_arg4;
  71. if (count > 4)
  72. cmd.arg[4] = p_arg5;
  73. if (count > 5)
  74. cmd.arg[5] = p_arg6;
  75. if (count > 6)
  76. cmd.arg[6] = p_arg7;
  77. if (count > 7)
  78. cmd.arg[7] = p_arg8;
  79. if (count > 8)
  80. cmd.arg[8] = p_arg9;
  81. if (count > 9)
  82. cmd.arg[9] = p_arg10;
  83. }
  84. void Tween::_process_pending_commands() {
  85. // For each pending command...
  86. for (List<PendingCommand>::Element *E = pending_commands.front(); E; E = E->next()) {
  87. // Get the command
  88. PendingCommand &cmd = E->get();
  89. Variant::CallError err;
  90. // Grab all of the arguments for the command
  91. Variant *arg[10] = {
  92. &cmd.arg[0],
  93. &cmd.arg[1],
  94. &cmd.arg[2],
  95. &cmd.arg[3],
  96. &cmd.arg[4],
  97. &cmd.arg[5],
  98. &cmd.arg[6],
  99. &cmd.arg[7],
  100. &cmd.arg[8],
  101. &cmd.arg[9],
  102. };
  103. // Execute the command (and retrieve any errors)
  104. this->call(cmd.key, (const Variant **)arg, cmd.args, err);
  105. }
  106. // Clear the pending commands
  107. pending_commands.clear();
  108. }
  109. bool Tween::_set(const StringName &p_name, const Variant &p_value) {
  110. // Set the correct attribute based on the given name
  111. String name = p_name;
  112. if (name == "playback/speed" || name == "speed") { // Backwards compatibility
  113. set_speed_scale(p_value);
  114. } else if (name == "playback/active") {
  115. set_active(p_value);
  116. } else if (name == "playback/repeat") {
  117. set_repeat(p_value);
  118. }
  119. return true;
  120. }
  121. bool Tween::_get(const StringName &p_name, Variant &r_ret) const {
  122. // Get the correct attribute based on the given name
  123. String name = p_name;
  124. if (name == "playback/speed") { // Backwards compatibility
  125. r_ret = speed_scale;
  126. } else if (name == "playback/active") {
  127. r_ret = is_active();
  128. } else if (name == "playback/repeat") {
  129. r_ret = is_repeat();
  130. }
  131. return true;
  132. }
  133. void Tween::_get_property_list(List<PropertyInfo> *p_list) const {
  134. // Add the property info for the Tween object
  135. p_list->push_back(PropertyInfo(Variant::BOOL, "playback/active", PROPERTY_HINT_NONE, ""));
  136. p_list->push_back(PropertyInfo(Variant::BOOL, "playback/repeat", PROPERTY_HINT_NONE, ""));
  137. p_list->push_back(PropertyInfo(Variant::REAL, "playback/speed", PROPERTY_HINT_RANGE, "-64,64,0.01"));
  138. }
  139. void Tween::_notification(int p_what) {
  140. // What notification did we receive?
  141. switch (p_what) {
  142. case NOTIFICATION_ENTER_TREE: {
  143. // Are we not already active?
  144. if (!is_active()) {
  145. // Make sure that a previous process state was not saved
  146. // Only process if "processing" is set
  147. set_physics_process_internal(false);
  148. set_process_internal(false);
  149. }
  150. } break;
  151. case NOTIFICATION_READY: {
  152. // Do nothing
  153. } break;
  154. case NOTIFICATION_INTERNAL_PROCESS: {
  155. // Are we processing during physics time?
  156. if (tween_process_mode == TWEEN_PROCESS_PHYSICS)
  157. // Do nothing since we aren't aligned with physics when we should be
  158. break;
  159. // Should we update?
  160. if (is_active())
  161. // Update the tweens
  162. _tween_process(get_process_delta_time());
  163. } break;
  164. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  165. // Are we processing during 'regular' time?
  166. if (tween_process_mode == TWEEN_PROCESS_IDLE)
  167. // Do nothing since we would only process during idle time
  168. break;
  169. // Should we update?
  170. if (is_active())
  171. // Update the tweens
  172. _tween_process(get_physics_process_delta_time());
  173. } break;
  174. case NOTIFICATION_EXIT_TREE: {
  175. // We've left the tree. Stop all tweens
  176. stop_all();
  177. } break;
  178. }
  179. }
  180. void Tween::_bind_methods() {
  181. // Bind getters and setters
  182. ClassDB::bind_method(D_METHOD("is_active"), &Tween::is_active);
  183. ClassDB::bind_method(D_METHOD("set_active", "active"), &Tween::set_active);
  184. ClassDB::bind_method(D_METHOD("is_repeat"), &Tween::is_repeat);
  185. ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &Tween::set_repeat);
  186. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &Tween::set_speed_scale);
  187. ClassDB::bind_method(D_METHOD("get_speed_scale"), &Tween::get_speed_scale);
  188. ClassDB::bind_method(D_METHOD("set_tween_process_mode", "mode"), &Tween::set_tween_process_mode);
  189. ClassDB::bind_method(D_METHOD("get_tween_process_mode"), &Tween::get_tween_process_mode);
  190. // Bind the various Tween control methods
  191. ClassDB::bind_method(D_METHOD("start"), &Tween::start);
  192. ClassDB::bind_method(D_METHOD("reset", "object", "key"), &Tween::reset, DEFVAL(""));
  193. ClassDB::bind_method(D_METHOD("reset_all"), &Tween::reset_all);
  194. ClassDB::bind_method(D_METHOD("stop", "object", "key"), &Tween::stop, DEFVAL(""));
  195. ClassDB::bind_method(D_METHOD("stop_all"), &Tween::stop_all);
  196. ClassDB::bind_method(D_METHOD("resume", "object", "key"), &Tween::resume, DEFVAL(""));
  197. ClassDB::bind_method(D_METHOD("resume_all"), &Tween::resume_all);
  198. ClassDB::bind_method(D_METHOD("remove", "object", "key"), &Tween::remove, DEFVAL(""));
  199. ClassDB::bind_method(D_METHOD("_remove_by_uid", "uid"), &Tween::_remove_by_uid);
  200. ClassDB::bind_method(D_METHOD("remove_all"), &Tween::remove_all);
  201. ClassDB::bind_method(D_METHOD("seek", "time"), &Tween::seek);
  202. ClassDB::bind_method(D_METHOD("tell"), &Tween::tell);
  203. ClassDB::bind_method(D_METHOD("get_runtime"), &Tween::get_runtime);
  204. // Bind interpolation and follow methods
  205. ClassDB::bind_method(D_METHOD("interpolate_property", "object", "property", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::interpolate_property, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0));
  206. ClassDB::bind_method(D_METHOD("interpolate_method", "object", "method", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::interpolate_method, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0));
  207. ClassDB::bind_method(D_METHOD("interpolate_callback", "object", "duration", "callback", "arg1", "arg2", "arg3", "arg4", "arg5"), &Tween::interpolate_callback, DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()));
  208. ClassDB::bind_method(D_METHOD("interpolate_deferred_callback", "object", "duration", "callback", "arg1", "arg2", "arg3", "arg4", "arg5"), &Tween::interpolate_deferred_callback, DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()));
  209. ClassDB::bind_method(D_METHOD("follow_property", "object", "property", "initial_val", "target", "target_property", "duration", "trans_type", "ease_type", "delay"), &Tween::follow_property, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0));
  210. ClassDB::bind_method(D_METHOD("follow_method", "object", "method", "initial_val", "target", "target_method", "duration", "trans_type", "ease_type", "delay"), &Tween::follow_method, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0));
  211. ClassDB::bind_method(D_METHOD("targeting_property", "object", "property", "initial", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::targeting_property, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0));
  212. ClassDB::bind_method(D_METHOD("targeting_method", "object", "method", "initial", "initial_method", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::targeting_method, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0));
  213. // Add the Tween signals
  214. ADD_SIGNAL(MethodInfo("tween_started", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key")));
  215. ADD_SIGNAL(MethodInfo("tween_step", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key"), PropertyInfo(Variant::REAL, "elapsed"), PropertyInfo(Variant::OBJECT, "value")));
  216. ADD_SIGNAL(MethodInfo("tween_completed", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key")));
  217. ADD_SIGNAL(MethodInfo("tween_all_completed"));
  218. // Add the properties and tie them to the getters and setters
  219. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "repeat"), "set_repeat", "is_repeat");
  220. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_tween_process_mode", "get_tween_process_mode");
  221. ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale");
  222. // Bind Idle vs Physics process
  223. BIND_ENUM_CONSTANT(TWEEN_PROCESS_PHYSICS);
  224. BIND_ENUM_CONSTANT(TWEEN_PROCESS_IDLE);
  225. // Bind the Transition type constants
  226. BIND_ENUM_CONSTANT(TRANS_LINEAR);
  227. BIND_ENUM_CONSTANT(TRANS_SINE);
  228. BIND_ENUM_CONSTANT(TRANS_QUINT);
  229. BIND_ENUM_CONSTANT(TRANS_QUART);
  230. BIND_ENUM_CONSTANT(TRANS_QUAD);
  231. BIND_ENUM_CONSTANT(TRANS_EXPO);
  232. BIND_ENUM_CONSTANT(TRANS_ELASTIC);
  233. BIND_ENUM_CONSTANT(TRANS_CUBIC);
  234. BIND_ENUM_CONSTANT(TRANS_CIRC);
  235. BIND_ENUM_CONSTANT(TRANS_BOUNCE);
  236. BIND_ENUM_CONSTANT(TRANS_BACK);
  237. // Bind the easing constants
  238. BIND_ENUM_CONSTANT(EASE_IN);
  239. BIND_ENUM_CONSTANT(EASE_OUT);
  240. BIND_ENUM_CONSTANT(EASE_IN_OUT);
  241. BIND_ENUM_CONSTANT(EASE_OUT_IN);
  242. }
  243. Variant Tween::_get_initial_val(const InterpolateData &p_data) const {
  244. // What type of data are we interpolating?
  245. switch (p_data.type) {
  246. case INTER_PROPERTY:
  247. case INTER_METHOD:
  248. case FOLLOW_PROPERTY:
  249. case FOLLOW_METHOD:
  250. // Simply use the given initial value
  251. return p_data.initial_val;
  252. case TARGETING_PROPERTY:
  253. case TARGETING_METHOD: {
  254. // Get the object that is being targeted
  255. Object *object = ObjectDB::get_instance(p_data.target_id);
  256. ERR_FAIL_COND_V(object == NULL, p_data.initial_val);
  257. // Are we targeting a property or a method?
  258. Variant initial_val;
  259. if (p_data.type == TARGETING_PROPERTY) {
  260. // Get the property from the target object
  261. bool valid = false;
  262. initial_val = object->get_indexed(p_data.target_key, &valid);
  263. ERR_FAIL_COND_V(!valid, p_data.initial_val);
  264. } else {
  265. // Call the method and get the initial value from it
  266. Variant::CallError error;
  267. initial_val = object->call(p_data.target_key[0], NULL, 0, error);
  268. ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, p_data.initial_val);
  269. }
  270. return initial_val;
  271. }
  272. case INTER_CALLBACK:
  273. // Callback does not have a special initial value
  274. break;
  275. }
  276. // If we've made it here, just return the delta value as the initial value
  277. return p_data.delta_val;
  278. }
  279. Variant Tween::_get_final_val(const InterpolateData &p_data) const {
  280. switch (p_data.type) {
  281. case FOLLOW_PROPERTY:
  282. case FOLLOW_METHOD: {
  283. // Get the object that is being followed
  284. Object *target = ObjectDB::get_instance(p_data.target_id);
  285. ERR_FAIL_COND_V(target == NULL, p_data.initial_val);
  286. // We want to figure out the final value
  287. Variant final_val;
  288. if (p_data.type == FOLLOW_PROPERTY) {
  289. // Read the property as-is
  290. bool valid = false;
  291. final_val = target->get_indexed(p_data.target_key, &valid);
  292. ERR_FAIL_COND_V(!valid, p_data.initial_val);
  293. } else {
  294. // We're looking at a method. Call the method on the target object
  295. Variant::CallError error;
  296. final_val = target->call(p_data.target_key[0], NULL, 0, error);
  297. ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, p_data.initial_val);
  298. }
  299. // If we're looking at an INT value, instead convert it to a REAL
  300. // This is better for interpolation
  301. if (final_val.get_type() == Variant::INT) final_val = final_val.operator real_t();
  302. return final_val;
  303. }
  304. default: {
  305. // If we're not following a final value/method, use the final value from the data
  306. return p_data.final_val;
  307. }
  308. }
  309. }
  310. Variant &Tween::_get_delta_val(InterpolateData &p_data) {
  311. // What kind of data are we interpolating?
  312. switch (p_data.type) {
  313. case INTER_PROPERTY:
  314. case INTER_METHOD:
  315. // Simply return the given delta value
  316. return p_data.delta_val;
  317. case FOLLOW_PROPERTY:
  318. case FOLLOW_METHOD: {
  319. // We're following an object, so grab that instance
  320. Object *target = ObjectDB::get_instance(p_data.target_id);
  321. ERR_FAIL_COND_V(target == NULL, p_data.initial_val);
  322. // We want to figure out the final value
  323. Variant final_val;
  324. if (p_data.type == FOLLOW_PROPERTY) {
  325. // Read the property as-is
  326. bool valid = false;
  327. final_val = target->get_indexed(p_data.target_key, &valid);
  328. ERR_FAIL_COND_V(!valid, p_data.initial_val);
  329. } else {
  330. // We're looking at a method. Call the method on the target object
  331. Variant::CallError error;
  332. final_val = target->call(p_data.target_key[0], NULL, 0, error);
  333. ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, p_data.initial_val);
  334. }
  335. // If we're looking at an INT value, instead convert it to a REAL
  336. // This is better for interpolation
  337. if (final_val.get_type() == Variant::INT) final_val = final_val.operator real_t();
  338. // Calculate the delta based on the initial value and the final value
  339. _calc_delta_val(p_data.initial_val, final_val, p_data.delta_val);
  340. return p_data.delta_val;
  341. }
  342. case TARGETING_PROPERTY:
  343. case TARGETING_METHOD: {
  344. // Grab the initial value from the data to calculate delta
  345. Variant initial_val = _get_initial_val(p_data);
  346. // If we're looking at an INT value, instead convert it to a REAL
  347. // This is better for interpolation
  348. if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t();
  349. // Calculate the delta based on the initial value and the final value
  350. _calc_delta_val(initial_val, p_data.final_val, p_data.delta_val);
  351. return p_data.delta_val;
  352. }
  353. case INTER_CALLBACK:
  354. // Callbacks have no special delta
  355. break;
  356. }
  357. // If we've made it here, use the initial value as the delta
  358. return p_data.initial_val;
  359. }
  360. Variant Tween::_run_equation(InterpolateData &p_data) {
  361. // Get the initial and delta values from the data
  362. Variant initial_val = _get_initial_val(p_data);
  363. Variant &delta_val = _get_delta_val(p_data);
  364. Variant result;
  365. #define APPLY_EQUATION(element) \
  366. r.element = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, i.element, d.element, p_data.duration);
  367. // What type of data are we interpolating?
  368. switch (initial_val.get_type()) {
  369. case Variant::BOOL:
  370. // Run the boolean specific equation (checking if it is at least 0.5)
  371. result = (_run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, initial_val, delta_val, p_data.duration)) >= 0.5;
  372. break;
  373. case Variant::INT:
  374. // Run the integer specific equation
  375. result = (int)_run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, (int)initial_val, (int)delta_val, p_data.duration);
  376. break;
  377. case Variant::REAL:
  378. // Run the REAL specific equation
  379. result = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, (real_t)initial_val, (real_t)delta_val, p_data.duration);
  380. break;
  381. case Variant::VECTOR2: {
  382. // Get vectors for initial and delta values
  383. Vector2 i = initial_val;
  384. Vector2 d = delta_val;
  385. Vector2 r;
  386. // Execute the equation and mutate the r vector
  387. // This uses the custom APPLY_EQUATION macro defined above
  388. APPLY_EQUATION(x);
  389. APPLY_EQUATION(y);
  390. result = r;
  391. } break;
  392. case Variant::RECT2: {
  393. // Get the Rect2 for initial and delta value
  394. Rect2 i = initial_val;
  395. Rect2 d = delta_val;
  396. Rect2 r;
  397. // Execute the equation for the position and size of Rect2
  398. APPLY_EQUATION(position.x);
  399. APPLY_EQUATION(position.y);
  400. APPLY_EQUATION(size.x);
  401. APPLY_EQUATION(size.y);
  402. result = r;
  403. } break;
  404. case Variant::VECTOR3: {
  405. // Get vectors for initial and delta values
  406. Vector3 i = initial_val;
  407. Vector3 d = delta_val;
  408. Vector3 r;
  409. // Execute the equation and mutate the r vector
  410. // This uses the custom APPLY_EQUATION macro defined above
  411. APPLY_EQUATION(x);
  412. APPLY_EQUATION(y);
  413. APPLY_EQUATION(z);
  414. result = r;
  415. } break;
  416. case Variant::TRANSFORM2D: {
  417. // Get the transforms for initial and delta values
  418. Transform2D i = initial_val;
  419. Transform2D d = delta_val;
  420. Transform2D r;
  421. // Execute the equation on the transforms and mutate the r transform
  422. // This uses the custom APPLY_EQUATION macro defined above
  423. APPLY_EQUATION(elements[0][0]);
  424. APPLY_EQUATION(elements[0][1]);
  425. APPLY_EQUATION(elements[1][0]);
  426. APPLY_EQUATION(elements[1][1]);
  427. APPLY_EQUATION(elements[2][0]);
  428. APPLY_EQUATION(elements[2][1]);
  429. result = r;
  430. } break;
  431. case Variant::QUAT: {
  432. // Get the quaternian for the initial and delta values
  433. Quat i = initial_val;
  434. Quat d = delta_val;
  435. Quat r;
  436. // Execute the equation on the quaternian values and mutate the r quaternian
  437. // This uses the custom APPLY_EQUATION macro defined above
  438. APPLY_EQUATION(x);
  439. APPLY_EQUATION(y);
  440. APPLY_EQUATION(z);
  441. APPLY_EQUATION(w);
  442. result = r;
  443. } break;
  444. case Variant::AABB: {
  445. // Get the AABB's for the initial and delta values
  446. AABB i = initial_val;
  447. AABB d = delta_val;
  448. AABB r;
  449. // Execute the equation for the position and size of the AABB's and mutate the r AABB
  450. // This uses the custom APPLY_EQUATION macro defined above
  451. APPLY_EQUATION(position.x);
  452. APPLY_EQUATION(position.y);
  453. APPLY_EQUATION(position.z);
  454. APPLY_EQUATION(size.x);
  455. APPLY_EQUATION(size.y);
  456. APPLY_EQUATION(size.z);
  457. result = r;
  458. } break;
  459. case Variant::BASIS: {
  460. // Get the basis for initial and delta values
  461. Basis i = initial_val;
  462. Basis d = delta_val;
  463. Basis r;
  464. // Execute the equation on all the basis and mutate the r basis
  465. // This uses the custom APPLY_EQUATION macro defined above
  466. APPLY_EQUATION(elements[0][0]);
  467. APPLY_EQUATION(elements[0][1]);
  468. APPLY_EQUATION(elements[0][2]);
  469. APPLY_EQUATION(elements[1][0]);
  470. APPLY_EQUATION(elements[1][1]);
  471. APPLY_EQUATION(elements[1][2]);
  472. APPLY_EQUATION(elements[2][0]);
  473. APPLY_EQUATION(elements[2][1]);
  474. APPLY_EQUATION(elements[2][2]);
  475. result = r;
  476. } break;
  477. case Variant::TRANSFORM: {
  478. // Get the transforms for the initial and delta values
  479. Transform i = initial_val;
  480. Transform d = delta_val;
  481. Transform r;
  482. // Execute the equation for each of the transforms and their origin and mutate the r transform
  483. // This uses the custom APPLY_EQUATION macro defined above
  484. APPLY_EQUATION(basis.elements[0][0]);
  485. APPLY_EQUATION(basis.elements[0][1]);
  486. APPLY_EQUATION(basis.elements[0][2]);
  487. APPLY_EQUATION(basis.elements[1][0]);
  488. APPLY_EQUATION(basis.elements[1][1]);
  489. APPLY_EQUATION(basis.elements[1][2]);
  490. APPLY_EQUATION(basis.elements[2][0]);
  491. APPLY_EQUATION(basis.elements[2][1]);
  492. APPLY_EQUATION(basis.elements[2][2]);
  493. APPLY_EQUATION(origin.x);
  494. APPLY_EQUATION(origin.y);
  495. APPLY_EQUATION(origin.z);
  496. result = r;
  497. } break;
  498. case Variant::COLOR: {
  499. // Get the Color for initial and delta value
  500. Color i = initial_val;
  501. Color d = delta_val;
  502. Color r;
  503. // Apply the equation on the Color RGBA, and mutate the r color
  504. // This uses the custom APPLY_EQUATION macro defined above
  505. APPLY_EQUATION(r);
  506. APPLY_EQUATION(g);
  507. APPLY_EQUATION(b);
  508. APPLY_EQUATION(a);
  509. result = r;
  510. } break;
  511. default: {
  512. // If unknown, just return the initial value
  513. result = initial_val;
  514. } break;
  515. };
  516. #undef APPLY_EQUATION
  517. // Return the result that was computed
  518. return result;
  519. }
  520. bool Tween::_apply_tween_value(InterpolateData &p_data, Variant &value) {
  521. // Get the object we want to apply the new value to
  522. Object *object = ObjectDB::get_instance(p_data.id);
  523. ERR_FAIL_COND_V(object == NULL, false);
  524. // What kind of data are we mutating?
  525. switch (p_data.type) {
  526. case INTER_PROPERTY:
  527. case FOLLOW_PROPERTY:
  528. case TARGETING_PROPERTY: {
  529. // Simply set the property on the object
  530. bool valid = false;
  531. object->set_indexed(p_data.key, value, &valid);
  532. return valid;
  533. }
  534. case INTER_METHOD:
  535. case FOLLOW_METHOD:
  536. case TARGETING_METHOD: {
  537. // We want to call the method on the target object
  538. Variant::CallError error;
  539. // Do we have a non-nil value passed in?
  540. if (value.get_type() != Variant::NIL) {
  541. // Pass it as an argument to the function call
  542. Variant *arg[1] = { &value };
  543. object->call(p_data.key[0], (const Variant **)arg, 1, error);
  544. } else {
  545. // Don't pass any argument
  546. object->call(p_data.key[0], NULL, 0, error);
  547. }
  548. // Did we get an error from the function call?
  549. return error.error == Variant::CallError::CALL_OK;
  550. }
  551. case INTER_CALLBACK:
  552. // Nothing to apply for a callback
  553. break;
  554. };
  555. // No issues found!
  556. return true;
  557. }
  558. void Tween::_tween_process(float p_delta) {
  559. // Process all of the pending commands
  560. _process_pending_commands();
  561. // If the scale is 0, make no progress on the tweens
  562. if (speed_scale == 0)
  563. return;
  564. // Update the delta and whether we are pending an update
  565. p_delta *= speed_scale;
  566. pending_update++;
  567. // Are we repeating the interpolations?
  568. if (repeat) {
  569. // For each interpolation...
  570. bool repeats_finished = true;
  571. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  572. // Get the data from it
  573. InterpolateData &data = E->get();
  574. // Is not finished?
  575. if (!data.finish) {
  576. // We aren't finished yet, no need to check the rest
  577. repeats_finished = false;
  578. break;
  579. }
  580. }
  581. // If we are all finished, we can reset all of the tweens
  582. if (repeats_finished)
  583. reset_all();
  584. }
  585. // Are all of the tweens complete?
  586. bool all_finished = true;
  587. // For each tween we wish to interpolate...
  588. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  589. // Get the data from it
  590. InterpolateData &data = E->get();
  591. // Track if we hit one that isn't finished yet
  592. all_finished = all_finished && data.finish;
  593. // Is the data not active or already finished? No need to go any further
  594. if (!data.active || data.finish)
  595. continue;
  596. // Get the target object for this interpolation
  597. Object *object = ObjectDB::get_instance(data.id);
  598. if (object == NULL)
  599. continue;
  600. // Are we still delaying this tween?
  601. bool prev_delaying = data.elapsed <= data.delay;
  602. data.elapsed += p_delta;
  603. if (data.elapsed < data.delay)
  604. continue;
  605. else if (prev_delaying) {
  606. // We can apply the tween's value to the data and emit that the tween has started
  607. _apply_tween_value(data, data.initial_val);
  608. emit_signal("tween_started", object, NodePath(Vector<StringName>(), data.key, false));
  609. }
  610. // Are we at the end of the tween?
  611. if (data.elapsed > (data.delay + data.duration)) {
  612. // Set the elapsed time to the end and mark this one as finished
  613. data.elapsed = data.delay + data.duration;
  614. data.finish = true;
  615. }
  616. // Are we interpolating a callback?
  617. if (data.type == INTER_CALLBACK) {
  618. // Is the tween completed?
  619. if (data.finish) {
  620. // Are we calling this callback deferred or immediately?
  621. if (data.call_deferred) {
  622. // Run the deferred function callback, applying the correct number of arguments
  623. switch (data.args) {
  624. case 0:
  625. object->call_deferred(data.key[0]);
  626. break;
  627. case 1:
  628. object->call_deferred(data.key[0], data.arg[0]);
  629. break;
  630. case 2:
  631. object->call_deferred(data.key[0], data.arg[0], data.arg[1]);
  632. break;
  633. case 3:
  634. object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2]);
  635. break;
  636. case 4:
  637. object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2], data.arg[3]);
  638. break;
  639. case 5:
  640. object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2], data.arg[3], data.arg[4]);
  641. break;
  642. }
  643. } else {
  644. // Call the function directly with the arguments
  645. Variant::CallError error;
  646. Variant *arg[5] = {
  647. &data.arg[0],
  648. &data.arg[1],
  649. &data.arg[2],
  650. &data.arg[3],
  651. &data.arg[4],
  652. };
  653. object->call(data.key[0], (const Variant **)arg, data.args, error);
  654. }
  655. }
  656. } else {
  657. // We can apply the value directly
  658. Variant result = _run_equation(data);
  659. _apply_tween_value(data, result);
  660. // Emit that the tween has taken a step
  661. emit_signal("tween_step", object, NodePath(Vector<StringName>(), data.key, false), data.elapsed, result);
  662. }
  663. // Is the tween now finished?
  664. if (data.finish) {
  665. // Set it to the final value directly
  666. Variant final_val = _get_final_val(data);
  667. _apply_tween_value(data, final_val);
  668. // Mark the tween as completed and emit the signal
  669. data.elapsed = 0;
  670. emit_signal("tween_completed", object, NodePath(Vector<StringName>(), data.key, false));
  671. // If we are not repeating the tween, remove it
  672. if (!repeat)
  673. call_deferred("_remove_by_uid", data.uid);
  674. } else if (!repeat) {
  675. // Check whether all tweens are finished
  676. all_finished = all_finished && data.finish;
  677. }
  678. }
  679. // One less update left to go
  680. pending_update--;
  681. // If all tweens are completed, we no longer need to be active
  682. if (all_finished) {
  683. set_active(false);
  684. emit_signal("tween_all_completed");
  685. }
  686. }
  687. void Tween::set_tween_process_mode(TweenProcessMode p_mode) {
  688. tween_process_mode = p_mode;
  689. }
  690. Tween::TweenProcessMode Tween::get_tween_process_mode() const {
  691. return tween_process_mode;
  692. }
  693. bool Tween::is_active() const {
  694. return is_processing_internal() || is_physics_processing_internal();
  695. }
  696. void Tween::set_active(bool p_active) {
  697. // Do nothing if it's the same active mode that we currently are
  698. if (is_active() == p_active)
  699. return;
  700. // Depending on physics or idle, set processing
  701. switch (tween_process_mode) {
  702. case TWEEN_PROCESS_IDLE: set_process_internal(p_active); break;
  703. case TWEEN_PROCESS_PHYSICS: set_physics_process_internal(p_active); break;
  704. }
  705. }
  706. bool Tween::is_repeat() const {
  707. return repeat;
  708. }
  709. void Tween::set_repeat(bool p_repeat) {
  710. repeat = p_repeat;
  711. }
  712. void Tween::set_speed_scale(float p_speed) {
  713. speed_scale = p_speed;
  714. }
  715. float Tween::get_speed_scale() const {
  716. return speed_scale;
  717. }
  718. bool Tween::start() {
  719. ERR_FAIL_COND_V_MSG(!is_inside_tree(), false, "Tween was not added to the SceneTree!");
  720. // Are there any pending updates?
  721. if (pending_update != 0) {
  722. // Start the tweens after deferring
  723. call_deferred("start");
  724. return true;
  725. }
  726. // We want to be activated
  727. set_active(true);
  728. return true;
  729. }
  730. bool Tween::reset(Object *p_object, StringName p_key) {
  731. // Find all interpolations that use the same object and target string
  732. pending_update++;
  733. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  734. // Get the target object
  735. InterpolateData &data = E->get();
  736. Object *object = ObjectDB::get_instance(data.id);
  737. if (object == NULL)
  738. continue;
  739. // Do we have the correct object and key?
  740. if (object == p_object && (data.concatenated_key == p_key || p_key == "")) {
  741. // Reset the tween to the initial state
  742. data.elapsed = 0;
  743. data.finish = false;
  744. // Also apply the initial state if there isn't a delay
  745. if (data.delay == 0)
  746. _apply_tween_value(data, data.initial_val);
  747. }
  748. }
  749. pending_update--;
  750. return true;
  751. }
  752. bool Tween::reset_all() {
  753. // Go through all interpolations
  754. pending_update++;
  755. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  756. // Get the target data and set it back to the initial state
  757. InterpolateData &data = E->get();
  758. data.elapsed = 0;
  759. data.finish = false;
  760. // If there isn't a delay, apply the value to the object
  761. if (data.delay == 0)
  762. _apply_tween_value(data, data.initial_val);
  763. }
  764. pending_update--;
  765. return true;
  766. }
  767. bool Tween::stop(Object *p_object, StringName p_key) {
  768. // Find the tween that has the given target object and string key
  769. pending_update++;
  770. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  771. // Get the object the tween is targeting
  772. InterpolateData &data = E->get();
  773. Object *object = ObjectDB::get_instance(data.id);
  774. if (object == NULL)
  775. continue;
  776. // Is this the correct object and does it have the given key?
  777. if (object == p_object && (data.concatenated_key == p_key || p_key == ""))
  778. // Disable the tween
  779. data.active = false;
  780. }
  781. pending_update--;
  782. return true;
  783. }
  784. bool Tween::stop_all() {
  785. // We no longer need to be active since all tweens have been stopped
  786. set_active(false);
  787. // For each interpolation...
  788. pending_update++;
  789. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  790. // Simply set it inactive
  791. InterpolateData &data = E->get();
  792. data.active = false;
  793. }
  794. pending_update--;
  795. return true;
  796. }
  797. bool Tween::resume(Object *p_object, StringName p_key) {
  798. // We need to be activated
  799. // TODO: What if no tween is found??
  800. set_active(true);
  801. // Find the tween that uses the given target object and string key
  802. pending_update++;
  803. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  804. // Grab the object
  805. InterpolateData &data = E->get();
  806. Object *object = ObjectDB::get_instance(data.id);
  807. if (object == NULL)
  808. continue;
  809. // If the object and string key match, activate it
  810. if (object == p_object && (data.concatenated_key == p_key || p_key == ""))
  811. data.active = true;
  812. }
  813. pending_update--;
  814. return true;
  815. }
  816. bool Tween::resume_all() {
  817. // Set ourselves active so we can process tweens
  818. // TODO: What if there are no tweens? We get set to active for no reason!
  819. set_active(true);
  820. // For each interpolation...
  821. pending_update++;
  822. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  823. // Simply grab it and set it to active
  824. InterpolateData &data = E->get();
  825. data.active = true;
  826. }
  827. pending_update--;
  828. return true;
  829. }
  830. bool Tween::remove(Object *p_object, StringName p_key) {
  831. // If we are still updating, call this function again later
  832. if (pending_update != 0) {
  833. call_deferred("remove", p_object, p_key);
  834. return true;
  835. }
  836. // For each interpolation...
  837. List<List<InterpolateData>::Element *> for_removal;
  838. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  839. // Get the target object
  840. InterpolateData &data = E->get();
  841. Object *object = ObjectDB::get_instance(data.id);
  842. if (object == NULL)
  843. continue;
  844. // If the target object and string key match, queue it for removal
  845. if (object == p_object && (data.concatenated_key == p_key || p_key == "")) {
  846. for_removal.push_back(E);
  847. }
  848. }
  849. // For each interpolation we wish to remove...
  850. for (List<List<InterpolateData>::Element *>::Element *E = for_removal.front(); E; E = E->next()) {
  851. // Erase it
  852. interpolates.erase(E->get());
  853. }
  854. return true;
  855. }
  856. void Tween::_remove_by_uid(int uid) {
  857. // If we are still updating, call this function again later
  858. if (pending_update != 0) {
  859. call_deferred("_remove_by_uid", uid);
  860. return;
  861. }
  862. // Find the interpolation that matches the given UID
  863. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  864. if (uid == E->get().uid) {
  865. // It matches, erase it and stop looking
  866. E->erase();
  867. break;
  868. }
  869. }
  870. }
  871. void Tween::_push_interpolate_data(InterpolateData &p_data) {
  872. pending_update++;
  873. // Add the new interpolation
  874. p_data.uid = ++uid;
  875. interpolates.push_back(p_data);
  876. pending_update--;
  877. }
  878. bool Tween::remove_all() {
  879. // If we are still updating, call this function again later
  880. if (pending_update != 0) {
  881. call_deferred("remove_all");
  882. return true;
  883. }
  884. // We no longer need to be active
  885. set_active(false);
  886. // Clear out all interpolations and reset the uid
  887. interpolates.clear();
  888. uid = 0;
  889. return true;
  890. }
  891. bool Tween::seek(real_t p_time) {
  892. // Go through each interpolation...
  893. pending_update++;
  894. for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  895. // Get the target data
  896. InterpolateData &data = E->get();
  897. // Update the elapsed data to be set to the target time
  898. data.elapsed = p_time;
  899. // Are we at the end?
  900. if (data.elapsed < data.delay) {
  901. // There is still time left to go
  902. data.finish = false;
  903. continue;
  904. } else if (data.elapsed >= (data.delay + data.duration)) {
  905. // We are past the end of it, set the elapsed time to the end and mark as finished
  906. data.elapsed = (data.delay + data.duration);
  907. data.finish = true;
  908. } else {
  909. // We are not finished with this interpolation yet
  910. data.finish = false;
  911. }
  912. // If we are a callback, do nothing special
  913. if (data.type == INTER_CALLBACK) {
  914. continue;
  915. }
  916. // Run the equation on the data and apply the value
  917. Variant result = _run_equation(data);
  918. _apply_tween_value(data, result);
  919. }
  920. pending_update--;
  921. return true;
  922. }
  923. real_t Tween::tell() const {
  924. // We want to grab the position of the furthest along tween
  925. pending_update++;
  926. real_t pos = 0;
  927. // For each interpolation...
  928. for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  929. // Get the data and figure out if it's position is further along than the previous ones
  930. const InterpolateData &data = E->get();
  931. if (data.elapsed > pos)
  932. // Save it if so
  933. pos = data.elapsed;
  934. }
  935. pending_update--;
  936. return pos;
  937. }
  938. real_t Tween::get_runtime() const {
  939. // If the tween isn't moving, it'll last forever
  940. if (speed_scale == 0) {
  941. return INFINITY;
  942. }
  943. pending_update++;
  944. // For each interpolation...
  945. real_t runtime = 0;
  946. for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) {
  947. // Get the tween data and see if it's runtime is greater than the previous tweens
  948. const InterpolateData &data = E->get();
  949. real_t t = data.delay + data.duration;
  950. if (t > runtime)
  951. // This is the longest running tween
  952. runtime = t;
  953. }
  954. pending_update--;
  955. // Adjust the runtime for the current speed scale
  956. return runtime / speed_scale;
  957. }
  958. bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final_val, Variant &p_delta_val) {
  959. // Get the initial, final, and delta values
  960. const Variant &initial_val = p_initial_val;
  961. const Variant &final_val = p_final_val;
  962. Variant &delta_val = p_delta_val;
  963. // What kind of data are we interpolating?
  964. switch (initial_val.get_type()) {
  965. case Variant::BOOL:
  966. // We'll treat booleans just like integers
  967. case Variant::INT:
  968. // Compute the integer delta
  969. delta_val = (int)final_val - (int)initial_val;
  970. break;
  971. case Variant::REAL:
  972. // Convert to REAL and find the delta
  973. delta_val = (real_t)final_val - (real_t)initial_val;
  974. break;
  975. case Variant::VECTOR2:
  976. // Convert to Vectors and find the delta
  977. delta_val = final_val.operator Vector2() - initial_val.operator Vector2();
  978. break;
  979. case Variant::RECT2: {
  980. // Build a new Rect2 and use the new position and sizes to make a delta
  981. Rect2 i = initial_val;
  982. Rect2 f = final_val;
  983. delta_val = Rect2(f.position - i.position, f.size - i.size);
  984. } break;
  985. case Variant::VECTOR3:
  986. // Convert to Vectors and find the delta
  987. delta_val = final_val.operator Vector3() - initial_val.operator Vector3();
  988. break;
  989. case Variant::TRANSFORM2D: {
  990. // Build a new transform which is the difference between the initial and final values
  991. Transform2D i = initial_val;
  992. Transform2D f = final_val;
  993. Transform2D d = Transform2D();
  994. d[0][0] = f.elements[0][0] - i.elements[0][0];
  995. d[0][1] = f.elements[0][1] - i.elements[0][1];
  996. d[1][0] = f.elements[1][0] - i.elements[1][0];
  997. d[1][1] = f.elements[1][1] - i.elements[1][1];
  998. d[2][0] = f.elements[2][0] - i.elements[2][0];
  999. d[2][1] = f.elements[2][1] - i.elements[2][1];
  1000. delta_val = d;
  1001. } break;
  1002. case Variant::QUAT:
  1003. // Convert to quaternianls and find the delta
  1004. delta_val = final_val.operator Quat() - initial_val.operator Quat();
  1005. break;
  1006. case Variant::AABB: {
  1007. // Build a new AABB and use the new position and sizes to make a delta
  1008. AABB i = initial_val;
  1009. AABB f = final_val;
  1010. delta_val = AABB(f.position - i.position, f.size - i.size);
  1011. } break;
  1012. case Variant::BASIS: {
  1013. // Build a new basis which is the delta between the initial and final values
  1014. Basis i = initial_val;
  1015. Basis f = final_val;
  1016. delta_val = Basis(f.elements[0][0] - i.elements[0][0],
  1017. f.elements[0][1] - i.elements[0][1],
  1018. f.elements[0][2] - i.elements[0][2],
  1019. f.elements[1][0] - i.elements[1][0],
  1020. f.elements[1][1] - i.elements[1][1],
  1021. f.elements[1][2] - i.elements[1][2],
  1022. f.elements[2][0] - i.elements[2][0],
  1023. f.elements[2][1] - i.elements[2][1],
  1024. f.elements[2][2] - i.elements[2][2]);
  1025. } break;
  1026. case Variant::TRANSFORM: {
  1027. // Build a new transform which is the difference between the initial and final values
  1028. Transform i = initial_val;
  1029. Transform f = final_val;
  1030. Transform d;
  1031. d.set(f.basis.elements[0][0] - i.basis.elements[0][0],
  1032. f.basis.elements[0][1] - i.basis.elements[0][1],
  1033. f.basis.elements[0][2] - i.basis.elements[0][2],
  1034. f.basis.elements[1][0] - i.basis.elements[1][0],
  1035. f.basis.elements[1][1] - i.basis.elements[1][1],
  1036. f.basis.elements[1][2] - i.basis.elements[1][2],
  1037. f.basis.elements[2][0] - i.basis.elements[2][0],
  1038. f.basis.elements[2][1] - i.basis.elements[2][1],
  1039. f.basis.elements[2][2] - i.basis.elements[2][2],
  1040. f.origin.x - i.origin.x,
  1041. f.origin.y - i.origin.y,
  1042. f.origin.z - i.origin.z);
  1043. delta_val = d;
  1044. } break;
  1045. case Variant::COLOR: {
  1046. // Make a new color which is the difference between each the color's RGBA attributes
  1047. Color i = initial_val;
  1048. Color f = final_val;
  1049. delta_val = Color(f.r - i.r, f.g - i.g, f.b - i.b, f.a - i.a);
  1050. } break;
  1051. default: {
  1052. static Variant::Type supported_types[] = {
  1053. Variant::BOOL,
  1054. Variant::INT,
  1055. Variant::REAL,
  1056. Variant::VECTOR2,
  1057. Variant::RECT2,
  1058. Variant::VECTOR3,
  1059. Variant::TRANSFORM2D,
  1060. Variant::QUAT,
  1061. Variant::AABB,
  1062. Variant::BASIS,
  1063. Variant::TRANSFORM,
  1064. Variant::COLOR,
  1065. };
  1066. int length = *(&supported_types + 1) - supported_types;
  1067. String error_msg = "Invalid parameter type. Supported types are: ";
  1068. for (int i = 0; i < length; i++) {
  1069. if (i != 0) {
  1070. error_msg += ", ";
  1071. }
  1072. error_msg += Variant::get_type_name(supported_types[i]);
  1073. }
  1074. error_msg += ".";
  1075. ERR_PRINT(error_msg);
  1076. return false;
  1077. }
  1078. };
  1079. return true;
  1080. }
  1081. bool Tween::_build_interpolation(InterpolateType p_interpolation_type, Object *p_object, NodePath *p_property, StringName *p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
  1082. // TODO: Add initialization+implementation for remaining interpolation types
  1083. // TODO: Fix this method's organization to take advantage of the type
  1084. // Make a new interpolation data
  1085. InterpolateData data;
  1086. data.active = true;
  1087. data.type = p_interpolation_type;
  1088. data.finish = false;
  1089. data.elapsed = 0;
  1090. // Validate and apply interpolation data
  1091. // Give it the object
  1092. ERR_FAIL_COND_V_MSG(p_object == NULL, false, "Invalid object provided to Tween.");
  1093. ERR_FAIL_COND_V_MSG(!ObjectDB::instance_validate(p_object), false, "Invalid object provided to Tween.");
  1094. data.id = p_object->get_instance_id();
  1095. // Validate the initial and final values
  1096. ERR_FAIL_COND_V_MSG(p_initial_val.get_type() != p_final_val.get_type(), false, "Initial value type '" + Variant::get_type_name(p_initial_val.get_type()) + "' does not match final value type '" + Variant::get_type_name(p_final_val.get_type()) + "'.");
  1097. data.initial_val = p_initial_val;
  1098. data.final_val = p_final_val;
  1099. // Check the Duration
  1100. ERR_FAIL_COND_V_MSG(p_duration < 0, false, "Only non-negative duration values allowed in Tweens.");
  1101. data.duration = p_duration;
  1102. // Tween Delay
  1103. ERR_FAIL_COND_V_MSG(p_delay < 0, false, "Only non-negative delay values allowed in Tweens.");
  1104. data.delay = p_delay;
  1105. // Transition type
  1106. ERR_FAIL_COND_V_MSG(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false, "Invalid transition type provided to Tween.");
  1107. data.trans_type = p_trans_type;
  1108. // Easing type
  1109. ERR_FAIL_COND_V_MSG(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false, "Invalid easing type provided to Tween.");
  1110. data.ease_type = p_ease_type;
  1111. // Is the property defined?
  1112. if (p_property) {
  1113. // Check that the object actually contains the given property
  1114. bool prop_valid = false;
  1115. p_object->get_indexed(p_property->get_subnames(), &prop_valid);
  1116. ERR_FAIL_COND_V_MSG(!prop_valid, false, "Tween target object has no property named: " + p_property->get_concatenated_subnames() + ".");
  1117. data.key = p_property->get_subnames();
  1118. data.concatenated_key = p_property->get_concatenated_subnames();
  1119. }
  1120. // Is the method defined?
  1121. if (p_method) {
  1122. // Does the object even have the requested method?
  1123. ERR_FAIL_COND_V_MSG(!p_object->has_method(*p_method), false, "Tween target object has no method named: " + *p_method + ".");
  1124. data.key.push_back(*p_method);
  1125. data.concatenated_key = *p_method;
  1126. }
  1127. // Is there not a valid delta?
  1128. if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val))
  1129. return false;
  1130. // Add this interpolation to the total
  1131. _push_interpolate_data(data);
  1132. return true;
  1133. }
  1134. bool Tween::interpolate_property(Object *p_object, NodePath p_property, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
  1135. // If we are busy updating, call this function again later
  1136. if (pending_update != 0) {
  1137. _add_pending_command("interpolate_property", p_object, p_property, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
  1138. return true;
  1139. }
  1140. // Get the property from the node path
  1141. p_property = p_property.get_as_property_path();
  1142. // If no initial value given, grab the initial value from the object
  1143. // TODO: Is this documented? This is very useful and removes a lot of clutter from tweens!
  1144. if (p_initial_val.get_type() == Variant::NIL) p_initial_val = p_object->get_indexed(p_property.get_subnames());
  1145. // Convert any integers into REALs as they are better for interpolation
  1146. if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
  1147. if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();
  1148. // Build the interpolation data
  1149. bool result = _build_interpolation(INTER_PROPERTY, p_object, &p_property, NULL, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
  1150. return result;
  1151. }
  1152. bool Tween::interpolate_method(Object *p_object, StringName p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
  1153. // If we are busy updating, call this function again later
  1154. if (pending_update != 0) {
  1155. _add_pending_command("interpolate_method", p_object, p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
  1156. return true;
  1157. }
  1158. // Convert any integers into REALs as they are better for interpolation
  1159. if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
  1160. if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();
  1161. // Build the interpolation data
  1162. bool result = _build_interpolation(INTER_METHOD, p_object, NULL, &p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
  1163. return result;
  1164. }
  1165. bool Tween::interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) {
  1166. // If we are already updating, call this function again later
  1167. if (pending_update != 0) {
  1168. _add_pending_command("interpolate_callback", p_object, p_duration, p_callback, p_arg1, p_arg2, p_arg3, p_arg4, p_arg5);
  1169. return true;
  1170. }
  1171. // Check that the target object is valid
  1172. ERR_FAIL_COND_V(p_object == NULL, false);
  1173. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
  1174. // Duration cannot be negative
  1175. ERR_FAIL_COND_V(p_duration < 0, false);
  1176. // Check whether the object even has the callback
  1177. ERR_FAIL_COND_V_MSG(!p_object->has_method(p_callback), false, "Object has no callback named: " + p_callback + ".");
  1178. // Build a new InterpolationData
  1179. InterpolateData data;
  1180. data.active = true;
  1181. data.type = INTER_CALLBACK;
  1182. data.finish = false;
  1183. data.call_deferred = false;
  1184. data.elapsed = 0;
  1185. // Give the data it's configuration
  1186. data.id = p_object->get_instance_id();
  1187. data.key.push_back(p_callback);
  1188. data.concatenated_key = p_callback;
  1189. data.duration = p_duration;
  1190. data.delay = 0;
  1191. // Add arguments to the interpolation
  1192. int args = 0;
  1193. if (p_arg5.get_type() != Variant::NIL)
  1194. args = 5;
  1195. else if (p_arg4.get_type() != Variant::NIL)
  1196. args = 4;
  1197. else if (p_arg3.get_type() != Variant::NIL)
  1198. args = 3;
  1199. else if (p_arg2.get_type() != Variant::NIL)
  1200. args = 2;
  1201. else if (p_arg1.get_type() != Variant::NIL)
  1202. args = 1;
  1203. else
  1204. args = 0;
  1205. data.args = args;
  1206. data.arg[0] = p_arg1;
  1207. data.arg[1] = p_arg2;
  1208. data.arg[2] = p_arg3;
  1209. data.arg[3] = p_arg4;
  1210. data.arg[4] = p_arg5;
  1211. // Add the new interpolation
  1212. _push_interpolate_data(data);
  1213. return true;
  1214. }
  1215. bool Tween::interpolate_deferred_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) {
  1216. // If we are already updating, call this function again later
  1217. if (pending_update != 0) {
  1218. _add_pending_command("interpolate_deferred_callback", p_object, p_duration, p_callback, p_arg1, p_arg2, p_arg3, p_arg4, p_arg5);
  1219. return true;
  1220. }
  1221. // Check that the target object is valid
  1222. ERR_FAIL_COND_V(p_object == NULL, false);
  1223. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
  1224. // No negative durations allowed
  1225. ERR_FAIL_COND_V(p_duration < 0, false);
  1226. // Confirm the callback exists on the object
  1227. ERR_FAIL_COND_V_MSG(!p_object->has_method(p_callback), false, "Object has no callback named: " + p_callback + ".");
  1228. // Create a new InterpolateData for the callback
  1229. InterpolateData data;
  1230. data.active = true;
  1231. data.type = INTER_CALLBACK;
  1232. data.finish = false;
  1233. data.call_deferred = true;
  1234. data.elapsed = 0;
  1235. // Give the data it's configuration
  1236. data.id = p_object->get_instance_id();
  1237. data.key.push_back(p_callback);
  1238. data.concatenated_key = p_callback;
  1239. data.duration = p_duration;
  1240. data.delay = 0;
  1241. // Collect arguments for the callback
  1242. int args = 0;
  1243. if (p_arg5.get_type() != Variant::NIL)
  1244. args = 5;
  1245. else if (p_arg4.get_type() != Variant::NIL)
  1246. args = 4;
  1247. else if (p_arg3.get_type() != Variant::NIL)
  1248. args = 3;
  1249. else if (p_arg2.get_type() != Variant::NIL)
  1250. args = 2;
  1251. else if (p_arg1.get_type() != Variant::NIL)
  1252. args = 1;
  1253. else
  1254. args = 0;
  1255. data.args = args;
  1256. data.arg[0] = p_arg1;
  1257. data.arg[1] = p_arg2;
  1258. data.arg[2] = p_arg3;
  1259. data.arg[3] = p_arg4;
  1260. data.arg[4] = p_arg5;
  1261. // Add the new interpolation
  1262. _push_interpolate_data(data);
  1263. return true;
  1264. }
  1265. bool Tween::follow_property(Object *p_object, NodePath p_property, Variant p_initial_val, Object *p_target, NodePath p_target_property, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
  1266. // If we are already updating, call this function again later
  1267. if (pending_update != 0) {
  1268. _add_pending_command("follow_property", p_object, p_property, p_initial_val, p_target, p_target_property, p_duration, p_trans_type, p_ease_type, p_delay);
  1269. return true;
  1270. }
  1271. // Get the two properties from their paths
  1272. p_property = p_property.get_as_property_path();
  1273. p_target_property = p_target_property.get_as_property_path();
  1274. // If no initial value is given, grab it from the source object
  1275. // TODO: Is this documented? It's really helpful for decluttering tweens
  1276. if (p_initial_val.get_type() == Variant::NIL) p_initial_val = p_object->get_indexed(p_property.get_subnames());
  1277. // Convert initial INT values to REAL as they are better for interpolation
  1278. if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
  1279. // Confirm the source and target objects are valid
  1280. ERR_FAIL_COND_V(p_object == NULL, false);
  1281. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
  1282. ERR_FAIL_COND_V(p_target == NULL, false);
  1283. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_target), false);
  1284. // No negative durations
  1285. ERR_FAIL_COND_V(p_duration < 0, false);
  1286. // Ensure transition and easing types are valid
  1287. ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
  1288. ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);
  1289. // No negative delays
  1290. ERR_FAIL_COND_V(p_delay < 0, false);
  1291. // Confirm the source and target objects have the desired properties
  1292. bool prop_valid = false;
  1293. p_object->get_indexed(p_property.get_subnames(), &prop_valid);
  1294. ERR_FAIL_COND_V(!prop_valid, false);
  1295. bool target_prop_valid = false;
  1296. Variant target_val = p_target->get_indexed(p_target_property.get_subnames(), &target_prop_valid);
  1297. ERR_FAIL_COND_V(!target_prop_valid, false);
  1298. // Convert target INT to REAL since it is better for interpolation
  1299. if (target_val.get_type() == Variant::INT) target_val = target_val.operator real_t();
  1300. // Verify that the target value and initial value are the same type
  1301. ERR_FAIL_COND_V(target_val.get_type() != p_initial_val.get_type(), false);
  1302. // Create a new InterpolateData
  1303. InterpolateData data;
  1304. data.active = true;
  1305. data.type = FOLLOW_PROPERTY;
  1306. data.finish = false;
  1307. data.elapsed = 0;
  1308. // Give the InterpolateData it's configuration
  1309. data.id = p_object->get_instance_id();
  1310. data.key = p_property.get_subnames();
  1311. data.concatenated_key = p_property.get_concatenated_subnames();
  1312. data.initial_val = p_initial_val;
  1313. data.target_id = p_target->get_instance_id();
  1314. data.target_key = p_target_property.get_subnames();
  1315. data.duration = p_duration;
  1316. data.trans_type = p_trans_type;
  1317. data.ease_type = p_ease_type;
  1318. data.delay = p_delay;
  1319. // Add the interpolation
  1320. _push_interpolate_data(data);
  1321. return true;
  1322. }
  1323. bool Tween::follow_method(Object *p_object, StringName p_method, Variant p_initial_val, Object *p_target, StringName p_target_method, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
  1324. // If we are currently updating, call this function again later
  1325. if (pending_update != 0) {
  1326. _add_pending_command("follow_method", p_object, p_method, p_initial_val, p_target, p_target_method, p_duration, p_trans_type, p_ease_type, p_delay);
  1327. return true;
  1328. }
  1329. // Convert initial INT values to REAL as they are better for interpolation
  1330. if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
  1331. // Verify the source and target objects are valid
  1332. ERR_FAIL_COND_V(p_object == NULL, false);
  1333. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
  1334. ERR_FAIL_COND_V(p_target == NULL, false);
  1335. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_target), false);
  1336. // No negative durations
  1337. ERR_FAIL_COND_V(p_duration < 0, false);
  1338. // Ensure that the transition and ease types are valid
  1339. ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
  1340. ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);
  1341. // No negative delays
  1342. ERR_FAIL_COND_V(p_delay < 0, false);
  1343. // Confirm both objects have the target methods
  1344. ERR_FAIL_COND_V_MSG(!p_object->has_method(p_method), false, "Object has no method named: " + p_method + ".");
  1345. ERR_FAIL_COND_V_MSG(!p_target->has_method(p_target_method), false, "Target has no method named: " + p_target_method + ".");
  1346. // Call the method to get the target value
  1347. Variant::CallError error;
  1348. Variant target_val = p_target->call(p_target_method, NULL, 0, error);
  1349. ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, false);
  1350. // Convert target INT values to REAL as they are better for interpolation
  1351. if (target_val.get_type() == Variant::INT) target_val = target_val.operator real_t();
  1352. ERR_FAIL_COND_V(target_val.get_type() != p_initial_val.get_type(), false);
  1353. // Make the new InterpolateData for the method follow
  1354. InterpolateData data;
  1355. data.active = true;
  1356. data.type = FOLLOW_METHOD;
  1357. data.finish = false;
  1358. data.elapsed = 0;
  1359. // Give the data it's configuration
  1360. data.id = p_object->get_instance_id();
  1361. data.key.push_back(p_method);
  1362. data.concatenated_key = p_method;
  1363. data.initial_val = p_initial_val;
  1364. data.target_id = p_target->get_instance_id();
  1365. data.target_key.push_back(p_target_method);
  1366. data.duration = p_duration;
  1367. data.trans_type = p_trans_type;
  1368. data.ease_type = p_ease_type;
  1369. data.delay = p_delay;
  1370. // Add the new interpolation
  1371. _push_interpolate_data(data);
  1372. return true;
  1373. }
  1374. bool Tween::targeting_property(Object *p_object, NodePath p_property, Object *p_initial, NodePath p_initial_property, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
  1375. // If we are currently updating, call this function again later
  1376. if (pending_update != 0) {
  1377. _add_pending_command("targeting_property", p_object, p_property, p_initial, p_initial_property, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
  1378. return true;
  1379. }
  1380. // Grab the target property and the target property
  1381. p_property = p_property.get_as_property_path();
  1382. p_initial_property = p_initial_property.get_as_property_path();
  1383. // Convert the initial INT values to REAL as they are better for Interpolation
  1384. if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();
  1385. // Verify both objects are valid
  1386. ERR_FAIL_COND_V(p_object == NULL, false);
  1387. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
  1388. ERR_FAIL_COND_V(p_initial == NULL, false);
  1389. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_initial), false);
  1390. // No negative durations
  1391. ERR_FAIL_COND_V(p_duration < 0, false);
  1392. // Ensure transition and easing types are valid
  1393. ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
  1394. ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);
  1395. // No negative delays
  1396. ERR_FAIL_COND_V(p_delay < 0, false);
  1397. // Ensure the initial and target properties exist on their objects
  1398. bool prop_valid = false;
  1399. p_object->get_indexed(p_property.get_subnames(), &prop_valid);
  1400. ERR_FAIL_COND_V(!prop_valid, false);
  1401. bool initial_prop_valid = false;
  1402. Variant initial_val = p_initial->get_indexed(p_initial_property.get_subnames(), &initial_prop_valid);
  1403. ERR_FAIL_COND_V(!initial_prop_valid, false);
  1404. // Convert the initial INT value to REAL as it is better for interpolation
  1405. if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t();
  1406. ERR_FAIL_COND_V(initial_val.get_type() != p_final_val.get_type(), false);
  1407. // Build the InterpolateData object
  1408. InterpolateData data;
  1409. data.active = true;
  1410. data.type = TARGETING_PROPERTY;
  1411. data.finish = false;
  1412. data.elapsed = 0;
  1413. // Give the data it's configuration
  1414. data.id = p_object->get_instance_id();
  1415. data.key = p_property.get_subnames();
  1416. data.concatenated_key = p_property.get_concatenated_subnames();
  1417. data.target_id = p_initial->get_instance_id();
  1418. data.target_key = p_initial_property.get_subnames();
  1419. data.initial_val = initial_val;
  1420. data.final_val = p_final_val;
  1421. data.duration = p_duration;
  1422. data.trans_type = p_trans_type;
  1423. data.ease_type = p_ease_type;
  1424. data.delay = p_delay;
  1425. // Ensure there is a valid delta
  1426. if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val))
  1427. return false;
  1428. // Add the interpolation
  1429. _push_interpolate_data(data);
  1430. return true;
  1431. }
  1432. bool Tween::targeting_method(Object *p_object, StringName p_method, Object *p_initial, StringName p_initial_method, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) {
  1433. // If we are currently updating, call this function again later
  1434. if (pending_update != 0) {
  1435. _add_pending_command("targeting_method", p_object, p_method, p_initial, p_initial_method, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay);
  1436. return true;
  1437. }
  1438. // Convert final INT values to REAL as they are better for interpolation
  1439. if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();
  1440. // Make sure the given objects are valid
  1441. ERR_FAIL_COND_V(p_object == NULL, false);
  1442. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false);
  1443. ERR_FAIL_COND_V(p_initial == NULL, false);
  1444. ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_initial), false);
  1445. // No negative durations
  1446. ERR_FAIL_COND_V(p_duration < 0, false);
  1447. // Ensure transition and easing types are valid
  1448. ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
  1449. ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);
  1450. // No negative delays
  1451. ERR_FAIL_COND_V(p_delay < 0, false);
  1452. // Make sure both objects have the given method
  1453. ERR_FAIL_COND_V_MSG(!p_object->has_method(p_method), false, "Object has no method named: " + p_method + ".");
  1454. ERR_FAIL_COND_V_MSG(!p_initial->has_method(p_initial_method), false, "Initial Object has no method named: " + p_initial_method + ".");
  1455. // Call the method to get the initial value
  1456. Variant::CallError error;
  1457. Variant initial_val = p_initial->call(p_initial_method, NULL, 0, error);
  1458. ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, false);
  1459. // Convert initial INT values to REAL as they aer better for interpolation
  1460. if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t();
  1461. ERR_FAIL_COND_V(initial_val.get_type() != p_final_val.get_type(), false);
  1462. // Build the new InterpolateData object
  1463. InterpolateData data;
  1464. data.active = true;
  1465. data.type = TARGETING_METHOD;
  1466. data.finish = false;
  1467. data.elapsed = 0;
  1468. // Configure the data
  1469. data.id = p_object->get_instance_id();
  1470. data.key.push_back(p_method);
  1471. data.concatenated_key = p_method;
  1472. data.target_id = p_initial->get_instance_id();
  1473. data.target_key.push_back(p_initial_method);
  1474. data.initial_val = initial_val;
  1475. data.final_val = p_final_val;
  1476. data.duration = p_duration;
  1477. data.trans_type = p_trans_type;
  1478. data.ease_type = p_ease_type;
  1479. data.delay = p_delay;
  1480. // Ensure there is a valid delta
  1481. if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val))
  1482. return false;
  1483. // Add the interpolation
  1484. _push_interpolate_data(data);
  1485. return true;
  1486. }
  1487. Tween::Tween() {
  1488. // Initialize tween attributes
  1489. tween_process_mode = TWEEN_PROCESS_IDLE;
  1490. repeat = false;
  1491. speed_scale = 1;
  1492. pending_update = 0;
  1493. uid = 0;
  1494. }
  1495. Tween::~Tween() {
  1496. }