animated_sprite.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /**************************************************************************/
  2. /* animated_sprite.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 "animated_sprite.h"
  31. #include "core/os/os.h"
  32. #include "scene/scene_string_names.h"
  33. #ifdef TOOLS_ENABLED
  34. #include "editor/editor_settings.h"
  35. #endif
  36. #define NORMAL_SUFFIX "_normal"
  37. #ifdef TOOLS_ENABLED
  38. Dictionary AnimatedSprite::_edit_get_state() const {
  39. Dictionary state = Node2D::_edit_get_state();
  40. state["offset"] = offset;
  41. return state;
  42. }
  43. void AnimatedSprite::_edit_set_state(const Dictionary &p_state) {
  44. Node2D::_edit_set_state(p_state);
  45. set_offset(p_state["offset"]);
  46. }
  47. void AnimatedSprite::_edit_set_pivot(const Point2 &p_pivot) {
  48. set_offset(get_offset() - p_pivot);
  49. set_position(get_transform().xform(p_pivot));
  50. }
  51. Point2 AnimatedSprite::_edit_get_pivot() const {
  52. return Vector2();
  53. }
  54. bool AnimatedSprite::_edit_use_pivot() const {
  55. return true;
  56. }
  57. Rect2 AnimatedSprite::_edit_get_rect() const {
  58. return _get_rect();
  59. }
  60. bool AnimatedSprite::_edit_use_rect() const {
  61. if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
  62. return false;
  63. }
  64. Ref<Texture> t;
  65. if (animation) {
  66. t = frames->get_frame(animation, frame);
  67. }
  68. return t.is_valid();
  69. }
  70. #endif
  71. Rect2 AnimatedSprite::get_anchorable_rect() const {
  72. return _get_rect();
  73. }
  74. Rect2 AnimatedSprite::_get_rect() const {
  75. if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
  76. return Rect2();
  77. }
  78. Ref<Texture> t;
  79. if (animation) {
  80. t = frames->get_frame(animation, frame);
  81. }
  82. if (t.is_null()) {
  83. return Rect2();
  84. }
  85. Size2 s = t->get_size();
  86. Point2 ofs = offset;
  87. if (centered) {
  88. ofs -= s / 2;
  89. }
  90. if (s == Size2(0, 0)) {
  91. s = Size2(1, 1);
  92. }
  93. return Rect2(ofs, s);
  94. }
  95. void SpriteFrames::add_frame(const StringName &p_anim, const Ref<Texture> &p_frame, int p_at_pos) {
  96. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  97. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  98. if (p_at_pos >= 0 && p_at_pos < E->get().frames.size()) {
  99. E->get().frames.insert(p_at_pos, p_frame);
  100. } else {
  101. E->get().frames.push_back(p_frame);
  102. }
  103. emit_changed();
  104. }
  105. int SpriteFrames::get_frame_count(const StringName &p_anim) const {
  106. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  107. ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
  108. return E->get().frames.size();
  109. }
  110. void SpriteFrames::remove_frame(const StringName &p_anim, int p_idx) {
  111. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  112. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  113. E->get().frames.remove(p_idx);
  114. emit_changed();
  115. }
  116. void SpriteFrames::clear(const StringName &p_anim) {
  117. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  118. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  119. E->get().frames.clear();
  120. emit_changed();
  121. }
  122. void SpriteFrames::clear_all() {
  123. animations.clear();
  124. add_animation("default"); // Also emits changed.
  125. }
  126. void SpriteFrames::add_animation(const StringName &p_anim) {
  127. ERR_FAIL_COND_MSG(animations.has(p_anim), "SpriteFrames already has animation '" + p_anim + "'.");
  128. animations[p_anim] = Anim();
  129. animations[p_anim].normal_name = String(p_anim) + NORMAL_SUFFIX;
  130. emit_changed();
  131. }
  132. bool SpriteFrames::has_animation(const StringName &p_anim) const {
  133. return animations.has(p_anim);
  134. }
  135. void SpriteFrames::remove_animation(const StringName &p_anim) {
  136. if (animations.erase(p_anim)) {
  137. emit_changed();
  138. }
  139. }
  140. void SpriteFrames::rename_animation(const StringName &p_prev, const StringName &p_next) {
  141. ERR_FAIL_COND_MSG(!animations.has(p_prev), "SpriteFrames doesn't have animation '" + String(p_prev) + "'.");
  142. ERR_FAIL_COND_MSG(animations.has(p_next), "Animation '" + String(p_next) + "' already exists.");
  143. Anim anim = animations[p_prev];
  144. animations.erase(p_prev);
  145. animations[p_next] = anim;
  146. animations[p_next].normal_name = String(p_next) + NORMAL_SUFFIX;
  147. emit_changed();
  148. }
  149. void SpriteFrames::get_animation_list(List<StringName> *r_animations) const {
  150. for (const Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
  151. r_animations->push_back(E->key());
  152. }
  153. }
  154. Vector<String> SpriteFrames::get_animation_names() const {
  155. Vector<String> names;
  156. for (const Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
  157. names.push_back(E->key());
  158. }
  159. names.sort();
  160. return names;
  161. }
  162. void SpriteFrames::set_animation_speed(const StringName &p_anim, float p_fps) {
  163. ERR_FAIL_COND_MSG(p_fps < 0, "Animation speed cannot be negative (" + itos(p_fps) + ").");
  164. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  165. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  166. E->get().speed = p_fps;
  167. }
  168. float SpriteFrames::get_animation_speed(const StringName &p_anim) const {
  169. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  170. ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
  171. return E->get().speed;
  172. }
  173. void SpriteFrames::set_animation_loop(const StringName &p_anim, bool p_loop) {
  174. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  175. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  176. E->get().loop = p_loop;
  177. }
  178. bool SpriteFrames::get_animation_loop(const StringName &p_anim) const {
  179. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  180. ERR_FAIL_COND_V_MSG(!E, false, "Animation '" + String(p_anim) + "' doesn't exist.");
  181. return E->get().loop;
  182. }
  183. void SpriteFrames::_set_frames(const Array &p_frames) {
  184. clear_all();
  185. Map<StringName, Anim>::Element *E = animations.find(SceneStringNames::get_singleton()->_default);
  186. ERR_FAIL_COND(!E);
  187. E->get().frames.resize(p_frames.size());
  188. for (int i = 0; i < E->get().frames.size(); i++) {
  189. E->get().frames.write[i] = p_frames[i];
  190. }
  191. }
  192. Array SpriteFrames::_get_frames() const {
  193. return Array();
  194. }
  195. Array SpriteFrames::_get_animations() const {
  196. Array anims;
  197. List<StringName> sorted_names;
  198. get_animation_list(&sorted_names);
  199. sorted_names.sort_custom<StringName::AlphCompare>();
  200. for (List<StringName>::Element *E = sorted_names.front(); E; E = E->next()) {
  201. const Anim &anim = animations[E->get()];
  202. Dictionary d;
  203. d["name"] = E->get();
  204. d["speed"] = anim.speed;
  205. d["loop"] = anim.loop;
  206. Array frames;
  207. for (int i = 0; i < anim.frames.size(); i++) {
  208. frames.push_back(anim.frames[i]);
  209. }
  210. d["frames"] = frames;
  211. anims.push_back(d);
  212. }
  213. return anims;
  214. }
  215. void SpriteFrames::_set_animations(const Array &p_animations) {
  216. animations.clear();
  217. for (int i = 0; i < p_animations.size(); i++) {
  218. Dictionary d = p_animations[i];
  219. ERR_CONTINUE(!d.has("name"));
  220. ERR_CONTINUE(!d.has("speed"));
  221. ERR_CONTINUE(!d.has("loop"));
  222. ERR_CONTINUE(!d.has("frames"));
  223. Anim anim;
  224. anim.speed = d["speed"];
  225. anim.loop = d["loop"];
  226. Array frames = d["frames"];
  227. for (int j = 0; j < frames.size(); j++) {
  228. RES res = frames[j];
  229. anim.frames.push_back(res);
  230. }
  231. animations[d["name"]] = anim;
  232. animations[d["name"]].normal_name = String(d["name"]) + NORMAL_SUFFIX;
  233. }
  234. }
  235. void SpriteFrames::_bind_methods() {
  236. ClassDB::bind_method(D_METHOD("add_animation", "anim"), &SpriteFrames::add_animation);
  237. ClassDB::bind_method(D_METHOD("has_animation", "anim"), &SpriteFrames::has_animation);
  238. ClassDB::bind_method(D_METHOD("remove_animation", "anim"), &SpriteFrames::remove_animation);
  239. ClassDB::bind_method(D_METHOD("rename_animation", "anim", "newname"), &SpriteFrames::rename_animation);
  240. ClassDB::bind_method(D_METHOD("get_animation_names"), &SpriteFrames::get_animation_names);
  241. ClassDB::bind_method(D_METHOD("set_animation_speed", "anim", "speed"), &SpriteFrames::set_animation_speed);
  242. ClassDB::bind_method(D_METHOD("get_animation_speed", "anim"), &SpriteFrames::get_animation_speed);
  243. ClassDB::bind_method(D_METHOD("set_animation_loop", "anim", "loop"), &SpriteFrames::set_animation_loop);
  244. ClassDB::bind_method(D_METHOD("get_animation_loop", "anim"), &SpriteFrames::get_animation_loop);
  245. ClassDB::bind_method(D_METHOD("add_frame", "anim", "frame", "at_position"), &SpriteFrames::add_frame, DEFVAL(-1));
  246. ClassDB::bind_method(D_METHOD("get_frame_count", "anim"), &SpriteFrames::get_frame_count);
  247. ClassDB::bind_method(D_METHOD("get_frame", "anim", "idx"), &SpriteFrames::get_frame);
  248. ClassDB::bind_method(D_METHOD("set_frame", "anim", "idx", "txt"), &SpriteFrames::set_frame);
  249. ClassDB::bind_method(D_METHOD("remove_frame", "anim", "idx"), &SpriteFrames::remove_frame);
  250. ClassDB::bind_method(D_METHOD("clear", "anim"), &SpriteFrames::clear);
  251. ClassDB::bind_method(D_METHOD("clear_all"), &SpriteFrames::clear_all);
  252. ClassDB::bind_method(D_METHOD("_set_frames"), &SpriteFrames::_set_frames);
  253. ClassDB::bind_method(D_METHOD("_get_frames"), &SpriteFrames::_get_frames);
  254. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "frames", PROPERTY_HINT_NONE, "", 0), "_set_frames", "_get_frames"); // Compatibility with Godot 2.1.
  255. ClassDB::bind_method(D_METHOD("_set_animations"), &SpriteFrames::_set_animations);
  256. ClassDB::bind_method(D_METHOD("_get_animations"), &SpriteFrames::_get_animations);
  257. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations", "_get_animations");
  258. }
  259. SpriteFrames::SpriteFrames() {
  260. add_animation(SceneStringNames::get_singleton()->_default);
  261. }
  262. void AnimatedSprite::_validate_property(PropertyInfo &property) const {
  263. if (!frames.is_valid()) {
  264. return;
  265. }
  266. if (property.name == "animation") {
  267. property.hint = PROPERTY_HINT_ENUM;
  268. List<StringName> names;
  269. frames->get_animation_list(&names);
  270. names.sort_custom<StringName::AlphCompare>();
  271. bool current_found = false;
  272. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  273. if (E->prev()) {
  274. property.hint_string += ",";
  275. }
  276. property.hint_string += String(E->get());
  277. if (animation == E->get()) {
  278. current_found = true;
  279. }
  280. }
  281. if (!current_found) {
  282. if (property.hint_string == String()) {
  283. property.hint_string = String(animation);
  284. } else {
  285. property.hint_string = String(animation) + "," + property.hint_string;
  286. }
  287. }
  288. }
  289. if (property.name == "frame") {
  290. if (playing) {
  291. property.usage = PROPERTY_USAGE_EDITOR;
  292. return;
  293. }
  294. property.hint = PROPERTY_HINT_RANGE;
  295. if (frames->has_animation(animation) && frames->get_frame_count(animation) > 1) {
  296. property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
  297. }
  298. property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  299. }
  300. }
  301. void AnimatedSprite::_notification(int p_what) {
  302. switch (p_what) {
  303. case NOTIFICATION_INTERNAL_PROCESS: {
  304. if (frames.is_null()) {
  305. return;
  306. }
  307. if (!frames->has_animation(animation)) {
  308. return;
  309. }
  310. if (frame < 0) {
  311. return;
  312. }
  313. if (!OS::get_singleton()->is_update_pending()) {
  314. return;
  315. }
  316. float remaining = get_process_delta_time();
  317. while (remaining) {
  318. float speed = frames->get_animation_speed(animation) * speed_scale;
  319. if (speed == 0) {
  320. return; //do nothing
  321. }
  322. if (timeout <= 0) {
  323. timeout = _get_frame_duration();
  324. int fc = frames->get_frame_count(animation);
  325. if ((!backwards && frame >= fc - 1) || (backwards && frame <= 0)) {
  326. if (frames->get_animation_loop(animation)) {
  327. if (backwards) {
  328. frame = fc - 1;
  329. } else {
  330. frame = 0;
  331. }
  332. emit_signal(SceneStringNames::get_singleton()->animation_finished);
  333. } else {
  334. if (backwards) {
  335. frame = 0;
  336. } else {
  337. frame = fc - 1;
  338. }
  339. if (!is_over) {
  340. is_over = true;
  341. emit_signal(SceneStringNames::get_singleton()->animation_finished);
  342. }
  343. }
  344. } else {
  345. if (backwards) {
  346. frame--;
  347. } else {
  348. frame++;
  349. }
  350. }
  351. update();
  352. _change_notify("frame");
  353. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  354. }
  355. float to_process = MIN(timeout, remaining);
  356. remaining -= to_process;
  357. timeout -= to_process;
  358. }
  359. } break;
  360. case NOTIFICATION_DRAW: {
  361. if (frames.is_null()) {
  362. return;
  363. }
  364. if (frame < 0) {
  365. return;
  366. }
  367. if (!frames->has_animation(animation)) {
  368. return;
  369. }
  370. Ref<Texture> texture = frames->get_frame(animation, frame);
  371. if (texture.is_null()) {
  372. return;
  373. }
  374. Ref<Texture> normal = frames->get_normal_frame(animation, frame);
  375. RID ci = get_canvas_item();
  376. Size2 s = texture->get_size();
  377. Point2 ofs = offset;
  378. if (centered) {
  379. ofs -= s / 2;
  380. }
  381. if (Engine::get_singleton()->get_use_gpu_pixel_snap()) {
  382. ofs = ofs.floor();
  383. }
  384. Rect2 dst_rect(ofs, s);
  385. if (hflip) {
  386. dst_rect.size.x = -dst_rect.size.x;
  387. }
  388. if (vflip) {
  389. dst_rect.size.y = -dst_rect.size.y;
  390. }
  391. texture->draw_rect_region(ci, dst_rect, Rect2(Vector2(), texture->get_size()), Color(1, 1, 1), false, normal);
  392. } break;
  393. }
  394. }
  395. void AnimatedSprite::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
  396. if (frames.is_valid()) {
  397. frames->disconnect("changed", this, "_res_changed");
  398. }
  399. frames = p_frames;
  400. if (frames.is_valid()) {
  401. frames->connect("changed", this, "_res_changed");
  402. }
  403. if (!frames.is_valid()) {
  404. frame = 0;
  405. } else {
  406. set_frame(frame);
  407. }
  408. _change_notify();
  409. _reset_timeout();
  410. update();
  411. update_configuration_warning();
  412. }
  413. Ref<SpriteFrames> AnimatedSprite::get_sprite_frames() const {
  414. return frames;
  415. }
  416. void AnimatedSprite::set_frame(int p_frame) {
  417. if (!frames.is_valid()) {
  418. return;
  419. }
  420. if (frames->has_animation(animation)) {
  421. int limit = frames->get_frame_count(animation);
  422. if (p_frame >= limit) {
  423. p_frame = limit - 1;
  424. }
  425. }
  426. if (p_frame < 0) {
  427. p_frame = 0;
  428. }
  429. if (frame == p_frame) {
  430. return;
  431. }
  432. frame = p_frame;
  433. _reset_timeout();
  434. update();
  435. _change_notify("frame");
  436. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  437. }
  438. int AnimatedSprite::get_frame() const {
  439. return frame;
  440. }
  441. void AnimatedSprite::set_speed_scale(float p_speed_scale) {
  442. float elapsed = _get_frame_duration() - timeout;
  443. speed_scale = MAX(p_speed_scale, 0.0f);
  444. // We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed
  445. _reset_timeout();
  446. timeout -= elapsed;
  447. }
  448. float AnimatedSprite::get_speed_scale() const {
  449. return speed_scale;
  450. }
  451. void AnimatedSprite::set_centered(bool p_center) {
  452. centered = p_center;
  453. update();
  454. item_rect_changed();
  455. }
  456. bool AnimatedSprite::is_centered() const {
  457. return centered;
  458. }
  459. void AnimatedSprite::set_offset(const Point2 &p_offset) {
  460. offset = p_offset;
  461. update();
  462. item_rect_changed();
  463. _change_notify("offset");
  464. }
  465. Point2 AnimatedSprite::get_offset() const {
  466. return offset;
  467. }
  468. void AnimatedSprite::set_flip_h(bool p_flip) {
  469. hflip = p_flip;
  470. update();
  471. }
  472. bool AnimatedSprite::is_flipped_h() const {
  473. return hflip;
  474. }
  475. void AnimatedSprite::set_flip_v(bool p_flip) {
  476. vflip = p_flip;
  477. update();
  478. }
  479. bool AnimatedSprite::is_flipped_v() const {
  480. return vflip;
  481. }
  482. void AnimatedSprite::_res_changed() {
  483. set_frame(frame);
  484. // Calling _change_notify("frame") and _change_notify("animation") instead wouldn't
  485. // make EditorInspector trigger calls to _validate_property(property) which would
  486. // lead to not updating valid values for "frame" and "animation" properties.
  487. _change_notify();
  488. update();
  489. }
  490. void AnimatedSprite::set_playing(bool p_playing) {
  491. if (playing == p_playing) {
  492. return;
  493. }
  494. playing = p_playing;
  495. _reset_timeout();
  496. set_process_internal(playing);
  497. property_list_changed_notify();
  498. }
  499. bool AnimatedSprite::is_playing() const {
  500. return playing;
  501. }
  502. void AnimatedSprite::play(const StringName &p_animation, const bool p_backwards) {
  503. backwards = p_backwards;
  504. if (p_animation) {
  505. set_animation(p_animation);
  506. if (frames.is_valid() && backwards && get_frame() == 0) {
  507. set_frame(frames->get_frame_count(p_animation) - 1);
  508. }
  509. }
  510. is_over = false;
  511. set_playing(true);
  512. }
  513. void AnimatedSprite::stop() {
  514. set_playing(false);
  515. }
  516. float AnimatedSprite::_get_frame_duration() {
  517. if (frames.is_valid() && frames->has_animation(animation)) {
  518. float speed = frames->get_animation_speed(animation) * speed_scale;
  519. if (speed > 0) {
  520. return 1.0 / speed;
  521. }
  522. }
  523. return 0.0;
  524. }
  525. void AnimatedSprite::_reset_timeout() {
  526. if (!playing) {
  527. return;
  528. }
  529. timeout = _get_frame_duration();
  530. is_over = false;
  531. }
  532. void AnimatedSprite::set_animation(const StringName &p_animation) {
  533. ERR_FAIL_COND_MSG(frames == nullptr, vformat("There is no animation with name '%s'.", p_animation));
  534. ERR_FAIL_COND_MSG(frames->get_animation_names().find(p_animation) == -1, vformat("There is no animation with name '%s'.", p_animation));
  535. if (animation == p_animation) {
  536. return;
  537. }
  538. animation = p_animation;
  539. _reset_timeout();
  540. set_frame(0);
  541. _change_notify();
  542. update();
  543. }
  544. StringName AnimatedSprite::get_animation() const {
  545. return animation;
  546. }
  547. String AnimatedSprite::get_configuration_warning() const {
  548. String warning = Node2D::get_configuration_warning();
  549. if (frames.is_null()) {
  550. if (warning != String()) {
  551. warning += "\n\n";
  552. }
  553. warning += TTR("A SpriteFrames resource must be created or set in the \"Frames\" property in order for AnimatedSprite to display frames.");
  554. }
  555. return warning;
  556. }
  557. void AnimatedSprite::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  558. #ifdef TOOLS_ENABLED
  559. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  560. #else
  561. const String quote_style = "\"";
  562. #endif
  563. if (p_idx == 0 && p_function == "play" && frames.is_valid()) {
  564. List<StringName> al;
  565. frames->get_animation_list(&al);
  566. for (List<StringName>::Element *E = al.front(); E; E = E->next()) {
  567. r_options->push_back(quote_style + String(E->get()) + quote_style);
  568. }
  569. }
  570. Node::get_argument_options(p_function, p_idx, r_options);
  571. }
  572. void AnimatedSprite::_bind_methods() {
  573. ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite::set_sprite_frames);
  574. ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite::get_sprite_frames);
  575. ClassDB::bind_method(D_METHOD("set_animation", "animation"), &AnimatedSprite::set_animation);
  576. ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite::get_animation);
  577. ClassDB::bind_method(D_METHOD("set_playing", "playing"), &AnimatedSprite::set_playing);
  578. ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite::is_playing);
  579. ClassDB::bind_method(D_METHOD("play", "anim", "backwards"), &AnimatedSprite::play, DEFVAL(StringName()), DEFVAL(false));
  580. ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite::stop);
  581. ClassDB::bind_method(D_METHOD("set_centered", "centered"), &AnimatedSprite::set_centered);
  582. ClassDB::bind_method(D_METHOD("is_centered"), &AnimatedSprite::is_centered);
  583. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AnimatedSprite::set_offset);
  584. ClassDB::bind_method(D_METHOD("get_offset"), &AnimatedSprite::get_offset);
  585. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &AnimatedSprite::set_flip_h);
  586. ClassDB::bind_method(D_METHOD("is_flipped_h"), &AnimatedSprite::is_flipped_h);
  587. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &AnimatedSprite::set_flip_v);
  588. ClassDB::bind_method(D_METHOD("is_flipped_v"), &AnimatedSprite::is_flipped_v);
  589. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite::set_frame);
  590. ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite::get_frame);
  591. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &AnimatedSprite::set_speed_scale);
  592. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedSprite::get_speed_scale);
  593. ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite::_res_changed);
  594. ADD_SIGNAL(MethodInfo("frame_changed"));
  595. ADD_SIGNAL(MethodInfo("animation_finished"));
  596. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
  597. ADD_PROPERTY(PropertyInfo(Variant::STRING, "animation"), "set_animation", "get_animation");
  598. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
  599. ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale"), "set_speed_scale", "get_speed_scale");
  600. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "set_playing", "is_playing");
  601. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
  602. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  603. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  604. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  605. }
  606. AnimatedSprite::AnimatedSprite() {
  607. centered = true;
  608. hflip = false;
  609. vflip = false;
  610. frame = 0;
  611. speed_scale = 1.0f;
  612. playing = false;
  613. backwards = false;
  614. animation = "default";
  615. timeout = 0;
  616. is_over = false;
  617. }