sprite.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*************************************************************************/
  2. /* sprite.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 "sprite.h"
  30. #include "core/core_string_names.h"
  31. #include "scene/scene_string_names.h"
  32. void Sprite::edit_set_pivot(const Point2& p_pivot) {
  33. set_offset(p_pivot);
  34. }
  35. Point2 Sprite::edit_get_pivot() const {
  36. return get_offset();
  37. }
  38. bool Sprite::edit_has_pivot() const {
  39. return true;
  40. }
  41. void Sprite::_notification(int p_what) {
  42. switch(p_what) {
  43. case NOTIFICATION_DRAW: {
  44. if (texture.is_null())
  45. return;
  46. RID ci = get_canvas_item();
  47. /*
  48. texture->draw(ci,Point2());
  49. break;
  50. */
  51. Size2i s;
  52. Rect2i src_rect;
  53. if (region) {
  54. s=region_rect.size;
  55. src_rect=region_rect;
  56. } else {
  57. s = texture->get_size();
  58. s=s/Size2i(hframes,vframes);
  59. src_rect.size=s;
  60. src_rect.pos.x+=(frame%hframes)*s.x;
  61. src_rect.pos.y+=(frame/hframes)*s.y;
  62. }
  63. Point2i ofs=offset;
  64. if (centered)
  65. ofs-=s/2;
  66. Rect2i dst_rect(ofs,s);
  67. if (hflip)
  68. dst_rect.size.x=-dst_rect.size.x;
  69. if (vflip)
  70. dst_rect.size.y=-dst_rect.size.y;
  71. texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  72. } break;
  73. }
  74. }
  75. void Sprite::set_texture(const Ref<Texture>& p_texture) {
  76. if (p_texture==texture)
  77. return;
  78. if (texture.is_valid()) {
  79. texture->disconnect(CoreStringNames::get_singleton()->changed,this,SceneStringNames::get_singleton()->update);
  80. }
  81. texture=p_texture;
  82. if (texture.is_valid()) {
  83. texture->set_flags(texture->get_flags()); //remove repeat from texture, it looks bad in sprites
  84. texture->connect(CoreStringNames::get_singleton()->changed,this,SceneStringNames::get_singleton()->update);
  85. }
  86. update();
  87. item_rect_changed();
  88. }
  89. Ref<Texture> Sprite::get_texture() const {
  90. return texture;
  91. }
  92. void Sprite::set_centered(bool p_center) {
  93. centered=p_center;
  94. update();
  95. item_rect_changed();
  96. }
  97. bool Sprite::is_centered() const {
  98. return centered;
  99. }
  100. void Sprite::set_offset(const Point2& p_offset) {
  101. offset=p_offset;
  102. update();
  103. item_rect_changed();
  104. }
  105. Point2 Sprite::get_offset() const {
  106. return offset;
  107. }
  108. void Sprite::set_flip_h(bool p_flip) {
  109. hflip=p_flip;
  110. update();
  111. }
  112. bool Sprite::is_flipped_h() const {
  113. return hflip;
  114. }
  115. void Sprite::set_flip_v(bool p_flip) {
  116. vflip=p_flip;
  117. update();
  118. }
  119. bool Sprite::is_flipped_v() const {
  120. return vflip;
  121. }
  122. void Sprite::set_region(bool p_region) {
  123. if (p_region==region)
  124. return;
  125. region=p_region;
  126. update();
  127. }
  128. bool Sprite::is_region() const{
  129. return region;
  130. }
  131. void Sprite::set_region_rect(const Rect2& p_region_rect) {
  132. bool changed=region_rect!=p_region_rect;
  133. region_rect=p_region_rect;
  134. if (region && changed) {
  135. update();
  136. item_rect_changed();
  137. }
  138. }
  139. Rect2 Sprite::get_region_rect() const {
  140. return region_rect;
  141. }
  142. void Sprite::set_frame(int p_frame) {
  143. ERR_FAIL_INDEX(p_frame,vframes*hframes);
  144. if (frame != p_frame)
  145. item_rect_changed();
  146. frame=p_frame;
  147. }
  148. int Sprite::get_frame() const {
  149. return frame;
  150. }
  151. void Sprite::set_vframes(int p_amount) {
  152. ERR_FAIL_COND(p_amount<1);
  153. vframes=p_amount;
  154. update();
  155. item_rect_changed();
  156. _change_notify("frame");
  157. }
  158. int Sprite::get_vframes() const {
  159. return vframes;
  160. }
  161. void Sprite::set_hframes(int p_amount) {
  162. ERR_FAIL_COND(p_amount<1);
  163. hframes=p_amount;
  164. update();
  165. item_rect_changed();
  166. _change_notify("frame");
  167. }
  168. int Sprite::get_hframes() const {
  169. return hframes;
  170. }
  171. void Sprite::set_modulate(const Color& p_color) {
  172. modulate=p_color;
  173. update();
  174. }
  175. Color Sprite::get_modulate() const{
  176. return modulate;
  177. }
  178. Rect2 Sprite::get_item_rect() const {
  179. if (texture.is_null())
  180. return Rect2(0,0,1,1);
  181. //if (texture.is_null())
  182. // return CanvasItem::get_item_rect();
  183. Size2i s;
  184. if (region) {
  185. s=region_rect.size;
  186. } else {
  187. s = texture->get_size();
  188. s=s/Point2(hframes,vframes);
  189. }
  190. Point2i ofs=offset;
  191. if (centered)
  192. ofs-=s/2;
  193. if (s==Size2(0,0))
  194. s=Size2(1,1);
  195. return Rect2(ofs,s);
  196. }
  197. void Sprite::_bind_methods() {
  198. ObjectTypeDB::bind_method(_MD("set_texture","texture:Texture"),&Sprite::set_texture);
  199. ObjectTypeDB::bind_method(_MD("get_texture:Texture"),&Sprite::get_texture);
  200. ObjectTypeDB::bind_method(_MD("set_centered","centered"),&Sprite::set_centered);
  201. ObjectTypeDB::bind_method(_MD("is_centered"),&Sprite::is_centered);
  202. ObjectTypeDB::bind_method(_MD("set_offset","offset"),&Sprite::set_offset);
  203. ObjectTypeDB::bind_method(_MD("get_offset"),&Sprite::get_offset);
  204. ObjectTypeDB::bind_method(_MD("set_flip_h","flip_h"),&Sprite::set_flip_h);
  205. ObjectTypeDB::bind_method(_MD("is_flipped_h"),&Sprite::is_flipped_h);
  206. ObjectTypeDB::bind_method(_MD("set_flip_v","flip_v"),&Sprite::set_flip_v);
  207. ObjectTypeDB::bind_method(_MD("is_flipped_v"),&Sprite::is_flipped_v);
  208. ObjectTypeDB::bind_method(_MD("set_region","enabled"),&Sprite::set_region);
  209. ObjectTypeDB::bind_method(_MD("is_region"),&Sprite::is_region);
  210. ObjectTypeDB::bind_method(_MD("set_region_rect","rect"),&Sprite::set_region_rect);
  211. ObjectTypeDB::bind_method(_MD("get_region_rect"),&Sprite::get_region_rect);
  212. ObjectTypeDB::bind_method(_MD("set_frame","frame"),&Sprite::set_frame);
  213. ObjectTypeDB::bind_method(_MD("get_frame"),&Sprite::get_frame);
  214. ObjectTypeDB::bind_method(_MD("set_vframes","vframes"),&Sprite::set_vframes);
  215. ObjectTypeDB::bind_method(_MD("get_vframes"),&Sprite::get_vframes);
  216. ObjectTypeDB::bind_method(_MD("set_hframes","hframes"),&Sprite::set_hframes);
  217. ObjectTypeDB::bind_method(_MD("get_hframes"),&Sprite::get_hframes);
  218. ObjectTypeDB::bind_method(_MD("set_modulate","modulate"),&Sprite::set_modulate);
  219. ObjectTypeDB::bind_method(_MD("get_modulate"),&Sprite::get_modulate);
  220. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_texture"),_SCS("get_texture"));
  221. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "centered"), _SCS("set_centered"),_SCS("is_centered"));
  222. ADD_PROPERTY( PropertyInfo( Variant::VECTOR2, "offset"), _SCS("set_offset"),_SCS("get_offset"));
  223. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "flip_h"), _SCS("set_flip_h"),_SCS("is_flipped_h"));
  224. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "flip_v"), _SCS("set_flip_v"),_SCS("is_flipped_v"));
  225. ADD_PROPERTY( PropertyInfo( Variant::INT, "vframes"), _SCS("set_vframes"),_SCS("get_vframes"));
  226. ADD_PROPERTY( PropertyInfo( Variant::INT, "hframes"), _SCS("set_hframes"),_SCS("get_hframes"));
  227. ADD_PROPERTY( PropertyInfo( Variant::INT, "frame"), _SCS("set_frame"),_SCS("get_frame"));
  228. ADD_PROPERTY( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate"));
  229. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "region"), _SCS("set_region"),_SCS("is_region"));
  230. ADD_PROPERTY( PropertyInfo( Variant::RECT2, "region_rect"), _SCS("set_region_rect"),_SCS("get_region_rect"));
  231. }
  232. Sprite::Sprite() {
  233. centered=true;
  234. hflip=false;
  235. vflip=false;
  236. region=false;
  237. frame=0;
  238. vframes=1;
  239. hframes=1;
  240. modulate=Color(1,1,1,1);
  241. }