style_box_flat.cpp 33 KB

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