sprite.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*************************************************************************/
  2. /* sprite.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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. if (region_rect==p_region_rect)
  143. return;
  144. region_rect=p_region_rect;
  145. if (region)
  146. item_rect_changed();
  147. _change_notify("region_rect");
  148. }
  149. Rect2 Sprite::get_region_rect() const {
  150. return region_rect;
  151. }
  152. void Sprite::set_frame(int p_frame) {
  153. ERR_FAIL_INDEX(p_frame,vframes*hframes);
  154. if (frame != p_frame)
  155. item_rect_changed();
  156. frame=p_frame;
  157. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  158. }
  159. int Sprite::get_frame() const {
  160. return frame;
  161. }
  162. void Sprite::set_vframes(int p_amount) {
  163. ERR_FAIL_COND(p_amount<1);
  164. vframes=p_amount;
  165. update();
  166. item_rect_changed();
  167. _change_notify("frame");
  168. }
  169. int Sprite::get_vframes() const {
  170. return vframes;
  171. }
  172. void Sprite::set_hframes(int p_amount) {
  173. ERR_FAIL_COND(p_amount<1);
  174. hframes=p_amount;
  175. update();
  176. item_rect_changed();
  177. _change_notify("frame");
  178. }
  179. int Sprite::get_hframes() const {
  180. return hframes;
  181. }
  182. void Sprite::set_modulate(const Color& p_color) {
  183. modulate=p_color;
  184. update();
  185. }
  186. Color Sprite::get_modulate() const{
  187. return modulate;
  188. }
  189. Rect2 Sprite::get_item_rect() const {
  190. if (texture.is_null())
  191. return Rect2(0,0,1,1);
  192. //if (texture.is_null())
  193. // return CanvasItem::get_item_rect();
  194. Size2i s;
  195. if (region) {
  196. s=region_rect.size;
  197. } else {
  198. s = texture->get_size();
  199. s=s/Point2(hframes,vframes);
  200. }
  201. Point2 ofs=offset;
  202. if (centered)
  203. ofs-=s/2;
  204. if (s==Size2(0,0))
  205. s=Size2(1,1);
  206. return Rect2(ofs,s);
  207. }
  208. void Sprite::_bind_methods() {
  209. ObjectTypeDB::bind_method(_MD("set_texture","texture:Texture"),&Sprite::set_texture);
  210. ObjectTypeDB::bind_method(_MD("get_texture:Texture"),&Sprite::get_texture);
  211. ObjectTypeDB::bind_method(_MD("set_centered","centered"),&Sprite::set_centered);
  212. ObjectTypeDB::bind_method(_MD("is_centered"),&Sprite::is_centered);
  213. ObjectTypeDB::bind_method(_MD("set_offset","offset"),&Sprite::set_offset);
  214. ObjectTypeDB::bind_method(_MD("get_offset"),&Sprite::get_offset);
  215. ObjectTypeDB::bind_method(_MD("set_flip_h","flip_h"),&Sprite::set_flip_h);
  216. ObjectTypeDB::bind_method(_MD("is_flipped_h"),&Sprite::is_flipped_h);
  217. ObjectTypeDB::bind_method(_MD("set_flip_v","flip_v"),&Sprite::set_flip_v);
  218. ObjectTypeDB::bind_method(_MD("is_flipped_v"),&Sprite::is_flipped_v);
  219. ObjectTypeDB::bind_method(_MD("set_region","enabled"),&Sprite::set_region);
  220. ObjectTypeDB::bind_method(_MD("is_region"),&Sprite::is_region);
  221. ObjectTypeDB::bind_method(_MD("set_region_rect","rect"),&Sprite::set_region_rect);
  222. ObjectTypeDB::bind_method(_MD("get_region_rect"),&Sprite::get_region_rect);
  223. ObjectTypeDB::bind_method(_MD("set_frame","frame"),&Sprite::set_frame);
  224. ObjectTypeDB::bind_method(_MD("get_frame"),&Sprite::get_frame);
  225. ObjectTypeDB::bind_method(_MD("set_vframes","vframes"),&Sprite::set_vframes);
  226. ObjectTypeDB::bind_method(_MD("get_vframes"),&Sprite::get_vframes);
  227. ObjectTypeDB::bind_method(_MD("set_hframes","hframes"),&Sprite::set_hframes);
  228. ObjectTypeDB::bind_method(_MD("get_hframes"),&Sprite::get_hframes);
  229. ObjectTypeDB::bind_method(_MD("set_modulate","modulate"),&Sprite::set_modulate);
  230. ObjectTypeDB::bind_method(_MD("get_modulate"),&Sprite::get_modulate);
  231. ADD_SIGNAL(MethodInfo("frame_changed"));
  232. ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_texture"),_SCS("get_texture"));
  233. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "centered"), _SCS("set_centered"),_SCS("is_centered"));
  234. ADD_PROPERTYNZ( PropertyInfo( Variant::VECTOR2, "offset"), _SCS("set_offset"),_SCS("get_offset"));
  235. ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "flip_h"), _SCS("set_flip_h"),_SCS("is_flipped_h"));
  236. ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "flip_v"), _SCS("set_flip_v"),_SCS("is_flipped_v"));
  237. ADD_PROPERTYNO( PropertyInfo( Variant::INT, "vframes",PROPERTY_HINT_RANGE,"1,16384,1"), _SCS("set_vframes"),_SCS("get_vframes"));
  238. ADD_PROPERTYNO( PropertyInfo( Variant::INT, "hframes",PROPERTY_HINT_RANGE,"1,16384,1"), _SCS("set_hframes"),_SCS("get_hframes"));
  239. ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "frame",PROPERTY_HINT_SPRITE_FRAME), _SCS("set_frame"),_SCS("get_frame"));
  240. ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate"));
  241. ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "region"), _SCS("set_region"),_SCS("is_region"));
  242. ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), _SCS("set_region_rect"),_SCS("get_region_rect"));
  243. }
  244. Sprite::Sprite() {
  245. centered=true;
  246. hflip=false;
  247. vflip=false;
  248. region=false;
  249. frame=0;
  250. vframes=1;
  251. hframes=1;
  252. modulate=Color(1,1,1,1);
  253. }
  254. //////////////////////////// VPSPRITE
  255. ///
  256. ///
  257. ///
  258. void ViewportSprite::edit_set_pivot(const Point2& p_pivot) {
  259. set_offset(p_pivot);
  260. }
  261. Point2 ViewportSprite::edit_get_pivot() const {
  262. return get_offset();
  263. }
  264. bool ViewportSprite::edit_has_pivot() const {
  265. return true;
  266. }
  267. void ViewportSprite::_notification(int p_what) {
  268. switch(p_what) {
  269. case NOTIFICATION_ENTER_TREE: {
  270. if (!viewport_path.is_empty()) {
  271. Node *n = get_node(viewport_path);
  272. ERR_FAIL_COND(!n);
  273. Viewport *vp=n->cast_to<Viewport>();
  274. ERR_FAIL_COND(!vp);
  275. Ref<RenderTargetTexture> rtt = vp->get_render_target_texture();
  276. texture=rtt;
  277. texture->connect("changed",this,"update");
  278. item_rect_changed();
  279. }
  280. } break;
  281. case NOTIFICATION_EXIT_TREE: {
  282. if (texture.is_valid()) {
  283. texture->disconnect("changed",this,"update");
  284. texture=Ref<Texture>();
  285. }
  286. } break;
  287. case NOTIFICATION_DRAW: {
  288. if (texture.is_null())
  289. return;
  290. RID ci = get_canvas_item();
  291. /*
  292. texture->draw(ci,Point2());
  293. break;
  294. */
  295. Size2i s;
  296. Rect2i src_rect;
  297. s = texture->get_size();
  298. src_rect.size=s;
  299. Point2 ofs=offset;
  300. if (centered)
  301. ofs-=s/2;
  302. if (OS::get_singleton()->get_use_pixel_snap()) {
  303. ofs=ofs.floor();
  304. }
  305. Rect2 dst_rect(ofs,s);
  306. texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  307. } break;
  308. }
  309. }
  310. void ViewportSprite::set_viewport_path(const NodePath& p_viewport) {
  311. viewport_path=p_viewport;
  312. update();
  313. if (!is_inside_tree())
  314. return;
  315. if (texture.is_valid()) {
  316. texture->disconnect("changed",this,"update");
  317. texture=Ref<Texture>();
  318. }
  319. if (viewport_path.is_empty())
  320. return;
  321. Node *n = get_node(viewport_path);
  322. ERR_FAIL_COND(!n);
  323. Viewport *vp=n->cast_to<Viewport>();
  324. ERR_FAIL_COND(!vp);
  325. Ref<RenderTargetTexture> rtt = vp->get_render_target_texture();
  326. texture=rtt;
  327. if (texture.is_valid()) {
  328. texture->connect("changed",this,"update");
  329. }
  330. item_rect_changed();
  331. }
  332. NodePath ViewportSprite::get_viewport_path() const {
  333. return viewport_path;
  334. }
  335. void ViewportSprite::set_centered(bool p_center) {
  336. centered=p_center;
  337. update();
  338. item_rect_changed();
  339. }
  340. bool ViewportSprite::is_centered() const {
  341. return centered;
  342. }
  343. void ViewportSprite::set_offset(const Point2& p_offset) {
  344. offset=p_offset;
  345. update();
  346. item_rect_changed();
  347. }
  348. Point2 ViewportSprite::get_offset() const {
  349. return offset;
  350. }
  351. void ViewportSprite::set_modulate(const Color& p_color) {
  352. modulate=p_color;
  353. update();
  354. }
  355. Color ViewportSprite::get_modulate() const{
  356. return modulate;
  357. }
  358. Rect2 ViewportSprite::get_item_rect() const {
  359. if (texture.is_null())
  360. return Rect2(0,0,1,1);
  361. //if (texture.is_null())
  362. // return CanvasItem::get_item_rect();
  363. Size2i s;
  364. s = texture->get_size();
  365. Point2 ofs=offset;
  366. if (centered)
  367. ofs-=s/2;
  368. if (s==Size2(0,0))
  369. s=Size2(1,1);
  370. return Rect2(ofs,s);
  371. }
  372. void ViewportSprite::_bind_methods() {
  373. ObjectTypeDB::bind_method(_MD("set_viewport_path","path"),&ViewportSprite::set_viewport_path);
  374. ObjectTypeDB::bind_method(_MD("get_viewport_path"),&ViewportSprite::get_viewport_path);
  375. ObjectTypeDB::bind_method(_MD("set_centered","centered"),&ViewportSprite::set_centered);
  376. ObjectTypeDB::bind_method(_MD("is_centered"),&ViewportSprite::is_centered);
  377. ObjectTypeDB::bind_method(_MD("set_offset","offset"),&ViewportSprite::set_offset);
  378. ObjectTypeDB::bind_method(_MD("get_offset"),&ViewportSprite::get_offset);
  379. ObjectTypeDB::bind_method(_MD("set_modulate","modulate"),&ViewportSprite::set_modulate);
  380. ObjectTypeDB::bind_method(_MD("get_modulate"),&ViewportSprite::get_modulate);
  381. ADD_PROPERTYNZ( PropertyInfo( Variant::NODE_PATH, "viewport"), _SCS("set_viewport_path"),_SCS("get_viewport_path"));
  382. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "centered"), _SCS("set_centered"),_SCS("is_centered"));
  383. ADD_PROPERTYNZ( PropertyInfo( Variant::VECTOR2, "offset"), _SCS("set_offset"),_SCS("get_offset"));
  384. ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate"));
  385. }
  386. ViewportSprite::ViewportSprite() {
  387. centered=true;
  388. modulate=Color(1,1,1,1);
  389. }