tween.cpp 62 KB

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