animated_sprite.cpp 20 KB

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