style_box.cpp 37 KB

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