style_box.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*************************************************************************/
  2. /* style_box.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 "style_box.h"
  30. bool StyleBox::test_mask(const Point2& p_point, const Rect2& p_rect) const {
  31. return true;
  32. }
  33. void StyleBox::set_default_margin(Margin p_margin, float p_value) {
  34. margin[p_margin]=p_value;
  35. emit_changed();
  36. }
  37. float StyleBox::get_default_margin(Margin p_margin) const{
  38. return margin[p_margin];
  39. }
  40. float StyleBox::get_margin(Margin p_margin) const {
  41. if (margin[p_margin]<0)
  42. return get_style_margin(p_margin);
  43. else
  44. return margin[p_margin];
  45. }
  46. Size2 StyleBox::get_minimum_size() const {
  47. return Size2( get_margin( MARGIN_LEFT) + get_margin( MARGIN_RIGHT ) , get_margin( MARGIN_TOP) + get_margin( MARGIN_BOTTOM ) );
  48. }
  49. Point2 StyleBox::get_offset() const {
  50. return Point2( get_margin( MARGIN_LEFT), get_margin( MARGIN_TOP) );
  51. }
  52. Size2 StyleBox::get_center_size() const {
  53. return Size2();
  54. }
  55. void StyleBox::_bind_methods() {
  56. ObjectTypeDB::bind_method(_MD("test_mask","point","rect"),&StyleBox::test_mask);
  57. ObjectTypeDB::bind_method(_MD("set_default_margin","margin","offset"),&StyleBox::set_default_margin);
  58. ObjectTypeDB::bind_method(_MD("get_default_margin","margin"),&StyleBox::get_default_margin);
  59. // ObjectTypeDB::bind_method(_MD("set_default_margin"),&StyleBox::set_default_margin);
  60. // ObjectTypeDB::bind_method(_MD("get_default_margin"),&StyleBox::get_default_margin);
  61. ObjectTypeDB::bind_method(_MD("get_margin","margin"),&StyleBox::get_margin);
  62. ObjectTypeDB::bind_method(_MD("get_minimum_size"),&StyleBox::get_minimum_size);
  63. ObjectTypeDB::bind_method(_MD("get_center_size"),&StyleBox::get_center_size);
  64. ObjectTypeDB::bind_method(_MD("get_offset"),&StyleBox::get_offset);
  65. ObjectTypeDB::bind_method(_MD("draw"),&StyleBox::draw);
  66. }
  67. StyleBox::StyleBox() {
  68. for (int i=0;i<4;i++) {
  69. margin[i]=-1;
  70. }
  71. }
  72. void StyleBoxTexture::set_texture(RES p_texture) {
  73. texture=p_texture;
  74. emit_changed();
  75. }
  76. RES StyleBoxTexture::get_texture() const {
  77. return texture;
  78. }
  79. void StyleBoxTexture::set_margin_size(Margin p_margin,float p_size) {
  80. margin[p_margin]=p_size;
  81. emit_changed();
  82. }
  83. float StyleBoxTexture::get_margin_size(Margin p_margin) const {
  84. return margin[p_margin];
  85. }
  86. float StyleBoxTexture::get_style_margin(Margin p_margin) const {
  87. return margin[p_margin];
  88. }
  89. void StyleBoxTexture::draw(RID p_canvas_item,const Rect2& p_rect) const {
  90. if (texture.is_null())
  91. return;
  92. Rect2 r=p_rect;
  93. r.pos.x-=expand_margin[MARGIN_LEFT];
  94. r.pos.y-=expand_margin[MARGIN_TOP];
  95. r.size.x+=expand_margin[MARGIN_LEFT]+expand_margin[MARGIN_RIGHT];
  96. r.size.y+=expand_margin[MARGIN_TOP]+expand_margin[MARGIN_BOTTOM];
  97. VisualServer::get_singleton()->canvas_item_add_style_box( p_canvas_item,r,texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),draw_center);
  98. }
  99. void StyleBoxTexture::set_draw_center(bool p_draw) {
  100. draw_center=p_draw;
  101. emit_changed();
  102. }
  103. bool StyleBoxTexture::get_draw_center() const {
  104. return draw_center;
  105. }
  106. Size2 StyleBoxTexture::get_center_size() const {
  107. if (texture.is_null())
  108. return Size2();
  109. return texture->get_size() - get_minimum_size();
  110. }
  111. void StyleBoxTexture::set_expand_margin_size(Margin p_expand_margin,float p_size) {
  112. ERR_FAIL_INDEX(p_expand_margin,4);
  113. expand_margin[p_expand_margin]=p_size;
  114. emit_changed();
  115. }
  116. float StyleBoxTexture::get_expand_margin_size(Margin p_expand_margin) const {
  117. ERR_FAIL_INDEX_V(p_expand_margin,4,0);
  118. return expand_margin[p_expand_margin];
  119. }
  120. void StyleBoxTexture::_bind_methods() {
  121. ObjectTypeDB::bind_method(_MD("set_texture","texture:Texture"),&StyleBoxTexture::set_texture);
  122. ObjectTypeDB::bind_method(_MD("get_texture:Texture"),&StyleBoxTexture::get_texture);
  123. ObjectTypeDB::bind_method(_MD("set_margin_size","margin","size"),&StyleBoxTexture::set_margin_size);
  124. ObjectTypeDB::bind_method(_MD("get_margin_size"),&StyleBoxTexture::get_margin_size);
  125. ObjectTypeDB::bind_method(_MD("set_expand_margin_size","margin","size"),&StyleBoxTexture::set_expand_margin_size);
  126. ObjectTypeDB::bind_method(_MD("get_expand_margin_size"),&StyleBoxTexture::get_expand_margin_size);
  127. ObjectTypeDB::bind_method(_MD("set_draw_center","enable"),&StyleBoxTexture::set_draw_center);
  128. ObjectTypeDB::bind_method(_MD("get_draw_center"),&StyleBoxTexture::get_draw_center);
  129. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture" ), _SCS("set_texture"),_SCS("get_texture") );
  130. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/left", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_LEFT );
  131. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/right", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_RIGHT );
  132. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/top", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_TOP);
  133. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/bottom", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_BOTTOM );
  134. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "expand_margin/left", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_expand_margin_size"),_SCS("get_expand_margin_size"), MARGIN_LEFT );
  135. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "expand_margin/right", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_expand_margin_size"),_SCS("get_expand_margin_size"), MARGIN_RIGHT );
  136. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "expand_margin/top", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_expand_margin_size"),_SCS("get_expand_margin_size"), MARGIN_TOP );
  137. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "expand_margin/bottom", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_expand_margin_size"),_SCS("get_expand_margin_size"), MARGIN_BOTTOM );
  138. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "draw_center" ) , _SCS("set_draw_center"),_SCS("get_draw_center"));
  139. }
  140. StyleBoxTexture::StyleBoxTexture() {
  141. for (int i=0;i<4;i++) {
  142. margin[i]=0;
  143. expand_margin[i]=0;
  144. }
  145. draw_center=true;
  146. }
  147. StyleBoxTexture::~StyleBoxTexture() {
  148. }
  149. ////////////////
  150. void StyleBoxFlat::set_bg_color(const Color& p_color) {
  151. bg_color=p_color;
  152. emit_changed();
  153. }
  154. void StyleBoxFlat::set_light_color(const Color& p_color) {
  155. light_color=p_color;
  156. emit_changed();
  157. }
  158. void StyleBoxFlat::set_dark_color(const Color& p_color) {
  159. dark_color=p_color;
  160. emit_changed();
  161. }
  162. Color StyleBoxFlat::get_bg_color() const {
  163. return bg_color;
  164. }
  165. Color StyleBoxFlat::get_light_color() const {
  166. return light_color;
  167. }
  168. Color StyleBoxFlat::get_dark_color() const {
  169. return dark_color;
  170. }
  171. void StyleBoxFlat::set_border_size(int p_size) {
  172. border_size=p_size;
  173. emit_changed();
  174. }
  175. int StyleBoxFlat::get_border_size() const {
  176. return border_size;
  177. }
  178. void StyleBoxFlat::set_border_blend(bool p_blend) {
  179. blend=p_blend;
  180. emit_changed();
  181. }
  182. bool StyleBoxFlat::get_border_blend() const {
  183. return blend;
  184. }
  185. void StyleBoxFlat::set_draw_center(bool p_draw) {
  186. draw_center=p_draw;
  187. emit_changed();
  188. }
  189. bool StyleBoxFlat::get_draw_center() const {
  190. return draw_center;
  191. }
  192. Size2 StyleBoxFlat::get_center_size() const {
  193. return Size2();
  194. }
  195. void StyleBoxFlat::draw(RID p_canvas_item,const Rect2& p_rect) const {
  196. VisualServer *vs = VisualServer::get_singleton();
  197. Rect2i r = p_rect;
  198. for (int i=0;i<border_size;i++) {
  199. Color color_upleft=light_color;
  200. Color color_downright=dark_color;
  201. if (blend) {
  202. color_upleft.r=(border_size-i)*color_upleft.r/border_size + i*bg_color.r/border_size;
  203. color_upleft.g=(border_size-i)*color_upleft.g/border_size + i*bg_color.g/border_size;
  204. color_upleft.b=(border_size-i)*color_upleft.b/border_size + i*bg_color.b/border_size;
  205. color_downright.r=(border_size-i)*color_downright.r/border_size + i*bg_color.r/border_size;
  206. color_downright.g=(border_size-i)*color_downright.g/border_size + i*bg_color.g/border_size;
  207. color_downright.b=(border_size-i)*color_downright.b/border_size + i*bg_color.b/border_size;
  208. }
  209. vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i( r.pos.x,r.pos.y+r.size.y-1), Size2(r.size.x ,1 )),color_downright);
  210. vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i( r.pos.x+r.size.x-1,r.pos.y ), Size2( 1 ,r.size.y )), color_downright);
  211. vs->canvas_item_add_rect(p_canvas_item, Rect2( r.pos, Size2(r.size.x ,1 )), color_upleft);
  212. vs->canvas_item_add_rect(p_canvas_item, Rect2( r.pos, Size2( 1 ,r.size.y )), color_upleft);
  213. r.pos.x++;
  214. r.pos.y++;
  215. r.size.x-=2;
  216. r.size.y-=2;
  217. }
  218. if (draw_center)
  219. vs->canvas_item_add_rect(p_canvas_item, Rect2( r.pos, r.size) , bg_color );
  220. }
  221. float StyleBoxFlat::get_style_margin(Margin p_margin) const {
  222. return border_size;
  223. }
  224. void StyleBoxFlat::_bind_methods() {
  225. ObjectTypeDB::bind_method(_MD("set_bg_color","color"),&StyleBoxFlat::set_bg_color);
  226. ObjectTypeDB::bind_method(_MD("get_bg_color"),&StyleBoxFlat::get_bg_color);
  227. ObjectTypeDB::bind_method(_MD("set_light_color","color"),&StyleBoxFlat::set_light_color);
  228. ObjectTypeDB::bind_method(_MD("get_light_color"),&StyleBoxFlat::get_light_color);
  229. ObjectTypeDB::bind_method(_MD("set_dark_color","color"),&StyleBoxFlat::set_dark_color);
  230. ObjectTypeDB::bind_method(_MD("get_dark_color"),&StyleBoxFlat::get_dark_color);
  231. ObjectTypeDB::bind_method(_MD("set_border_size","size"),&StyleBoxFlat::set_border_size);
  232. ObjectTypeDB::bind_method(_MD("get_border_size"),&StyleBoxFlat::get_border_size);
  233. ObjectTypeDB::bind_method(_MD("set_border_blend","blend"),&StyleBoxFlat::set_border_blend);
  234. ObjectTypeDB::bind_method(_MD("get_border_blend"),&StyleBoxFlat::get_border_blend);
  235. ObjectTypeDB::bind_method(_MD("set_draw_center","size"),&StyleBoxFlat::set_draw_center);
  236. ObjectTypeDB::bind_method(_MD("get_draw_center"),&StyleBoxFlat::get_draw_center);
  237. ADD_PROPERTY( PropertyInfo( Variant::COLOR, "bg_color"), _SCS("set_bg_color"),_SCS("get_bg_color") );
  238. ADD_PROPERTY( PropertyInfo( Variant::COLOR, "light_color"),_SCS("set_light_color"),_SCS("get_light_color"));
  239. ADD_PROPERTY( PropertyInfo( Variant::COLOR, "dark_color"),_SCS("set_dark_color"),_SCS("get_dark_color"));
  240. ADD_PROPERTY( PropertyInfo( Variant::INT, "border_size",PROPERTY_HINT_RANGE,"0,4096"),_SCS("set_border_size"),_SCS("get_border_size"));
  241. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "border_blend"),_SCS("set_border_blend"),_SCS("get_border_blend"));
  242. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "draw_bg"),_SCS("set_draw_center"),_SCS("get_draw_center"));
  243. }
  244. StyleBoxFlat::StyleBoxFlat() {
  245. bg_color=Color(0.6,0.6,0.6);
  246. light_color=Color(0.8,0.8,0.8);
  247. dark_color=Color(0.8,0.8,0.8);
  248. draw_center=true;
  249. blend=true;
  250. border_size=0;
  251. }
  252. StyleBoxFlat::~StyleBoxFlat() {
  253. }
  254. ////////////////
  255. void StyleBoxImageMask::_bind_methods() {
  256. ObjectTypeDB::bind_method(_MD("set_image","image"),&StyleBoxImageMask::set_image);
  257. ObjectTypeDB::bind_method(_MD("get_image"),&StyleBoxImageMask::get_image);
  258. ObjectTypeDB::bind_method(_MD("set_expand","expand"),&StyleBoxImageMask::set_expand);
  259. ObjectTypeDB::bind_method(_MD("get_expand"),&StyleBoxImageMask::get_expand);
  260. ObjectTypeDB::bind_method(_MD("set_expand_margin_size","margin","size"),&StyleBoxImageMask::set_expand_margin_size);
  261. ObjectTypeDB::bind_method(_MD("get_expand_margin_size"),&StyleBoxImageMask::get_expand_margin_size);
  262. ADD_PROPERTY( PropertyInfo(Variant::IMAGE, "image"), _SCS("set_image"), _SCS("get_image"));
  263. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "expand"), _SCS("set_expand"), _SCS("get_expand"));
  264. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "expand_margin/left", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_expand_margin_size"),_SCS("get_expand_margin_size"), MARGIN_LEFT );
  265. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "expand_margin/right", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_expand_margin_size"),_SCS("get_expand_margin_size"), MARGIN_RIGHT );
  266. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "expand_margin/top", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_expand_margin_size"),_SCS("get_expand_margin_size"), MARGIN_TOP );
  267. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "expand_margin/bottom", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_expand_margin_size"),_SCS("get_expand_margin_size"), MARGIN_BOTTOM );
  268. }
  269. bool StyleBoxImageMask::test_mask(const Point2& p_point, const Rect2& p_rect) const {
  270. if (image.empty())
  271. return false;
  272. if (p_rect.size.x<1)
  273. return false;
  274. if (p_rect.size.y<1)
  275. return false;
  276. Size2i imgsize(image.get_width(),image.get_height());
  277. if (imgsize.x<=0 || imgsize.y<=0)
  278. return false;
  279. Point2i img_expand_size( imgsize.x - expand_margin[MARGIN_LEFT] - expand_margin[MARGIN_RIGHT], imgsize.y - expand_margin[MARGIN_TOP] - expand_margin[MARGIN_BOTTOM]);
  280. Point2i rect_expand_size( p_rect.size.x - expand_margin[MARGIN_LEFT] - expand_margin[MARGIN_RIGHT], p_rect.size.y - expand_margin[MARGIN_TOP] - expand_margin[MARGIN_BOTTOM]);
  281. if (rect_expand_size.x<1)
  282. rect_expand_size.x=1;
  283. if (rect_expand_size.y<1)
  284. rect_expand_size.y=1;
  285. Point2i click_pos;
  286. //treat x
  287. if (p_point.x<p_rect.pos.x)
  288. click_pos.x=0;
  289. else if (expand) {
  290. if (p_point.x>=p_rect.pos.x+p_rect.size.x)
  291. click_pos.x=imgsize.x-1;
  292. else if ((p_point.x-p_rect.pos.x)<expand_margin[MARGIN_LEFT])
  293. click_pos.x=p_point.x;
  294. else if ((p_point.x-(p_rect.pos.x+p_rect.size.x))<expand_margin[MARGIN_RIGHT])
  295. click_pos.x=imgsize.x-(p_point.x-(p_rect.pos.x+p_rect.size.x));
  296. else //expand
  297. click_pos.x=(p_point.x-p_rect.pos.x-expand_margin[MARGIN_LEFT])*img_expand_size.x/rect_expand_size.x;
  298. } else if ((p_point.x-p_rect.pos.x) > imgsize.x)
  299. click_pos.x=imgsize.x;
  300. //treat y
  301. if (p_point.y<p_rect.pos.y)
  302. click_pos.y=0;
  303. else if (expand) {
  304. if (p_point.y>=p_rect.pos.y+p_rect.size.y)
  305. click_pos.y=imgsize.y-1;
  306. else if ((p_point.y-p_rect.pos.y)<expand_margin[MARGIN_TOP])
  307. click_pos.y=p_point.y;
  308. else if ((p_point.y-(p_rect.pos.y+p_rect.size.y))<expand_margin[MARGIN_BOTTOM])
  309. click_pos.y=imgsize.y-(p_point.y-(p_rect.pos.y+p_rect.size.y));
  310. else //expand
  311. click_pos.y=(p_point.y-p_rect.pos.y-expand_margin[MARGIN_TOP])*img_expand_size.y/rect_expand_size.y;
  312. } else if ((p_point.y-p_rect.pos.y) > imgsize.y)
  313. click_pos.y=imgsize.y;
  314. return image.get_pixel(click_pos.x,click_pos.y).gray()>0.5;
  315. }
  316. void StyleBoxImageMask::set_image(const Image& p_image) {
  317. image=p_image;
  318. }
  319. Image StyleBoxImageMask::get_image() const {
  320. return image;
  321. }
  322. void StyleBoxImageMask::set_expand(bool p_expand) {
  323. expand=p_expand;
  324. }
  325. bool StyleBoxImageMask::get_expand() const {
  326. return expand;
  327. }
  328. void StyleBoxImageMask::set_expand_margin_size(Margin p_expand_margin,float p_size) {
  329. ERR_FAIL_INDEX(p_expand_margin,4);
  330. expand_margin[p_expand_margin]=p_size;
  331. }
  332. float StyleBoxImageMask::get_expand_margin_size(Margin p_expand_margin) const {
  333. ERR_FAIL_INDEX_V(p_expand_margin,4,0);
  334. return expand_margin[p_expand_margin];
  335. }
  336. StyleBoxImageMask::StyleBoxImageMask() {
  337. for (int i=0;i<4;i++) {
  338. expand_margin[i]=0;
  339. }
  340. expand=true;
  341. }