sprite_frames.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*************************************************************************/
  2. /* sprite_frames.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "sprite_frames.h"
  31. #include "scene/scene_string_names.h"
  32. void SpriteFrames::add_frame(const StringName &p_anim, const Ref<Texture2D> &p_frame, int p_at_pos) {
  33. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  34. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  35. if (p_at_pos >= 0 && p_at_pos < E->get().frames.size()) {
  36. E->get().frames.insert(p_at_pos, p_frame);
  37. } else {
  38. E->get().frames.push_back(p_frame);
  39. }
  40. emit_changed();
  41. }
  42. int SpriteFrames::get_frame_count(const StringName &p_anim) const {
  43. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  44. ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
  45. return E->get().frames.size();
  46. }
  47. void SpriteFrames::remove_frame(const StringName &p_anim, int p_idx) {
  48. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  49. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  50. E->get().frames.remove(p_idx);
  51. emit_changed();
  52. }
  53. void SpriteFrames::clear(const StringName &p_anim) {
  54. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  55. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  56. E->get().frames.clear();
  57. emit_changed();
  58. }
  59. void SpriteFrames::clear_all() {
  60. animations.clear();
  61. add_animation("default");
  62. }
  63. void SpriteFrames::add_animation(const StringName &p_anim) {
  64. ERR_FAIL_COND_MSG(animations.has(p_anim), "SpriteFrames already has animation '" + p_anim + "'.");
  65. animations[p_anim] = Anim();
  66. }
  67. bool SpriteFrames::has_animation(const StringName &p_anim) const {
  68. return animations.has(p_anim);
  69. }
  70. void SpriteFrames::remove_animation(const StringName &p_anim) {
  71. animations.erase(p_anim);
  72. }
  73. void SpriteFrames::rename_animation(const StringName &p_prev, const StringName &p_next) {
  74. ERR_FAIL_COND_MSG(!animations.has(p_prev), "SpriteFrames doesn't have animation '" + String(p_prev) + "'.");
  75. ERR_FAIL_COND_MSG(animations.has(p_next), "Animation '" + String(p_next) + "' already exists.");
  76. Anim anim = animations[p_prev];
  77. animations.erase(p_prev);
  78. animations[p_next] = anim;
  79. }
  80. Vector<String> SpriteFrames::_get_animation_list() const {
  81. Vector<String> ret;
  82. List<StringName> al;
  83. get_animation_list(&al);
  84. for (const StringName &E : al) {
  85. ret.push_back(E);
  86. }
  87. return ret;
  88. }
  89. void SpriteFrames::get_animation_list(List<StringName> *r_animations) const {
  90. for (const Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
  91. r_animations->push_back(E->key());
  92. }
  93. }
  94. Vector<String> SpriteFrames::get_animation_names() const {
  95. Vector<String> names;
  96. for (const Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
  97. names.push_back(E->key());
  98. }
  99. names.sort();
  100. return names;
  101. }
  102. void SpriteFrames::set_animation_speed(const StringName &p_anim, double p_fps) {
  103. ERR_FAIL_COND_MSG(p_fps < 0, "Animation speed cannot be negative (" + itos(p_fps) + ").");
  104. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  105. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  106. E->get().speed = p_fps;
  107. }
  108. double SpriteFrames::get_animation_speed(const StringName &p_anim) const {
  109. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  110. ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
  111. return E->get().speed;
  112. }
  113. void SpriteFrames::set_animation_loop(const StringName &p_anim, bool p_loop) {
  114. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  115. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  116. E->get().loop = p_loop;
  117. }
  118. bool SpriteFrames::get_animation_loop(const StringName &p_anim) const {
  119. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  120. ERR_FAIL_COND_V_MSG(!E, false, "Animation '" + String(p_anim) + "' doesn't exist.");
  121. return E->get().loop;
  122. }
  123. void SpriteFrames::_set_frames(const Array &p_frames) {
  124. clear_all();
  125. Map<StringName, Anim>::Element *E = animations.find(SceneStringNames::get_singleton()->_default);
  126. ERR_FAIL_COND(!E);
  127. E->get().frames.resize(p_frames.size());
  128. for (int i = 0; i < E->get().frames.size(); i++) {
  129. E->get().frames.write[i] = p_frames[i];
  130. }
  131. }
  132. Array SpriteFrames::_get_frames() const {
  133. return Array();
  134. }
  135. Array SpriteFrames::_get_animations() const {
  136. Array anims;
  137. for (Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
  138. Dictionary d;
  139. d["name"] = E->key();
  140. d["speed"] = E->get().speed;
  141. d["loop"] = E->get().loop;
  142. Array frames;
  143. for (int i = 0; i < E->get().frames.size(); i++) {
  144. frames.push_back(E->get().frames[i]);
  145. }
  146. d["frames"] = frames;
  147. anims.push_back(d);
  148. }
  149. return anims;
  150. }
  151. void SpriteFrames::_set_animations(const Array &p_animations) {
  152. animations.clear();
  153. for (int i = 0; i < p_animations.size(); i++) {
  154. Dictionary d = p_animations[i];
  155. ERR_CONTINUE(!d.has("name"));
  156. ERR_CONTINUE(!d.has("speed"));
  157. ERR_CONTINUE(!d.has("loop"));
  158. ERR_CONTINUE(!d.has("frames"));
  159. Anim anim;
  160. anim.speed = d["speed"];
  161. anim.loop = d["loop"];
  162. Array frames = d["frames"];
  163. for (int j = 0; j < frames.size(); j++) {
  164. RES res = frames[j];
  165. anim.frames.push_back(res);
  166. }
  167. animations[d["name"]] = anim;
  168. }
  169. }
  170. void SpriteFrames::_bind_methods() {
  171. ClassDB::bind_method(D_METHOD("add_animation", "anim"), &SpriteFrames::add_animation);
  172. ClassDB::bind_method(D_METHOD("has_animation", "anim"), &SpriteFrames::has_animation);
  173. ClassDB::bind_method(D_METHOD("remove_animation", "anim"), &SpriteFrames::remove_animation);
  174. ClassDB::bind_method(D_METHOD("rename_animation", "anim", "newname"), &SpriteFrames::rename_animation);
  175. ClassDB::bind_method(D_METHOD("get_animation_names"), &SpriteFrames::get_animation_names);
  176. ClassDB::bind_method(D_METHOD("set_animation_speed", "anim", "speed"), &SpriteFrames::set_animation_speed);
  177. ClassDB::bind_method(D_METHOD("get_animation_speed", "anim"), &SpriteFrames::get_animation_speed);
  178. ClassDB::bind_method(D_METHOD("set_animation_loop", "anim", "loop"), &SpriteFrames::set_animation_loop);
  179. ClassDB::bind_method(D_METHOD("get_animation_loop", "anim"), &SpriteFrames::get_animation_loop);
  180. ClassDB::bind_method(D_METHOD("add_frame", "anim", "frame", "at_position"), &SpriteFrames::add_frame, DEFVAL(-1));
  181. ClassDB::bind_method(D_METHOD("get_frame_count", "anim"), &SpriteFrames::get_frame_count);
  182. ClassDB::bind_method(D_METHOD("get_frame", "anim", "idx"), &SpriteFrames::get_frame);
  183. ClassDB::bind_method(D_METHOD("set_frame", "anim", "idx", "txt"), &SpriteFrames::set_frame);
  184. ClassDB::bind_method(D_METHOD("remove_frame", "anim", "idx"), &SpriteFrames::remove_frame);
  185. ClassDB::bind_method(D_METHOD("clear", "anim"), &SpriteFrames::clear);
  186. ClassDB::bind_method(D_METHOD("clear_all"), &SpriteFrames::clear_all);
  187. ClassDB::bind_method(D_METHOD("_set_frames"), &SpriteFrames::_set_frames);
  188. ClassDB::bind_method(D_METHOD("_get_frames"), &SpriteFrames::_get_frames);
  189. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "frames", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "_set_frames", "_get_frames"); //compatibility
  190. ClassDB::bind_method(D_METHOD("_set_animations"), &SpriteFrames::_set_animations);
  191. ClassDB::bind_method(D_METHOD("_get_animations"), &SpriteFrames::_get_animations);
  192. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations", "_get_animations"); //compatibility
  193. }
  194. SpriteFrames::SpriteFrames() {
  195. add_animation(SceneStringNames::get_singleton()->_default);
  196. }