style_box_flat.cpp 28 KB

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