sprite_frames.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*************************************************************************/
  2. /* sprite_frames.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 "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. HashMap<StringName, Anim>::Iterator 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->value.frames.size()) {
  36. E->value.frames.insert(p_at_pos, p_frame);
  37. } else {
  38. E->value.frames.push_back(p_frame);
  39. }
  40. emit_changed();
  41. }
  42. int SpriteFrames::get_frame_count(const StringName &p_anim) const {
  43. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  44. ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
  45. return E->value.frames.size();
  46. }
  47. void SpriteFrames::remove_frame(const StringName &p_anim, int p_idx) {
  48. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  49. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  50. E->value.frames.remove_at(p_idx);
  51. emit_changed();
  52. }
  53. void SpriteFrames::clear(const StringName &p_anim) {
  54. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  55. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  56. E->value.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. void SpriteFrames::get_animation_list(List<StringName> *r_animations) const {
  81. for (const KeyValue<StringName, Anim> &E : animations) {
  82. r_animations->push_back(E.key);
  83. }
  84. }
  85. Vector<String> SpriteFrames::get_animation_names() const {
  86. Vector<String> names;
  87. for (const KeyValue<StringName, Anim> &E : animations) {
  88. names.push_back(E.key);
  89. }
  90. names.sort();
  91. return names;
  92. }
  93. void SpriteFrames::set_animation_speed(const StringName &p_anim, double p_fps) {
  94. ERR_FAIL_COND_MSG(p_fps < 0, "Animation speed cannot be negative (" + itos(p_fps) + ").");
  95. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  96. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  97. E->value.speed = p_fps;
  98. }
  99. double SpriteFrames::get_animation_speed(const StringName &p_anim) const {
  100. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  101. ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
  102. return E->value.speed;
  103. }
  104. void SpriteFrames::set_animation_loop(const StringName &p_anim, bool p_loop) {
  105. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  106. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  107. E->value.loop = p_loop;
  108. }
  109. bool SpriteFrames::get_animation_loop(const StringName &p_anim) const {
  110. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  111. ERR_FAIL_COND_V_MSG(!E, false, "Animation '" + String(p_anim) + "' doesn't exist.");
  112. return E->value.loop;
  113. }
  114. Array SpriteFrames::_get_animations() const {
  115. Array anims;
  116. List<StringName> sorted_names;
  117. get_animation_list(&sorted_names);
  118. sorted_names.sort_custom<StringName::AlphCompare>();
  119. for (const StringName &name : sorted_names) {
  120. const Anim &anim = animations[name];
  121. Dictionary d;
  122. d["name"] = name;
  123. d["speed"] = anim.speed;
  124. d["loop"] = anim.loop;
  125. Array frames;
  126. for (int i = 0; i < anim.frames.size(); i++) {
  127. frames.push_back(anim.frames[i]);
  128. }
  129. d["frames"] = frames;
  130. anims.push_back(d);
  131. }
  132. return anims;
  133. }
  134. void SpriteFrames::_set_animations(const Array &p_animations) {
  135. animations.clear();
  136. for (int i = 0; i < p_animations.size(); i++) {
  137. Dictionary d = p_animations[i];
  138. ERR_CONTINUE(!d.has("name"));
  139. ERR_CONTINUE(!d.has("speed"));
  140. ERR_CONTINUE(!d.has("loop"));
  141. ERR_CONTINUE(!d.has("frames"));
  142. Anim anim;
  143. anim.speed = d["speed"];
  144. anim.loop = d["loop"];
  145. Array frames = d["frames"];
  146. for (int j = 0; j < frames.size(); j++) {
  147. Ref<Resource> res = frames[j];
  148. anim.frames.push_back(res);
  149. }
  150. animations[d["name"]] = anim;
  151. }
  152. }
  153. void SpriteFrames::_bind_methods() {
  154. ClassDB::bind_method(D_METHOD("add_animation", "anim"), &SpriteFrames::add_animation);
  155. ClassDB::bind_method(D_METHOD("has_animation", "anim"), &SpriteFrames::has_animation);
  156. ClassDB::bind_method(D_METHOD("remove_animation", "anim"), &SpriteFrames::remove_animation);
  157. ClassDB::bind_method(D_METHOD("rename_animation", "anim", "newname"), &SpriteFrames::rename_animation);
  158. ClassDB::bind_method(D_METHOD("get_animation_names"), &SpriteFrames::get_animation_names);
  159. ClassDB::bind_method(D_METHOD("set_animation_speed", "anim", "speed"), &SpriteFrames::set_animation_speed);
  160. ClassDB::bind_method(D_METHOD("get_animation_speed", "anim"), &SpriteFrames::get_animation_speed);
  161. ClassDB::bind_method(D_METHOD("set_animation_loop", "anim", "loop"), &SpriteFrames::set_animation_loop);
  162. ClassDB::bind_method(D_METHOD("get_animation_loop", "anim"), &SpriteFrames::get_animation_loop);
  163. ClassDB::bind_method(D_METHOD("add_frame", "anim", "frame", "at_position"), &SpriteFrames::add_frame, DEFVAL(-1));
  164. ClassDB::bind_method(D_METHOD("get_frame_count", "anim"), &SpriteFrames::get_frame_count);
  165. ClassDB::bind_method(D_METHOD("get_frame", "anim", "idx"), &SpriteFrames::get_frame);
  166. ClassDB::bind_method(D_METHOD("set_frame", "anim", "idx", "txt"), &SpriteFrames::set_frame);
  167. ClassDB::bind_method(D_METHOD("remove_frame", "anim", "idx"), &SpriteFrames::remove_frame);
  168. ClassDB::bind_method(D_METHOD("clear", "anim"), &SpriteFrames::clear);
  169. ClassDB::bind_method(D_METHOD("clear_all"), &SpriteFrames::clear_all);
  170. // `animations` property is for serialization.
  171. ClassDB::bind_method(D_METHOD("_set_animations", "animations"), &SpriteFrames::_set_animations);
  172. ClassDB::bind_method(D_METHOD("_get_animations"), &SpriteFrames::_get_animations);
  173. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations", "_get_animations");
  174. }
  175. SpriteFrames::SpriteFrames() {
  176. add_animation(SceneStringNames::get_singleton()->_default);
  177. }