style_box.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /*************************************************************************/
  2. /* style_box.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/main/canvas_item.h"
  32. #include <limits.h>
  33. float StyleBox::get_style_margin(Side p_side) const {
  34. float ret;
  35. if (GDVIRTUAL_REQUIRED_CALL(_get_style_margin, p_side, ret)) {
  36. return ret;
  37. }
  38. return 0;
  39. }
  40. bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
  41. bool ret;
  42. if (GDVIRTUAL_CALL(_test_mask, p_point, p_rect, ret)) {
  43. return ret;
  44. }
  45. return true;
  46. }
  47. void StyleBox::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  48. if (GDVIRTUAL_REQUIRED_CALL(_draw, p_canvas_item, p_rect)) {
  49. return;
  50. }
  51. }
  52. void StyleBox::set_default_margin(Side p_side, float p_value) {
  53. ERR_FAIL_INDEX((int)p_side, 4);
  54. margin[p_side] = p_value;
  55. emit_changed();
  56. }
  57. float StyleBox::get_default_margin(Side p_side) const {
  58. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  59. return margin[p_side];
  60. }
  61. float StyleBox::get_margin(Side p_side) const {
  62. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  63. if (margin[p_side] < 0) {
  64. return get_style_margin(p_side);
  65. } else {
  66. return margin[p_side];
  67. }
  68. }
  69. CanvasItem *StyleBox::get_current_item_drawn() const {
  70. return CanvasItem::get_current_item_drawn();
  71. }
  72. Size2 StyleBox::get_minimum_size() const {
  73. return Size2(get_margin(SIDE_LEFT) + get_margin(SIDE_RIGHT), get_margin(SIDE_TOP) + get_margin(SIDE_BOTTOM));
  74. }
  75. Point2 StyleBox::get_offset() const {
  76. return Point2(get_margin(SIDE_LEFT), get_margin(SIDE_TOP));
  77. }
  78. Size2 StyleBox::get_center_size() const {
  79. Size2 ret;
  80. if (GDVIRTUAL_CALL(_get_center_size, ret)) {
  81. return ret;
  82. }
  83. return Size2();
  84. }
  85. Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const {
  86. Rect2 ret;
  87. if (GDVIRTUAL_CALL(_get_draw_rect, p_rect, ret)) {
  88. return ret;
  89. }
  90. return p_rect;
  91. }
  92. void StyleBox::_bind_methods() {
  93. ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask);
  94. ClassDB::bind_method(D_METHOD("set_default_margin", "margin", "offset"), &StyleBox::set_default_margin);
  95. ClassDB::bind_method(D_METHOD("get_default_margin", "margin"), &StyleBox::get_default_margin);
  96. ClassDB::bind_method(D_METHOD("get_margin", "margin"), &StyleBox::get_margin);
  97. ClassDB::bind_method(D_METHOD("get_minimum_size"), &StyleBox::get_minimum_size);
  98. ClassDB::bind_method(D_METHOD("get_center_size"), &StyleBox::get_center_size);
  99. ClassDB::bind_method(D_METHOD("get_offset"), &StyleBox::get_offset);
  100. ClassDB::bind_method(D_METHOD("get_current_item_drawn"), &StyleBox::get_current_item_drawn);
  101. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "rect"), &StyleBox::draw);
  102. ADD_GROUP("Content Margins", "content_margin_");
  103. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_left", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_default_margin", "get_default_margin", SIDE_LEFT);
  104. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_top", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_default_margin", "get_default_margin", SIDE_TOP);
  105. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_right", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_default_margin", "get_default_margin", SIDE_RIGHT);
  106. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_bottom", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_default_margin", "get_default_margin", SIDE_BOTTOM);
  107. GDVIRTUAL_BIND(_get_style_margin, "side")
  108. GDVIRTUAL_BIND(_test_mask, "point", "rect")
  109. GDVIRTUAL_BIND(_get_center_size)
  110. GDVIRTUAL_BIND(_get_draw_rect, "rect")
  111. GDVIRTUAL_BIND(_draw, "to_canvas_item", "rect")
  112. }
  113. StyleBox::StyleBox() {
  114. for (int i = 0; i < 4; i++) {
  115. margin[i] = -1;
  116. }
  117. }
  118. void StyleBoxTexture::set_texture(Ref<Texture2D> p_texture) {
  119. if (texture == p_texture) {
  120. return;
  121. }
  122. texture = p_texture;
  123. if (p_texture.is_null()) {
  124. region_rect = Rect2(0, 0, 0, 0);
  125. } else {
  126. region_rect = Rect2(Point2(), texture->get_size());
  127. }
  128. emit_changed();
  129. }
  130. Ref<Texture2D> StyleBoxTexture::get_texture() const {
  131. return texture;
  132. }
  133. void StyleBoxTexture::set_margin_size(Side p_side, float p_size) {
  134. ERR_FAIL_INDEX((int)p_side, 4);
  135. margin[p_side] = p_size;
  136. emit_changed();
  137. }
  138. float StyleBoxTexture::get_margin_size(Side p_side) const {
  139. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  140. return margin[p_side];
  141. }
  142. float StyleBoxTexture::get_style_margin(Side p_side) const {
  143. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  144. return margin[p_side];
  145. }
  146. Rect2 StyleBoxTexture::get_draw_rect(const Rect2 &p_rect) const {
  147. return p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]);
  148. }
  149. void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  150. if (texture.is_null()) {
  151. return;
  152. }
  153. Rect2 rect = p_rect;
  154. Rect2 src_rect = region_rect;
  155. texture->get_rect_region(rect, src_rect, rect, src_rect);
  156. rect.position.x -= expand_margin[SIDE_LEFT];
  157. rect.position.y -= expand_margin[SIDE_TOP];
  158. rect.size.x += expand_margin[SIDE_LEFT] + expand_margin[SIDE_RIGHT];
  159. rect.size.y += expand_margin[SIDE_TOP] + expand_margin[SIDE_BOTTOM];
  160. RenderingServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), Vector2(margin[SIDE_LEFT], margin[SIDE_TOP]), Vector2(margin[SIDE_RIGHT], margin[SIDE_BOTTOM]), RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center, modulate);
  161. }
  162. void StyleBoxTexture::set_draw_center(bool p_enabled) {
  163. draw_center = p_enabled;
  164. emit_changed();
  165. }
  166. bool StyleBoxTexture::is_draw_center_enabled() const {
  167. return draw_center;
  168. }
  169. Size2 StyleBoxTexture::get_center_size() const {
  170. if (texture.is_null()) {
  171. return Size2();
  172. }
  173. return region_rect.size - get_minimum_size();
  174. }
  175. void StyleBoxTexture::set_expand_margin_size(Side p_side, float p_size) {
  176. ERR_FAIL_INDEX((int)p_side, 4);
  177. expand_margin[p_side] = p_size;
  178. emit_changed();
  179. }
  180. void StyleBoxTexture::set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) {
  181. expand_margin[SIDE_LEFT] = p_left;
  182. expand_margin[SIDE_TOP] = p_top;
  183. expand_margin[SIDE_RIGHT] = p_right;
  184. expand_margin[SIDE_BOTTOM] = p_bottom;
  185. emit_changed();
  186. }
  187. void StyleBoxTexture::set_expand_margin_size_all(float p_expand_margin_size) {
  188. for (int i = 0; i < 4; i++) {
  189. expand_margin[i] = p_expand_margin_size;
  190. }
  191. emit_changed();
  192. }
  193. float StyleBoxTexture::get_expand_margin_size(Side p_side) const {
  194. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  195. return expand_margin[p_side];
  196. }
  197. void StyleBoxTexture::set_region_rect(const Rect2 &p_region_rect) {
  198. if (region_rect == p_region_rect) {
  199. return;
  200. }
  201. region_rect = p_region_rect;
  202. emit_changed();
  203. }
  204. Rect2 StyleBoxTexture::get_region_rect() const {
  205. return region_rect;
  206. }
  207. void StyleBoxTexture::set_h_axis_stretch_mode(AxisStretchMode p_mode) {
  208. ERR_FAIL_INDEX((int)p_mode, 3);
  209. axis_h = p_mode;
  210. emit_changed();
  211. }
  212. StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_h_axis_stretch_mode() const {
  213. return axis_h;
  214. }
  215. void StyleBoxTexture::set_v_axis_stretch_mode(AxisStretchMode p_mode) {
  216. ERR_FAIL_INDEX((int)p_mode, 3);
  217. axis_v = p_mode;
  218. emit_changed();
  219. }
  220. StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_v_axis_stretch_mode() const {
  221. return axis_v;
  222. }
  223. void StyleBoxTexture::set_modulate(const Color &p_modulate) {
  224. if (modulate == p_modulate) {
  225. return;
  226. }
  227. modulate = p_modulate;
  228. emit_changed();
  229. }
  230. Color StyleBoxTexture::get_modulate() const {
  231. return modulate;
  232. }
  233. void StyleBoxTexture::_bind_methods() {
  234. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &StyleBoxTexture::set_texture);
  235. ClassDB::bind_method(D_METHOD("get_texture"), &StyleBoxTexture::get_texture);
  236. ClassDB::bind_method(D_METHOD("set_margin_size", "margin", "size"), &StyleBoxTexture::set_margin_size);
  237. ClassDB::bind_method(D_METHOD("get_margin_size", "margin"), &StyleBoxTexture::get_margin_size);
  238. ClassDB::bind_method(D_METHOD("set_expand_margin_size", "margin", "size"), &StyleBoxTexture::set_expand_margin_size);
  239. ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxTexture::set_expand_margin_size_all);
  240. ClassDB::bind_method(D_METHOD("set_expand_margin_individual", "size_left", "size_top", "size_right", "size_bottom"), &StyleBoxTexture::set_expand_margin_size_individual);
  241. ClassDB::bind_method(D_METHOD("get_expand_margin_size", "margin"), &StyleBoxTexture::get_expand_margin_size);
  242. ClassDB::bind_method(D_METHOD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect);
  243. ClassDB::bind_method(D_METHOD("get_region_rect"), &StyleBoxTexture::get_region_rect);
  244. ClassDB::bind_method(D_METHOD("set_draw_center", "enable"), &StyleBoxTexture::set_draw_center);
  245. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxTexture::is_draw_center_enabled);
  246. ClassDB::bind_method(D_METHOD("set_modulate", "color"), &StyleBoxTexture::set_modulate);
  247. ClassDB::bind_method(D_METHOD("get_modulate"), &StyleBoxTexture::get_modulate);
  248. ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &StyleBoxTexture::set_h_axis_stretch_mode);
  249. ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &StyleBoxTexture::get_h_axis_stretch_mode);
  250. ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &StyleBoxTexture::set_v_axis_stretch_mode);
  251. ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &StyleBoxTexture::get_v_axis_stretch_mode);
  252. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  253. ADD_GROUP("Margins", "margin_");
  254. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_margin_size", "get_margin_size", SIDE_LEFT);
  255. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_margin_size", "get_margin_size", SIDE_TOP);
  256. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_margin_size", "get_margin_size", SIDE_RIGHT);
  257. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_margin_size", "get_margin_size", SIDE_BOTTOM);
  258. ADD_GROUP("Expand Margins", "expand_margin_");
  259. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin_size", "get_expand_margin_size", SIDE_LEFT);
  260. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin_size", "get_expand_margin_size", SIDE_TOP);
  261. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin_size", "get_expand_margin_size", SIDE_RIGHT);
  262. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin_size", "get_expand_margin_size", SIDE_BOTTOM);
  263. ADD_GROUP("Axis Stretch", "axis_stretch_");
  264. 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");
  265. 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");
  266. ADD_GROUP("Sub-Region", "region_");
  267. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect");
  268. ADD_GROUP("Modulate", "modulate_");
  269. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate_color"), "set_modulate", "get_modulate");
  270. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  271. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
  272. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
  273. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
  274. }
  275. StyleBoxTexture::StyleBoxTexture() {}
  276. StyleBoxTexture::~StyleBoxTexture() {}
  277. ////////////////
  278. void StyleBoxFlat::set_bg_color(const Color &p_color) {
  279. bg_color = p_color;
  280. emit_changed();
  281. }
  282. Color StyleBoxFlat::get_bg_color() const {
  283. return bg_color;
  284. }
  285. void StyleBoxFlat::set_border_color(const Color &p_color) {
  286. border_color = p_color;
  287. emit_changed();
  288. }
  289. Color StyleBoxFlat::get_border_color() const {
  290. return border_color;
  291. }
  292. void StyleBoxFlat::set_border_width_all(int p_size) {
  293. border_width[0] = p_size;
  294. border_width[1] = p_size;
  295. border_width[2] = p_size;
  296. border_width[3] = p_size;
  297. emit_changed();
  298. }
  299. int StyleBoxFlat::get_border_width_min() const {
  300. return MIN(MIN(border_width[0], border_width[1]), MIN(border_width[2], border_width[3]));
  301. }
  302. void StyleBoxFlat::set_border_width(Side p_side, int p_width) {
  303. ERR_FAIL_INDEX((int)p_side, 4);
  304. border_width[p_side] = p_width;
  305. emit_changed();
  306. }
  307. int StyleBoxFlat::get_border_width(Side p_side) const {
  308. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  309. return border_width[p_side];
  310. }
  311. void StyleBoxFlat::set_border_blend(bool p_blend) {
  312. blend_border = p_blend;
  313. emit_changed();
  314. }
  315. bool StyleBoxFlat::get_border_blend() const {
  316. return blend_border;
  317. }
  318. void StyleBoxFlat::set_corner_radius_all(int radius) {
  319. for (int i = 0; i < 4; i++) {
  320. corner_radius[i] = radius;
  321. }
  322. emit_changed();
  323. }
  324. void StyleBoxFlat::set_corner_radius_individual(const int radius_top_left, const int radius_top_right, const int radius_bottom_right, const int radius_bottom_left) {
  325. corner_radius[0] = radius_top_left;
  326. corner_radius[1] = radius_top_right;
  327. corner_radius[2] = radius_bottom_right;
  328. corner_radius[3] = radius_bottom_left;
  329. emit_changed();
  330. }
  331. void StyleBoxFlat::set_corner_radius(const Corner p_corner, const int radius) {
  332. ERR_FAIL_INDEX((int)p_corner, 4);
  333. corner_radius[p_corner] = radius;
  334. emit_changed();
  335. }
  336. int StyleBoxFlat::get_corner_radius(const Corner p_corner) const {
  337. ERR_FAIL_INDEX_V((int)p_corner, 4, 0);
  338. return corner_radius[p_corner];
  339. }
  340. void StyleBoxFlat::set_expand_margin_size(Side p_side, float p_size) {
  341. ERR_FAIL_INDEX((int)p_side, 4);
  342. expand_margin[p_side] = p_size;
  343. emit_changed();
  344. }
  345. void StyleBoxFlat::set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) {
  346. expand_margin[SIDE_LEFT] = p_left;
  347. expand_margin[SIDE_TOP] = p_top;
  348. expand_margin[SIDE_RIGHT] = p_right;
  349. expand_margin[SIDE_BOTTOM] = p_bottom;
  350. emit_changed();
  351. }
  352. void StyleBoxFlat::set_expand_margin_size_all(float p_expand_margin_size) {
  353. for (int i = 0; i < 4; i++) {
  354. expand_margin[i] = p_expand_margin_size;
  355. }
  356. emit_changed();
  357. }
  358. float StyleBoxFlat::get_expand_margin_size(Side p_side) const {
  359. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  360. return expand_margin[p_side];
  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_skew(Vector2 p_skew) {
  370. skew = p_skew;
  371. emit_changed();
  372. }
  373. Vector2 StyleBoxFlat::get_skew() const {
  374. return skew;
  375. }
  376. void StyleBoxFlat::set_shadow_color(const Color &p_color) {
  377. shadow_color = p_color;
  378. emit_changed();
  379. }
  380. Color StyleBoxFlat::get_shadow_color() const {
  381. return shadow_color;
  382. }
  383. void StyleBoxFlat::set_shadow_size(const int &p_size) {
  384. shadow_size = p_size;
  385. emit_changed();
  386. }
  387. int StyleBoxFlat::get_shadow_size() const {
  388. return shadow_size;
  389. }
  390. void StyleBoxFlat::set_shadow_offset(const Point2 &p_offset) {
  391. shadow_offset = p_offset;
  392. emit_changed();
  393. }
  394. Point2 StyleBoxFlat::get_shadow_offset() const {
  395. return shadow_offset;
  396. }
  397. void StyleBoxFlat::set_anti_aliased(const bool &p_anti_aliased) {
  398. anti_aliased = p_anti_aliased;
  399. emit_changed();
  400. notify_property_list_changed();
  401. }
  402. bool StyleBoxFlat::is_anti_aliased() const {
  403. return anti_aliased;
  404. }
  405. void StyleBoxFlat::set_aa_size(const real_t p_aa_size) {
  406. aa_size = CLAMP(p_aa_size, 0.01, 10);
  407. emit_changed();
  408. }
  409. real_t StyleBoxFlat::get_aa_size() const {
  410. return aa_size;
  411. }
  412. void StyleBoxFlat::set_corner_detail(const int &p_corner_detail) {
  413. corner_detail = CLAMP(p_corner_detail, 1, 20);
  414. emit_changed();
  415. }
  416. int StyleBoxFlat::get_corner_detail() const {
  417. return corner_detail;
  418. }
  419. Size2 StyleBoxFlat::get_center_size() const {
  420. return Size2();
  421. }
  422. inline void set_inner_corner_radius(const Rect2 style_rect, const Rect2 inner_rect, const real_t corner_radius[4], real_t *inner_corner_radius) {
  423. real_t border_left = inner_rect.position.x - style_rect.position.x;
  424. real_t border_top = inner_rect.position.y - style_rect.position.y;
  425. real_t border_right = style_rect.size.width - inner_rect.size.width - border_left;
  426. real_t border_bottom = style_rect.size.height - inner_rect.size.height - border_top;
  427. real_t rad;
  428. // Top left.
  429. rad = MIN(border_top, border_left);
  430. inner_corner_radius[0] = MAX(corner_radius[0] - rad, 0);
  431. // Top right;
  432. rad = MIN(border_top, border_right);
  433. inner_corner_radius[1] = MAX(corner_radius[1] - rad, 0);
  434. // Bottom right.
  435. rad = MIN(border_bottom, border_right);
  436. inner_corner_radius[2] = MAX(corner_radius[2] - rad, 0);
  437. // Bottom left.
  438. rad = MIN(border_bottom, border_left);
  439. inner_corner_radius[3] = MAX(corner_radius[3] - rad, 0);
  440. }
  441. inline void draw_ring(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color> &colors, const Rect2 &style_rect, const real_t corner_radius[4],
  442. const Rect2 &ring_rect, const Rect2 &inner_rect, const Color &inner_color, const Color &outer_color, const int corner_detail, const Vector2 &skew, bool fill_center = false) {
  443. int vert_offset = verts.size();
  444. if (!vert_offset) {
  445. vert_offset = 0;
  446. }
  447. int adapted_corner_detail = (corner_radius[0] == 0 && corner_radius[1] == 0 && corner_radius[2] == 0 && corner_radius[3] == 0) ? 1 : corner_detail;
  448. real_t ring_corner_radius[4];
  449. set_inner_corner_radius(style_rect, ring_rect, corner_radius, ring_corner_radius);
  450. // Corner radius center points.
  451. Vector<Point2> outer_points = {
  452. ring_rect.position + Vector2(ring_corner_radius[0], ring_corner_radius[0]), //tl
  453. Point2(ring_rect.position.x + ring_rect.size.x - ring_corner_radius[1], ring_rect.position.y + ring_corner_radius[1]), //tr
  454. ring_rect.position + ring_rect.size - Vector2(ring_corner_radius[2], ring_corner_radius[2]), //br
  455. Point2(ring_rect.position.x + ring_corner_radius[3], ring_rect.position.y + ring_rect.size.y - ring_corner_radius[3]) //bl
  456. };
  457. real_t inner_corner_radius[4];
  458. set_inner_corner_radius(style_rect, inner_rect, corner_radius, inner_corner_radius);
  459. Vector<Point2> inner_points = {
  460. inner_rect.position + Vector2(inner_corner_radius[0], inner_corner_radius[0]), //tl
  461. Point2(inner_rect.position.x + inner_rect.size.x - inner_corner_radius[1], inner_rect.position.y + inner_corner_radius[1]), //tr
  462. inner_rect.position + inner_rect.size - Vector2(inner_corner_radius[2], inner_corner_radius[2]), //br
  463. Point2(inner_rect.position.x + inner_corner_radius[3], inner_rect.position.y + inner_rect.size.y - inner_corner_radius[3]) //bl
  464. };
  465. // Calculate the vertices.
  466. for (int corner_index = 0; corner_index < 4; corner_index++) {
  467. for (int detail = 0; detail <= adapted_corner_detail; detail++) {
  468. for (int inner_outer = 0; inner_outer < 2; inner_outer++) {
  469. real_t radius;
  470. Color color;
  471. Point2 corner_point;
  472. if (inner_outer == 0) {
  473. radius = inner_corner_radius[corner_index];
  474. color = inner_color;
  475. corner_point = inner_points[corner_index];
  476. } else {
  477. radius = ring_corner_radius[corner_index];
  478. color = outer_color;
  479. corner_point = outer_points[corner_index];
  480. }
  481. const real_t x = radius * (real_t)cos((corner_index + detail / (double)adapted_corner_detail) * (Math_TAU / 4.0) + Math_PI) + corner_point.x;
  482. const real_t y = radius * (real_t)sin((corner_index + detail / (double)adapted_corner_detail) * (Math_TAU / 4.0) + Math_PI) + corner_point.y;
  483. const float x_skew = -skew.x * (y - ring_rect.get_center().y);
  484. const float y_skew = -skew.y * (x - ring_rect.get_center().x);
  485. verts.push_back(Vector2(x + x_skew, y + y_skew));
  486. colors.push_back(color);
  487. }
  488. }
  489. }
  490. int ring_vert_count = verts.size() - vert_offset;
  491. // Fill the indices and the colors for the border.
  492. for (int i = 0; i < ring_vert_count; i++) {
  493. indices.push_back(vert_offset + ((i + 0) % ring_vert_count));
  494. indices.push_back(vert_offset + ((i + 2) % ring_vert_count));
  495. indices.push_back(vert_offset + ((i + 1) % ring_vert_count));
  496. }
  497. if (fill_center) {
  498. //Fill the indices and the colors for the center.
  499. for (int index = 0; index < ring_vert_count / 2; index += 2) {
  500. int i = index;
  501. // Polygon 1.
  502. indices.push_back(vert_offset + i);
  503. indices.push_back(vert_offset + ring_vert_count - 4 - i);
  504. indices.push_back(vert_offset + i + 2);
  505. // Polygon 2.
  506. indices.push_back(vert_offset + i);
  507. indices.push_back(vert_offset + ring_vert_count - 2 - i);
  508. indices.push_back(vert_offset + ring_vert_count - 4 - i);
  509. }
  510. }
  511. }
  512. inline void adapt_values(int p_index_a, int p_index_b, real_t *adapted_values, const real_t *p_values, const real_t p_width, const real_t p_max_a, const real_t p_max_b) {
  513. if (p_values[p_index_a] + p_values[p_index_b] > p_width) {
  514. real_t factor;
  515. real_t new_value;
  516. factor = (real_t)p_width / (real_t)(p_values[p_index_a] + p_values[p_index_b]);
  517. new_value = (p_values[p_index_a] * factor);
  518. if (new_value < adapted_values[p_index_a]) {
  519. adapted_values[p_index_a] = new_value;
  520. }
  521. new_value = (p_values[p_index_b] * factor);
  522. if (new_value < adapted_values[p_index_b]) {
  523. adapted_values[p_index_b] = new_value;
  524. }
  525. } else {
  526. adapted_values[p_index_a] = MIN(p_values[p_index_a], adapted_values[p_index_a]);
  527. adapted_values[p_index_b] = MIN(p_values[p_index_b], adapted_values[p_index_b]);
  528. }
  529. adapted_values[p_index_a] = MIN(p_max_a, adapted_values[p_index_a]);
  530. adapted_values[p_index_b] = MIN(p_max_b, adapted_values[p_index_b]);
  531. }
  532. Rect2 StyleBoxFlat::get_draw_rect(const Rect2 &p_rect) const {
  533. Rect2 draw_rect = p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]);
  534. if (shadow_size > 0) {
  535. Rect2 shadow_rect = draw_rect.grow(shadow_size);
  536. shadow_rect.position += shadow_offset;
  537. draw_rect = draw_rect.merge(shadow_rect);
  538. }
  539. return draw_rect;
  540. }
  541. void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  542. bool draw_border = (border_width[0] > 0) || (border_width[1] > 0) || (border_width[2] > 0) || (border_width[3] > 0);
  543. bool draw_shadow = (shadow_size > 0);
  544. if (!draw_border && !draw_center && !draw_shadow) {
  545. return;
  546. }
  547. Rect2 style_rect = p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]);
  548. if (Math::is_zero_approx(style_rect.size.width) || Math::is_zero_approx(style_rect.size.height)) {
  549. return;
  550. }
  551. const bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0);
  552. // Only enable antialiasing if it is actually needed. This improve performances
  553. // and maximizes sharpness for non-skewed StyleBoxes with sharp corners.
  554. const bool aa_on = (rounded_corners || !skew.is_equal_approx(Vector2())) && anti_aliased;
  555. const bool blend_on = blend_border && draw_border;
  556. Color border_color_alpha = Color(border_color.r, border_color.g, border_color.b, 0);
  557. Color border_color_blend = (draw_center ? bg_color : border_color_alpha);
  558. Color border_color_inner = blend_on ? border_color_blend : border_color;
  559. // Adapt borders (prevent weird overlapping/glitchy drawings).
  560. real_t width = MAX(style_rect.size.width, 0);
  561. real_t height = MAX(style_rect.size.height, 0);
  562. real_t adapted_border[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 };
  563. adapt_values(SIDE_TOP, SIDE_BOTTOM, adapted_border, border_width, height, height, height);
  564. adapt_values(SIDE_LEFT, SIDE_RIGHT, adapted_border, border_width, width, width, width);
  565. // Adapt corners (prevent weird overlapping/glitchy drawings).
  566. real_t adapted_corner[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 };
  567. adapt_values(CORNER_TOP_RIGHT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, height, height - adapted_border[SIDE_BOTTOM], height - adapted_border[SIDE_TOP]);
  568. adapt_values(CORNER_TOP_LEFT, CORNER_BOTTOM_LEFT, adapted_corner, corner_radius, height, height - adapted_border[SIDE_BOTTOM], height - adapted_border[SIDE_TOP]);
  569. adapt_values(CORNER_TOP_LEFT, CORNER_TOP_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[SIDE_RIGHT], width - adapted_border[SIDE_LEFT]);
  570. adapt_values(CORNER_BOTTOM_LEFT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[SIDE_RIGHT], width - adapted_border[SIDE_LEFT]);
  571. Rect2 infill_rect = style_rect.grow_individual(-adapted_border[SIDE_LEFT], -adapted_border[SIDE_TOP], -adapted_border[SIDE_RIGHT], -adapted_border[SIDE_BOTTOM]);
  572. Rect2 border_style_rect = style_rect;
  573. if (aa_on) {
  574. for (int i = 0; i < 4; i++) {
  575. if (border_width[i] > 0) {
  576. border_style_rect = border_style_rect.grow_side((Side)i, -aa_size);
  577. }
  578. }
  579. }
  580. Vector<Point2> verts;
  581. Vector<int> indices;
  582. Vector<Color> colors;
  583. Vector<Point2> uvs;
  584. // Create shadow
  585. if (draw_shadow) {
  586. Rect2 shadow_inner_rect = style_rect;
  587. shadow_inner_rect.position += shadow_offset;
  588. Rect2 shadow_rect = style_rect.grow(shadow_size);
  589. shadow_rect.position += shadow_offset;
  590. Color shadow_color_transparent = Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
  591. draw_ring(verts, indices, colors, shadow_inner_rect, adapted_corner,
  592. shadow_rect, shadow_inner_rect, shadow_color, shadow_color_transparent, corner_detail, skew);
  593. if (draw_center) {
  594. draw_ring(verts, indices, colors, shadow_inner_rect, adapted_corner,
  595. shadow_inner_rect, shadow_inner_rect, shadow_color, shadow_color, corner_detail, skew, true);
  596. }
  597. }
  598. // Create border (no AA).
  599. if (draw_border && !aa_on) {
  600. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  601. border_style_rect, infill_rect, border_color_inner, border_color, corner_detail, skew);
  602. }
  603. // Create infill (no AA).
  604. if (draw_center && (!aa_on || blend_on || !draw_border)) {
  605. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  606. infill_rect, infill_rect, bg_color, bg_color, corner_detail, skew, true);
  607. }
  608. if (aa_on) {
  609. real_t aa_border_width[4];
  610. real_t aa_fill_width[4];
  611. if (draw_border) {
  612. for (int i = 0; i < 4; i++) {
  613. if (border_width[i] > 0) {
  614. aa_border_width[i] = aa_size;
  615. aa_fill_width[i] = 0;
  616. } else {
  617. aa_border_width[i] = 0;
  618. aa_fill_width[i] = aa_size;
  619. }
  620. }
  621. } else {
  622. for (int i = 0; i < 4; i++) {
  623. aa_border_width[i] = 0;
  624. aa_fill_width[i] = aa_size;
  625. }
  626. }
  627. Rect2 infill_inner_rect = infill_rect.grow_individual(-aa_border_width[SIDE_LEFT], -aa_border_width[SIDE_TOP],
  628. -aa_border_width[SIDE_RIGHT], -aa_border_width[SIDE_BOTTOM]);
  629. if (draw_center) {
  630. if (!blend_on && draw_border) {
  631. Rect2 infill_inner_rect_aa = infill_inner_rect.grow_individual(aa_border_width[SIDE_LEFT], aa_border_width[SIDE_TOP],
  632. aa_border_width[SIDE_RIGHT], aa_border_width[SIDE_BOTTOM]);
  633. // Create infill within AA border.
  634. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  635. infill_inner_rect_aa, infill_inner_rect_aa, bg_color, bg_color, corner_detail, skew, true);
  636. }
  637. if (!blend_on || !draw_border) {
  638. Rect2 infill_rect_aa = infill_rect.grow_individual(aa_fill_width[SIDE_LEFT], aa_fill_width[SIDE_TOP],
  639. aa_fill_width[SIDE_RIGHT], aa_fill_width[SIDE_BOTTOM]);
  640. Color alpha_bg = Color(bg_color.r, bg_color.g, bg_color.b, 0);
  641. // Create infill fake AA gradient.
  642. draw_ring(verts, indices, colors, style_rect, adapted_corner,
  643. infill_rect_aa, infill_rect, bg_color, alpha_bg, corner_detail, skew);
  644. }
  645. }
  646. if (draw_border) {
  647. Rect2 infill_rect_aa = infill_rect.grow_individual(aa_border_width[SIDE_LEFT], aa_border_width[SIDE_TOP],
  648. aa_border_width[SIDE_RIGHT], aa_border_width[SIDE_BOTTOM]);
  649. Rect2 style_rect_aa = style_rect.grow_individual(aa_border_width[SIDE_LEFT], aa_border_width[SIDE_TOP],
  650. aa_border_width[SIDE_RIGHT], aa_border_width[SIDE_BOTTOM]);
  651. Rect2 border_style_rect_aa = border_style_rect.grow_individual(aa_border_width[SIDE_LEFT], aa_border_width[SIDE_TOP],
  652. aa_border_width[SIDE_RIGHT], aa_border_width[SIDE_BOTTOM]);
  653. // Create border.
  654. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  655. border_style_rect_aa, ((blend_on) ? infill_rect : infill_rect_aa), border_color_inner, border_color, corner_detail, skew);
  656. if (!blend_on) {
  657. // Create inner border fake AA gradient.
  658. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  659. infill_rect_aa, infill_rect, border_color_blend, border_color, corner_detail, skew);
  660. }
  661. // Create outer border fake AA gradient.
  662. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  663. style_rect_aa, border_style_rect_aa, border_color, border_color_alpha, corner_detail, skew);
  664. }
  665. }
  666. // Compute UV coordinates.
  667. Rect2 uv_rect = style_rect.grow(aa_on ? aa_size : 0);
  668. uvs.resize(verts.size());
  669. for (int i = 0; i < verts.size(); i++) {
  670. uvs.write[i].x = (verts[i].x - uv_rect.position.x) / uv_rect.size.width;
  671. uvs.write[i].y = (verts[i].y - uv_rect.position.y) / uv_rect.size.height;
  672. }
  673. // Draw stylebox.
  674. RenderingServer *vs = RenderingServer::get_singleton();
  675. vs->canvas_item_add_triangle_array(p_canvas_item, indices, verts, colors, uvs);
  676. }
  677. float StyleBoxFlat::get_style_margin(Side p_side) const {
  678. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  679. return border_width[p_side];
  680. }
  681. void StyleBoxFlat::_validate_property(PropertyInfo &p_property) const {
  682. if (!anti_aliased && p_property.name == "anti_aliasing_size") {
  683. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  684. }
  685. }
  686. void StyleBoxFlat::_bind_methods() {
  687. ClassDB::bind_method(D_METHOD("set_bg_color", "color"), &StyleBoxFlat::set_bg_color);
  688. ClassDB::bind_method(D_METHOD("get_bg_color"), &StyleBoxFlat::get_bg_color);
  689. ClassDB::bind_method(D_METHOD("set_border_color", "color"), &StyleBoxFlat::set_border_color);
  690. ClassDB::bind_method(D_METHOD("get_border_color"), &StyleBoxFlat::get_border_color);
  691. ClassDB::bind_method(D_METHOD("set_border_width_all", "width"), &StyleBoxFlat::set_border_width_all);
  692. ClassDB::bind_method(D_METHOD("get_border_width_min"), &StyleBoxFlat::get_border_width_min);
  693. ClassDB::bind_method(D_METHOD("set_border_width", "margin", "width"), &StyleBoxFlat::set_border_width);
  694. ClassDB::bind_method(D_METHOD("get_border_width", "margin"), &StyleBoxFlat::get_border_width);
  695. ClassDB::bind_method(D_METHOD("set_border_blend", "blend"), &StyleBoxFlat::set_border_blend);
  696. ClassDB::bind_method(D_METHOD("get_border_blend"), &StyleBoxFlat::get_border_blend);
  697. 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);
  698. ClassDB::bind_method(D_METHOD("set_corner_radius_all", "radius"), &StyleBoxFlat::set_corner_radius_all);
  699. ClassDB::bind_method(D_METHOD("set_corner_radius", "corner", "radius"), &StyleBoxFlat::set_corner_radius);
  700. ClassDB::bind_method(D_METHOD("get_corner_radius", "corner"), &StyleBoxFlat::get_corner_radius);
  701. ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxFlat::set_expand_margin_size);
  702. ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxFlat::set_expand_margin_size_all);
  703. ClassDB::bind_method(D_METHOD("set_expand_margin_individual", "size_left", "size_top", "size_right", "size_bottom"), &StyleBoxFlat::set_expand_margin_size_individual);
  704. ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxFlat::get_expand_margin_size);
  705. ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &StyleBoxFlat::set_draw_center);
  706. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxFlat::is_draw_center_enabled);
  707. ClassDB::bind_method(D_METHOD("set_skew", "skew"), &StyleBoxFlat::set_skew);
  708. ClassDB::bind_method(D_METHOD("get_skew"), &StyleBoxFlat::get_skew);
  709. ClassDB::bind_method(D_METHOD("set_shadow_color", "color"), &StyleBoxFlat::set_shadow_color);
  710. ClassDB::bind_method(D_METHOD("get_shadow_color"), &StyleBoxFlat::get_shadow_color);
  711. ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &StyleBoxFlat::set_shadow_size);
  712. ClassDB::bind_method(D_METHOD("get_shadow_size"), &StyleBoxFlat::get_shadow_size);
  713. ClassDB::bind_method(D_METHOD("set_shadow_offset", "offset"), &StyleBoxFlat::set_shadow_offset);
  714. ClassDB::bind_method(D_METHOD("get_shadow_offset"), &StyleBoxFlat::get_shadow_offset);
  715. ClassDB::bind_method(D_METHOD("set_anti_aliased", "anti_aliased"), &StyleBoxFlat::set_anti_aliased);
  716. ClassDB::bind_method(D_METHOD("is_anti_aliased"), &StyleBoxFlat::is_anti_aliased);
  717. ClassDB::bind_method(D_METHOD("set_aa_size", "size"), &StyleBoxFlat::set_aa_size);
  718. ClassDB::bind_method(D_METHOD("get_aa_size"), &StyleBoxFlat::get_aa_size);
  719. ClassDB::bind_method(D_METHOD("set_corner_detail", "detail"), &StyleBoxFlat::set_corner_detail);
  720. ClassDB::bind_method(D_METHOD("get_corner_detail"), &StyleBoxFlat::get_corner_detail);
  721. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), "set_bg_color", "get_bg_color");
  722. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  723. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "skew"), "set_skew", "get_skew");
  724. ADD_GROUP("Border Width", "border_width_");
  725. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_LEFT);
  726. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_top", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_TOP);
  727. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_RIGHT);
  728. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_bottom", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_BOTTOM);
  729. ADD_GROUP("Border", "border_");
  730. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "border_color"), "set_border_color", "get_border_color");
  731. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "border_blend"), "set_border_blend", "get_border_blend");
  732. ADD_GROUP("Corner Radius", "corner_radius_");
  733. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_TOP_LEFT);
  734. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_TOP_RIGHT);
  735. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_RIGHT);
  736. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_LEFT);
  737. ADD_PROPERTY(PropertyInfo(Variant::INT, "corner_detail", PROPERTY_HINT_RANGE, "1,20,1"), "set_corner_detail", "get_corner_detail");
  738. ADD_GROUP("Expand Margins", "expand_margin_");
  739. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_LEFT);
  740. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_TOP);
  741. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_RIGHT);
  742. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM);
  743. ADD_GROUP("Shadow", "shadow_");
  744. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
  745. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_shadow_size", "get_shadow_size");
  746. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shadow_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_shadow_offset", "get_shadow_offset");
  747. ADD_GROUP("Anti Aliasing", "anti_aliasing_");
  748. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "anti_aliasing"), "set_anti_aliased", "is_anti_aliased");
  749. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "anti_aliasing_size", PROPERTY_HINT_RANGE, "0.01,10,0.001,suffix:px"), "set_aa_size", "get_aa_size");
  750. }
  751. StyleBoxFlat::StyleBoxFlat() {}
  752. StyleBoxFlat::~StyleBoxFlat() {}
  753. void StyleBoxLine::set_color(const Color &p_color) {
  754. color = p_color;
  755. emit_changed();
  756. }
  757. Color StyleBoxLine::get_color() const {
  758. return color;
  759. }
  760. void StyleBoxLine::set_thickness(int p_thickness) {
  761. thickness = p_thickness;
  762. emit_changed();
  763. }
  764. int StyleBoxLine::get_thickness() const {
  765. return thickness;
  766. }
  767. void StyleBoxLine::set_vertical(bool p_vertical) {
  768. vertical = p_vertical;
  769. emit_changed();
  770. }
  771. bool StyleBoxLine::is_vertical() const {
  772. return vertical;
  773. }
  774. void StyleBoxLine::set_grow_end(float p_grow_end) {
  775. grow_end = p_grow_end;
  776. emit_changed();
  777. }
  778. float StyleBoxLine::get_grow_end() const {
  779. return grow_end;
  780. }
  781. void StyleBoxLine::set_grow_begin(float p_grow_begin) {
  782. grow_begin = p_grow_begin;
  783. emit_changed();
  784. }
  785. float StyleBoxLine::get_grow_begin() const {
  786. return grow_begin;
  787. }
  788. void StyleBoxLine::_bind_methods() {
  789. ClassDB::bind_method(D_METHOD("set_color", "color"), &StyleBoxLine::set_color);
  790. ClassDB::bind_method(D_METHOD("get_color"), &StyleBoxLine::get_color);
  791. ClassDB::bind_method(D_METHOD("set_thickness", "thickness"), &StyleBoxLine::set_thickness);
  792. ClassDB::bind_method(D_METHOD("get_thickness"), &StyleBoxLine::get_thickness);
  793. ClassDB::bind_method(D_METHOD("set_grow_begin", "offset"), &StyleBoxLine::set_grow_begin);
  794. ClassDB::bind_method(D_METHOD("get_grow_begin"), &StyleBoxLine::get_grow_begin);
  795. ClassDB::bind_method(D_METHOD("set_grow_end", "offset"), &StyleBoxLine::set_grow_end);
  796. ClassDB::bind_method(D_METHOD("get_grow_end"), &StyleBoxLine::get_grow_end);
  797. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &StyleBoxLine::set_vertical);
  798. ClassDB::bind_method(D_METHOD("is_vertical"), &StyleBoxLine::is_vertical);
  799. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  800. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_begin", PROPERTY_HINT_RANGE, "-300,300,1,suffix:px"), "set_grow_begin", "get_grow_begin");
  801. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_end", PROPERTY_HINT_RANGE, "-300,300,1,suffix:px"), "set_grow_end", "get_grow_end");
  802. ADD_PROPERTY(PropertyInfo(Variant::INT, "thickness", PROPERTY_HINT_RANGE, "0,10,suffix:px"), "set_thickness", "get_thickness");
  803. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  804. }
  805. float StyleBoxLine::get_style_margin(Side p_side) const {
  806. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  807. if (vertical) {
  808. if (p_side == SIDE_LEFT || p_side == SIDE_RIGHT) {
  809. return thickness / 2.0;
  810. }
  811. } else if (p_side == SIDE_TOP || p_side == SIDE_BOTTOM) {
  812. return thickness / 2.0;
  813. }
  814. return 0;
  815. }
  816. Size2 StyleBoxLine::get_center_size() const {
  817. return Size2();
  818. }
  819. void StyleBoxLine::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  820. RenderingServer *vs = RenderingServer::get_singleton();
  821. Rect2i r = p_rect;
  822. if (vertical) {
  823. r.position.y -= grow_begin;
  824. r.size.y += (grow_begin + grow_end);
  825. r.size.x = thickness;
  826. } else {
  827. r.position.x -= grow_begin;
  828. r.size.x += (grow_begin + grow_end);
  829. r.size.y = thickness;
  830. }
  831. vs->canvas_item_add_rect(p_canvas_item, r, color);
  832. }
  833. StyleBoxLine::StyleBoxLine() {}
  834. StyleBoxLine::~StyleBoxLine() {}