animated_sprite_2d.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*************************************************************************/
  2. /* animated_sprite_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_2d.h"
  31. #include "scene/main/viewport.h"
  32. #include "scene/scene_string_names.h"
  33. #ifdef TOOLS_ENABLED
  34. Dictionary AnimatedSprite2D::_edit_get_state() const {
  35. Dictionary state = Node2D::_edit_get_state();
  36. state["offset"] = offset;
  37. return state;
  38. }
  39. void AnimatedSprite2D::_edit_set_state(const Dictionary &p_state) {
  40. Node2D::_edit_set_state(p_state);
  41. set_offset(p_state["offset"]);
  42. }
  43. void AnimatedSprite2D::_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 AnimatedSprite2D::_edit_get_pivot() const {
  48. return Vector2();
  49. }
  50. bool AnimatedSprite2D::_edit_use_pivot() const {
  51. return true;
  52. }
  53. Rect2 AnimatedSprite2D::_edit_get_rect() const {
  54. return _get_rect();
  55. }
  56. bool AnimatedSprite2D::_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<Texture2D> t;
  61. if (animation) {
  62. t = frames->get_frame(animation, frame);
  63. }
  64. return t.is_valid();
  65. }
  66. #endif
  67. Rect2 AnimatedSprite2D::get_anchorable_rect() const {
  68. return _get_rect();
  69. }
  70. Rect2 AnimatedSprite2D::_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<Texture2D> t;
  75. if (animation) {
  76. t = frames->get_frame(animation, frame);
  77. }
  78. if (t.is_null()) {
  79. return Rect2();
  80. }
  81. Size2 s = t->get_size();
  82. Point2 ofs = offset;
  83. if (centered) {
  84. ofs -= s / 2;
  85. }
  86. if (s == Size2(0, 0)) {
  87. s = Size2(1, 1);
  88. }
  89. return Rect2(ofs, s);
  90. }
  91. void AnimatedSprite2D::_validate_property(PropertyInfo &p_property) const {
  92. if (!frames.is_valid()) {
  93. return;
  94. }
  95. if (p_property.name == "animation") {
  96. p_property.hint = PROPERTY_HINT_ENUM;
  97. List<StringName> names;
  98. frames->get_animation_list(&names);
  99. names.sort_custom<StringName::AlphCompare>();
  100. bool current_found = false;
  101. bool is_first_element = true;
  102. for (const StringName &E : names) {
  103. if (!is_first_element) {
  104. p_property.hint_string += ",";
  105. } else {
  106. is_first_element = false;
  107. }
  108. p_property.hint_string += String(E);
  109. if (animation == E) {
  110. current_found = true;
  111. }
  112. }
  113. if (!current_found) {
  114. if (p_property.hint_string.is_empty()) {
  115. p_property.hint_string = String(animation);
  116. } else {
  117. p_property.hint_string = String(animation) + "," + p_property.hint_string;
  118. }
  119. }
  120. }
  121. if (p_property.name == "frame") {
  122. p_property.hint = PROPERTY_HINT_RANGE;
  123. if (frames->has_animation(animation) && frames->get_frame_count(animation) > 0) {
  124. p_property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
  125. } else {
  126. // Avoid an error, `hint_string` is required for `PROPERTY_HINT_RANGE`.
  127. p_property.hint_string = "0,0,1";
  128. }
  129. p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  130. }
  131. }
  132. void AnimatedSprite2D::_notification(int p_what) {
  133. switch (p_what) {
  134. case NOTIFICATION_INTERNAL_PROCESS: {
  135. if (frames.is_null()) {
  136. return;
  137. }
  138. if (!frames->has_animation(animation)) {
  139. return;
  140. }
  141. if (frame < 0) {
  142. return;
  143. }
  144. double remaining = get_process_delta_time();
  145. while (remaining) {
  146. double speed = frames->get_animation_speed(animation) * speed_scale;
  147. if (speed == 0) {
  148. return; // Do nothing.
  149. }
  150. if (timeout <= 0) {
  151. timeout = _get_frame_duration();
  152. int fc = frames->get_frame_count(animation);
  153. if ((!backwards && frame >= fc - 1) || (backwards && frame <= 0)) {
  154. if (frames->get_animation_loop(animation)) {
  155. if (backwards) {
  156. frame = fc - 1;
  157. } else {
  158. frame = 0;
  159. }
  160. emit_signal(SceneStringNames::get_singleton()->animation_finished);
  161. } else {
  162. if (backwards) {
  163. frame = 0;
  164. } else {
  165. frame = fc - 1;
  166. }
  167. if (!is_over) {
  168. is_over = true;
  169. emit_signal(SceneStringNames::get_singleton()->animation_finished);
  170. }
  171. }
  172. } else {
  173. if (backwards) {
  174. frame--;
  175. } else {
  176. frame++;
  177. }
  178. }
  179. queue_redraw();
  180. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  181. }
  182. double to_process = MIN(timeout, remaining);
  183. remaining -= to_process;
  184. timeout -= to_process;
  185. }
  186. } break;
  187. case NOTIFICATION_DRAW: {
  188. if (frames.is_null()) {
  189. return;
  190. }
  191. if (frame < 0) {
  192. return;
  193. }
  194. if (!frames->has_animation(animation)) {
  195. return;
  196. }
  197. Ref<Texture2D> texture = frames->get_frame(animation, frame);
  198. if (texture.is_null()) {
  199. return;
  200. }
  201. RID ci = get_canvas_item();
  202. Size2 s = texture->get_size();
  203. Point2 ofs = offset;
  204. if (centered) {
  205. ofs -= s / 2;
  206. }
  207. if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
  208. ofs = ofs.floor();
  209. }
  210. Rect2 dst_rect(ofs, s);
  211. if (hflip) {
  212. dst_rect.size.x = -dst_rect.size.x;
  213. }
  214. if (vflip) {
  215. dst_rect.size.y = -dst_rect.size.y;
  216. }
  217. texture->draw_rect_region(ci, dst_rect, Rect2(Vector2(), texture->get_size()), Color(1, 1, 1), false);
  218. } break;
  219. }
  220. }
  221. void AnimatedSprite2D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
  222. if (frames.is_valid()) {
  223. frames->disconnect("changed", callable_mp(this, &AnimatedSprite2D::_res_changed));
  224. }
  225. frames = p_frames;
  226. if (frames.is_valid()) {
  227. frames->connect("changed", callable_mp(this, &AnimatedSprite2D::_res_changed));
  228. }
  229. if (!frames.is_valid()) {
  230. frame = 0;
  231. } else {
  232. set_frame(frame);
  233. }
  234. notify_property_list_changed();
  235. _reset_timeout();
  236. queue_redraw();
  237. update_configuration_warnings();
  238. }
  239. Ref<SpriteFrames> AnimatedSprite2D::get_sprite_frames() const {
  240. return frames;
  241. }
  242. void AnimatedSprite2D::set_frame(int p_frame) {
  243. if (!frames.is_valid()) {
  244. return;
  245. }
  246. if (frames->has_animation(animation)) {
  247. int limit = frames->get_frame_count(animation);
  248. if (p_frame >= limit) {
  249. p_frame = limit - 1;
  250. }
  251. }
  252. if (p_frame < 0) {
  253. p_frame = 0;
  254. }
  255. if (frame == p_frame) {
  256. return;
  257. }
  258. frame = p_frame;
  259. _reset_timeout();
  260. queue_redraw();
  261. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  262. }
  263. int AnimatedSprite2D::get_frame() const {
  264. return frame;
  265. }
  266. void AnimatedSprite2D::set_speed_scale(double p_speed_scale) {
  267. double elapsed = _get_frame_duration() - timeout;
  268. speed_scale = MAX(p_speed_scale, 0.0f);
  269. // We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed
  270. _reset_timeout();
  271. timeout -= elapsed;
  272. }
  273. double AnimatedSprite2D::get_speed_scale() const {
  274. return speed_scale;
  275. }
  276. void AnimatedSprite2D::set_centered(bool p_center) {
  277. centered = p_center;
  278. queue_redraw();
  279. item_rect_changed();
  280. }
  281. bool AnimatedSprite2D::is_centered() const {
  282. return centered;
  283. }
  284. void AnimatedSprite2D::set_offset(const Point2 &p_offset) {
  285. offset = p_offset;
  286. queue_redraw();
  287. item_rect_changed();
  288. }
  289. Point2 AnimatedSprite2D::get_offset() const {
  290. return offset;
  291. }
  292. void AnimatedSprite2D::set_flip_h(bool p_flip) {
  293. hflip = p_flip;
  294. queue_redraw();
  295. }
  296. bool AnimatedSprite2D::is_flipped_h() const {
  297. return hflip;
  298. }
  299. void AnimatedSprite2D::set_flip_v(bool p_flip) {
  300. vflip = p_flip;
  301. queue_redraw();
  302. }
  303. bool AnimatedSprite2D::is_flipped_v() const {
  304. return vflip;
  305. }
  306. void AnimatedSprite2D::_res_changed() {
  307. set_frame(frame);
  308. queue_redraw();
  309. }
  310. void AnimatedSprite2D::set_playing(bool p_playing) {
  311. if (playing == p_playing) {
  312. return;
  313. }
  314. playing = p_playing;
  315. _reset_timeout();
  316. set_process_internal(playing);
  317. }
  318. bool AnimatedSprite2D::is_playing() const {
  319. return playing;
  320. }
  321. void AnimatedSprite2D::play(const StringName &p_animation, const bool p_backwards) {
  322. backwards = p_backwards;
  323. if (p_animation) {
  324. set_animation(p_animation);
  325. if (frames.is_valid() && backwards && get_frame() == 0) {
  326. set_frame(frames->get_frame_count(p_animation) - 1);
  327. }
  328. }
  329. is_over = false;
  330. set_playing(true);
  331. }
  332. void AnimatedSprite2D::stop() {
  333. set_playing(false);
  334. }
  335. double AnimatedSprite2D::_get_frame_duration() {
  336. if (frames.is_valid() && frames->has_animation(animation)) {
  337. double speed = frames->get_animation_speed(animation) * speed_scale;
  338. if (speed > 0) {
  339. return 1.0 / speed;
  340. }
  341. }
  342. return 0.0;
  343. }
  344. void AnimatedSprite2D::_reset_timeout() {
  345. if (!playing) {
  346. return;
  347. }
  348. timeout = _get_frame_duration();
  349. is_over = false;
  350. }
  351. void AnimatedSprite2D::set_animation(const StringName &p_animation) {
  352. ERR_FAIL_COND_MSG(frames == nullptr, vformat("There is no animation with name '%s'.", p_animation));
  353. ERR_FAIL_COND_MSG(!frames->get_animation_names().has(p_animation), vformat("There is no animation with name '%s'.", p_animation));
  354. if (animation == p_animation) {
  355. return;
  356. }
  357. animation = p_animation;
  358. _reset_timeout();
  359. set_frame(0);
  360. notify_property_list_changed();
  361. queue_redraw();
  362. }
  363. StringName AnimatedSprite2D::get_animation() const {
  364. return animation;
  365. }
  366. TypedArray<String> AnimatedSprite2D::get_configuration_warnings() const {
  367. TypedArray<String> warnings = Node::get_configuration_warnings();
  368. if (frames.is_null()) {
  369. warnings.push_back(RTR("A SpriteFrames resource must be created or set in the \"Frames\" property in order for AnimatedSprite to display frames."));
  370. }
  371. return warnings;
  372. }
  373. void AnimatedSprite2D::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  374. if (p_idx == 0 && p_function == "play" && frames.is_valid()) {
  375. List<StringName> al;
  376. frames->get_animation_list(&al);
  377. for (const StringName &name : al) {
  378. r_options->push_back(String(name).quote());
  379. }
  380. }
  381. Node::get_argument_options(p_function, p_idx, r_options);
  382. }
  383. void AnimatedSprite2D::_bind_methods() {
  384. ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite2D::set_sprite_frames);
  385. ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite2D::get_sprite_frames);
  386. ClassDB::bind_method(D_METHOD("set_animation", "animation"), &AnimatedSprite2D::set_animation);
  387. ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite2D::get_animation);
  388. ClassDB::bind_method(D_METHOD("set_playing", "playing"), &AnimatedSprite2D::set_playing);
  389. ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite2D::is_playing);
  390. ClassDB::bind_method(D_METHOD("play", "anim", "backwards"), &AnimatedSprite2D::play, DEFVAL(StringName()), DEFVAL(false));
  391. ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite2D::stop);
  392. ClassDB::bind_method(D_METHOD("set_centered", "centered"), &AnimatedSprite2D::set_centered);
  393. ClassDB::bind_method(D_METHOD("is_centered"), &AnimatedSprite2D::is_centered);
  394. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AnimatedSprite2D::set_offset);
  395. ClassDB::bind_method(D_METHOD("get_offset"), &AnimatedSprite2D::get_offset);
  396. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &AnimatedSprite2D::set_flip_h);
  397. ClassDB::bind_method(D_METHOD("is_flipped_h"), &AnimatedSprite2D::is_flipped_h);
  398. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &AnimatedSprite2D::set_flip_v);
  399. ClassDB::bind_method(D_METHOD("is_flipped_v"), &AnimatedSprite2D::is_flipped_v);
  400. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite2D::set_frame);
  401. ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite2D::get_frame);
  402. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &AnimatedSprite2D::set_speed_scale);
  403. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedSprite2D::get_speed_scale);
  404. ADD_SIGNAL(MethodInfo("frame_changed"));
  405. ADD_SIGNAL(MethodInfo("animation_finished"));
  406. ADD_GROUP("Animation", "");
  407. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
  408. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
  409. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
  410. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale"), "set_speed_scale", "get_speed_scale");
  411. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "set_playing", "is_playing");
  412. ADD_GROUP("Offset", "");
  413. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
  414. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  415. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  416. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  417. }
  418. AnimatedSprite2D::AnimatedSprite2D() {
  419. }