style_box.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*************************************************************************/
  2. /* style_box.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "style_box.h"
  31. #include <limits.h>
  32. bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
  33. return true;
  34. }
  35. void StyleBox::set_default_margin(Margin p_margin, float p_value) {
  36. margin[p_margin] = p_value;
  37. emit_changed();
  38. }
  39. float StyleBox::get_default_margin(Margin p_margin) const {
  40. return margin[p_margin];
  41. }
  42. float StyleBox::get_margin(Margin p_margin) const {
  43. if (margin[p_margin] < 0)
  44. return get_style_margin(p_margin);
  45. else
  46. return margin[p_margin];
  47. }
  48. Size2 StyleBox::get_minimum_size() const {
  49. return Size2(get_margin(MARGIN_LEFT) + get_margin(MARGIN_RIGHT), get_margin(MARGIN_TOP) + get_margin(MARGIN_BOTTOM));
  50. }
  51. Point2 StyleBox::get_offset() const {
  52. return Point2(get_margin(MARGIN_LEFT), get_margin(MARGIN_TOP));
  53. }
  54. Size2 StyleBox::get_center_size() const {
  55. return Size2();
  56. }
  57. void StyleBox::_bind_methods() {
  58. ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask);
  59. ClassDB::bind_method(D_METHOD("set_default_margin", "margin", "offset"), &StyleBox::set_default_margin);
  60. ClassDB::bind_method(D_METHOD("get_default_margin", "margin"), &StyleBox::get_default_margin);
  61. //ClassDB::bind_method(D_METHOD("set_default_margin"),&StyleBox::set_default_margin);
  62. //ClassDB::bind_method(D_METHOD("get_default_margin"),&StyleBox::get_default_margin);
  63. ClassDB::bind_method(D_METHOD("get_margin", "margin"), &StyleBox::get_margin);
  64. ClassDB::bind_method(D_METHOD("get_minimum_size"), &StyleBox::get_minimum_size);
  65. ClassDB::bind_method(D_METHOD("get_center_size"), &StyleBox::get_center_size);
  66. ClassDB::bind_method(D_METHOD("get_offset"), &StyleBox::get_offset);
  67. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "rect"), &StyleBox::draw);
  68. ADD_GROUP("Content Margin", "content_margin_");
  69. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin_left", PROPERTY_HINT_RANGE, "-1,2048,1"), "set_default_margin", "get_default_margin", MARGIN_LEFT);
  70. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin_right", PROPERTY_HINT_RANGE, "-1,2048,1"), "set_default_margin", "get_default_margin", MARGIN_RIGHT);
  71. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin_top", PROPERTY_HINT_RANGE, "-1,2048,1"), "set_default_margin", "get_default_margin", MARGIN_TOP);
  72. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin_bottom", PROPERTY_HINT_RANGE, "-1,2048,1"), "set_default_margin", "get_default_margin", MARGIN_BOTTOM);
  73. }
  74. StyleBox::StyleBox() {
  75. for (int i = 0; i < 4; i++) {
  76. margin[i] = -1;
  77. }
  78. }
  79. void StyleBoxTexture::set_texture(RES p_texture) {
  80. if (texture == p_texture)
  81. return;
  82. texture = p_texture;
  83. region_rect = Rect2(Point2(), texture->get_size());
  84. emit_signal("texture_changed");
  85. emit_changed();
  86. _change_notify("texture");
  87. }
  88. RES StyleBoxTexture::get_texture() const {
  89. return texture;
  90. }
  91. void StyleBoxTexture::set_normal_map(RES p_normal_map) {
  92. if (normal_map == p_normal_map)
  93. return;
  94. normal_map = p_normal_map;
  95. emit_changed();
  96. }
  97. RES StyleBoxTexture::get_normal_map() const {
  98. return normal_map;
  99. }
  100. void StyleBoxTexture::set_margin_size(Margin p_margin, float p_size) {
  101. margin[p_margin] = p_size;
  102. emit_changed();
  103. }
  104. float StyleBoxTexture::get_margin_size(Margin p_margin) const {
  105. return margin[p_margin];
  106. }
  107. float StyleBoxTexture::get_style_margin(Margin p_margin) const {
  108. return margin[p_margin];
  109. }
  110. void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  111. if (texture.is_null())
  112. return;
  113. Rect2 rect = p_rect;
  114. Rect2 src_rect = region_rect;
  115. texture->get_rect_region(rect, src_rect, rect, src_rect);
  116. rect.position.x -= expand_margin[MARGIN_LEFT];
  117. rect.position.y -= expand_margin[MARGIN_TOP];
  118. rect.size.x += expand_margin[MARGIN_LEFT] + expand_margin[MARGIN_RIGHT];
  119. rect.size.y += expand_margin[MARGIN_TOP] + expand_margin[MARGIN_BOTTOM];
  120. RID normal_rid;
  121. if (normal_map.is_valid())
  122. normal_rid = normal_map->get_rid();
  123. VisualServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), Vector2(margin[MARGIN_LEFT], margin[MARGIN_TOP]), Vector2(margin[MARGIN_RIGHT], margin[MARGIN_BOTTOM]), VS::NinePatchAxisMode(axis_h), VS::NinePatchAxisMode(axis_v), draw_center, modulate, normal_rid);
  124. }
  125. void StyleBoxTexture::set_draw_center(bool p_enabled) {
  126. draw_center = p_enabled;
  127. emit_changed();
  128. }
  129. bool StyleBoxTexture::is_draw_center_enabled() const {
  130. return draw_center;
  131. }
  132. Size2 StyleBoxTexture::get_center_size() const {
  133. if (texture.is_null())
  134. return Size2();
  135. return texture->get_size() - get_minimum_size();
  136. }
  137. void StyleBoxTexture::set_expand_margin_size(Margin p_expand_margin, float p_size) {
  138. ERR_FAIL_INDEX(p_expand_margin, 4);
  139. expand_margin[p_expand_margin] = p_size;
  140. emit_changed();
  141. }
  142. void StyleBoxTexture::set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) {
  143. expand_margin[MARGIN_LEFT] = p_left;
  144. expand_margin[MARGIN_TOP] = p_top;
  145. expand_margin[MARGIN_RIGHT] = p_right;
  146. expand_margin[MARGIN_BOTTOM] = p_bottom;
  147. emit_changed();
  148. }
  149. void StyleBoxTexture::set_expand_margin_size_all(float p_expand_margin_size) {
  150. for (int i = 0; i < 4; i++) {
  151. expand_margin[i] = p_expand_margin_size;
  152. }
  153. emit_changed();
  154. }
  155. float StyleBoxTexture::get_expand_margin_size(Margin p_expand_margin) const {
  156. ERR_FAIL_INDEX_V(p_expand_margin, 4, 0);
  157. return expand_margin[p_expand_margin];
  158. }
  159. void StyleBoxTexture::set_region_rect(const Rect2 &p_region_rect) {
  160. if (region_rect == p_region_rect)
  161. return;
  162. region_rect = p_region_rect;
  163. emit_changed();
  164. }
  165. Rect2 StyleBoxTexture::get_region_rect() const {
  166. return region_rect;
  167. }
  168. void StyleBoxTexture::set_h_axis_stretch_mode(AxisStretchMode p_mode) {
  169. axis_h = p_mode;
  170. emit_changed();
  171. }
  172. StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_h_axis_stretch_mode() const {
  173. return axis_h;
  174. }
  175. void StyleBoxTexture::set_v_axis_stretch_mode(AxisStretchMode p_mode) {
  176. axis_v = p_mode;
  177. emit_changed();
  178. }
  179. StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_v_axis_stretch_mode() const {
  180. return axis_v;
  181. }
  182. void StyleBoxTexture::set_modulate(const Color &p_modulate) {
  183. if (modulate == p_modulate)
  184. return;
  185. modulate = p_modulate;
  186. emit_changed();
  187. }
  188. Color StyleBoxTexture::get_modulate() const {
  189. return modulate;
  190. }
  191. void StyleBoxTexture::_bind_methods() {
  192. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &StyleBoxTexture::set_texture);
  193. ClassDB::bind_method(D_METHOD("get_texture"), &StyleBoxTexture::get_texture);
  194. ClassDB::bind_method(D_METHOD("set_normal_map", "normal_map"), &StyleBoxTexture::set_normal_map);
  195. ClassDB::bind_method(D_METHOD("get_normal_map"), &StyleBoxTexture::get_normal_map);
  196. ClassDB::bind_method(D_METHOD("set_margin_size", "margin", "size"), &StyleBoxTexture::set_margin_size);
  197. ClassDB::bind_method(D_METHOD("get_margin_size", "margin"), &StyleBoxTexture::get_margin_size);
  198. ClassDB::bind_method(D_METHOD("set_expand_margin_size", "margin", "size"), &StyleBoxTexture::set_expand_margin_size);
  199. ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxTexture::set_expand_margin_size_all);
  200. ClassDB::bind_method(D_METHOD("set_expand_margin_individual", "size_left", "size_top", "size_right", "size_bottom"), &StyleBoxTexture::set_expand_margin_size_individual);
  201. ClassDB::bind_method(D_METHOD("get_expand_margin_size", "margin"), &StyleBoxTexture::get_expand_margin_size);
  202. ClassDB::bind_method(D_METHOD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect);
  203. ClassDB::bind_method(D_METHOD("get_region_rect"), &StyleBoxTexture::get_region_rect);
  204. ClassDB::bind_method(D_METHOD("set_draw_center", "enable"), &StyleBoxTexture::set_draw_center);
  205. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxTexture::is_draw_center_enabled);
  206. ClassDB::bind_method(D_METHOD("set_modulate", "color"), &StyleBoxTexture::set_modulate);
  207. ClassDB::bind_method(D_METHOD("get_modulate"), &StyleBoxTexture::get_modulate);
  208. ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &StyleBoxTexture::set_h_axis_stretch_mode);
  209. ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &StyleBoxTexture::get_h_axis_stretch_mode);
  210. ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &StyleBoxTexture::set_v_axis_stretch_mode);
  211. ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &StyleBoxTexture::get_v_axis_stretch_mode);
  212. ADD_SIGNAL(MethodInfo("texture_changed"));
  213. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  214. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_map", "get_normal_map");
  215. ADD_PROPERTYNZ(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
  216. ADD_GROUP("Margin", "margin_");
  217. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin_left", PROPERTY_HINT_RANGE, "0,2048,1"), "set_margin_size", "get_margin_size", MARGIN_LEFT);
  218. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin_right", PROPERTY_HINT_RANGE, "0,2048,1"), "set_margin_size", "get_margin_size", MARGIN_RIGHT);
  219. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin_top", PROPERTY_HINT_RANGE, "0,2048,1"), "set_margin_size", "get_margin_size", MARGIN_TOP);
  220. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1"), "set_margin_size", "get_margin_size", MARGIN_BOTTOM);
  221. ADD_GROUP("Expand Margin", "expand_margin_");
  222. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin_size", "get_expand_margin_size", MARGIN_LEFT);
  223. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin_size", "get_expand_margin_size", MARGIN_RIGHT);
  224. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin_size", "get_expand_margin_size", MARGIN_TOP);
  225. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin_size", "get_expand_margin_size", MARGIN_BOTTOM);
  226. ADD_GROUP("Axis Stretch", "axis_stretch_");
  227. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode");
  228. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode");
  229. ADD_GROUP("Modulate", "modulate_");
  230. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate_color"), "set_modulate", "get_modulate");
  231. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  232. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
  233. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
  234. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
  235. }
  236. StyleBoxTexture::StyleBoxTexture() {
  237. for (int i = 0; i < 4; i++) {
  238. margin[i] = 0;
  239. expand_margin[i] = 0;
  240. }
  241. draw_center = true;
  242. modulate = Color(1, 1, 1, 1);
  243. axis_h = AXIS_STRETCH_MODE_STRETCH;
  244. axis_v = AXIS_STRETCH_MODE_STRETCH;
  245. }
  246. StyleBoxTexture::~StyleBoxTexture() {
  247. }
  248. ////////////////
  249. void StyleBoxFlat::set_bg_color(const Color &p_color) {
  250. bg_color = p_color;
  251. emit_changed();
  252. }
  253. Color StyleBoxFlat::get_bg_color() const {
  254. return bg_color;
  255. }
  256. void StyleBoxFlat::set_border_color_all(const Color &p_color) {
  257. for (int i = 0; i < 4; i++) {
  258. border_color.write()[i] = p_color;
  259. }
  260. emit_changed();
  261. }
  262. Color StyleBoxFlat::get_border_color_all() const {
  263. return border_color[MARGIN_TOP];
  264. }
  265. void StyleBoxFlat::set_border_color(Margin p_border, const Color &p_color) {
  266. border_color.write()[p_border] = p_color;
  267. emit_changed();
  268. }
  269. Color StyleBoxFlat::get_border_color(Margin p_border) const {
  270. return border_color[p_border];
  271. }
  272. void StyleBoxFlat::set_border_width_all(int p_size) {
  273. border_width[0] = p_size;
  274. border_width[1] = p_size;
  275. border_width[2] = p_size;
  276. border_width[3] = p_size;
  277. emit_changed();
  278. }
  279. int StyleBoxFlat::get_border_width_min() const {
  280. return MIN(MIN(border_width[0], border_width[1]), MIN(border_width[2], border_width[3]));
  281. }
  282. void StyleBoxFlat::set_border_width(Margin p_margin, int p_width) {
  283. border_width[p_margin] = p_width;
  284. emit_changed();
  285. }
  286. int StyleBoxFlat::get_border_width(Margin p_margin) const {
  287. return border_width[p_margin];
  288. }
  289. void StyleBoxFlat::set_border_blend(bool p_blend) {
  290. blend_border = p_blend;
  291. emit_changed();
  292. }
  293. bool StyleBoxFlat::get_border_blend() const {
  294. return blend_border;
  295. }
  296. void StyleBoxFlat::set_corner_radius_all(int radius) {
  297. for (int i = 0; i < 4; i++) {
  298. corner_radius[i] = radius;
  299. }
  300. emit_changed();
  301. }
  302. void StyleBoxFlat::set_corner_radius_individual(const int radius_top_left, const int radius_top_right, const int radius_botton_right, const int radius_bottom_left) {
  303. corner_radius[0] = radius_top_left;
  304. corner_radius[1] = radius_top_right;
  305. corner_radius[2] = radius_botton_right;
  306. corner_radius[3] = radius_bottom_left;
  307. emit_changed();
  308. }
  309. int StyleBoxFlat::get_corner_radius_min() const {
  310. int smallest = corner_radius[0];
  311. for (int i = 1; i < 4; i++) {
  312. if (smallest > corner_radius[i]) {
  313. smallest = corner_radius[i];
  314. }
  315. }
  316. return smallest;
  317. }
  318. void StyleBoxFlat::set_corner_radius(const Corner p_corner, const int radius) {
  319. corner_radius[p_corner] = radius;
  320. emit_changed();
  321. }
  322. int StyleBoxFlat::get_corner_radius(const Corner p_corner) const {
  323. return corner_radius[p_corner];
  324. }
  325. void StyleBoxFlat::set_expand_margin_size(Margin p_expand_margin, float p_size) {
  326. expand_margin[p_expand_margin] = p_size;
  327. emit_changed();
  328. }
  329. void StyleBoxFlat::set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) {
  330. expand_margin[MARGIN_LEFT] = p_left;
  331. expand_margin[MARGIN_TOP] = p_top;
  332. expand_margin[MARGIN_RIGHT] = p_right;
  333. expand_margin[MARGIN_BOTTOM] = p_bottom;
  334. emit_changed();
  335. }
  336. void StyleBoxFlat::set_expand_margin_size_all(float p_expand_margin_size) {
  337. for (int i = 0; i < 4; i++) {
  338. expand_margin[i] = p_expand_margin_size;
  339. }
  340. emit_changed();
  341. }
  342. float StyleBoxFlat::get_expand_margin_size(Margin p_expand_margin) const {
  343. return expand_margin[p_expand_margin];
  344. }
  345. void StyleBoxFlat::set_draw_center(bool p_enabled) {
  346. draw_center = p_enabled;
  347. emit_changed();
  348. }
  349. bool StyleBoxFlat::is_draw_center_enabled() const {
  350. return draw_center;
  351. }
  352. void StyleBoxFlat::set_shadow_color(const Color &p_color) {
  353. shadow_color = p_color;
  354. emit_changed();
  355. }
  356. Color StyleBoxFlat::get_shadow_color() const {
  357. return shadow_color;
  358. }
  359. void StyleBoxFlat::set_shadow_size(const int &p_size) {
  360. shadow_size = p_size;
  361. emit_changed();
  362. }
  363. int StyleBoxFlat::get_shadow_size() const {
  364. return shadow_size;
  365. }
  366. void StyleBoxFlat::set_anti_aliased(const bool &p_anti_aliased) {
  367. anti_aliased = p_anti_aliased;
  368. emit_changed();
  369. }
  370. bool StyleBoxFlat::is_anti_aliased() const {
  371. return anti_aliased;
  372. }
  373. void StyleBoxFlat::set_aa_size(const int &p_aa_size) {
  374. aa_size = CLAMP(p_aa_size, 1, 5);
  375. emit_changed();
  376. }
  377. int StyleBoxFlat::get_aa_size() const {
  378. return aa_size;
  379. }
  380. void StyleBoxFlat::set_corner_detail(const int &p_corner_detail) {
  381. corner_detail = CLAMP(p_corner_detail, 1, 128);
  382. emit_changed();
  383. }
  384. int StyleBoxFlat::get_corner_detail() const {
  385. return corner_detail;
  386. }
  387. Size2 StyleBoxFlat::get_center_size() const {
  388. return Size2();
  389. }
  390. inline void set_inner_corner_radius(const Rect2 style_rect, const Rect2 inner_rect, const int corner_radius[4], int *inner_corner_radius) {
  391. int border_left = inner_rect.position.x - style_rect.position.x;
  392. int border_top = inner_rect.position.y - style_rect.position.y;
  393. int border_right = style_rect.size.width - inner_rect.size.width - border_left;
  394. int border_bottom = style_rect.size.height - inner_rect.size.height - border_top;
  395. int rad;
  396. //tl
  397. rad = MIN(border_top, border_left);
  398. inner_corner_radius[0] = MAX(corner_radius[0] - rad, 0);
  399. //tr
  400. rad = MIN(border_top, border_bottom);
  401. inner_corner_radius[1] = MAX(corner_radius[1] - rad, 0);
  402. //br
  403. rad = MIN(border_bottom, border_right);
  404. inner_corner_radius[2] = MAX(corner_radius[2] - rad, 0);
  405. //bl
  406. rad = MIN(border_bottom, border_left);
  407. inner_corner_radius[3] = MAX(corner_radius[3] - rad, 0);
  408. }
  409. inline void draw_ring(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color> &colors, const Rect2 style_rect, const int corner_radius[4],
  410. const Rect2 ring_rect, const int border_width[4], const Color inner_color[4], const Color outer_color[4], const int corner_detail) {
  411. int vert_offset = verts.size();
  412. if (!vert_offset) {
  413. vert_offset = 0;
  414. }
  415. int adapted_corner_detail = (corner_radius[0] == 0 && corner_radius[1] == 0 && corner_radius[2] == 0 && corner_radius[3] == 0) ? 1 : corner_detail;
  416. int rings = (border_width[0] == 0 && border_width[1] == 0 && border_width[2] == 0 && border_width[3] == 0) ? 1 : 2;
  417. rings = 2;
  418. int ring_corner_radius[4];
  419. set_inner_corner_radius(style_rect, ring_rect, corner_radius, ring_corner_radius);
  420. //corner radius center points
  421. Vector<Point2> outer_points;
  422. outer_points.push_back(ring_rect.position + Vector2(ring_corner_radius[0], ring_corner_radius[0])); //tl
  423. outer_points.push_back(Point2(ring_rect.position.x + ring_rect.size.x - ring_corner_radius[1], ring_rect.position.y + ring_corner_radius[1])); //tr
  424. outer_points.push_back(ring_rect.position + ring_rect.size - Vector2(ring_corner_radius[2], ring_corner_radius[2])); //br
  425. outer_points.push_back(Point2(ring_rect.position.x + ring_corner_radius[3], ring_rect.position.y + ring_rect.size.y - ring_corner_radius[3])); //bl
  426. Rect2 inner_rect;
  427. inner_rect = ring_rect.grow_individual(-border_width[MARGIN_LEFT], -border_width[MARGIN_TOP], -border_width[MARGIN_RIGHT], -border_width[MARGIN_BOTTOM]);
  428. int inner_corner_radius[4];
  429. Vector<Point2> inner_points;
  430. set_inner_corner_radius(style_rect, inner_rect, corner_radius, inner_corner_radius);
  431. inner_points.push_back(inner_rect.position + Vector2(inner_corner_radius[0], inner_corner_radius[0])); //tl
  432. inner_points.push_back(Point2(inner_rect.position.x + inner_rect.size.x - inner_corner_radius[1], inner_rect.position.y + inner_corner_radius[1])); //tr
  433. inner_points.push_back(inner_rect.position + inner_rect.size - Vector2(inner_corner_radius[2], inner_corner_radius[2])); //br
  434. inner_points.push_back(Point2(inner_rect.position.x + inner_corner_radius[3], inner_rect.position.y + inner_rect.size.y - inner_corner_radius[3])); //bl
  435. //calculate the vert array
  436. for (int corner_index = 0; corner_index < 4; corner_index++) {
  437. for (int detail = 0; detail <= adapted_corner_detail; detail++) {
  438. for (int inner_outer = (2 - rings); inner_outer < 2; inner_outer++) {
  439. float radius;
  440. Color color;
  441. Point2 corner_point;
  442. if (inner_outer == 0) {
  443. radius = inner_corner_radius[corner_index];
  444. color = *inner_color;
  445. corner_point = inner_points[corner_index];
  446. } else {
  447. radius = ring_corner_radius[corner_index];
  448. color = *outer_color;
  449. corner_point = outer_points[corner_index];
  450. }
  451. float x = radius * (float)cos((double)corner_index * Math_PI / 2.0 + (double)detail / (double)adapted_corner_detail * Math_PI / 2.0 + Math_PI) + corner_point.x;
  452. float y = radius * (float)sin((double)corner_index * Math_PI / 2.0 + (double)detail / (double)adapted_corner_detail * Math_PI / 2.0 + Math_PI) + corner_point.y;
  453. verts.push_back(Vector2(x, y));
  454. colors.push_back(color);
  455. }
  456. }
  457. }
  458. if (rings == 2) {
  459. int vert_count = (adapted_corner_detail + 1) * 4 * rings;
  460. //fill the indices and the colors for the border
  461. for (int i = 0; i < vert_count; i++) {
  462. //poly 1
  463. indices.push_back(vert_offset + ((i + 0) % vert_count));
  464. indices.push_back(vert_offset + ((i + 2) % vert_count));
  465. indices.push_back(vert_offset + ((i + 1) % vert_count));
  466. //poly 2
  467. indices.push_back(vert_offset + ((i + 1) % vert_count));
  468. indices.push_back(vert_offset + ((i + 2) % vert_count));
  469. indices.push_back(vert_offset + ((i + 3) % vert_count));
  470. }
  471. }
  472. }
  473. inline void adapt_values(int p_index_a, int p_index_b, int *adapted_values, const int *p_values, const real_t p_width, const int p_max_a, const int p_max_b) {
  474. if (p_values[p_index_a] + p_values[p_index_b] > p_width) {
  475. float factor;
  476. int newValue;
  477. factor = (float)p_width / (float)(p_values[p_index_a] + p_values[p_index_b]);
  478. newValue = (int)(p_values[p_index_a] * factor);
  479. if (newValue < adapted_values[p_index_a]) {
  480. adapted_values[p_index_a] = newValue;
  481. }
  482. newValue = (int)(p_values[p_index_b] * factor);
  483. if (newValue < adapted_values[p_index_b]) {
  484. adapted_values[p_index_b] = newValue;
  485. }
  486. } else {
  487. adapted_values[p_index_a] = MIN(p_values[p_index_a], adapted_values[p_index_a]);
  488. adapted_values[p_index_b] = MIN(p_values[p_index_b], adapted_values[p_index_b]);
  489. }
  490. adapted_values[p_index_a] = MIN(p_max_a, adapted_values[p_index_a]);
  491. adapted_values[p_index_b] = MIN(p_max_b, adapted_values[p_index_b]);
  492. }
  493. void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  494. //PREPARATIONS
  495. bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0);
  496. bool aa_on = rounded_corners && anti_aliased;
  497. Rect2 style_rect = p_rect.grow_individual(expand_margin[MARGIN_LEFT], expand_margin[MARGIN_TOP], expand_margin[MARGIN_RIGHT], expand_margin[MARGIN_BOTTOM]);
  498. if (aa_on) {
  499. style_rect = style_rect.grow(-((aa_size + 1) / 2));
  500. }
  501. //adapt borders (prevent weired overlapping/glitchy drawings)
  502. int width = style_rect.size.width;
  503. int height = style_rect.size.height;
  504. int adapted_border[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
  505. adapt_values(MARGIN_TOP, MARGIN_BOTTOM, adapted_border, border_width, height, height, height);
  506. adapt_values(MARGIN_LEFT, MARGIN_RIGHT, adapted_border, border_width, width, width, width);
  507. //adapt corners (prevent weired overlapping/glitchy drawings)
  508. int adapted_corner[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
  509. adapt_values(CORNER_TOP_RIGHT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, height, height - adapted_border[MARGIN_BOTTOM], height - adapted_border[MARGIN_TOP]);
  510. adapt_values(CORNER_TOP_LEFT, CORNER_BOTTOM_LEFT, adapted_corner, corner_radius, height, height - adapted_border[MARGIN_BOTTOM], height - adapted_border[MARGIN_TOP]);
  511. adapt_values(CORNER_TOP_LEFT, CORNER_TOP_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[MARGIN_RIGHT], width - adapted_border[MARGIN_LEFT]);
  512. adapt_values(CORNER_BOTTOM_LEFT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[MARGIN_RIGHT], width - adapted_border[MARGIN_LEFT]);
  513. Rect2 infill_rect = style_rect.grow_individual(-adapted_border[MARGIN_LEFT], -adapted_border[MARGIN_TOP], -adapted_border[MARGIN_RIGHT], -adapted_border[MARGIN_BOTTOM]);
  514. Vector<Point2> verts;
  515. Vector<int> indices;
  516. Vector<Color> colors;
  517. //DRAWING
  518. VisualServer *vs = VisualServer::get_singleton();
  519. //DRAW SHADOW
  520. if (shadow_size > 0) {
  521. int shadow_width[4] = { shadow_size, shadow_size, shadow_size, shadow_size };
  522. Color shadow_colors[4] = { shadow_color, shadow_color, shadow_color, shadow_color };
  523. Color shadow_colors_transparent[4];
  524. for (int i = 0; i < 4; i++) {
  525. shadow_colors_transparent[i] = Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
  526. }
  527. draw_ring(verts, indices, colors, style_rect, adapted_corner,
  528. style_rect.grow(shadow_size), shadow_width, shadow_colors, shadow_colors_transparent, corner_detail);
  529. }
  530. //DRAW border
  531. Color bg_color_array[4] = { bg_color, bg_color, bg_color, bg_color };
  532. const Color *inner_color = ((blend_border) ? bg_color_array : border_color.read().ptr());
  533. draw_ring(verts, indices, colors, style_rect, adapted_corner,
  534. style_rect, adapted_border, inner_color, border_color.read().ptr(), corner_detail);
  535. //DRAW INFILL
  536. if (draw_center) {
  537. int temp_vert_offset = verts.size();
  538. int no_border[4] = { 0, 0, 0, 0 };
  539. draw_ring(verts, indices, colors, style_rect, adapted_corner,
  540. infill_rect, no_border, &bg_color, &bg_color, corner_detail);
  541. int added_vert_count = verts.size() - temp_vert_offset;
  542. //fill the indices and the colors for the center
  543. for (int index = 0; index <= added_vert_count / 2; index += 2) {
  544. int i = index;
  545. //poly 1
  546. indices.push_back(temp_vert_offset + i);
  547. indices.push_back(temp_vert_offset + added_vert_count - 4 - i);
  548. indices.push_back(temp_vert_offset + i + 2);
  549. //poly 1
  550. indices.push_back(temp_vert_offset + i);
  551. indices.push_back(temp_vert_offset + added_vert_count - 2 - i);
  552. indices.push_back(temp_vert_offset + added_vert_count - 4 - i);
  553. }
  554. }
  555. if (aa_on) {
  556. //HELPER ARRAYS
  557. Color border_color_alpha[4];
  558. for (int i = 0; i < 4; i++) {
  559. Color c = border_color.read().ptr()[i];
  560. border_color_alpha[i] = Color(c.r, c.g, c.b, 0);
  561. }
  562. Color alpha_bg = Color(bg_color.r, bg_color.g, bg_color.b, 0);
  563. Color bg_color_array_alpha[4] = { alpha_bg, alpha_bg, alpha_bg, alpha_bg };
  564. int aa_border_width[4] = { aa_size, aa_size, aa_size, aa_size };
  565. if (draw_center) {
  566. if (!blend_border) {
  567. //INFILL AA
  568. draw_ring(verts, indices, colors, style_rect, adapted_corner,
  569. infill_rect.grow(aa_size), aa_border_width, bg_color_array, bg_color_array_alpha, corner_detail);
  570. }
  571. } else if (!(border_width[0] == 0 && border_width[1] == 0 && border_width[2] == 0 && border_width[3] == 0)) {
  572. //DRAW INNER BORDER AA
  573. draw_ring(verts, indices, colors, style_rect, adapted_corner,
  574. infill_rect, aa_border_width, border_color_alpha, border_color.read().ptr(), corner_detail);
  575. }
  576. //DRAW OUTER BORDER AA
  577. if (!(border_width[0] == 0 && border_width[1] == 0 && border_width[2] == 0 && border_width[3] == 0)) {
  578. draw_ring(verts, indices, colors, style_rect, adapted_corner,
  579. style_rect.grow(aa_size), aa_border_width, border_color.read().ptr(), border_color_alpha, corner_detail);
  580. }
  581. }
  582. vs->canvas_item_add_triangle_array(p_canvas_item, indices, verts, colors);
  583. }
  584. float StyleBoxFlat::get_style_margin(Margin p_margin) const {
  585. return border_width[p_margin];
  586. }
  587. void StyleBoxFlat::_bind_methods() {
  588. ClassDB::bind_method(D_METHOD("set_bg_color", "color"), &StyleBoxFlat::set_bg_color);
  589. ClassDB::bind_method(D_METHOD("get_bg_color"), &StyleBoxFlat::get_bg_color);
  590. ClassDB::bind_method(D_METHOD("set_border_color", "color"), &StyleBoxFlat::set_border_color_all);
  591. ClassDB::bind_method(D_METHOD("get_border_color"), &StyleBoxFlat::get_border_color_all);
  592. ClassDB::bind_method(D_METHOD("set_border_width_all", "width"), &StyleBoxFlat::set_border_width_all);
  593. ClassDB::bind_method(D_METHOD("get_border_width_min"), &StyleBoxFlat::get_border_width_min);
  594. ClassDB::bind_method(D_METHOD("set_border_width", "margin", "width"), &StyleBoxFlat::set_border_width);
  595. ClassDB::bind_method(D_METHOD("get_border_width", "margin"), &StyleBoxFlat::get_border_width);
  596. ClassDB::bind_method(D_METHOD("set_border_blend", "blend"), &StyleBoxFlat::set_border_blend);
  597. ClassDB::bind_method(D_METHOD("get_border_blend"), &StyleBoxFlat::get_border_blend);
  598. ClassDB::bind_method(D_METHOD("set_corner_radius_individual", "radius_top_left", "radius_top_right", "radius_bottom_right", "radius_bottom_left"), &StyleBoxFlat::set_corner_radius_individual);
  599. ClassDB::bind_method(D_METHOD("set_corner_radius_all", "radius"), &StyleBoxFlat::set_corner_radius_all);
  600. ClassDB::bind_method(D_METHOD("set_corner_radius", "corner", "radius"), &StyleBoxFlat::set_corner_radius);
  601. ClassDB::bind_method(D_METHOD("get_corner_radius", "corner"), &StyleBoxFlat::get_corner_radius);
  602. ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxFlat::set_expand_margin_size);
  603. ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxFlat::set_expand_margin_size_all);
  604. ClassDB::bind_method(D_METHOD("set_expand_margin_individual", "size_left", "size_top", "size_right", "size_bottom"), &StyleBoxFlat::set_expand_margin_size_individual);
  605. ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxFlat::get_expand_margin_size);
  606. ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &StyleBoxFlat::set_draw_center);
  607. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxFlat::is_draw_center_enabled);
  608. ClassDB::bind_method(D_METHOD("set_shadow_color", "color"), &StyleBoxFlat::set_shadow_color);
  609. ClassDB::bind_method(D_METHOD("get_shadow_color"), &StyleBoxFlat::get_shadow_color);
  610. ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &StyleBoxFlat::set_shadow_size);
  611. ClassDB::bind_method(D_METHOD("get_shadow_size"), &StyleBoxFlat::get_shadow_size);
  612. ClassDB::bind_method(D_METHOD("set_anti_aliased", "anti_aliased"), &StyleBoxFlat::set_anti_aliased);
  613. ClassDB::bind_method(D_METHOD("is_anti_aliased"), &StyleBoxFlat::is_anti_aliased);
  614. ClassDB::bind_method(D_METHOD("set_aa_size", "size"), &StyleBoxFlat::set_aa_size);
  615. ClassDB::bind_method(D_METHOD("get_aa_size"), &StyleBoxFlat::get_aa_size);
  616. ClassDB::bind_method(D_METHOD("set_corner_detail", "detail"), &StyleBoxFlat::set_corner_detail);
  617. ClassDB::bind_method(D_METHOD("get_corner_detail"), &StyleBoxFlat::get_corner_detail);
  618. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), "set_bg_color", "get_bg_color");
  619. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  620. ADD_GROUP("Border Width", "border_width_");
  621. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_left", PROPERTY_HINT_RANGE, "0,1024,1"), "set_border_width", "get_border_width", MARGIN_LEFT);
  622. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_top", PROPERTY_HINT_RANGE, "0,1024,1"), "set_border_width", "get_border_width", MARGIN_TOP);
  623. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_right", PROPERTY_HINT_RANGE, "0,1024,1"), "set_border_width", "get_border_width", MARGIN_RIGHT);
  624. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_bottom", PROPERTY_HINT_RANGE, "0,1024,1"), "set_border_width", "get_border_width", MARGIN_BOTTOM);
  625. ADD_GROUP("Border", "border_");
  626. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "border_color"), "set_border_color", "get_border_color");
  627. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "border_blend"), "set_border_blend", "get_border_blend");
  628. ADD_GROUP("Corner Radius", "corner_radius_");
  629. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_left", PROPERTY_HINT_RANGE, "0,1024,1"), "set_corner_radius", "get_corner_radius", CORNER_TOP_LEFT);
  630. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_right", PROPERTY_HINT_RANGE, "0,1024,1"), "set_corner_radius", "get_corner_radius", CORNER_TOP_RIGHT);
  631. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_right", PROPERTY_HINT_RANGE, "0,1024,1"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_RIGHT);
  632. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_left", PROPERTY_HINT_RANGE, "0,1024,1"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_LEFT);
  633. ADD_PROPERTY(PropertyInfo(Variant::INT, "corner_detail", PROPERTY_HINT_RANGE, "1,128,1"), "set_corner_detail", "get_corner_detail");
  634. ADD_GROUP("Expand Margin", "expand_margin_");
  635. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin", "get_expand_margin", MARGIN_LEFT);
  636. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin", "get_expand_margin", MARGIN_RIGHT);
  637. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin", "get_expand_margin", MARGIN_TOP);
  638. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin", "get_expand_margin", MARGIN_BOTTOM);
  639. ADD_GROUP("Shadow", "shadow_");
  640. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
  641. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size"), "set_shadow_size", "get_shadow_size");
  642. ADD_GROUP("Anti Aliasing", "anti_aliasing_");
  643. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "anti_aliasing"), "set_anti_aliased", "is_anti_aliased");
  644. ADD_PROPERTY(PropertyInfo(Variant::INT, "anti_aliasing_size", PROPERTY_HINT_RANGE, "1,5,1"), "set_aa_size", "get_aa_size");
  645. }
  646. StyleBoxFlat::StyleBoxFlat() {
  647. bg_color = Color(0.6, 0.6, 0.6);
  648. shadow_color = Color(0, 0, 0, 0.6);
  649. border_color.append(Color(0.8, 0.8, 0.8));
  650. border_color.append(Color(0.8, 0.8, 0.8));
  651. border_color.append(Color(0.8, 0.8, 0.8));
  652. border_color.append(Color(0.8, 0.8, 0.8));
  653. blend_border = false;
  654. draw_center = true;
  655. anti_aliased = true;
  656. shadow_size = 0;
  657. corner_detail = 8;
  658. aa_size = 1;
  659. border_width[0] = 0;
  660. border_width[1] = 0;
  661. border_width[2] = 0;
  662. border_width[3] = 0;
  663. expand_margin[0] = 0;
  664. expand_margin[1] = 0;
  665. expand_margin[2] = 0;
  666. expand_margin[3] = 0;
  667. corner_radius[0] = 0;
  668. corner_radius[1] = 0;
  669. corner_radius[2] = 0;
  670. corner_radius[3] = 0;
  671. }
  672. StyleBoxFlat::~StyleBoxFlat() {
  673. }
  674. void StyleBoxLine::set_color(const Color &p_color) {
  675. color = p_color;
  676. emit_changed();
  677. }
  678. Color StyleBoxLine::get_color() const {
  679. return color;
  680. }
  681. void StyleBoxLine::set_thickness(int p_thickness) {
  682. thickness = p_thickness;
  683. emit_changed();
  684. }
  685. int StyleBoxLine::get_thickness() const {
  686. return thickness;
  687. }
  688. void StyleBoxLine::set_vertical(bool p_vertical) {
  689. vertical = p_vertical;
  690. }
  691. bool StyleBoxLine::is_vertical() const {
  692. return vertical;
  693. }
  694. void StyleBoxLine::set_grow(float p_grow) {
  695. grow = p_grow;
  696. emit_changed();
  697. }
  698. float StyleBoxLine::get_grow() const {
  699. return grow;
  700. }
  701. void StyleBoxLine::_bind_methods() {
  702. ClassDB::bind_method(D_METHOD("set_color", "color"), &StyleBoxLine::set_color);
  703. ClassDB::bind_method(D_METHOD("get_color"), &StyleBoxLine::get_color);
  704. ClassDB::bind_method(D_METHOD("set_thickness", "thickness"), &StyleBoxLine::set_thickness);
  705. ClassDB::bind_method(D_METHOD("get_thickness"), &StyleBoxLine::get_thickness);
  706. ClassDB::bind_method(D_METHOD("set_grow", "grow"), &StyleBoxLine::set_grow);
  707. ClassDB::bind_method(D_METHOD("get_grow"), &StyleBoxLine::get_grow);
  708. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &StyleBoxLine::set_vertical);
  709. ClassDB::bind_method(D_METHOD("is_vertical"), &StyleBoxLine::is_vertical);
  710. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  711. ADD_PROPERTY(PropertyInfo(Variant::INT, "thickness", PROPERTY_HINT_RANGE, "0,10"), "set_thickness", "get_thickness");
  712. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  713. }
  714. float StyleBoxLine::get_style_margin(Margin p_margin) const {
  715. return thickness;
  716. }
  717. Size2 StyleBoxLine::get_center_size() const {
  718. return Size2();
  719. }
  720. void StyleBoxLine::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  721. VisualServer *vs = VisualServer::get_singleton();
  722. Rect2i r = p_rect;
  723. if (vertical) {
  724. r.position.y -= grow;
  725. r.size.y += grow * 2;
  726. r.size.x = thickness;
  727. } else {
  728. r.position.x -= grow;
  729. r.size.x += grow * 2;
  730. r.size.y = thickness;
  731. }
  732. vs->canvas_item_add_rect(p_canvas_item, r, color);
  733. }
  734. StyleBoxLine::StyleBoxLine() {
  735. grow = 1.0;
  736. thickness = 1;
  737. color = Color(0.0, 0.0, 0.0);
  738. vertical = false;
  739. }
  740. StyleBoxLine::~StyleBoxLine() {}