style_box_flat.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /**************************************************************************/
  2. /* style_box_flat.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_flat.h"
  31. #include "servers/rendering_server.h"
  32. float StyleBoxFlat::get_style_margin(Side p_side) const {
  33. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  34. return border_width[p_side];
  35. }
  36. void StyleBoxFlat::_validate_property(PropertyInfo &p_property) const {
  37. if (!anti_aliased && p_property.name == "anti_aliasing_size") {
  38. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  39. }
  40. }
  41. void StyleBoxFlat::set_bg_color(const Color &p_color) {
  42. bg_color = p_color;
  43. emit_changed();
  44. }
  45. Color StyleBoxFlat::get_bg_color() const {
  46. return bg_color;
  47. }
  48. void StyleBoxFlat::set_border_color(const Color &p_color) {
  49. border_color = p_color;
  50. emit_changed();
  51. }
  52. Color StyleBoxFlat::get_border_color() const {
  53. return border_color;
  54. }
  55. void StyleBoxFlat::set_border_width_all(int p_size) {
  56. border_width[0] = p_size;
  57. border_width[1] = p_size;
  58. border_width[2] = p_size;
  59. border_width[3] = p_size;
  60. emit_changed();
  61. }
  62. int StyleBoxFlat::get_border_width_min() const {
  63. return MIN(MIN(border_width[0], border_width[1]), MIN(border_width[2], border_width[3]));
  64. }
  65. void StyleBoxFlat::set_border_width(Side p_side, int p_width) {
  66. ERR_FAIL_INDEX((int)p_side, 4);
  67. border_width[p_side] = p_width;
  68. emit_changed();
  69. }
  70. int StyleBoxFlat::get_border_width(Side p_side) const {
  71. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  72. return border_width[p_side];
  73. }
  74. void StyleBoxFlat::set_border_blend(bool p_blend) {
  75. blend_border = p_blend;
  76. emit_changed();
  77. }
  78. bool StyleBoxFlat::get_border_blend() const {
  79. return blend_border;
  80. }
  81. void StyleBoxFlat::set_corner_radius(const Corner p_corner, const int radius) {
  82. ERR_FAIL_INDEX((int)p_corner, 4);
  83. corner_radius[p_corner] = radius;
  84. emit_changed();
  85. }
  86. void StyleBoxFlat::set_corner_radius_all(int radius) {
  87. for (int i = 0; i < 4; i++) {
  88. corner_radius[i] = radius;
  89. }
  90. emit_changed();
  91. }
  92. 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) {
  93. corner_radius[0] = radius_top_left;
  94. corner_radius[1] = radius_top_right;
  95. corner_radius[2] = radius_bottom_right;
  96. corner_radius[3] = radius_bottom_left;
  97. emit_changed();
  98. }
  99. int StyleBoxFlat::get_corner_radius(const Corner p_corner) const {
  100. ERR_FAIL_INDEX_V((int)p_corner, 4, 0);
  101. return corner_radius[p_corner];
  102. }
  103. void StyleBoxFlat::set_corner_detail(const int &p_corner_detail) {
  104. corner_detail = CLAMP(p_corner_detail, 1, 20);
  105. emit_changed();
  106. }
  107. int StyleBoxFlat::get_corner_detail() const {
  108. return corner_detail;
  109. }
  110. void StyleBoxFlat::set_expand_margin(Side p_side, float p_size) {
  111. ERR_FAIL_INDEX((int)p_side, 4);
  112. expand_margin[p_side] = p_size;
  113. emit_changed();
  114. }
  115. void StyleBoxFlat::set_expand_margin_all(float p_expand_margin_size) {
  116. for (int i = 0; i < 4; i++) {
  117. expand_margin[i] = p_expand_margin_size;
  118. }
  119. emit_changed();
  120. }
  121. void StyleBoxFlat::set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom) {
  122. expand_margin[SIDE_LEFT] = p_left;
  123. expand_margin[SIDE_TOP] = p_top;
  124. expand_margin[SIDE_RIGHT] = p_right;
  125. expand_margin[SIDE_BOTTOM] = p_bottom;
  126. emit_changed();
  127. }
  128. float StyleBoxFlat::get_expand_margin(Side p_side) const {
  129. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  130. return expand_margin[p_side];
  131. }
  132. void StyleBoxFlat::set_draw_center(bool p_enabled) {
  133. draw_center = p_enabled;
  134. emit_changed();
  135. }
  136. bool StyleBoxFlat::is_draw_center_enabled() const {
  137. return draw_center;
  138. }
  139. void StyleBoxFlat::set_skew(Vector2 p_skew) {
  140. skew = p_skew;
  141. emit_changed();
  142. }
  143. Vector2 StyleBoxFlat::get_skew() const {
  144. return skew;
  145. }
  146. void StyleBoxFlat::set_shadow_color(const Color &p_color) {
  147. shadow_color = p_color;
  148. emit_changed();
  149. }
  150. Color StyleBoxFlat::get_shadow_color() const {
  151. return shadow_color;
  152. }
  153. void StyleBoxFlat::set_shadow_size(const int &p_size) {
  154. shadow_size = p_size;
  155. emit_changed();
  156. }
  157. int StyleBoxFlat::get_shadow_size() const {
  158. return shadow_size;
  159. }
  160. void StyleBoxFlat::set_shadow_offset(const Point2 &p_offset) {
  161. shadow_offset = p_offset;
  162. emit_changed();
  163. }
  164. Point2 StyleBoxFlat::get_shadow_offset() const {
  165. return shadow_offset;
  166. }
  167. void StyleBoxFlat::set_anti_aliased(const bool &p_anti_aliased) {
  168. anti_aliased = p_anti_aliased;
  169. emit_changed();
  170. notify_property_list_changed();
  171. }
  172. bool StyleBoxFlat::is_anti_aliased() const {
  173. return anti_aliased;
  174. }
  175. void StyleBoxFlat::set_aa_size(const real_t p_aa_size) {
  176. aa_size = CLAMP(p_aa_size, 0.01, 10);
  177. emit_changed();
  178. }
  179. real_t StyleBoxFlat::get_aa_size() const {
  180. return aa_size;
  181. }
  182. 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) {
  183. real_t border_left = inner_rect.position.x - style_rect.position.x;
  184. real_t border_top = inner_rect.position.y - style_rect.position.y;
  185. real_t border_right = style_rect.size.width - inner_rect.size.width - border_left;
  186. real_t border_bottom = style_rect.size.height - inner_rect.size.height - border_top;
  187. inner_corner_radius[0] = MAX(corner_radius[0] - MIN(border_top, border_left), 0); // Top left.
  188. inner_corner_radius[1] = MAX(corner_radius[1] - MIN(border_top, border_right), 0); // Top right.
  189. inner_corner_radius[2] = MAX(corner_radius[2] - MIN(border_bottom, border_right), 0); // Bottom right.
  190. inner_corner_radius[3] = MAX(corner_radius[3] - MIN(border_bottom, border_left), 0); // Bottom left.
  191. }
  192. inline void draw_rounded_rectangle(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color> &colors, const Rect2 &style_rect, const real_t corner_radius[4],
  193. const Rect2 &ring_rect, const Rect2 &inner_rect, const Color &inner_color, const Color &outer_color, const int corner_detail, const Vector2 &skew, bool is_filled = false) {
  194. int vert_offset = verts.size();
  195. int adapted_corner_detail = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0) ? corner_detail : 1;
  196. bool draw_border = !is_filled;
  197. real_t ring_corner_radius[4];
  198. set_inner_corner_radius(style_rect, ring_rect, corner_radius, ring_corner_radius);
  199. // Corner radius center points.
  200. Vector<Point2> outer_points = {
  201. ring_rect.position + Vector2(ring_corner_radius[0], ring_corner_radius[0]), //tl
  202. Point2(ring_rect.position.x + ring_rect.size.x - ring_corner_radius[1], ring_rect.position.y + ring_corner_radius[1]), //tr
  203. ring_rect.position + ring_rect.size - Vector2(ring_corner_radius[2], ring_corner_radius[2]), //br
  204. Point2(ring_rect.position.x + ring_corner_radius[3], ring_rect.position.y + ring_rect.size.y - ring_corner_radius[3]) //bl
  205. };
  206. real_t inner_corner_radius[4];
  207. set_inner_corner_radius(style_rect, inner_rect, corner_radius, inner_corner_radius);
  208. Vector<Point2> inner_points = {
  209. inner_rect.position + Vector2(inner_corner_radius[0], inner_corner_radius[0]), //tl
  210. Point2(inner_rect.position.x + inner_rect.size.x - inner_corner_radius[1], inner_rect.position.y + inner_corner_radius[1]), //tr
  211. inner_rect.position + inner_rect.size - Vector2(inner_corner_radius[2], inner_corner_radius[2]), //br
  212. Point2(inner_rect.position.x + inner_corner_radius[3], inner_rect.position.y + inner_rect.size.y - inner_corner_radius[3]) //bl
  213. };
  214. // Calculate the vertices.
  215. // If the center is filled, we do not draw the border and directly use the inner ring as reference. Because all calls to this
  216. // method either draw a ring or a filled rounded rectangle, but not both.
  217. real_t quarter_arc_rad = Math_PI / 2.0;
  218. Point2 style_rect_center = style_rect.get_center();
  219. int colors_size = colors.size();
  220. int verts_size = verts.size();
  221. int new_verts_amount = (adapted_corner_detail + 1) * (draw_border ? 8 : 4);
  222. colors.resize(colors_size + new_verts_amount);
  223. verts.resize(verts_size + new_verts_amount);
  224. Color *colors_ptr = colors.ptrw();
  225. Vector2 *verts_ptr = verts.ptrw();
  226. for (int corner_idx = 0; corner_idx < 4; corner_idx++) {
  227. for (int detail = 0; detail <= adapted_corner_detail; detail++) {
  228. int idx_ofs = (adapted_corner_detail + 1) * corner_idx + detail;
  229. if (draw_border) {
  230. idx_ofs *= 2;
  231. }
  232. const real_t pt_angle = (corner_idx + detail / (double)adapted_corner_detail) * quarter_arc_rad + Math_PI;
  233. const real_t angle_cosine = cos(pt_angle);
  234. const real_t angle_sine = sin(pt_angle);
  235. {
  236. const real_t x = inner_corner_radius[corner_idx] * angle_cosine + inner_points[corner_idx].x;
  237. const real_t y = inner_corner_radius[corner_idx] * angle_sine + inner_points[corner_idx].y;
  238. const float x_skew = -skew.x * (y - style_rect_center.y);
  239. const float y_skew = -skew.y * (x - style_rect_center.x);
  240. verts_ptr[verts_size + idx_ofs] = Vector2(x + x_skew, y + y_skew);
  241. colors_ptr[colors_size + idx_ofs] = inner_color;
  242. }
  243. if (draw_border) {
  244. const real_t x = ring_corner_radius[corner_idx] * angle_cosine + outer_points[corner_idx].x;
  245. const real_t y = ring_corner_radius[corner_idx] * angle_sine + outer_points[corner_idx].y;
  246. const float x_skew = -skew.x * (y - style_rect_center.y);
  247. const float y_skew = -skew.y * (x - style_rect_center.x);
  248. verts_ptr[verts_size + idx_ofs + 1] = Vector2(x + x_skew, y + y_skew);
  249. colors_ptr[colors_size + idx_ofs + 1] = outer_color;
  250. }
  251. }
  252. }
  253. int ring_vert_count = verts.size() - vert_offset;
  254. // Fill the indices and the colors for the border.
  255. if (draw_border) {
  256. int indices_size = indices.size();
  257. indices.resize(indices_size + ring_vert_count * 3);
  258. int *indices_ptr = indices.ptrw();
  259. for (int i = 0; i < ring_vert_count; i++) {
  260. int idx_ofs = indices_size + i * 3;
  261. indices_ptr[idx_ofs] = vert_offset + i % ring_vert_count;
  262. indices_ptr[idx_ofs + 1] = vert_offset + (i + 2) % ring_vert_count;
  263. indices_ptr[idx_ofs + 2] = vert_offset + (i + 1) % ring_vert_count;
  264. }
  265. }
  266. if (is_filled) {
  267. // Compute the triangles pattern to draw the rounded rectangle.
  268. // Consists of vertical stripes of two triangles each.
  269. int stripes_count = ring_vert_count / 2 - 1;
  270. int last_vert_id = ring_vert_count - 1;
  271. int indices_size = indices.size();
  272. indices.resize(indices_size + stripes_count * 6);
  273. int *indices_ptr = indices.ptrw();
  274. for (int i = 0; i < stripes_count; i++) {
  275. int idx_ofs = indices_size + i * 6;
  276. // Polygon 1.
  277. indices_ptr[idx_ofs] = vert_offset + i;
  278. indices_ptr[idx_ofs + 1] = vert_offset + last_vert_id - i - 1;
  279. indices_ptr[idx_ofs + 2] = vert_offset + i + 1;
  280. // Polygon 2.
  281. indices_ptr[idx_ofs + 3] = vert_offset + i;
  282. indices_ptr[idx_ofs + 4] = vert_offset + last_vert_id - i;
  283. indices_ptr[idx_ofs + 5] = vert_offset + last_vert_id - i - 1;
  284. }
  285. }
  286. }
  287. 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) {
  288. real_t value_a = p_values[p_index_a];
  289. real_t value_b = p_values[p_index_b];
  290. real_t factor = MIN(1.0, p_width / (value_a + value_b));
  291. adapted_values[p_index_a] = MIN(MIN(value_a * factor, p_max_a), adapted_values[p_index_a]);
  292. adapted_values[p_index_b] = MIN(MIN(value_b * factor, p_max_b), adapted_values[p_index_b]);
  293. }
  294. Rect2 StyleBoxFlat::get_draw_rect(const Rect2 &p_rect) const {
  295. Rect2 draw_rect = p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]);
  296. if (shadow_size > 0) {
  297. Rect2 shadow_rect = draw_rect.grow(shadow_size);
  298. shadow_rect.position += shadow_offset;
  299. draw_rect = draw_rect.merge(shadow_rect);
  300. }
  301. return draw_rect;
  302. }
  303. void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  304. bool draw_border = (border_width[0] > 0) || (border_width[1] > 0) || (border_width[2] > 0) || (border_width[3] > 0);
  305. bool draw_shadow = (shadow_size > 0);
  306. if (!draw_border && !draw_center && !draw_shadow) {
  307. return;
  308. }
  309. Rect2 style_rect = p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]);
  310. if (Math::is_zero_approx(style_rect.size.width) || Math::is_zero_approx(style_rect.size.height)) {
  311. return;
  312. }
  313. const bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0);
  314. // Only enable antialiasing if it is actually needed. This improves performance
  315. // and maximizes sharpness for non-skewed StyleBoxes with sharp corners.
  316. const bool aa_on = (rounded_corners || !skew.is_zero_approx()) && anti_aliased;
  317. const bool blend_on = blend_border && draw_border;
  318. Color border_color_alpha = Color(border_color.r, border_color.g, border_color.b, 0);
  319. Color border_color_blend = (draw_center ? bg_color : border_color_alpha);
  320. Color border_color_inner = blend_on ? border_color_blend : border_color;
  321. // Adapt borders (prevent weird overlapping/glitchy drawings).
  322. real_t width = MAX(style_rect.size.width, 0);
  323. real_t height = MAX(style_rect.size.height, 0);
  324. real_t adapted_border[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 };
  325. adapt_values(SIDE_TOP, SIDE_BOTTOM, adapted_border, border_width, height, height, height);
  326. adapt_values(SIDE_LEFT, SIDE_RIGHT, adapted_border, border_width, width, width, width);
  327. // Adapt corners (prevent weird overlapping/glitchy drawings).
  328. real_t adapted_corner[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 };
  329. adapt_values(CORNER_TOP_RIGHT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, height, height - adapted_border[SIDE_BOTTOM], height - adapted_border[SIDE_TOP]);
  330. adapt_values(CORNER_TOP_LEFT, CORNER_BOTTOM_LEFT, adapted_corner, corner_radius, height, height - adapted_border[SIDE_BOTTOM], height - adapted_border[SIDE_TOP]);
  331. adapt_values(CORNER_TOP_LEFT, CORNER_TOP_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[SIDE_RIGHT], width - adapted_border[SIDE_LEFT]);
  332. adapt_values(CORNER_BOTTOM_LEFT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[SIDE_RIGHT], width - adapted_border[SIDE_LEFT]);
  333. Rect2 infill_rect = style_rect.grow_individual(-adapted_border[SIDE_LEFT], -adapted_border[SIDE_TOP], -adapted_border[SIDE_RIGHT], -adapted_border[SIDE_BOTTOM]);
  334. Rect2 border_style_rect = style_rect;
  335. if (aa_on) {
  336. for (int i = 0; i < 4; i++) {
  337. if (border_width[i] > 0) {
  338. border_style_rect = border_style_rect.grow_side((Side)i, -aa_size);
  339. }
  340. }
  341. }
  342. Vector<Point2> verts;
  343. Vector<int> indices;
  344. Vector<Color> colors;
  345. Vector<Point2> uvs;
  346. // Create shadow.
  347. if (draw_shadow) {
  348. Rect2 shadow_inner_rect = style_rect;
  349. shadow_inner_rect.position += shadow_offset;
  350. Rect2 shadow_rect = style_rect.grow(shadow_size);
  351. shadow_rect.position += shadow_offset;
  352. Color shadow_color_transparent = Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
  353. draw_rounded_rectangle(verts, indices, colors, shadow_inner_rect, adapted_corner,
  354. shadow_rect, shadow_inner_rect, shadow_color, shadow_color_transparent, corner_detail, skew);
  355. if (draw_center) {
  356. draw_rounded_rectangle(verts, indices, colors, shadow_inner_rect, adapted_corner,
  357. shadow_inner_rect, shadow_inner_rect, shadow_color, shadow_color, corner_detail, skew, true);
  358. }
  359. }
  360. // Create border (no AA).
  361. if (draw_border && !aa_on) {
  362. draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner,
  363. border_style_rect, infill_rect, border_color_inner, border_color, corner_detail, skew);
  364. }
  365. // Create infill (no AA).
  366. if (draw_center && (!aa_on || blend_on)) {
  367. draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner,
  368. infill_rect, infill_rect, bg_color, bg_color, corner_detail, skew, true);
  369. }
  370. if (aa_on) {
  371. real_t aa_border_width[4];
  372. real_t aa_border_width_half[4];
  373. real_t aa_fill_width[4];
  374. real_t aa_fill_width_half[4];
  375. if (draw_border) {
  376. for (int i = 0; i < 4; i++) {
  377. if (border_width[i] > 0) {
  378. aa_border_width[i] = aa_size;
  379. aa_border_width_half[i] = aa_size / 2;
  380. aa_fill_width[i] = 0;
  381. aa_fill_width_half[i] = 0;
  382. } else {
  383. aa_border_width[i] = 0;
  384. aa_border_width_half[i] = 0;
  385. aa_fill_width[i] = aa_size;
  386. aa_fill_width_half[i] = aa_size / 2;
  387. }
  388. }
  389. } else {
  390. for (int i = 0; i < 4; i++) {
  391. aa_border_width[i] = 0;
  392. aa_border_width_half[i] = 0;
  393. aa_fill_width[i] = aa_size;
  394. aa_fill_width_half[i] = aa_size / 2;
  395. }
  396. }
  397. if (draw_center) {
  398. // Infill rect, transparent side of antialiasing gradient (base infill rect enlarged by AA size)
  399. Rect2 infill_rect_aa_transparent = infill_rect.grow_individual(aa_fill_width_half[SIDE_LEFT], aa_fill_width_half[SIDE_TOP],
  400. aa_fill_width_half[SIDE_RIGHT], aa_fill_width_half[SIDE_BOTTOM]);
  401. // Infill rect, colored side of antialiasing gradient (base infill rect shrunk by AA size)
  402. Rect2 infill_rect_aa_colored = infill_rect_aa_transparent.grow_individual(-aa_fill_width[SIDE_LEFT], -aa_fill_width[SIDE_TOP],
  403. -aa_fill_width[SIDE_RIGHT], -aa_fill_width[SIDE_BOTTOM]);
  404. if (!blend_on) {
  405. // Create center fill, not antialiased yet
  406. draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner,
  407. infill_rect_aa_colored, infill_rect_aa_colored, bg_color, bg_color, corner_detail, skew, true);
  408. }
  409. if (!blend_on || !draw_border) {
  410. Color alpha_bg = Color(bg_color.r, bg_color.g, bg_color.b, 0);
  411. // Add antialiasing on the center fill
  412. draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner,
  413. infill_rect_aa_transparent, infill_rect_aa_colored, bg_color, alpha_bg, corner_detail, skew);
  414. }
  415. }
  416. if (draw_border) {
  417. // Inner border recct, fully colored side of antialiasing gradient (base inner rect enlarged by AA size)
  418. Rect2 inner_rect_aa_colored = infill_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP],
  419. aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]);
  420. // Inner border rect, transparent side of antialiasing gradient (base inner rect shrunk by AA size)
  421. Rect2 inner_rect_aa_transparent = inner_rect_aa_colored.grow_individual(-aa_border_width[SIDE_LEFT], -aa_border_width[SIDE_TOP],
  422. -aa_border_width[SIDE_RIGHT], -aa_border_width[SIDE_BOTTOM]);
  423. // Outer border rect, transparent side of antialiasing gradient (base outer rect enlarged by AA size)
  424. Rect2 outer_rect_aa_transparent = style_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP],
  425. aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]);
  426. // Outer border rect, colored side of antialiasing gradient (base outer rect shrunk by AA size)
  427. Rect2 outer_rect_aa_colored = border_style_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP],
  428. aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]);
  429. // Create border ring, not antialiased yet
  430. draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner,
  431. outer_rect_aa_colored, ((blend_on) ? infill_rect : inner_rect_aa_colored), border_color_inner, border_color, corner_detail, skew);
  432. if (!blend_on) {
  433. // Add antialiasing on the ring inner border
  434. draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner,
  435. inner_rect_aa_colored, inner_rect_aa_transparent, border_color_blend, border_color, corner_detail, skew);
  436. }
  437. // Add antialiasing on the ring outer border
  438. draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner,
  439. outer_rect_aa_transparent, outer_rect_aa_colored, border_color, border_color_alpha, corner_detail, skew);
  440. }
  441. }
  442. // Compute UV coordinates.
  443. Rect2 uv_rect = style_rect.grow(aa_on ? aa_size : 0);
  444. uvs.resize(verts.size());
  445. Point2 *uvs_ptr = uvs.ptrw();
  446. for (int i = 0; i < verts.size(); i++) {
  447. uvs_ptr[i].x = (verts[i].x - uv_rect.position.x) / uv_rect.size.width;
  448. uvs_ptr[i].y = (verts[i].y - uv_rect.position.y) / uv_rect.size.height;
  449. }
  450. // Draw stylebox.
  451. RenderingServer *vs = RenderingServer::get_singleton();
  452. vs->canvas_item_add_triangle_array(p_canvas_item, indices, verts, colors, uvs);
  453. }
  454. void StyleBoxFlat::_bind_methods() {
  455. ClassDB::bind_method(D_METHOD("set_bg_color", "color"), &StyleBoxFlat::set_bg_color);
  456. ClassDB::bind_method(D_METHOD("get_bg_color"), &StyleBoxFlat::get_bg_color);
  457. ClassDB::bind_method(D_METHOD("set_border_color", "color"), &StyleBoxFlat::set_border_color);
  458. ClassDB::bind_method(D_METHOD("get_border_color"), &StyleBoxFlat::get_border_color);
  459. ClassDB::bind_method(D_METHOD("set_border_width_all", "width"), &StyleBoxFlat::set_border_width_all);
  460. ClassDB::bind_method(D_METHOD("get_border_width_min"), &StyleBoxFlat::get_border_width_min);
  461. ClassDB::bind_method(D_METHOD("set_border_width", "margin", "width"), &StyleBoxFlat::set_border_width);
  462. ClassDB::bind_method(D_METHOD("get_border_width", "margin"), &StyleBoxFlat::get_border_width);
  463. ClassDB::bind_method(D_METHOD("set_border_blend", "blend"), &StyleBoxFlat::set_border_blend);
  464. ClassDB::bind_method(D_METHOD("get_border_blend"), &StyleBoxFlat::get_border_blend);
  465. ClassDB::bind_method(D_METHOD("set_corner_radius_all", "radius"), &StyleBoxFlat::set_corner_radius_all);
  466. ClassDB::bind_method(D_METHOD("set_corner_radius", "corner", "radius"), &StyleBoxFlat::set_corner_radius);
  467. ClassDB::bind_method(D_METHOD("get_corner_radius", "corner"), &StyleBoxFlat::get_corner_radius);
  468. ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxFlat::set_expand_margin);
  469. ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxFlat::set_expand_margin_all);
  470. ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxFlat::get_expand_margin);
  471. ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &StyleBoxFlat::set_draw_center);
  472. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxFlat::is_draw_center_enabled);
  473. ClassDB::bind_method(D_METHOD("set_skew", "skew"), &StyleBoxFlat::set_skew);
  474. ClassDB::bind_method(D_METHOD("get_skew"), &StyleBoxFlat::get_skew);
  475. ClassDB::bind_method(D_METHOD("set_shadow_color", "color"), &StyleBoxFlat::set_shadow_color);
  476. ClassDB::bind_method(D_METHOD("get_shadow_color"), &StyleBoxFlat::get_shadow_color);
  477. ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &StyleBoxFlat::set_shadow_size);
  478. ClassDB::bind_method(D_METHOD("get_shadow_size"), &StyleBoxFlat::get_shadow_size);
  479. ClassDB::bind_method(D_METHOD("set_shadow_offset", "offset"), &StyleBoxFlat::set_shadow_offset);
  480. ClassDB::bind_method(D_METHOD("get_shadow_offset"), &StyleBoxFlat::get_shadow_offset);
  481. ClassDB::bind_method(D_METHOD("set_anti_aliased", "anti_aliased"), &StyleBoxFlat::set_anti_aliased);
  482. ClassDB::bind_method(D_METHOD("is_anti_aliased"), &StyleBoxFlat::is_anti_aliased);
  483. ClassDB::bind_method(D_METHOD("set_aa_size", "size"), &StyleBoxFlat::set_aa_size);
  484. ClassDB::bind_method(D_METHOD("get_aa_size"), &StyleBoxFlat::get_aa_size);
  485. ClassDB::bind_method(D_METHOD("set_corner_detail", "detail"), &StyleBoxFlat::set_corner_detail);
  486. ClassDB::bind_method(D_METHOD("get_corner_detail"), &StyleBoxFlat::get_corner_detail);
  487. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), "set_bg_color", "get_bg_color");
  488. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  489. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "skew"), "set_skew", "get_skew");
  490. ADD_GROUP("Border Width", "border_width_");
  491. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_left", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_border_width", "get_border_width", SIDE_LEFT);
  492. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_top", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_border_width", "get_border_width", SIDE_TOP);
  493. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_right", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_border_width", "get_border_width", SIDE_RIGHT);
  494. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_bottom", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_border_width", "get_border_width", SIDE_BOTTOM);
  495. ADD_GROUP("Border", "border_");
  496. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "border_color"), "set_border_color", "get_border_color");
  497. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "border_blend"), "set_border_blend", "get_border_blend");
  498. ADD_GROUP("Corner Radius", "corner_radius_");
  499. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_left", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_TOP_LEFT);
  500. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_right", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_TOP_RIGHT);
  501. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_right", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_RIGHT);
  502. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_left", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_LEFT);
  503. ADD_PROPERTY(PropertyInfo(Variant::INT, "corner_detail", PROPERTY_HINT_RANGE, "1,20,1"), "set_corner_detail", "get_corner_detail");
  504. ADD_GROUP("Expand Margins", "expand_margin_");
  505. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_LEFT);
  506. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_TOP);
  507. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_RIGHT);
  508. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM);
  509. ADD_GROUP("Shadow", "shadow_");
  510. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
  511. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_shadow_size", "get_shadow_size");
  512. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shadow_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_shadow_offset", "get_shadow_offset");
  513. ADD_GROUP("Anti Aliasing", "anti_aliasing_");
  514. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "anti_aliasing"), "set_anti_aliased", "is_anti_aliased");
  515. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "anti_aliasing_size", PROPERTY_HINT_RANGE, "0.01,10,0.001,suffix:px"), "set_aa_size", "get_aa_size");
  516. }
  517. StyleBoxFlat::StyleBoxFlat() {}
  518. StyleBoxFlat::~StyleBoxFlat() {}