animated_sprite.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*************************************************************************/
  2. /* animated_sprite.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "animated_sprite.h"
  30. #include "scene/scene_string_names.h"
  31. void AnimatedSprite::edit_set_pivot(const Point2& p_pivot) {
  32. set_offset(p_pivot);
  33. }
  34. Point2 AnimatedSprite::edit_get_pivot() const {
  35. return get_offset();
  36. }
  37. bool AnimatedSprite::edit_has_pivot() const {
  38. return true;
  39. }
  40. void SpriteFrames::add_frame(const Ref<Texture>& p_frame,int p_at_pos) {
  41. if (p_at_pos>=0 && p_at_pos<frames.size())
  42. frames.insert(p_at_pos,p_frame);
  43. else
  44. frames.push_back(p_frame);
  45. emit_changed();
  46. }
  47. int SpriteFrames::get_frame_count() const {
  48. return frames.size();
  49. }
  50. void SpriteFrames::remove_frame(int p_idx) {
  51. frames.remove(p_idx);
  52. emit_changed();
  53. }
  54. void SpriteFrames::clear() {
  55. frames.clear();
  56. emit_changed();
  57. }
  58. Array SpriteFrames::_get_frames() const {
  59. Array arr;
  60. arr.resize(frames.size());
  61. for(int i=0;i<frames.size();i++)
  62. arr[i]=frames[i];
  63. return arr;
  64. }
  65. void SpriteFrames::_set_frames(const Array& p_frames) {
  66. frames.resize(p_frames.size());
  67. for(int i=0;i<frames.size();i++)
  68. frames[i]=p_frames[i];
  69. }
  70. void SpriteFrames::_bind_methods() {
  71. ObjectTypeDB::bind_method(_MD("add_frame","frame","atpos"),&SpriteFrames::add_frame,DEFVAL(-1));
  72. ObjectTypeDB::bind_method(_MD("get_frame_count"),&SpriteFrames::get_frame_count);
  73. ObjectTypeDB::bind_method(_MD("get_frame","idx"),&SpriteFrames::get_frame);
  74. ObjectTypeDB::bind_method(_MD("set_frame","idx","txt"),&SpriteFrames::set_frame);
  75. ObjectTypeDB::bind_method(_MD("remove_frame","idx"),&SpriteFrames::remove_frame);
  76. ObjectTypeDB::bind_method(_MD("clear"),&SpriteFrames::clear);
  77. ObjectTypeDB::bind_method(_MD("_set_frames"),&SpriteFrames::_set_frames);
  78. ObjectTypeDB::bind_method(_MD("_get_frames"),&SpriteFrames::_get_frames);
  79. ADD_PROPERTYNZ( PropertyInfo(Variant::ARRAY,"frames",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_frames"),_SCS("_get_frames"));
  80. }
  81. SpriteFrames::SpriteFrames() {
  82. }
  83. //////////////////////////
  84. void AnimatedSprite::_notification(int p_what) {
  85. switch(p_what) {
  86. case NOTIFICATION_DRAW: {
  87. if (frames.is_null())
  88. return;
  89. if (frame<0 || frame>=frames->get_frame_count())
  90. return;
  91. Ref<Texture> texture = frames->get_frame(frame);
  92. if (texture.is_null())
  93. return;
  94. //print_line("DECIDED TO DRAW");
  95. RID ci = get_canvas_item();
  96. /*
  97. texture->draw(ci,Point2());
  98. break;
  99. */
  100. Size2i s;
  101. s = texture->get_size();
  102. Point2i ofs=offset;
  103. if (centered)
  104. ofs-=s/2;
  105. Rect2i dst_rect(ofs,s);
  106. if (hflip)
  107. dst_rect.size.x=-dst_rect.size.x;
  108. if (vflip)
  109. dst_rect.size.y=-dst_rect.size.y;
  110. texture->draw_rect(ci,dst_rect,false,modulate);
  111. // VisualServer::get_singleton()->canvas_item_add_texture_rect_region(ci,dst_rect,texture->get_rid(),src_rect,modulate);
  112. } break;
  113. }
  114. }
  115. void AnimatedSprite::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
  116. if (frames.is_valid())
  117. frames->disconnect("changed",this,"_res_changed");
  118. frames=p_frames;
  119. if (frames.is_valid())
  120. frames->connect("changed",this,"_res_changed");
  121. if (!frames.is_valid()) {
  122. frame=0;
  123. } else {
  124. set_frame(frame);
  125. }
  126. update();
  127. }
  128. Ref<SpriteFrames> AnimatedSprite::get_sprite_frames() const {
  129. return frames;
  130. }
  131. void AnimatedSprite::set_frame(int p_frame) {
  132. if (!frames.is_valid()) {
  133. return;
  134. }
  135. if (p_frame>=frames->get_frame_count())
  136. p_frame=frames->get_frame_count()-1;
  137. if (p_frame<0)
  138. p_frame=0;
  139. if (frame==p_frame)
  140. return;
  141. frame=p_frame;
  142. update();
  143. _change_notify("frame");
  144. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  145. }
  146. int AnimatedSprite::get_frame() const {
  147. return frame;
  148. }
  149. void AnimatedSprite::set_centered(bool p_center) {
  150. centered=p_center;
  151. update();
  152. item_rect_changed();
  153. }
  154. bool AnimatedSprite::is_centered() const {
  155. return centered;
  156. }
  157. void AnimatedSprite::set_offset(const Point2& p_offset) {
  158. offset=p_offset;
  159. update();
  160. item_rect_changed();
  161. _change_notify("offset");
  162. }
  163. Point2 AnimatedSprite::get_offset() const {
  164. return offset;
  165. }
  166. void AnimatedSprite::set_flip_h(bool p_flip) {
  167. hflip=p_flip;
  168. update();
  169. }
  170. bool AnimatedSprite::is_flipped_h() const {
  171. return hflip;
  172. }
  173. void AnimatedSprite::set_flip_v(bool p_flip) {
  174. vflip=p_flip;
  175. update();
  176. }
  177. bool AnimatedSprite::is_flipped_v() const {
  178. return vflip;
  179. }
  180. void AnimatedSprite::set_modulate(const Color& p_color) {
  181. modulate=p_color;
  182. update();
  183. }
  184. Color AnimatedSprite::get_modulate() const{
  185. return modulate;
  186. }
  187. Rect2 AnimatedSprite::get_item_rect() const {
  188. if (!frames.is_valid() || !frames->get_frame_count() || frame<0 || frame>=frames->get_frame_count()) {
  189. return Node2D::get_item_rect();
  190. }
  191. Ref<Texture> t = frames->get_frame(frame);
  192. if (t.is_null())
  193. return Node2D::get_item_rect();
  194. Size2i s = t->get_size();
  195. Point2i ofs=offset;
  196. if (centered)
  197. ofs-=s/2;
  198. if (s==Size2(0,0))
  199. s=Size2(1,1);
  200. return Rect2(ofs,s);
  201. }
  202. void AnimatedSprite::_res_changed() {
  203. set_frame(frame);
  204. update();
  205. }
  206. void AnimatedSprite::_bind_methods() {
  207. ObjectTypeDB::bind_method(_MD("set_sprite_frames","sprite_frames:SpriteFrames"),&AnimatedSprite::set_sprite_frames);
  208. ObjectTypeDB::bind_method(_MD("get_sprite_frames:SpriteFrames"),&AnimatedSprite::get_sprite_frames);
  209. ObjectTypeDB::bind_method(_MD("set_centered","centered"),&AnimatedSprite::set_centered);
  210. ObjectTypeDB::bind_method(_MD("is_centered"),&AnimatedSprite::is_centered);
  211. ObjectTypeDB::bind_method(_MD("set_offset","offset"),&AnimatedSprite::set_offset);
  212. ObjectTypeDB::bind_method(_MD("get_offset"),&AnimatedSprite::get_offset);
  213. ObjectTypeDB::bind_method(_MD("set_flip_h","flip_h"),&AnimatedSprite::set_flip_h);
  214. ObjectTypeDB::bind_method(_MD("is_flipped_h"),&AnimatedSprite::is_flipped_h);
  215. ObjectTypeDB::bind_method(_MD("set_flip_v","flip_v"),&AnimatedSprite::set_flip_v);
  216. ObjectTypeDB::bind_method(_MD("is_flipped_v"),&AnimatedSprite::is_flipped_v);
  217. ObjectTypeDB::bind_method(_MD("set_frame","frame"),&AnimatedSprite::set_frame);
  218. ObjectTypeDB::bind_method(_MD("get_frame"),&AnimatedSprite::get_frame);
  219. ObjectTypeDB::bind_method(_MD("set_modulate","modulate"),&AnimatedSprite::set_modulate);
  220. ObjectTypeDB::bind_method(_MD("get_modulate"),&AnimatedSprite::get_modulate);
  221. ObjectTypeDB::bind_method(_MD("_res_changed"),&AnimatedSprite::_res_changed);
  222. ADD_SIGNAL(MethodInfo("frame_changed"));
  223. ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "frames",PROPERTY_HINT_RESOURCE_TYPE,"SpriteFrames"), _SCS("set_sprite_frames"),_SCS("get_sprite_frames"));
  224. ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "frame",PROPERTY_HINT_SPRITE_FRAME), _SCS("set_frame"),_SCS("get_frame"));
  225. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "centered"), _SCS("set_centered"),_SCS("is_centered"));
  226. ADD_PROPERTYNZ( PropertyInfo( Variant::VECTOR2, "offset"), _SCS("set_offset"),_SCS("get_offset"));
  227. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "flip_h"), _SCS("set_flip_h"),_SCS("is_flipped_h"));
  228. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "flip_v"), _SCS("set_flip_v"),_SCS("is_flipped_v"));
  229. ADD_PROPERTY( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate"));
  230. }
  231. AnimatedSprite::AnimatedSprite() {
  232. centered=true;
  233. hflip=false;
  234. vflip=false;
  235. frame=0;
  236. modulate=Color(1,1,1,1);
  237. }