sprite_frames.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**************************************************************************/
  2. /* sprite_frames.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 "sprite_frames.h"
  31. #include "scene/scene_string_names.h"
  32. void SpriteFrames::add_frame(const StringName &p_anim, const Ref<Texture2D> &p_texture, float p_duration, 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. p_duration = MAX(SPRITE_FRAME_MINIMUM_DURATION, p_duration);
  36. Frame frame = { p_texture, p_duration };
  37. if (p_at_pos >= 0 && p_at_pos < E->value.frames.size()) {
  38. E->value.frames.insert(p_at_pos, frame);
  39. } else {
  40. E->value.frames.push_back(frame);
  41. }
  42. emit_changed();
  43. }
  44. void SpriteFrames::set_frame(const StringName &p_anim, int p_idx, const Ref<Texture2D> &p_texture, float p_duration) {
  45. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  46. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  47. ERR_FAIL_COND(p_idx < 0);
  48. if (p_idx >= E->value.frames.size()) {
  49. return;
  50. }
  51. p_duration = MAX(SPRITE_FRAME_MINIMUM_DURATION, p_duration);
  52. Frame frame = { p_texture, p_duration };
  53. E->value.frames.write[p_idx] = frame;
  54. emit_changed();
  55. }
  56. int SpriteFrames::get_frame_count(const StringName &p_anim) const {
  57. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  58. ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
  59. return E->value.frames.size();
  60. }
  61. void SpriteFrames::remove_frame(const StringName &p_anim, int p_idx) {
  62. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  63. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  64. E->value.frames.remove_at(p_idx);
  65. emit_changed();
  66. }
  67. void SpriteFrames::clear(const StringName &p_anim) {
  68. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  69. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  70. E->value.frames.clear();
  71. emit_changed();
  72. }
  73. void SpriteFrames::clear_all() {
  74. animations.clear();
  75. add_animation(SceneStringName(default_));
  76. }
  77. void SpriteFrames::add_animation(const StringName &p_anim) {
  78. ERR_FAIL_COND_MSG(animations.has(p_anim), "SpriteFrames already has animation '" + p_anim + "'.");
  79. animations[p_anim] = Anim();
  80. }
  81. bool SpriteFrames::has_animation(const StringName &p_anim) const {
  82. return animations.has(p_anim);
  83. }
  84. void SpriteFrames::duplicate_animation(const StringName &p_from, const StringName &p_to) {
  85. ERR_FAIL_COND_MSG(!animations.has(p_from), vformat("SpriteFrames doesn't have animation '%s'.", p_from));
  86. ERR_FAIL_COND_MSG(animations.has(p_to), vformat("Animation '%s' already exists.", p_to));
  87. animations[p_to] = animations[p_from];
  88. }
  89. void SpriteFrames::remove_animation(const StringName &p_anim) {
  90. animations.erase(p_anim);
  91. }
  92. void SpriteFrames::rename_animation(const StringName &p_prev, const StringName &p_next) {
  93. ERR_FAIL_COND_MSG(!animations.has(p_prev), "SpriteFrames doesn't have animation '" + String(p_prev) + "'.");
  94. ERR_FAIL_COND_MSG(animations.has(p_next), "Animation '" + String(p_next) + "' already exists.");
  95. Anim anim = animations[p_prev];
  96. animations.erase(p_prev);
  97. animations[p_next] = anim;
  98. }
  99. void SpriteFrames::get_animation_list(List<StringName> *r_animations) const {
  100. for (const KeyValue<StringName, Anim> &E : animations) {
  101. r_animations->push_back(E.key);
  102. }
  103. }
  104. Vector<String> SpriteFrames::get_animation_names() const {
  105. Vector<String> names;
  106. for (const KeyValue<StringName, Anim> &E : animations) {
  107. names.push_back(E.key);
  108. }
  109. names.sort();
  110. return names;
  111. }
  112. void SpriteFrames::set_animation_speed(const StringName &p_anim, double p_fps) {
  113. ERR_FAIL_COND_MSG(p_fps < 0, "Animation speed cannot be negative (" + itos(p_fps) + ").");
  114. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  115. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  116. E->value.speed = p_fps;
  117. }
  118. double SpriteFrames::get_animation_speed(const StringName &p_anim) const {
  119. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  120. ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
  121. return E->value.speed;
  122. }
  123. void SpriteFrames::set_animation_loop(const StringName &p_anim, bool p_loop) {
  124. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  125. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  126. E->value.loop = p_loop;
  127. }
  128. bool SpriteFrames::get_animation_loop(const StringName &p_anim) const {
  129. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  130. ERR_FAIL_COND_V_MSG(!E, false, "Animation '" + String(p_anim) + "' doesn't exist.");
  131. return E->value.loop;
  132. }
  133. Array SpriteFrames::_get_animations() const {
  134. Array anims;
  135. List<StringName> sorted_names;
  136. get_animation_list(&sorted_names);
  137. sorted_names.sort_custom<StringName::AlphCompare>();
  138. for (const StringName &anim_name : sorted_names) {
  139. const Anim &anim = animations[anim_name];
  140. Dictionary d;
  141. d["name"] = anim_name;
  142. d["speed"] = anim.speed;
  143. d["loop"] = anim.loop;
  144. Array frames;
  145. for (int i = 0; i < anim.frames.size(); i++) {
  146. Dictionary f;
  147. f["texture"] = anim.frames[i].texture;
  148. f["duration"] = anim.frames[i].duration;
  149. frames.push_back(f);
  150. }
  151. d["frames"] = frames;
  152. anims.push_back(d);
  153. }
  154. return anims;
  155. }
  156. void SpriteFrames::_set_animations(const Array &p_animations) {
  157. animations.clear();
  158. for (int i = 0; i < p_animations.size(); i++) {
  159. Dictionary d = p_animations[i];
  160. ERR_CONTINUE(!d.has("name"));
  161. ERR_CONTINUE(!d.has("speed"));
  162. ERR_CONTINUE(!d.has("loop"));
  163. ERR_CONTINUE(!d.has("frames"));
  164. Anim anim;
  165. anim.speed = d["speed"];
  166. anim.loop = d["loop"];
  167. Array frames = d["frames"];
  168. for (int j = 0; j < frames.size(); j++) {
  169. #ifndef DISABLE_DEPRECATED
  170. // For compatibility.
  171. Ref<Resource> res = frames[j];
  172. if (res.is_valid()) {
  173. Frame frame = { res, 1.0 };
  174. anim.frames.push_back(frame);
  175. continue;
  176. }
  177. #endif
  178. Dictionary f = frames[j];
  179. ERR_CONTINUE(!f.has("texture"));
  180. ERR_CONTINUE(!f.has("duration"));
  181. Frame frame = { f["texture"], MAX(SPRITE_FRAME_MINIMUM_DURATION, (float)f["duration"]) };
  182. anim.frames.push_back(frame);
  183. }
  184. animations[d["name"]] = anim;
  185. }
  186. }
  187. #ifdef TOOLS_ENABLED
  188. void SpriteFrames::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  189. const String pf = p_function;
  190. if (p_idx == 0) {
  191. if (pf == "has_animation" || pf == "remove_animation" || pf == "rename_animation" ||
  192. pf == "set_animation_speed" || pf == "get_animation_speed" ||
  193. pf == "set_animation_loop" || pf == "get_animation_loop" ||
  194. pf == "add_frame" || pf == "set_frame" || pf == "remove_frame" ||
  195. pf == "get_frame_count" || pf == "get_frame_texture" || pf == "get_frame_duration" ||
  196. pf == "clear") {
  197. for (const String &E : get_animation_names()) {
  198. r_options->push_back(E.quote());
  199. }
  200. }
  201. }
  202. Resource::get_argument_options(p_function, p_idx, r_options);
  203. }
  204. #endif
  205. void SpriteFrames::_bind_methods() {
  206. ClassDB::bind_method(D_METHOD("add_animation", "anim"), &SpriteFrames::add_animation);
  207. ClassDB::bind_method(D_METHOD("has_animation", "anim"), &SpriteFrames::has_animation);
  208. ClassDB::bind_method(D_METHOD("duplicate_animation", "anim_from", "anim_to"), &SpriteFrames::duplicate_animation);
  209. ClassDB::bind_method(D_METHOD("remove_animation", "anim"), &SpriteFrames::remove_animation);
  210. ClassDB::bind_method(D_METHOD("rename_animation", "anim", "newname"), &SpriteFrames::rename_animation);
  211. ClassDB::bind_method(D_METHOD("get_animation_names"), &SpriteFrames::get_animation_names);
  212. ClassDB::bind_method(D_METHOD("set_animation_speed", "anim", "fps"), &SpriteFrames::set_animation_speed);
  213. ClassDB::bind_method(D_METHOD("get_animation_speed", "anim"), &SpriteFrames::get_animation_speed);
  214. ClassDB::bind_method(D_METHOD("set_animation_loop", "anim", "loop"), &SpriteFrames::set_animation_loop);
  215. ClassDB::bind_method(D_METHOD("get_animation_loop", "anim"), &SpriteFrames::get_animation_loop);
  216. ClassDB::bind_method(D_METHOD("add_frame", "anim", "texture", "duration", "at_position"), &SpriteFrames::add_frame, DEFVAL(1.0), DEFVAL(-1));
  217. ClassDB::bind_method(D_METHOD("set_frame", "anim", "idx", "texture", "duration"), &SpriteFrames::set_frame, DEFVAL(1.0));
  218. ClassDB::bind_method(D_METHOD("remove_frame", "anim", "idx"), &SpriteFrames::remove_frame);
  219. ClassDB::bind_method(D_METHOD("get_frame_count", "anim"), &SpriteFrames::get_frame_count);
  220. ClassDB::bind_method(D_METHOD("get_frame_texture", "anim", "idx"), &SpriteFrames::get_frame_texture);
  221. ClassDB::bind_method(D_METHOD("get_frame_duration", "anim", "idx"), &SpriteFrames::get_frame_duration);
  222. ClassDB::bind_method(D_METHOD("clear", "anim"), &SpriteFrames::clear);
  223. ClassDB::bind_method(D_METHOD("clear_all"), &SpriteFrames::clear_all);
  224. // `animations` property is for serialization.
  225. ClassDB::bind_method(D_METHOD("_set_animations", "animations"), &SpriteFrames::_set_animations);
  226. ClassDB::bind_method(D_METHOD("_get_animations"), &SpriteFrames::_get_animations);
  227. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations", "_get_animations");
  228. }
  229. SpriteFrames::SpriteFrames() {
  230. add_animation(SceneStringName(default_));
  231. }