style_box_flat.cpp 29 KB

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