Debian Codegraph @

scene/resources/style_box_flat.cpp

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