tween.cpp 59 KB

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