sprite.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*************************************************************************/
  2. /* 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 "sprite.h"
  30. #include "core/core_string_names.h"
  31. #include "scene/scene_string_names.h"
  32. #include "scene/main/viewport.h"
  33. void Sprite::edit_set_pivot(const Point2& p_pivot) {
  34. set_offset(p_pivot);
  35. }
  36. Point2 Sprite::edit_get_pivot() const {
  37. return get_offset();
  38. }
  39. bool Sprite::edit_has_pivot() const {
  40. return true;
  41. }
  42. void Sprite::_notification(int p_what) {
  43. switch(p_what) {
  44. case NOTIFICATION_DRAW: {
  45. if (texture.is_null())
  46. return;
  47. RID ci = get_canvas_item();
  48. /*
  49. texture->draw(ci,Point2());
  50. break;
  51. */
  52. Size2 s;
  53. Rect2 src_rect;
  54. if (region) {
  55. s=region_rect.size;
  56. src_rect=region_rect;
  57. } else {
  58. s = Size2(texture->get_size());
  59. s=s/Size2(hframes,vframes);
  60. src_rect.size=s;
  61. src_rect.pos.x+=float(frame%hframes)*s.x;
  62. src_rect.pos.y+=float(frame/hframes)*s.y;
  63. }
  64. Point2 ofs=offset;
  65. if (centered)
  66. ofs-=s/2;
  67. Rect2 dst_rect(ofs,s);
  68. if (hflip)
  69. dst_rect.size.x=-dst_rect.size.x;
  70. if (vflip)
  71. dst_rect.size.y=-dst_rect.size.y;
  72. texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  73. } break;
  74. }
  75. }
  76. void Sprite::set_texture(const Ref<Texture>& p_texture) {
  77. if (p_texture==texture)
  78. return;
  79. #ifdef DEBUG_ENABLED
  80. if (texture.is_valid()) {
  81. texture->disconnect(CoreStringNames::get_singleton()->changed,this,SceneStringNames::get_singleton()->update);
  82. }
  83. #endif
  84. texture=p_texture;
  85. #ifdef DEBUG_ENABLED
  86. if (texture.is_valid()) {
  87. texture->set_flags(texture->get_flags()); //remove repeat from texture, it looks bad in sprites
  88. texture->connect(CoreStringNames::get_singleton()->changed,this,SceneStringNames::get_singleton()->update);
  89. }
  90. #endif
  91. update();
  92. item_rect_changed();
  93. }
  94. Ref<Texture> Sprite::get_texture() const {
  95. return texture;
  96. }
  97. void Sprite::set_centered(bool p_center) {
  98. centered=p_center;
  99. update();
  100. item_rect_changed();
  101. }
  102. bool Sprite::is_centered() const {
  103. return centered;
  104. }
  105. void Sprite::set_offset(const Point2& p_offset) {
  106. offset=p_offset;
  107. update();
  108. item_rect_changed();
  109. _change_notify("offset");
  110. }
  111. Point2 Sprite::get_offset() const {
  112. return offset;
  113. }
  114. void Sprite::set_flip_h(bool p_flip) {
  115. hflip=p_flip;
  116. update();
  117. }
  118. bool Sprite::is_flipped_h() const {
  119. return hflip;
  120. }
  121. void Sprite::set_flip_v(bool p_flip) {
  122. vflip=p_flip;
  123. update();
  124. }
  125. bool Sprite::is_flipped_v() const {
  126. return vflip;
  127. }
  128. void Sprite::set_region(bool p_region) {
  129. if (p_region==region)
  130. return;
  131. region=p_region;
  132. update();
  133. }
  134. bool Sprite::is_region() const{
  135. return region;
  136. }
  137. void Sprite::set_region_rect(const Rect2& p_region_rect) {
  138. bool changed=region_rect!=p_region_rect;
  139. region_rect=p_region_rect;
  140. if (region && changed) {
  141. update();
  142. item_rect_changed();
  143. _change_notify("region_rect");
  144. }
  145. }
  146. Rect2 Sprite::get_region_rect() const {
  147. return region_rect;
  148. }
  149. void Sprite::set_frame(int p_frame) {
  150. ERR_FAIL_INDEX(p_frame,vframes*hframes);
  151. if (frame != p_frame)
  152. item_rect_changed();
  153. frame=p_frame;
  154. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  155. }
  156. int Sprite::get_frame() const {
  157. return frame;
  158. }
  159. void Sprite::set_vframes(int p_amount) {
  160. ERR_FAIL_COND(p_amount<1);
  161. vframes=p_amount;
  162. update();
  163. item_rect_changed();
  164. _change_notify("frame");
  165. }
  166. int Sprite::get_vframes() const {
  167. return vframes;
  168. }
  169. void Sprite::set_hframes(int p_amount) {
  170. ERR_FAIL_COND(p_amount<1);
  171. hframes=p_amount;
  172. update();
  173. item_rect_changed();
  174. _change_notify("frame");
  175. }
  176. int Sprite::get_hframes() const {
  177. return hframes;
  178. }
  179. void Sprite::set_modulate(const Color& p_color) {
  180. modulate=p_color;
  181. update();
  182. }
  183. Color Sprite::get_modulate() const{
  184. return modulate;
  185. }
  186. Rect2 Sprite::get_item_rect() const {
  187. if (texture.is_null())
  188. return Rect2(0,0,1,1);
  189. //if (texture.is_null())
  190. // return CanvasItem::get_item_rect();
  191. Size2i s;
  192. if (region) {
  193. s=region_rect.size;
  194. } else {
  195. s = texture->get_size();
  196. s=s/Point2(hframes,vframes);
  197. }
  198. Point2 ofs=offset;
  199. if (centered)
  200. ofs-=s/2;
  201. if (s==Size2(0,0))
  202. s=Size2(1,1);
  203. return Rect2(ofs,s);
  204. }
  205. void Sprite::_bind_methods() {
  206. ObjectTypeDB::bind_method(_MD("set_texture","texture:Texture"),&Sprite::set_texture);
  207. ObjectTypeDB::bind_method(_MD("get_texture:Texture"),&Sprite::get_texture);
  208. ObjectTypeDB::bind_method(_MD("set_centered","centered"),&Sprite::set_centered);
  209. ObjectTypeDB::bind_method(_MD("is_centered"),&Sprite::is_centered);
  210. ObjectTypeDB::bind_method(_MD("set_offset","offset"),&Sprite::set_offset);
  211. ObjectTypeDB::bind_method(_MD("get_offset"),&Sprite::get_offset);
  212. ObjectTypeDB::bind_method(_MD("set_flip_h","flip_h"),&Sprite::set_flip_h);
  213. ObjectTypeDB::bind_method(_MD("is_flipped_h"),&Sprite::is_flipped_h);
  214. ObjectTypeDB::bind_method(_MD("set_flip_v","flip_v"),&Sprite::set_flip_v);
  215. ObjectTypeDB::bind_method(_MD("is_flipped_v"),&Sprite::is_flipped_v);
  216. ObjectTypeDB::bind_method(_MD("set_region","enabled"),&Sprite::set_region);
  217. ObjectTypeDB::bind_method(_MD("is_region"),&Sprite::is_region);
  218. ObjectTypeDB::bind_method(_MD("set_region_rect","rect"),&Sprite::set_region_rect);
  219. ObjectTypeDB::bind_method(_MD("get_region_rect"),&Sprite::get_region_rect);
  220. ObjectTypeDB::bind_method(_MD("set_frame","frame"),&Sprite::set_frame);
  221. ObjectTypeDB::bind_method(_MD("get_frame"),&Sprite::get_frame);
  222. ObjectTypeDB::bind_method(_MD("set_vframes","vframes"),&Sprite::set_vframes);
  223. ObjectTypeDB::bind_method(_MD("get_vframes"),&Sprite::get_vframes);
  224. ObjectTypeDB::bind_method(_MD("set_hframes","hframes"),&Sprite::set_hframes);
  225. ObjectTypeDB::bind_method(_MD("get_hframes"),&Sprite::get_hframes);
  226. ObjectTypeDB::bind_method(_MD("set_modulate","modulate"),&Sprite::set_modulate);
  227. ObjectTypeDB::bind_method(_MD("get_modulate"),&Sprite::get_modulate);
  228. ADD_SIGNAL(MethodInfo("frame_changed"));
  229. ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_texture"),_SCS("get_texture"));
  230. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "centered"), _SCS("set_centered"),_SCS("is_centered"));
  231. ADD_PROPERTYNZ( PropertyInfo( Variant::VECTOR2, "offset"), _SCS("set_offset"),_SCS("get_offset"));
  232. ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "flip_h"), _SCS("set_flip_h"),_SCS("is_flipped_h"));
  233. ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "flip_v"), _SCS("set_flip_v"),_SCS("is_flipped_v"));
  234. ADD_PROPERTYNO( PropertyInfo( Variant::INT, "vframes",PROPERTY_HINT_RANGE,"1,16384,1"), _SCS("set_vframes"),_SCS("get_vframes"));
  235. ADD_PROPERTYNO( PropertyInfo( Variant::INT, "hframes",PROPERTY_HINT_RANGE,"1,16384,1"), _SCS("set_hframes"),_SCS("get_hframes"));
  236. ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "frame",PROPERTY_HINT_SPRITE_FRAME), _SCS("set_frame"),_SCS("get_frame"));
  237. ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate"));
  238. ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "region"), _SCS("set_region"),_SCS("is_region"));
  239. ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), _SCS("set_region_rect"),_SCS("get_region_rect"));
  240. }
  241. Sprite::Sprite() {
  242. centered=true;
  243. hflip=false;
  244. vflip=false;
  245. region=false;
  246. frame=0;
  247. vframes=1;
  248. hframes=1;
  249. modulate=Color(1,1,1,1);
  250. }
  251. //////////////////////////// VPSPRITE
  252. ///
  253. ///
  254. ///
  255. void ViewportSprite::edit_set_pivot(const Point2& p_pivot) {
  256. set_offset(p_pivot);
  257. }
  258. Point2 ViewportSprite::edit_get_pivot() const {
  259. return get_offset();
  260. }
  261. bool ViewportSprite::edit_has_pivot() const {
  262. return true;
  263. }
  264. void ViewportSprite::_notification(int p_what) {
  265. switch(p_what) {
  266. case NOTIFICATION_ENTER_TREE: {
  267. if (!viewport_path.is_empty()) {
  268. Node *n = get_node(viewport_path);
  269. ERR_FAIL_COND(!n);
  270. Viewport *vp=n->cast_to<Viewport>();
  271. ERR_FAIL_COND(!vp);
  272. Ref<RenderTargetTexture> rtt = vp->get_render_target_texture();
  273. texture=rtt;
  274. texture->connect("changed",this,"update");
  275. item_rect_changed();
  276. }
  277. } break;
  278. case NOTIFICATION_EXIT_TREE: {
  279. if (texture.is_valid()) {
  280. texture->disconnect("changed",this,"update");
  281. texture=Ref<Texture>();
  282. }
  283. } break;
  284. case NOTIFICATION_DRAW: {
  285. if (texture.is_null())
  286. return;
  287. RID ci = get_canvas_item();
  288. /*
  289. texture->draw(ci,Point2());
  290. break;
  291. */
  292. Size2i s;
  293. Rect2i src_rect;
  294. s = texture->get_size();
  295. src_rect.size=s;
  296. Point2 ofs=offset;
  297. if (centered)
  298. ofs-=s/2;
  299. Rect2 dst_rect(ofs,s);
  300. texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  301. } break;
  302. }
  303. }
  304. void ViewportSprite::set_viewport_path(const NodePath& p_viewport) {
  305. viewport_path=p_viewport;
  306. update();
  307. if (!is_inside_tree())
  308. return;
  309. if (texture.is_valid()) {
  310. texture->disconnect("changed",this,"update");
  311. texture=Ref<Texture>();
  312. }
  313. if (viewport_path.is_empty())
  314. return;
  315. Node *n = get_node(viewport_path);
  316. ERR_FAIL_COND(!n);
  317. Viewport *vp=n->cast_to<Viewport>();
  318. ERR_FAIL_COND(!vp);
  319. Ref<RenderTargetTexture> rtt = vp->get_render_target_texture();
  320. texture=rtt;
  321. if (texture.is_valid()) {
  322. texture->connect("changed",this,"update");
  323. }
  324. item_rect_changed();
  325. }
  326. NodePath ViewportSprite::get_viewport_path() const {
  327. return viewport_path;
  328. }
  329. void ViewportSprite::set_centered(bool p_center) {
  330. centered=p_center;
  331. update();
  332. item_rect_changed();
  333. }
  334. bool ViewportSprite::is_centered() const {
  335. return centered;
  336. }
  337. void ViewportSprite::set_offset(const Point2& p_offset) {
  338. offset=p_offset;
  339. update();
  340. item_rect_changed();
  341. }
  342. Point2 ViewportSprite::get_offset() const {
  343. return offset;
  344. }
  345. void ViewportSprite::set_modulate(const Color& p_color) {
  346. modulate=p_color;
  347. update();
  348. }
  349. Color ViewportSprite::get_modulate() const{
  350. return modulate;
  351. }
  352. Rect2 ViewportSprite::get_item_rect() const {
  353. if (texture.is_null())
  354. return Rect2(0,0,1,1);
  355. //if (texture.is_null())
  356. // return CanvasItem::get_item_rect();
  357. Size2i s;
  358. s = texture->get_size();
  359. Point2 ofs=offset;
  360. if (centered)
  361. ofs-=s/2;
  362. if (s==Size2(0,0))
  363. s=Size2(1,1);
  364. return Rect2(ofs,s);
  365. }
  366. void ViewportSprite::_bind_methods() {
  367. ObjectTypeDB::bind_method(_MD("set_viewport_path","path"),&ViewportSprite::set_viewport_path);
  368. ObjectTypeDB::bind_method(_MD("get_viewport_path"),&ViewportSprite::get_viewport_path);
  369. ObjectTypeDB::bind_method(_MD("set_centered","centered"),&ViewportSprite::set_centered);
  370. ObjectTypeDB::bind_method(_MD("is_centered"),&ViewportSprite::is_centered);
  371. ObjectTypeDB::bind_method(_MD("set_offset","offset"),&ViewportSprite::set_offset);
  372. ObjectTypeDB::bind_method(_MD("get_offset"),&ViewportSprite::get_offset);
  373. ObjectTypeDB::bind_method(_MD("set_modulate","modulate"),&ViewportSprite::set_modulate);
  374. ObjectTypeDB::bind_method(_MD("get_modulate"),&ViewportSprite::get_modulate);
  375. ADD_PROPERTYNZ( PropertyInfo( Variant::NODE_PATH, "viewport"), _SCS("set_viewport_path"),_SCS("get_viewport_path"));
  376. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "centered"), _SCS("set_centered"),_SCS("is_centered"));
  377. ADD_PROPERTYNZ( PropertyInfo( Variant::VECTOR2, "offset"), _SCS("set_offset"),_SCS("get_offset"));
  378. ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate"));
  379. }
  380. ViewportSprite::ViewportSprite() {
  381. centered=true;
  382. modulate=Color(1,1,1,1);
  383. }