sprite.cpp 13 KB

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