scene_tree_tween.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /**************************************************************************/
  2. /* scene_tree_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 "scene_tree_tween.h"
  31. #include "core/method_bind_ext.gen.inc"
  32. #include "scene/animation/tween.h"
  33. #include "scene/main/node.h"
  34. #include "scene/scene_string_names.h"
  35. void Tweener::set_tween(Ref<SceneTreeTween> p_tween) {
  36. tween = p_tween;
  37. }
  38. void Tweener::clear_tween() {
  39. tween.unref();
  40. }
  41. void Tweener::_bind_methods() {
  42. ADD_SIGNAL(MethodInfo("finished"));
  43. }
  44. void SceneTreeTween::start_tweeners() {
  45. if (tweeners.empty()) {
  46. dead = true;
  47. ERR_FAIL_MSG("SceneTreeTween without commands, aborting");
  48. }
  49. List<Ref<Tweener>> &step = tweeners.write[current_step];
  50. for (int i = 0; i < step.size(); i++) {
  51. Ref<Tweener> &tweener = step[i];
  52. tweener->start();
  53. }
  54. }
  55. Ref<PropertyTweener> SceneTreeTween::tween_property(Object *p_target, NodePath p_property, Variant p_to, float p_duration) {
  56. ERR_FAIL_NULL_V(p_target, nullptr);
  57. ERR_FAIL_COND_V_MSG(!valid, nullptr, "SceneTreeTween invalid. Either finished or created outside scene tree.");
  58. ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a SceneTreeTween that has started. Use stop() first.");
  59. Variant::Type property_type = p_target->get_indexed(p_property.get_as_property_path().get_subnames()).get_type();
  60. if (property_type != p_to.get_type()) {
  61. // Cast p_to between floats and ints to avoid minor annoyances.
  62. if (property_type == Variant::REAL && p_to.get_type() == Variant::INT) {
  63. p_to = float(p_to);
  64. } else if (property_type == Variant::INT && p_to.get_type() == Variant::REAL) {
  65. p_to = int(p_to);
  66. } else {
  67. ERR_FAIL_V_MSG(Ref<PropertyTweener>(), "Type mismatch between property and final value: " + Variant::get_type_name(property_type) + " and " + Variant::get_type_name(p_to.get_type()));
  68. }
  69. }
  70. Ref<PropertyTweener> tweener = memnew(PropertyTweener(p_target, p_property, p_to, p_duration));
  71. append(tweener);
  72. return tweener;
  73. }
  74. Ref<IntervalTweener> SceneTreeTween::tween_interval(float p_time) {
  75. ERR_FAIL_COND_V_MSG(!valid, nullptr, "SceneTreeTween invalid. Either finished or created outside scene tree.");
  76. ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a SceneTreeTween that has started. Use stop() first.");
  77. Ref<IntervalTweener> tweener = memnew(IntervalTweener(p_time));
  78. append(tweener);
  79. return tweener;
  80. }
  81. Ref<CallbackTweener> SceneTreeTween::tween_callback(Object *p_target, StringName p_method, const Vector<Variant> &p_binds) {
  82. ERR_FAIL_NULL_V(p_target, nullptr);
  83. ERR_FAIL_COND_V_MSG(!valid, nullptr, "SceneTreeTween invalid. Either finished or created outside scene tree.");
  84. ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a SceneTreeTween that has started. Use stop() first.");
  85. Ref<CallbackTweener> tweener = memnew(CallbackTweener(p_target, p_method, p_binds));
  86. append(tweener);
  87. return tweener;
  88. }
  89. Ref<MethodTweener> SceneTreeTween::tween_method(Object *p_target, StringName p_method, Variant p_from, Variant p_to, float p_duration, const Vector<Variant> &p_binds) {
  90. ERR_FAIL_NULL_V(p_target, nullptr);
  91. ERR_FAIL_COND_V_MSG(!valid, nullptr, "SceneTreeTween invalid. Either finished or created outside scene tree.");
  92. ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a SceneTreeTween that has started. Use stop() first.");
  93. Ref<MethodTweener> tweener = memnew(MethodTweener(p_target, p_method, p_from, p_to, p_duration, p_binds));
  94. append(tweener);
  95. return tweener;
  96. }
  97. void SceneTreeTween::append(Ref<Tweener> p_tweener) {
  98. p_tweener->set_tween(this);
  99. if (parallel_enabled) {
  100. current_step = MAX(current_step, 0);
  101. } else {
  102. current_step++;
  103. }
  104. parallel_enabled = default_parallel;
  105. tweeners.resize(current_step + 1);
  106. tweeners.write[current_step].push_back(p_tweener);
  107. }
  108. void SceneTreeTween::stop() {
  109. started = false;
  110. running = false;
  111. dead = false;
  112. total_time = 0;
  113. }
  114. void SceneTreeTween::pause() {
  115. running = false;
  116. }
  117. void SceneTreeTween::play() {
  118. ERR_FAIL_COND_MSG(!valid, "SceneTreeTween invalid. Either finished or created outside scene tree.");
  119. ERR_FAIL_COND_MSG(dead, "Can't play finished SceneTreeTween, use stop() first to reset its state.");
  120. running = true;
  121. }
  122. void SceneTreeTween::kill() {
  123. running = false; // For the sake of is_running().
  124. dead = true;
  125. }
  126. bool SceneTreeTween::is_running() const {
  127. return running;
  128. }
  129. bool SceneTreeTween::is_valid() const {
  130. return valid;
  131. }
  132. void SceneTreeTween::clear() {
  133. valid = false;
  134. for (int i = 0; i < tweeners.size(); i++) {
  135. List<Ref<Tweener>> &step = tweeners.write[i];
  136. for (int j = 0; j < step.size(); j++) {
  137. Ref<Tweener> &tweener = step[j];
  138. tweener->clear_tween();
  139. }
  140. }
  141. tweeners.clear();
  142. }
  143. Ref<SceneTreeTween> SceneTreeTween::bind_node(Node *p_node) {
  144. ERR_FAIL_NULL_V(p_node, this);
  145. bound_node = p_node->get_instance_id();
  146. is_bound = true;
  147. return this;
  148. }
  149. Ref<SceneTreeTween> SceneTreeTween::set_process_mode(Tween::TweenProcessMode p_mode) {
  150. process_mode = p_mode;
  151. return this;
  152. }
  153. Ref<SceneTreeTween> SceneTreeTween::set_pause_mode(TweenPauseMode p_mode) {
  154. pause_mode = p_mode;
  155. return this;
  156. }
  157. Ref<SceneTreeTween> SceneTreeTween::set_parallel(bool p_parallel) {
  158. default_parallel = p_parallel;
  159. parallel_enabled = p_parallel;
  160. return this;
  161. }
  162. Ref<SceneTreeTween> SceneTreeTween::set_loops(int p_loops) {
  163. loops = p_loops;
  164. return this;
  165. }
  166. Ref<SceneTreeTween> SceneTreeTween::set_speed_scale(float p_speed) {
  167. speed_scale = p_speed;
  168. return this;
  169. }
  170. Ref<SceneTreeTween> SceneTreeTween::set_trans(Tween::TransitionType p_trans) {
  171. default_transition = p_trans;
  172. return this;
  173. }
  174. Ref<SceneTreeTween> SceneTreeTween::set_ease(Tween::EaseType p_ease) {
  175. default_ease = p_ease;
  176. return this;
  177. }
  178. Tween::TweenProcessMode SceneTreeTween::get_process_mode() const {
  179. return process_mode;
  180. }
  181. SceneTreeTween::TweenPauseMode SceneTreeTween::get_pause_mode() const {
  182. return pause_mode;
  183. }
  184. Tween::TransitionType SceneTreeTween::get_trans() const {
  185. return default_transition;
  186. }
  187. Tween::EaseType SceneTreeTween::get_ease() const {
  188. return default_ease;
  189. }
  190. Ref<SceneTreeTween> SceneTreeTween::parallel() {
  191. parallel_enabled = true;
  192. return this;
  193. }
  194. Ref<SceneTreeTween> SceneTreeTween::chain() {
  195. parallel_enabled = false;
  196. return this;
  197. }
  198. bool SceneTreeTween::custom_step(float p_delta) {
  199. bool r = running;
  200. running = true;
  201. bool ret = step(p_delta);
  202. running = running && r; // Running might turn false when SceneTreeTween finished;
  203. return ret;
  204. }
  205. bool SceneTreeTween::step(float p_delta) {
  206. if (dead) {
  207. return false;
  208. }
  209. if (!running) {
  210. return true;
  211. }
  212. if (is_bound) {
  213. Node *bound_node = get_bound_node();
  214. if (bound_node) {
  215. if (!bound_node->is_inside_tree()) {
  216. return true;
  217. }
  218. } else {
  219. return false;
  220. }
  221. }
  222. if (!started) {
  223. ERR_FAIL_COND_V_MSG(tweeners.empty(), false, "SceneTreeTween started, but has no Tweeners.");
  224. current_step = 0;
  225. loops_done = 0;
  226. total_time = 0;
  227. start_tweeners();
  228. started = true;
  229. }
  230. float rem_delta = p_delta * speed_scale;
  231. bool step_active = false;
  232. total_time += rem_delta;
  233. #ifdef DEBUG_ENABLED
  234. float initial_delta = rem_delta;
  235. bool potential_infinite = false;
  236. #endif
  237. while (rem_delta > 0 && running) {
  238. float step_delta = rem_delta;
  239. step_active = false;
  240. List<Ref<Tweener>> &step = tweeners.write[current_step];
  241. for (int i = 0; i < step.size(); i++) {
  242. Ref<Tweener> &tweener = step[i];
  243. // Modified inside Tweener.step().
  244. float temp_delta = rem_delta;
  245. // Turns to true if any Tweener returns true (i.e. is still not finished).
  246. step_active = tweener->step(temp_delta) || step_active;
  247. step_delta = MIN(temp_delta, step_delta);
  248. }
  249. rem_delta = step_delta;
  250. if (!step_active) {
  251. emit_signal(SceneStringNames::get_singleton()->step_finished, current_step);
  252. current_step++;
  253. if (current_step == tweeners.size()) {
  254. loops_done++;
  255. if (loops_done == loops) {
  256. running = false;
  257. dead = true;
  258. emit_signal(SceneStringNames::get_singleton()->finished);
  259. break;
  260. } else {
  261. emit_signal(SceneStringNames::get_singleton()->loop_finished, loops_done);
  262. current_step = 0;
  263. start_tweeners();
  264. #ifdef DEBUG_ENABLED
  265. if (loops <= 0 && Math::is_equal_approx(rem_delta, initial_delta)) {
  266. if (!potential_infinite) {
  267. potential_infinite = true;
  268. } else {
  269. // Looped twice without using any time, this is 100% certain infinite loop.
  270. ERR_FAIL_V_MSG(false, "Infinite loop detected. Check set_loops() description for more info.");
  271. }
  272. }
  273. #endif
  274. }
  275. } else {
  276. start_tweeners();
  277. }
  278. }
  279. }
  280. return true;
  281. }
  282. bool SceneTreeTween::can_process(bool p_tree_paused) const {
  283. if (is_bound && pause_mode == TWEEN_PAUSE_BOUND) {
  284. Node *bound_node = get_bound_node();
  285. if (bound_node) {
  286. return bound_node->is_inside_tree() && bound_node->can_process();
  287. }
  288. }
  289. return !p_tree_paused || pause_mode == TWEEN_PAUSE_PROCESS;
  290. }
  291. Node *SceneTreeTween::get_bound_node() const {
  292. if (is_bound) {
  293. return Object::cast_to<Node>(ObjectDB::get_instance(bound_node));
  294. } else {
  295. return nullptr;
  296. }
  297. }
  298. float SceneTreeTween::get_total_time() const {
  299. return total_time;
  300. }
  301. Variant SceneTreeTween::interpolate_variant(Variant p_initial_val, Variant p_delta_val, float p_time, float p_duration, Tween::TransitionType p_trans, Tween::EaseType p_ease) const {
  302. ERR_FAIL_INDEX_V(p_trans, Tween::TRANS_COUNT, Variant());
  303. ERR_FAIL_INDEX_V(p_ease, Tween::EASE_COUNT, Variant());
  304. // Helper macro to run equation on sub-elements of the value (e.g. x and y of Vector2).
  305. #define APPLY_EQUATION(element) \
  306. r.element = Tween::run_equation(p_trans, p_ease, p_time, i.element, d.element, p_duration);
  307. switch (p_initial_val.get_type()) {
  308. case Variant::BOOL: {
  309. return (Tween::run_equation(p_trans, p_ease, p_time, p_initial_val, p_delta_val, p_duration)) >= 0.5;
  310. }
  311. case Variant::INT: {
  312. return (int)Tween::run_equation(p_trans, p_ease, p_time, (int)p_initial_val, (int)p_delta_val, p_duration);
  313. }
  314. case Variant::REAL: {
  315. return Tween::run_equation(p_trans, p_ease, p_time, (real_t)p_initial_val, (real_t)p_delta_val, p_duration);
  316. }
  317. case Variant::VECTOR2: {
  318. Vector2 i = p_initial_val;
  319. Vector2 d = p_delta_val;
  320. Vector2 r;
  321. APPLY_EQUATION(x);
  322. APPLY_EQUATION(y);
  323. return r;
  324. }
  325. case Variant::RECT2: {
  326. Rect2 i = p_initial_val;
  327. Rect2 d = p_delta_val;
  328. Rect2 r;
  329. APPLY_EQUATION(position.x);
  330. APPLY_EQUATION(position.y);
  331. APPLY_EQUATION(size.x);
  332. APPLY_EQUATION(size.y);
  333. return r;
  334. }
  335. case Variant::VECTOR3: {
  336. Vector3 i = p_initial_val;
  337. Vector3 d = p_delta_val;
  338. Vector3 r;
  339. APPLY_EQUATION(x);
  340. APPLY_EQUATION(y);
  341. APPLY_EQUATION(z);
  342. return r;
  343. }
  344. case Variant::TRANSFORM2D: {
  345. Transform2D i = p_initial_val;
  346. Transform2D d = p_delta_val;
  347. Transform2D r;
  348. APPLY_EQUATION(elements[0][0]);
  349. APPLY_EQUATION(elements[0][1]);
  350. APPLY_EQUATION(elements[1][0]);
  351. APPLY_EQUATION(elements[1][1]);
  352. APPLY_EQUATION(elements[2][0]);
  353. APPLY_EQUATION(elements[2][1]);
  354. return r;
  355. }
  356. case Variant::QUAT: {
  357. Quat i = p_initial_val;
  358. Quat d = p_delta_val;
  359. Quat r;
  360. APPLY_EQUATION(x);
  361. APPLY_EQUATION(y);
  362. APPLY_EQUATION(z);
  363. APPLY_EQUATION(w);
  364. return r;
  365. }
  366. case Variant::AABB: {
  367. AABB i = p_initial_val;
  368. AABB d = p_delta_val;
  369. AABB r;
  370. APPLY_EQUATION(position.x);
  371. APPLY_EQUATION(position.y);
  372. APPLY_EQUATION(position.z);
  373. APPLY_EQUATION(size.x);
  374. APPLY_EQUATION(size.y);
  375. APPLY_EQUATION(size.z);
  376. return r;
  377. }
  378. case Variant::BASIS: {
  379. Basis i = p_initial_val;
  380. Basis d = p_delta_val;
  381. Basis r;
  382. APPLY_EQUATION(elements[0][0]);
  383. APPLY_EQUATION(elements[0][1]);
  384. APPLY_EQUATION(elements[0][2]);
  385. APPLY_EQUATION(elements[1][0]);
  386. APPLY_EQUATION(elements[1][1]);
  387. APPLY_EQUATION(elements[1][2]);
  388. APPLY_EQUATION(elements[2][0]);
  389. APPLY_EQUATION(elements[2][1]);
  390. APPLY_EQUATION(elements[2][2]);
  391. return r;
  392. }
  393. case Variant::TRANSFORM: {
  394. Transform i = p_initial_val;
  395. Transform d = p_delta_val;
  396. Transform r;
  397. APPLY_EQUATION(basis.elements[0][0]);
  398. APPLY_EQUATION(basis.elements[0][1]);
  399. APPLY_EQUATION(basis.elements[0][2]);
  400. APPLY_EQUATION(basis.elements[1][0]);
  401. APPLY_EQUATION(basis.elements[1][1]);
  402. APPLY_EQUATION(basis.elements[1][2]);
  403. APPLY_EQUATION(basis.elements[2][0]);
  404. APPLY_EQUATION(basis.elements[2][1]);
  405. APPLY_EQUATION(basis.elements[2][2]);
  406. APPLY_EQUATION(origin.x);
  407. APPLY_EQUATION(origin.y);
  408. APPLY_EQUATION(origin.z);
  409. return r;
  410. }
  411. case Variant::COLOR: {
  412. Color i = p_initial_val;
  413. Color d = p_delta_val;
  414. Color r;
  415. APPLY_EQUATION(r);
  416. APPLY_EQUATION(g);
  417. APPLY_EQUATION(b);
  418. APPLY_EQUATION(a);
  419. return r;
  420. }
  421. default: {
  422. return p_initial_val;
  423. }
  424. };
  425. #undef APPLY_EQUATION
  426. }
  427. Variant SceneTreeTween::calculate_delta_value(Variant p_intial_val, Variant p_final_val) {
  428. ERR_FAIL_COND_V_MSG(p_intial_val.get_type() != p_final_val.get_type(), p_intial_val, "Type mismatch between initial and final value: " + Variant::get_type_name(p_intial_val.get_type()) + " and " + Variant::get_type_name(p_final_val.get_type()));
  429. switch (p_intial_val.get_type()) {
  430. case Variant::BOOL: {
  431. return (int)p_final_val - (int)p_intial_val;
  432. }
  433. case Variant::RECT2: {
  434. Rect2 i = p_intial_val;
  435. Rect2 f = p_final_val;
  436. return Rect2(f.position - i.position, f.size - i.size);
  437. }
  438. case Variant::TRANSFORM2D: {
  439. Transform2D i = p_intial_val;
  440. Transform2D f = p_final_val;
  441. return Transform2D(f.elements[0][0] - i.elements[0][0],
  442. f.elements[0][1] - i.elements[0][1],
  443. f.elements[1][0] - i.elements[1][0],
  444. f.elements[1][1] - i.elements[1][1],
  445. f.elements[2][0] - i.elements[2][0],
  446. f.elements[2][1] - i.elements[2][1]);
  447. }
  448. case Variant::AABB: {
  449. AABB i = p_intial_val;
  450. AABB f = p_final_val;
  451. return AABB(f.position - i.position, f.size - i.size);
  452. }
  453. case Variant::BASIS: {
  454. Basis i = p_intial_val;
  455. Basis f = p_final_val;
  456. return Basis(f.elements[0][0] - i.elements[0][0],
  457. f.elements[0][1] - i.elements[0][1],
  458. f.elements[0][2] - i.elements[0][2],
  459. f.elements[1][0] - i.elements[1][0],
  460. f.elements[1][1] - i.elements[1][1],
  461. f.elements[1][2] - i.elements[1][2],
  462. f.elements[2][0] - i.elements[2][0],
  463. f.elements[2][1] - i.elements[2][1],
  464. f.elements[2][2] - i.elements[2][2]);
  465. }
  466. case Variant::TRANSFORM: {
  467. Transform i = p_intial_val;
  468. Transform f = p_final_val;
  469. return Transform(f.basis.elements[0][0] - i.basis.elements[0][0],
  470. f.basis.elements[0][1] - i.basis.elements[0][1],
  471. f.basis.elements[0][2] - i.basis.elements[0][2],
  472. f.basis.elements[1][0] - i.basis.elements[1][0],
  473. f.basis.elements[1][1] - i.basis.elements[1][1],
  474. f.basis.elements[1][2] - i.basis.elements[1][2],
  475. f.basis.elements[2][0] - i.basis.elements[2][0],
  476. f.basis.elements[2][1] - i.basis.elements[2][1],
  477. f.basis.elements[2][2] - i.basis.elements[2][2],
  478. f.origin.x - i.origin.x,
  479. f.origin.y - i.origin.y,
  480. f.origin.z - i.origin.z);
  481. }
  482. default: {
  483. return Variant::evaluate(Variant::OP_SUBTRACT, p_final_val, p_intial_val);
  484. }
  485. };
  486. }
  487. void SceneTreeTween::_bind_methods() {
  488. ClassDB::bind_method(D_METHOD("tween_property", "object", "property", "final_val", "duration"), &SceneTreeTween::tween_property);
  489. ClassDB::bind_method(D_METHOD("tween_interval", "time"), &SceneTreeTween::tween_interval);
  490. ClassDB::bind_method(D_METHOD("tween_callback", "object", "method", "binds"), &SceneTreeTween::tween_callback, DEFVAL(Array()));
  491. ClassDB::bind_method(D_METHOD("tween_method", "object", "method", "from", "to", "duration", "binds"), &SceneTreeTween::tween_method, DEFVAL(Array()));
  492. ClassDB::bind_method(D_METHOD("custom_step", "delta"), &SceneTreeTween::custom_step);
  493. ClassDB::bind_method(D_METHOD("stop"), &SceneTreeTween::stop);
  494. ClassDB::bind_method(D_METHOD("pause"), &SceneTreeTween::pause);
  495. ClassDB::bind_method(D_METHOD("play"), &SceneTreeTween::play);
  496. ClassDB::bind_method(D_METHOD("kill"), &SceneTreeTween::kill);
  497. ClassDB::bind_method(D_METHOD("get_total_elapsed_time"), &SceneTreeTween::get_total_time);
  498. ClassDB::bind_method(D_METHOD("is_running"), &SceneTreeTween::is_running);
  499. ClassDB::bind_method(D_METHOD("is_valid"), &SceneTreeTween::is_valid);
  500. ClassDB::bind_method(D_METHOD("bind_node", "node"), &SceneTreeTween::bind_node);
  501. ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &SceneTreeTween::set_process_mode);
  502. ClassDB::bind_method(D_METHOD("set_pause_mode", "mode"), &SceneTreeTween::set_pause_mode);
  503. ClassDB::bind_method(D_METHOD("set_parallel", "parallel"), &SceneTreeTween::set_parallel, DEFVAL(true));
  504. ClassDB::bind_method(D_METHOD("set_loops", "loops"), &SceneTreeTween::set_loops, DEFVAL(0));
  505. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &SceneTreeTween::set_speed_scale);
  506. ClassDB::bind_method(D_METHOD("set_trans", "trans"), &SceneTreeTween::set_trans);
  507. ClassDB::bind_method(D_METHOD("set_ease", "ease"), &SceneTreeTween::set_ease);
  508. ClassDB::bind_method(D_METHOD("parallel"), &SceneTreeTween::parallel);
  509. ClassDB::bind_method(D_METHOD("chain"), &SceneTreeTween::chain);
  510. ClassDB::bind_method(D_METHOD("interpolate_value", "initial_value", "delta_value", "elapsed_time", "duration", "trans_type", "ease_type"), &SceneTreeTween::interpolate_variant);
  511. ADD_SIGNAL(MethodInfo("step_finished", PropertyInfo(Variant::INT, "idx")));
  512. ADD_SIGNAL(MethodInfo("loop_finished", PropertyInfo(Variant::INT, "loop_count")));
  513. ADD_SIGNAL(MethodInfo("finished"));
  514. BIND_ENUM_CONSTANT(TWEEN_PAUSE_BOUND);
  515. BIND_ENUM_CONSTANT(TWEEN_PAUSE_STOP);
  516. BIND_ENUM_CONSTANT(TWEEN_PAUSE_PROCESS);
  517. }
  518. SceneTreeTween::SceneTreeTween(bool p_valid) {
  519. valid = p_valid;
  520. }
  521. Ref<PropertyTweener> PropertyTweener::from(Variant p_value) {
  522. initial_val = p_value;
  523. do_continue = false;
  524. return this;
  525. }
  526. Ref<PropertyTweener> PropertyTweener::from_current() {
  527. do_continue = false;
  528. return this;
  529. }
  530. Ref<PropertyTweener> PropertyTweener::as_relative() {
  531. relative = true;
  532. return this;
  533. }
  534. Ref<PropertyTweener> PropertyTweener::set_trans(Tween::TransitionType p_trans) {
  535. trans_type = p_trans;
  536. return this;
  537. }
  538. Ref<PropertyTweener> PropertyTweener::set_ease(Tween::EaseType p_ease) {
  539. ease_type = p_ease;
  540. return this;
  541. }
  542. Ref<PropertyTweener> PropertyTweener::set_delay(float p_delay) {
  543. delay = p_delay;
  544. return this;
  545. }
  546. void PropertyTweener::start() {
  547. elapsed_time = 0;
  548. finished = false;
  549. Object *target_instance = ObjectDB::get_instance(target);
  550. if (!target_instance) {
  551. WARN_PRINT("Target object freed before starting, aborting Tweener.");
  552. return;
  553. }
  554. if (do_continue) {
  555. initial_val = target_instance->get_indexed(property);
  556. }
  557. if (relative) {
  558. final_val = Variant::evaluate(Variant::Operator::OP_ADD, initial_val, base_final_val);
  559. }
  560. delta_val = tween->calculate_delta_value(initial_val, final_val);
  561. }
  562. bool PropertyTweener::step(float &r_delta) {
  563. if (finished) {
  564. // This is needed in case there's a parallel Tweener with longer duration.
  565. return false;
  566. }
  567. Object *target_instance = ObjectDB::get_instance(target);
  568. if (!target_instance) {
  569. return false;
  570. }
  571. elapsed_time += r_delta;
  572. if (elapsed_time < delay) {
  573. r_delta = 0;
  574. return true;
  575. }
  576. float time = MIN(elapsed_time - delay, duration);
  577. if (time < duration) {
  578. target_instance->set_indexed(property, tween->interpolate_variant(initial_val, delta_val, time, duration, trans_type, ease_type));
  579. r_delta = 0;
  580. return true;
  581. } else {
  582. target_instance->set_indexed(property, final_val);
  583. finished = true;
  584. r_delta = elapsed_time - delay - duration;
  585. emit_signal(SceneStringNames::get_singleton()->finished);
  586. return false;
  587. }
  588. }
  589. void PropertyTweener::set_tween(Ref<SceneTreeTween> p_tween) {
  590. tween = p_tween;
  591. if (trans_type == Tween::TRANS_COUNT) {
  592. trans_type = tween->get_trans();
  593. }
  594. if (ease_type == Tween::EASE_COUNT) {
  595. ease_type = tween->get_ease();
  596. }
  597. }
  598. void PropertyTweener::_bind_methods() {
  599. ClassDB::bind_method(D_METHOD("from", "value"), &PropertyTweener::from);
  600. ClassDB::bind_method(D_METHOD("from_current"), &PropertyTweener::from_current);
  601. ClassDB::bind_method(D_METHOD("as_relative"), &PropertyTweener::as_relative);
  602. ClassDB::bind_method(D_METHOD("set_trans", "trans"), &PropertyTweener::set_trans);
  603. ClassDB::bind_method(D_METHOD("set_ease", "ease"), &PropertyTweener::set_ease);
  604. ClassDB::bind_method(D_METHOD("set_delay", "delay"), &PropertyTweener::set_delay);
  605. }
  606. PropertyTweener::PropertyTweener(Object *p_target, NodePath p_property, Variant p_to, float p_duration) {
  607. target = p_target->get_instance_id();
  608. property = p_property.get_as_property_path().get_subnames();
  609. initial_val = p_target->get_indexed(property);
  610. base_final_val = p_to;
  611. final_val = base_final_val;
  612. duration = p_duration;
  613. }
  614. PropertyTweener::PropertyTweener() {
  615. ERR_FAIL_MSG("Can't create empty PropertyTweener. Use get_tree().tween_property() or tween_property() instead.");
  616. }
  617. void IntervalTweener::start() {
  618. elapsed_time = 0;
  619. finished = false;
  620. }
  621. bool IntervalTweener::step(float &r_delta) {
  622. if (finished) {
  623. return false;
  624. }
  625. elapsed_time += r_delta;
  626. if (elapsed_time < duration) {
  627. r_delta = 0;
  628. return true;
  629. } else {
  630. finished = true;
  631. r_delta = elapsed_time - duration;
  632. emit_signal(SceneStringNames::get_singleton()->finished);
  633. return false;
  634. }
  635. }
  636. IntervalTweener::IntervalTweener(float p_time) {
  637. duration = p_time;
  638. }
  639. IntervalTweener::IntervalTweener() {
  640. ERR_FAIL_MSG("Can't create empty IntervalTweener. Use get_tree().tween_property() or tween_property() instead.");
  641. }
  642. Ref<CallbackTweener> CallbackTweener::set_delay(float p_delay) {
  643. delay = p_delay;
  644. return this;
  645. }
  646. void CallbackTweener::start() {
  647. elapsed_time = 0;
  648. finished = false;
  649. }
  650. bool CallbackTweener::step(float &r_delta) {
  651. if (finished) {
  652. return false;
  653. }
  654. Object *target_instance = ObjectDB::get_instance(target);
  655. if (!target_instance) {
  656. return false;
  657. }
  658. elapsed_time += r_delta;
  659. if (elapsed_time >= delay) {
  660. Vector<const Variant *> bind_mem;
  661. if (binds.size()) {
  662. bind_mem.resize(binds.size());
  663. for (int i = 0; i < binds.size(); i++) {
  664. bind_mem.write[i] = &binds[i];
  665. }
  666. }
  667. const Variant **args = (const Variant **)bind_mem.ptr();
  668. int argc = bind_mem.size();
  669. Variant::CallError ce;
  670. target_instance->call(method, args, argc, ce);
  671. if (ce.error != Variant::CallError::CALL_OK) {
  672. ERR_FAIL_V_MSG(false, "Error calling method from CallbackTweener: " + Variant::get_call_error_text(target_instance, method, args, argc, ce));
  673. }
  674. finished = true;
  675. r_delta = elapsed_time - delay;
  676. emit_signal(SceneStringNames::get_singleton()->finished);
  677. return false;
  678. }
  679. r_delta = 0;
  680. return true;
  681. }
  682. void CallbackTweener::_bind_methods() {
  683. ClassDB::bind_method(D_METHOD("set_delay", "delay"), &CallbackTweener::set_delay);
  684. }
  685. CallbackTweener::CallbackTweener(Object *p_target, StringName p_method, const Vector<Variant> &p_binds) {
  686. target = p_target->get_instance_id();
  687. method = p_method;
  688. binds = p_binds;
  689. }
  690. CallbackTweener::CallbackTweener() {
  691. ERR_FAIL_MSG("Can't create empty CallbackTweener. Use get_tree().tween_callback() instead.");
  692. }
  693. Ref<MethodTweener> MethodTweener::set_delay(float p_delay) {
  694. delay = p_delay;
  695. return this;
  696. }
  697. Ref<MethodTweener> MethodTweener::set_trans(Tween::TransitionType p_trans) {
  698. trans_type = p_trans;
  699. return this;
  700. }
  701. Ref<MethodTweener> MethodTweener::set_ease(Tween::EaseType p_ease) {
  702. ease_type = p_ease;
  703. return this;
  704. }
  705. void MethodTweener::start() {
  706. elapsed_time = 0;
  707. finished = false;
  708. }
  709. bool MethodTweener::step(float &r_delta) {
  710. if (finished) {
  711. return false;
  712. }
  713. Object *target_instance = ObjectDB::get_instance(target);
  714. if (!target_instance) {
  715. return false;
  716. }
  717. elapsed_time += r_delta;
  718. if (elapsed_time < delay) {
  719. r_delta = 0;
  720. return true;
  721. }
  722. Variant current_val;
  723. float time = MIN(elapsed_time - delay, duration);
  724. if (time < duration) {
  725. current_val = tween->interpolate_variant(initial_val, delta_val, time, duration, trans_type, ease_type);
  726. } else {
  727. current_val = final_val;
  728. }
  729. Vector<const Variant *> bind_mem;
  730. if (binds.empty()) {
  731. bind_mem.push_back(&current_val);
  732. } else {
  733. bind_mem.resize(1 + binds.size());
  734. bind_mem.write[0] = &current_val;
  735. for (int i = 0; i < binds.size(); i++) {
  736. bind_mem.write[1 + i] = &binds[i];
  737. }
  738. }
  739. const Variant **args = (const Variant **)bind_mem.ptr();
  740. int argc = bind_mem.size();
  741. Variant::CallError ce;
  742. target_instance->call(method, args, argc, ce);
  743. if (ce.error != Variant::CallError::CALL_OK) {
  744. ERR_FAIL_V_MSG(false, "Error calling method from MethodTweener: " + Variant::get_call_error_text(target_instance, method, args, argc, ce));
  745. }
  746. if (time < duration) {
  747. r_delta = 0;
  748. return true;
  749. } else {
  750. finished = true;
  751. r_delta = elapsed_time - delay - duration;
  752. emit_signal(SceneStringNames::get_singleton()->finished);
  753. return false;
  754. }
  755. }
  756. void MethodTweener::set_tween(Ref<SceneTreeTween> p_tween) {
  757. tween = p_tween;
  758. if (trans_type == Tween::TRANS_COUNT) {
  759. trans_type = tween->get_trans();
  760. }
  761. if (ease_type == Tween::EASE_COUNT) {
  762. ease_type = tween->get_ease();
  763. }
  764. }
  765. void MethodTweener::_bind_methods() {
  766. ClassDB::bind_method(D_METHOD("set_delay", "delay"), &MethodTweener::set_delay);
  767. ClassDB::bind_method(D_METHOD("set_trans", "trans"), &MethodTweener::set_trans);
  768. ClassDB::bind_method(D_METHOD("set_ease", "ease"), &MethodTweener::set_ease);
  769. }
  770. MethodTweener::MethodTweener(Object *p_target, StringName p_method, Variant p_from, Variant p_to, float p_duration, const Vector<Variant> &p_binds) {
  771. target = p_target->get_instance_id();
  772. method = p_method;
  773. binds = p_binds;
  774. initial_val = p_from;
  775. delta_val = tween->calculate_delta_value(p_from, p_to);
  776. final_val = p_to;
  777. duration = p_duration;
  778. }
  779. MethodTweener::MethodTweener() {
  780. ERR_FAIL_MSG("Can't create empty MethodTweener. Use get_tree().tween_method() instead.");
  781. }