style_box.cpp 38 KB

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