style_box.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /**************************************************************************/
  2. /* style_box.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.h"
  31. #include "scene/2d/canvas_item.h"
  32. #include <limits.h>
  33. bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
  34. return true;
  35. }
  36. void StyleBox::set_default_margin(Margin p_margin, float p_value) {
  37. ERR_FAIL_INDEX((int)p_margin, 4);
  38. margin[p_margin] = p_value;
  39. emit_changed();
  40. }
  41. float StyleBox::get_default_margin(Margin p_margin) const {
  42. ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
  43. return margin[p_margin];
  44. }
  45. float StyleBox::get_margin(Margin p_margin) const {
  46. ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
  47. if (margin[p_margin] < 0) {
  48. return get_style_margin(p_margin);
  49. } else {
  50. return margin[p_margin];
  51. }
  52. }
  53. CanvasItem *StyleBox::get_current_item_drawn() const {
  54. return CanvasItem::get_current_item_drawn();
  55. }
  56. Size2 StyleBox::get_minimum_size() const {
  57. return Size2(get_margin(MARGIN_LEFT) + get_margin(MARGIN_RIGHT), get_margin(MARGIN_TOP) + get_margin(MARGIN_BOTTOM));
  58. }
  59. Point2 StyleBox::get_offset() const {
  60. return Point2(get_margin(MARGIN_LEFT), get_margin(MARGIN_TOP));
  61. }
  62. Size2 StyleBox::get_center_size() const {
  63. return Size2();
  64. }
  65. Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const {
  66. return p_rect;
  67. }
  68. void StyleBox::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask);
  70. ClassDB::bind_method(D_METHOD("set_default_margin", "margin", "offset"), &StyleBox::set_default_margin);
  71. ClassDB::bind_method(D_METHOD("get_default_margin", "margin"), &StyleBox::get_default_margin);
  72. ClassDB::bind_method(D_METHOD("get_margin", "margin"), &StyleBox::get_margin);
  73. ClassDB::bind_method(D_METHOD("get_minimum_size"), &StyleBox::get_minimum_size);
  74. ClassDB::bind_method(D_METHOD("get_center_size"), &StyleBox::get_center_size);
  75. ClassDB::bind_method(D_METHOD("get_offset"), &StyleBox::get_offset);
  76. ClassDB::bind_method(D_METHOD("get_current_item_drawn"), &StyleBox::get_current_item_drawn);
  77. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "rect"), &StyleBox::draw);
  78. ADD_GROUP("Content Margin", "content_margin_");
  79. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin_left", PROPERTY_HINT_RANGE, "-1,2048,1"), "set_default_margin", "get_default_margin", MARGIN_LEFT);
  80. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin_right", PROPERTY_HINT_RANGE, "-1,2048,1"), "set_default_margin", "get_default_margin", MARGIN_RIGHT);
  81. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin_top", PROPERTY_HINT_RANGE, "-1,2048,1"), "set_default_margin", "get_default_margin", MARGIN_TOP);
  82. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin_bottom", PROPERTY_HINT_RANGE, "-1,2048,1"), "set_default_margin", "get_default_margin", MARGIN_BOTTOM);
  83. }
  84. StyleBox::StyleBox() {
  85. for (int i = 0; i < 4; i++) {
  86. margin[i] = -1;
  87. }
  88. }
  89. void StyleBoxTexture::set_texture(Ref<Texture> p_texture) {
  90. if (texture == p_texture) {
  91. return;
  92. }
  93. texture = p_texture;
  94. if (p_texture.is_null()) {
  95. region_rect = Rect2(0, 0, 0, 0);
  96. } else {
  97. region_rect = Rect2(Point2(), texture->get_size());
  98. }
  99. emit_signal("texture_changed");
  100. emit_changed();
  101. _change_notify("texture");
  102. }
  103. Ref<Texture> StyleBoxTexture::get_texture() const {
  104. return texture;
  105. }
  106. void StyleBoxTexture::set_normal_map(Ref<Texture> p_normal_map) {
  107. if (normal_map == p_normal_map) {
  108. return;
  109. }
  110. normal_map = p_normal_map;
  111. emit_changed();
  112. }
  113. Ref<Texture> StyleBoxTexture::get_normal_map() const {
  114. return normal_map;
  115. }
  116. void StyleBoxTexture::set_margin_size(Margin p_margin, float p_size) {
  117. ERR_FAIL_INDEX((int)p_margin, 4);
  118. margin[p_margin] = p_size;
  119. emit_changed();
  120. static const char *margin_prop[4] = {
  121. "content_margin_left",
  122. "content_margin_top",
  123. "content_margin_right",
  124. "content_margin_bottom",
  125. };
  126. _change_notify(margin_prop[p_margin]);
  127. }
  128. float StyleBoxTexture::get_margin_size(Margin p_margin) const {
  129. ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
  130. return margin[p_margin];
  131. }
  132. float StyleBoxTexture::get_style_margin(Margin p_margin) const {
  133. ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
  134. return margin[p_margin];
  135. }
  136. Rect2 StyleBoxTexture::get_draw_rect(const Rect2 &p_rect) const {
  137. return p_rect.grow_individual(expand_margin[MARGIN_LEFT], expand_margin[MARGIN_TOP], expand_margin[MARGIN_RIGHT], expand_margin[MARGIN_BOTTOM]);
  138. }
  139. void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  140. if (texture.is_null()) {
  141. return;
  142. }
  143. Rect2 rect = p_rect;
  144. Rect2 src_rect = region_rect;
  145. texture->get_rect_region(rect, src_rect, rect, src_rect);
  146. rect.position.x -= expand_margin[MARGIN_LEFT];
  147. rect.position.y -= expand_margin[MARGIN_TOP];
  148. rect.size.x += expand_margin[MARGIN_LEFT] + expand_margin[MARGIN_RIGHT];
  149. rect.size.y += expand_margin[MARGIN_TOP] + expand_margin[MARGIN_BOTTOM];
  150. RID normal_rid;
  151. if (normal_map.is_valid()) {
  152. normal_rid = normal_map->get_rid();
  153. }
  154. VisualServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), Vector2(margin[MARGIN_LEFT], margin[MARGIN_TOP]), Vector2(margin[MARGIN_RIGHT], margin[MARGIN_BOTTOM]), VS::NinePatchAxisMode(axis_h), VS::NinePatchAxisMode(axis_v), draw_center, modulate, normal_rid);
  155. }
  156. void StyleBoxTexture::set_draw_center(bool p_enabled) {
  157. draw_center = p_enabled;
  158. emit_changed();
  159. }
  160. bool StyleBoxTexture::is_draw_center_enabled() const {
  161. return draw_center;
  162. }
  163. Size2 StyleBoxTexture::get_center_size() const {
  164. if (texture.is_null()) {
  165. return Size2();
  166. }
  167. return region_rect.size - get_minimum_size();
  168. }
  169. void StyleBoxTexture::set_expand_margin_size(Margin p_expand_margin, float p_size) {
  170. ERR_FAIL_INDEX((int)p_expand_margin, 4);
  171. expand_margin[p_expand_margin] = p_size;
  172. emit_changed();
  173. }
  174. void StyleBoxTexture::set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) {
  175. expand_margin[MARGIN_LEFT] = p_left;
  176. expand_margin[MARGIN_TOP] = p_top;
  177. expand_margin[MARGIN_RIGHT] = p_right;
  178. expand_margin[MARGIN_BOTTOM] = p_bottom;
  179. emit_changed();
  180. }
  181. void StyleBoxTexture::set_expand_margin_size_all(float p_expand_margin_size) {
  182. for (int i = 0; i < 4; i++) {
  183. expand_margin[i] = p_expand_margin_size;
  184. }
  185. emit_changed();
  186. }
  187. float StyleBoxTexture::get_expand_margin_size(Margin p_expand_margin) const {
  188. ERR_FAIL_INDEX_V((int)p_expand_margin, 4, 0);
  189. return expand_margin[p_expand_margin];
  190. }
  191. void StyleBoxTexture::set_region_rect(const Rect2 &p_region_rect) {
  192. if (region_rect == p_region_rect) {
  193. return;
  194. }
  195. region_rect = p_region_rect;
  196. emit_changed();
  197. _change_notify("region");
  198. }
  199. Rect2 StyleBoxTexture::get_region_rect() const {
  200. return region_rect;
  201. }
  202. void StyleBoxTexture::set_h_axis_stretch_mode(AxisStretchMode p_mode) {
  203. ERR_FAIL_INDEX((int)p_mode, 3);
  204. axis_h = p_mode;
  205. emit_changed();
  206. }
  207. StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_h_axis_stretch_mode() const {
  208. return axis_h;
  209. }
  210. void StyleBoxTexture::set_v_axis_stretch_mode(AxisStretchMode p_mode) {
  211. ERR_FAIL_INDEX((int)p_mode, 3);
  212. axis_v = p_mode;
  213. emit_changed();
  214. }
  215. StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_v_axis_stretch_mode() const {
  216. return axis_v;
  217. }
  218. void StyleBoxTexture::set_modulate(const Color &p_modulate) {
  219. if (modulate == p_modulate) {
  220. return;
  221. }
  222. modulate = p_modulate;
  223. emit_changed();
  224. }
  225. Color StyleBoxTexture::get_modulate() const {
  226. return modulate;
  227. }
  228. void StyleBoxTexture::_bind_methods() {
  229. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &StyleBoxTexture::set_texture);
  230. ClassDB::bind_method(D_METHOD("get_texture"), &StyleBoxTexture::get_texture);
  231. ClassDB::bind_method(D_METHOD("set_normal_map", "normal_map"), &StyleBoxTexture::set_normal_map);
  232. ClassDB::bind_method(D_METHOD("get_normal_map"), &StyleBoxTexture::get_normal_map);
  233. ClassDB::bind_method(D_METHOD("set_margin_size", "margin", "size"), &StyleBoxTexture::set_margin_size);
  234. ClassDB::bind_method(D_METHOD("get_margin_size", "margin"), &StyleBoxTexture::get_margin_size);
  235. ClassDB::bind_method(D_METHOD("set_expand_margin_size", "margin", "size"), &StyleBoxTexture::set_expand_margin_size);
  236. ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxTexture::set_expand_margin_size_all);
  237. ClassDB::bind_method(D_METHOD("set_expand_margin_individual", "size_left", "size_top", "size_right", "size_bottom"), &StyleBoxTexture::set_expand_margin_size_individual);
  238. ClassDB::bind_method(D_METHOD("get_expand_margin_size", "margin"), &StyleBoxTexture::get_expand_margin_size);
  239. ClassDB::bind_method(D_METHOD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect);
  240. ClassDB::bind_method(D_METHOD("get_region_rect"), &StyleBoxTexture::get_region_rect);
  241. ClassDB::bind_method(D_METHOD("set_draw_center", "enable"), &StyleBoxTexture::set_draw_center);
  242. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxTexture::is_draw_center_enabled);
  243. ClassDB::bind_method(D_METHOD("set_modulate", "color"), &StyleBoxTexture::set_modulate);
  244. ClassDB::bind_method(D_METHOD("get_modulate"), &StyleBoxTexture::get_modulate);
  245. ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &StyleBoxTexture::set_h_axis_stretch_mode);
  246. ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &StyleBoxTexture::get_h_axis_stretch_mode);
  247. ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &StyleBoxTexture::set_v_axis_stretch_mode);
  248. ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &StyleBoxTexture::get_v_axis_stretch_mode);
  249. ADD_SIGNAL(MethodInfo("texture_changed"));
  250. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  251. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_map", "get_normal_map");
  252. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
  253. ADD_GROUP("Margin", "margin_");
  254. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin_left", PROPERTY_HINT_RANGE, "0,2048,1"), "set_margin_size", "get_margin_size", MARGIN_LEFT);
  255. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin_right", PROPERTY_HINT_RANGE, "0,2048,1"), "set_margin_size", "get_margin_size", MARGIN_RIGHT);
  256. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin_top", PROPERTY_HINT_RANGE, "0,2048,1"), "set_margin_size", "get_margin_size", MARGIN_TOP);
  257. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1"), "set_margin_size", "get_margin_size", MARGIN_BOTTOM);
  258. ADD_GROUP("Expand Margin", "expand_margin_");
  259. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin_size", "get_expand_margin_size", MARGIN_LEFT);
  260. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin_size", "get_expand_margin_size", MARGIN_RIGHT);
  261. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin_size", "get_expand_margin_size", MARGIN_TOP);
  262. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin_size", "get_expand_margin_size", MARGIN_BOTTOM);
  263. ADD_GROUP("Axis Stretch", "axis_stretch_");
  264. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode");
  265. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode");
  266. ADD_GROUP("Modulate", "modulate_");
  267. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate_color"), "set_modulate", "get_modulate");
  268. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  269. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
  270. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
  271. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
  272. }
  273. StyleBoxTexture::StyleBoxTexture() {
  274. for (int i = 0; i < 4; i++) {
  275. margin[i] = 0;
  276. expand_margin[i] = 0;
  277. }
  278. draw_center = true;
  279. modulate = Color(1, 1, 1, 1);
  280. axis_h = AXIS_STRETCH_MODE_STRETCH;
  281. axis_v = AXIS_STRETCH_MODE_STRETCH;
  282. }
  283. StyleBoxTexture::~StyleBoxTexture() {
  284. }
  285. ////////////////
  286. void StyleBoxFlat::set_bg_color(const Color &p_color) {
  287. bg_color = p_color;
  288. emit_changed();
  289. }
  290. Color StyleBoxFlat::get_bg_color() const {
  291. return bg_color;
  292. }
  293. void StyleBoxFlat::set_border_color(const Color &p_color) {
  294. border_color = p_color;
  295. emit_changed();
  296. }
  297. Color StyleBoxFlat::get_border_color() const {
  298. return border_color;
  299. }
  300. void StyleBoxFlat::set_border_width_all(int p_size) {
  301. border_width[0] = p_size;
  302. border_width[1] = p_size;
  303. border_width[2] = p_size;
  304. border_width[3] = p_size;
  305. emit_changed();
  306. }
  307. int StyleBoxFlat::get_border_width_min() const {
  308. return MIN(MIN(border_width[0], border_width[1]), MIN(border_width[2], border_width[3]));
  309. }
  310. void StyleBoxFlat::set_border_width(Margin p_margin, int p_width) {
  311. ERR_FAIL_INDEX((int)p_margin, 4);
  312. border_width[p_margin] = p_width;
  313. emit_changed();
  314. }
  315. int StyleBoxFlat::get_border_width(Margin p_margin) const {
  316. ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
  317. return border_width[p_margin];
  318. }
  319. void StyleBoxFlat::set_border_blend(bool p_blend) {
  320. blend_border = p_blend;
  321. emit_changed();
  322. }
  323. bool StyleBoxFlat::get_border_blend() const {
  324. return blend_border;
  325. }
  326. void StyleBoxFlat::set_corner_radius_all(int radius) {
  327. for (int i = 0; i < 4; i++) {
  328. corner_radius[i] = radius;
  329. }
  330. emit_changed();
  331. }
  332. 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) {
  333. corner_radius[0] = radius_top_left;
  334. corner_radius[1] = radius_top_right;
  335. corner_radius[2] = radius_bottom_right;
  336. corner_radius[3] = radius_bottom_left;
  337. emit_changed();
  338. }
  339. int StyleBoxFlat::get_corner_radius_min() const {
  340. int smallest = corner_radius[0];
  341. for (int i = 1; i < 4; i++) {
  342. if (smallest > corner_radius[i]) {
  343. smallest = corner_radius[i];
  344. }
  345. }
  346. return smallest;
  347. }
  348. void StyleBoxFlat::set_corner_radius(const Corner p_corner, const int radius) {
  349. ERR_FAIL_INDEX((int)p_corner, 4);
  350. corner_radius[p_corner] = radius;
  351. emit_changed();
  352. }
  353. int StyleBoxFlat::get_corner_radius(const Corner p_corner) const {
  354. ERR_FAIL_INDEX_V((int)p_corner, 4, 0);
  355. return corner_radius[p_corner];
  356. }
  357. void StyleBoxFlat::set_expand_margin_size(Margin p_expand_margin, float p_size) {
  358. ERR_FAIL_INDEX((int)p_expand_margin, 4);
  359. expand_margin[p_expand_margin] = p_size;
  360. emit_changed();
  361. }
  362. void StyleBoxFlat::set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) {
  363. expand_margin[MARGIN_LEFT] = p_left;
  364. expand_margin[MARGIN_TOP] = p_top;
  365. expand_margin[MARGIN_RIGHT] = p_right;
  366. expand_margin[MARGIN_BOTTOM] = p_bottom;
  367. emit_changed();
  368. }
  369. void StyleBoxFlat::set_expand_margin_size_all(float p_expand_margin_size) {
  370. for (int i = 0; i < 4; i++) {
  371. expand_margin[i] = p_expand_margin_size;
  372. }
  373. emit_changed();
  374. }
  375. float StyleBoxFlat::get_expand_margin_size(Margin p_expand_margin) const {
  376. ERR_FAIL_INDEX_V((int)p_expand_margin, 4, 0.0);
  377. return expand_margin[p_expand_margin];
  378. }
  379. void StyleBoxFlat::set_draw_center(bool p_enabled) {
  380. draw_center = p_enabled;
  381. emit_changed();
  382. }
  383. bool StyleBoxFlat::is_draw_center_enabled() const {
  384. return draw_center;
  385. }
  386. void StyleBoxFlat::set_skew(Vector2 p_skew) {
  387. skew = p_skew;
  388. emit_changed();
  389. }
  390. Vector2 StyleBoxFlat::get_skew() const {
  391. return skew;
  392. }
  393. void StyleBoxFlat::set_shadow_color(const Color &p_color) {
  394. shadow_color = p_color;
  395. emit_changed();
  396. }
  397. Color StyleBoxFlat::get_shadow_color() const {
  398. return shadow_color;
  399. }
  400. void StyleBoxFlat::set_shadow_size(const int &p_size) {
  401. shadow_size = p_size;
  402. emit_changed();
  403. }
  404. int StyleBoxFlat::get_shadow_size() const {
  405. return shadow_size;
  406. }
  407. void StyleBoxFlat::set_shadow_offset(const Point2 &p_offset) {
  408. shadow_offset = p_offset;
  409. emit_changed();
  410. }
  411. Point2 StyleBoxFlat::get_shadow_offset() const {
  412. return shadow_offset;
  413. }
  414. void StyleBoxFlat::set_anti_aliased(const bool &p_anti_aliased) {
  415. anti_aliased = p_anti_aliased;
  416. emit_changed();
  417. }
  418. bool StyleBoxFlat::is_anti_aliased() const {
  419. return anti_aliased;
  420. }
  421. void StyleBoxFlat::set_aa_size(const real_t &p_aa_size) {
  422. aa_size = CLAMP(p_aa_size, 0.01, 10);
  423. emit_changed();
  424. }
  425. float StyleBoxFlat::get_aa_size() const {
  426. return aa_size;
  427. }
  428. void StyleBoxFlat::set_corner_detail(const int &p_corner_detail) {
  429. corner_detail = CLAMP(p_corner_detail, 1, 20);
  430. emit_changed();
  431. }
  432. int StyleBoxFlat::get_corner_detail() const {
  433. return corner_detail;
  434. }
  435. Size2 StyleBoxFlat::get_center_size() const {
  436. return Size2();
  437. }
  438. 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) {
  439. real_t border_left = inner_rect.position.x - style_rect.position.x;
  440. real_t border_top = inner_rect.position.y - style_rect.position.y;
  441. real_t border_right = style_rect.size.width - inner_rect.size.width - border_left;
  442. real_t border_bottom = style_rect.size.height - inner_rect.size.height - border_top;
  443. real_t rad;
  444. // Top left.
  445. rad = MIN(border_top, border_left);
  446. inner_corner_radius[0] = MAX(corner_radius[0] - rad, 0);
  447. // Top right;
  448. rad = MIN(border_top, border_right);
  449. inner_corner_radius[1] = MAX(corner_radius[1] - rad, 0);
  450. // Bottom right.
  451. rad = MIN(border_bottom, border_right);
  452. inner_corner_radius[2] = MAX(corner_radius[2] - rad, 0);
  453. // Bottom left.
  454. rad = MIN(border_bottom, border_left);
  455. inner_corner_radius[3] = MAX(corner_radius[3] - rad, 0);
  456. }
  457. inline void draw_ring(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color> &colors, const Rect2 &style_rect, const real_t corner_radius[4],
  458. const Rect2 &ring_rect, const Rect2 &inner_rect, const Color &inner_color, const Color &outer_color, const int corner_detail, const Vector2 &skew, bool fill_center = false) {
  459. int vert_offset = verts.size();
  460. if (!vert_offset) {
  461. vert_offset = 0;
  462. }
  463. int adapted_corner_detail = (corner_radius[0] == 0 && corner_radius[1] == 0 && corner_radius[2] == 0 && corner_radius[3] == 0) ? 1 : corner_detail;
  464. real_t ring_corner_radius[4];
  465. set_inner_corner_radius(style_rect, ring_rect, corner_radius, ring_corner_radius);
  466. // Corner radius center points.
  467. Vector<Point2> outer_points;
  468. outer_points.push_back(ring_rect.position + Vector2(ring_corner_radius[0], ring_corner_radius[0])); //tl
  469. outer_points.push_back(Point2(ring_rect.position.x + ring_rect.size.x - ring_corner_radius[1], ring_rect.position.y + ring_corner_radius[1])); //tr
  470. outer_points.push_back(ring_rect.position + ring_rect.size - Vector2(ring_corner_radius[2], ring_corner_radius[2])); //br
  471. outer_points.push_back(Point2(ring_rect.position.x + ring_corner_radius[3], ring_rect.position.y + ring_rect.size.y - ring_corner_radius[3])); //bl
  472. real_t inner_corner_radius[4];
  473. set_inner_corner_radius(style_rect, inner_rect, corner_radius, inner_corner_radius);
  474. Vector<Point2> inner_points;
  475. inner_points.push_back(inner_rect.position + Vector2(inner_corner_radius[0], inner_corner_radius[0])); //tl
  476. inner_points.push_back(Point2(inner_rect.position.x + inner_rect.size.x - inner_corner_radius[1], inner_rect.position.y + inner_corner_radius[1])); //tr
  477. inner_points.push_back(inner_rect.position + inner_rect.size - Vector2(inner_corner_radius[2], inner_corner_radius[2])); //br
  478. inner_points.push_back(Point2(inner_rect.position.x + inner_corner_radius[3], inner_rect.position.y + inner_rect.size.y - inner_corner_radius[3])); //bl
  479. // Calculate the vertices.
  480. for (int corner_index = 0; corner_index < 4; corner_index++) {
  481. for (int detail = 0; detail <= adapted_corner_detail; detail++) {
  482. for (int inner_outer = 0; inner_outer < 2; inner_outer++) {
  483. real_t radius;
  484. Color color;
  485. Point2 corner_point;
  486. if (inner_outer == 0) {
  487. radius = inner_corner_radius[corner_index];
  488. color = inner_color;
  489. corner_point = inner_points[corner_index];
  490. } else {
  491. radius = ring_corner_radius[corner_index];
  492. color = outer_color;
  493. corner_point = outer_points[corner_index];
  494. }
  495. const float x = radius * (float)cos((double)corner_index * Math_PI / 2.0 + (double)detail / (double)adapted_corner_detail * Math_PI / 2.0 + Math_PI) + corner_point.x;
  496. const float y = radius * (float)sin((double)corner_index * Math_PI / 2.0 + (double)detail / (double)adapted_corner_detail * Math_PI / 2.0 + Math_PI) + corner_point.y;
  497. const float x_skew = -skew.x * (y - ring_rect.get_center().y);
  498. const float y_skew = -skew.y * (x - ring_rect.get_center().x);
  499. verts.push_back(Vector2(x + x_skew, y + y_skew));
  500. colors.push_back(color);
  501. }
  502. }
  503. }
  504. int ring_vert_count = verts.size() - vert_offset;
  505. // Fill the indices and the colors for the border.
  506. for (int i = 0; i < ring_vert_count; i++) {
  507. indices.push_back(vert_offset + ((i + 0) % ring_vert_count));
  508. indices.push_back(vert_offset + ((i + 2) % ring_vert_count));
  509. indices.push_back(vert_offset + ((i + 1) % ring_vert_count));
  510. }
  511. if (fill_center) {
  512. //Fill the indices and the colors for the center.
  513. for (int index = 0; index < ring_vert_count / 2; index += 2) {
  514. int i = index;
  515. // Polygon 1.
  516. indices.push_back(vert_offset + i);
  517. indices.push_back(vert_offset + ring_vert_count - 4 - i);
  518. indices.push_back(vert_offset + i + 2);
  519. // Polygon 2.
  520. indices.push_back(vert_offset + i);
  521. indices.push_back(vert_offset + ring_vert_count - 2 - i);
  522. indices.push_back(vert_offset + ring_vert_count - 4 - i);
  523. }
  524. }
  525. }
  526. 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) {
  527. if (p_values[p_index_a] + p_values[p_index_b] > p_width) {
  528. real_t factor;
  529. real_t new_value;
  530. factor = (real_t)p_width / (real_t)(p_values[p_index_a] + p_values[p_index_b]);
  531. new_value = (p_values[p_index_a] * factor);
  532. if (new_value < adapted_values[p_index_a]) {
  533. adapted_values[p_index_a] = new_value;
  534. }
  535. new_value = (p_values[p_index_b] * factor);
  536. if (new_value < adapted_values[p_index_b]) {
  537. adapted_values[p_index_b] = new_value;
  538. }
  539. } else {
  540. adapted_values[p_index_a] = MIN(p_values[p_index_a], adapted_values[p_index_a]);
  541. adapted_values[p_index_b] = MIN(p_values[p_index_b], adapted_values[p_index_b]);
  542. }
  543. adapted_values[p_index_a] = MIN(p_max_a, adapted_values[p_index_a]);
  544. adapted_values[p_index_b] = MIN(p_max_b, adapted_values[p_index_b]);
  545. }
  546. Rect2 StyleBoxFlat::get_draw_rect(const Rect2 &p_rect) const {
  547. Rect2 draw_rect = p_rect.grow_individual(expand_margin[MARGIN_LEFT], expand_margin[MARGIN_TOP], expand_margin[MARGIN_RIGHT], expand_margin[MARGIN_BOTTOM]);
  548. if (shadow_size > 0) {
  549. Rect2 shadow_rect = draw_rect.grow(shadow_size);
  550. shadow_rect.position += shadow_offset;
  551. draw_rect = draw_rect.merge(shadow_rect);
  552. }
  553. return draw_rect;
  554. }
  555. void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  556. bool draw_border = (border_width[0] > 0) || (border_width[1] > 0) || (border_width[2] > 0) || (border_width[3] > 0);
  557. bool draw_shadow = (shadow_size > 0);
  558. if (!draw_border && !draw_center && !draw_shadow) {
  559. return;
  560. }
  561. Rect2 style_rect = p_rect.grow_individual(expand_margin[MARGIN_LEFT], expand_margin[MARGIN_TOP], expand_margin[MARGIN_RIGHT], expand_margin[MARGIN_BOTTOM]);
  562. if (Math::is_zero_approx(style_rect.size.width) || Math::is_zero_approx(style_rect.size.height)) {
  563. return;
  564. }
  565. const bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0);
  566. // Only enable antialiasing if it is actually needed. This improve performances
  567. // and maximizes sharpness for non-skewed StyleBoxes with sharp corners.
  568. const bool aa_on = (rounded_corners || !skew.is_equal_approx(Vector2())) && anti_aliased;
  569. const bool blend_on = blend_border && draw_border;
  570. Color border_color_alpha = Color(border_color.r, border_color.g, border_color.b, 0);
  571. Color border_color_blend = (draw_center ? bg_color : border_color_alpha);
  572. Color border_color_inner = blend_on ? border_color_blend : border_color;
  573. // Adapt borders (prevent weird overlapping/glitchy drawings).
  574. real_t width = MAX(style_rect.size.width, 0);
  575. real_t height = MAX(style_rect.size.height, 0);
  576. real_t adapted_border[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 };
  577. adapt_values(MARGIN_TOP, MARGIN_BOTTOM, adapted_border, border_width, height, height, height);
  578. adapt_values(MARGIN_LEFT, MARGIN_RIGHT, adapted_border, border_width, width, width, width);
  579. // Adapt corners (prevent weird overlapping/glitchy drawings).
  580. real_t adapted_corner[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 };
  581. adapt_values(CORNER_TOP_RIGHT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, height, height - adapted_border[MARGIN_BOTTOM], height - adapted_border[MARGIN_TOP]);
  582. adapt_values(CORNER_TOP_LEFT, CORNER_BOTTOM_LEFT, adapted_corner, corner_radius, height, height - adapted_border[MARGIN_BOTTOM], height - adapted_border[MARGIN_TOP]);
  583. adapt_values(CORNER_TOP_LEFT, CORNER_TOP_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[MARGIN_RIGHT], width - adapted_border[MARGIN_LEFT]);
  584. adapt_values(CORNER_BOTTOM_LEFT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[MARGIN_RIGHT], width - adapted_border[MARGIN_LEFT]);
  585. Rect2 infill_rect = style_rect.grow_individual(-adapted_border[MARGIN_LEFT], -adapted_border[MARGIN_TOP], -adapted_border[MARGIN_RIGHT], -adapted_border[MARGIN_BOTTOM]);
  586. Rect2 border_style_rect = style_rect;
  587. if (aa_on) {
  588. for (int i = 0; i < 4; i++) {
  589. if (border_width[i] > 0) {
  590. border_style_rect = border_style_rect.grow_margin((Margin)i, -aa_size);
  591. }
  592. }
  593. }
  594. Vector<Point2> verts;
  595. Vector<int> indices;
  596. Vector<Color> colors;
  597. Vector<Point2> uvs;
  598. // Create shadow
  599. if (draw_shadow) {
  600. Rect2 shadow_inner_rect = style_rect;
  601. shadow_inner_rect.position += shadow_offset;
  602. Rect2 shadow_rect = style_rect.grow(shadow_size);
  603. shadow_rect.position += shadow_offset;
  604. Color shadow_color_transparent = Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
  605. draw_ring(verts, indices, colors, shadow_inner_rect, adapted_corner,
  606. shadow_rect, shadow_inner_rect, shadow_color, shadow_color_transparent, corner_detail, skew);
  607. if (draw_center) {
  608. draw_ring(verts, indices, colors, shadow_inner_rect, adapted_corner,
  609. shadow_inner_rect, shadow_inner_rect, shadow_color, shadow_color, corner_detail, skew, true);
  610. }
  611. }
  612. // Create border (no AA).
  613. if (draw_border && !aa_on) {
  614. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  615. border_style_rect, infill_rect, border_color_inner, border_color, corner_detail, skew);
  616. }
  617. // Create infill (no AA).
  618. if (draw_center && (!aa_on || blend_on || !draw_border)) {
  619. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  620. infill_rect, infill_rect, bg_color, bg_color, corner_detail, skew, true);
  621. }
  622. if (aa_on) {
  623. real_t aa_border_width[4];
  624. real_t aa_fill_width[4];
  625. if (draw_border) {
  626. for (int i = 0; i < 4; i++) {
  627. if (border_width[i] > 0) {
  628. aa_border_width[i] = aa_size;
  629. aa_fill_width[i] = 0;
  630. } else {
  631. aa_border_width[i] = 0;
  632. aa_fill_width[i] = aa_size;
  633. }
  634. }
  635. } else {
  636. for (int i = 0; i < 4; i++) {
  637. aa_border_width[i] = 0;
  638. aa_fill_width[i] = aa_size;
  639. }
  640. }
  641. Rect2 infill_inner_rect = infill_rect.grow_individual(-aa_border_width[MARGIN_LEFT], -aa_border_width[MARGIN_TOP],
  642. -aa_border_width[MARGIN_RIGHT], -aa_border_width[MARGIN_BOTTOM]);
  643. if (draw_center) {
  644. if (!blend_on && draw_border) {
  645. Rect2 infill_inner_rect_aa = infill_inner_rect.grow_individual(aa_border_width[MARGIN_LEFT], aa_border_width[MARGIN_TOP],
  646. aa_border_width[MARGIN_RIGHT], aa_border_width[MARGIN_BOTTOM]);
  647. // Create infill within AA border.
  648. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  649. infill_inner_rect_aa, infill_inner_rect_aa, bg_color, bg_color, corner_detail, skew, true);
  650. }
  651. if (!blend_on || !draw_border) {
  652. Rect2 infill_rect_aa = infill_rect.grow_individual(aa_fill_width[MARGIN_LEFT], aa_fill_width[MARGIN_TOP],
  653. aa_fill_width[MARGIN_RIGHT], aa_fill_width[MARGIN_BOTTOM]);
  654. Color alpha_bg = Color(bg_color.r, bg_color.g, bg_color.b, 0);
  655. // Create infill fake AA gradient.
  656. draw_ring(verts, indices, colors, style_rect, adapted_corner,
  657. infill_rect_aa, infill_rect, bg_color, alpha_bg, corner_detail, skew);
  658. }
  659. }
  660. if (draw_border) {
  661. Rect2 infill_rect_aa = infill_rect.grow_individual(aa_border_width[MARGIN_LEFT], aa_border_width[MARGIN_TOP],
  662. aa_border_width[MARGIN_RIGHT], aa_border_width[MARGIN_BOTTOM]);
  663. Rect2 style_rect_aa = style_rect.grow_individual(aa_border_width[MARGIN_LEFT], aa_border_width[MARGIN_TOP],
  664. aa_border_width[MARGIN_RIGHT], aa_border_width[MARGIN_BOTTOM]);
  665. Rect2 border_style_rect_aa = border_style_rect.grow_individual(aa_border_width[MARGIN_LEFT], aa_border_width[MARGIN_TOP],
  666. aa_border_width[MARGIN_RIGHT], aa_border_width[MARGIN_BOTTOM]);
  667. // Create border.
  668. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  669. border_style_rect_aa, ((blend_on) ? infill_rect : infill_rect_aa), border_color_inner, border_color, corner_detail, skew);
  670. if (!blend_on) {
  671. // Create inner border fake AA gradient.
  672. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  673. infill_rect_aa, infill_rect, border_color_blend, border_color, corner_detail, skew);
  674. }
  675. // Create outer border fake AA gradient.
  676. draw_ring(verts, indices, colors, border_style_rect, adapted_corner,
  677. style_rect_aa, border_style_rect_aa, border_color, border_color_alpha, corner_detail, skew);
  678. }
  679. }
  680. // Compute UV coordinates.
  681. Rect2 uv_rect = style_rect.grow(aa_on ? aa_size : 0);
  682. uvs.resize(verts.size());
  683. for (int i = 0; i < verts.size(); i++) {
  684. uvs.write[i].x = (verts[i].x - uv_rect.position.x) / uv_rect.size.width;
  685. uvs.write[i].y = (verts[i].y - uv_rect.position.y) / uv_rect.size.height;
  686. }
  687. // Draw stylebox.
  688. VisualServer *vs = VisualServer::get_singleton();
  689. vs->canvas_item_add_triangle_array(p_canvas_item, indices, verts, colors, uvs);
  690. }
  691. float StyleBoxFlat::get_style_margin(Margin p_margin) const {
  692. ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
  693. return border_width[p_margin];
  694. }
  695. void StyleBoxFlat::_bind_methods() {
  696. ClassDB::bind_method(D_METHOD("set_bg_color", "color"), &StyleBoxFlat::set_bg_color);
  697. ClassDB::bind_method(D_METHOD("get_bg_color"), &StyleBoxFlat::get_bg_color);
  698. ClassDB::bind_method(D_METHOD("set_border_color", "color"), &StyleBoxFlat::set_border_color);
  699. ClassDB::bind_method(D_METHOD("get_border_color"), &StyleBoxFlat::get_border_color);
  700. ClassDB::bind_method(D_METHOD("set_border_width_all", "width"), &StyleBoxFlat::set_border_width_all);
  701. ClassDB::bind_method(D_METHOD("get_border_width_min"), &StyleBoxFlat::get_border_width_min);
  702. ClassDB::bind_method(D_METHOD("set_border_width", "margin", "width"), &StyleBoxFlat::set_border_width);
  703. ClassDB::bind_method(D_METHOD("get_border_width", "margin"), &StyleBoxFlat::get_border_width);
  704. ClassDB::bind_method(D_METHOD("set_border_blend", "blend"), &StyleBoxFlat::set_border_blend);
  705. ClassDB::bind_method(D_METHOD("get_border_blend"), &StyleBoxFlat::get_border_blend);
  706. ClassDB::bind_method(D_METHOD("set_corner_radius_individual", "radius_top_left", "radius_top_right", "radius_bottom_right", "radius_bottom_left"), &StyleBoxFlat::set_corner_radius_individual);
  707. ClassDB::bind_method(D_METHOD("set_corner_radius_all", "radius"), &StyleBoxFlat::set_corner_radius_all);
  708. ClassDB::bind_method(D_METHOD("set_corner_radius", "corner", "radius"), &StyleBoxFlat::set_corner_radius);
  709. ClassDB::bind_method(D_METHOD("get_corner_radius", "corner"), &StyleBoxFlat::get_corner_radius);
  710. ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxFlat::set_expand_margin_size);
  711. ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxFlat::set_expand_margin_size_all);
  712. ClassDB::bind_method(D_METHOD("set_expand_margin_individual", "size_left", "size_top", "size_right", "size_bottom"), &StyleBoxFlat::set_expand_margin_size_individual);
  713. ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxFlat::get_expand_margin_size);
  714. ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &StyleBoxFlat::set_draw_center);
  715. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxFlat::is_draw_center_enabled);
  716. ClassDB::bind_method(D_METHOD("set_skew", "skew"), &StyleBoxFlat::set_skew);
  717. ClassDB::bind_method(D_METHOD("get_skew"), &StyleBoxFlat::get_skew);
  718. ClassDB::bind_method(D_METHOD("set_shadow_color", "color"), &StyleBoxFlat::set_shadow_color);
  719. ClassDB::bind_method(D_METHOD("get_shadow_color"), &StyleBoxFlat::get_shadow_color);
  720. ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &StyleBoxFlat::set_shadow_size);
  721. ClassDB::bind_method(D_METHOD("get_shadow_size"), &StyleBoxFlat::get_shadow_size);
  722. ClassDB::bind_method(D_METHOD("set_shadow_offset", "offset"), &StyleBoxFlat::set_shadow_offset);
  723. ClassDB::bind_method(D_METHOD("get_shadow_offset"), &StyleBoxFlat::get_shadow_offset);
  724. ClassDB::bind_method(D_METHOD("set_anti_aliased", "anti_aliased"), &StyleBoxFlat::set_anti_aliased);
  725. ClassDB::bind_method(D_METHOD("is_anti_aliased"), &StyleBoxFlat::is_anti_aliased);
  726. ClassDB::bind_method(D_METHOD("set_aa_size", "size"), &StyleBoxFlat::set_aa_size);
  727. ClassDB::bind_method(D_METHOD("get_aa_size"), &StyleBoxFlat::get_aa_size);
  728. ClassDB::bind_method(D_METHOD("set_corner_detail", "detail"), &StyleBoxFlat::set_corner_detail);
  729. ClassDB::bind_method(D_METHOD("get_corner_detail"), &StyleBoxFlat::get_corner_detail);
  730. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), "set_bg_color", "get_bg_color");
  731. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  732. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "skew"), "set_skew", "get_skew");
  733. ADD_GROUP("Border Width", "border_width_");
  734. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_left", PROPERTY_HINT_RANGE, "0,1024,1"), "set_border_width", "get_border_width", MARGIN_LEFT);
  735. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_top", PROPERTY_HINT_RANGE, "0,1024,1"), "set_border_width", "get_border_width", MARGIN_TOP);
  736. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_right", PROPERTY_HINT_RANGE, "0,1024,1"), "set_border_width", "get_border_width", MARGIN_RIGHT);
  737. ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_bottom", PROPERTY_HINT_RANGE, "0,1024,1"), "set_border_width", "get_border_width", MARGIN_BOTTOM);
  738. ADD_GROUP("Border", "border_");
  739. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "border_color"), "set_border_color", "get_border_color");
  740. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "border_blend"), "set_border_blend", "get_border_blend");
  741. ADD_GROUP("Corner Radius", "corner_radius_");
  742. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_left", PROPERTY_HINT_RANGE, "0,1024,1"), "set_corner_radius", "get_corner_radius", CORNER_TOP_LEFT);
  743. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_right", PROPERTY_HINT_RANGE, "0,1024,1"), "set_corner_radius", "get_corner_radius", CORNER_TOP_RIGHT);
  744. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_right", PROPERTY_HINT_RANGE, "0,1024,1"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_RIGHT);
  745. ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_left", PROPERTY_HINT_RANGE, "0,1024,1"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_LEFT);
  746. ADD_PROPERTY(PropertyInfo(Variant::INT, "corner_detail", PROPERTY_HINT_RANGE, "1,20,1"), "set_corner_detail", "get_corner_detail");
  747. ADD_GROUP("Expand Margin", "expand_margin_");
  748. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin", "get_expand_margin", MARGIN_LEFT);
  749. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin", "get_expand_margin", MARGIN_RIGHT);
  750. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin", "get_expand_margin", MARGIN_TOP);
  751. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1"), "set_expand_margin", "get_expand_margin", MARGIN_BOTTOM);
  752. ADD_GROUP("Shadow", "shadow_");
  753. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
  754. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_shadow_size", "get_shadow_size");
  755. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shadow_offset"), "set_shadow_offset", "get_shadow_offset");
  756. ADD_GROUP("Anti Aliasing", "anti_aliasing_");
  757. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "anti_aliasing"), "set_anti_aliased", "is_anti_aliased");
  758. ADD_PROPERTY(PropertyInfo(Variant::REAL, "anti_aliasing_size", PROPERTY_HINT_RANGE, "0.01,10,0.001"), "set_aa_size", "get_aa_size");
  759. }
  760. StyleBoxFlat::StyleBoxFlat() {
  761. bg_color = Color(0.6, 0.6, 0.6);
  762. shadow_color = Color(0, 0, 0, 0.6);
  763. border_color = Color(0.8, 0.8, 0.8);
  764. blend_border = false;
  765. draw_center = true;
  766. anti_aliased = true;
  767. shadow_size = 0;
  768. shadow_offset = Point2(0, 0);
  769. corner_detail = 8;
  770. aa_size = 0.625;
  771. border_width[0] = 0;
  772. border_width[1] = 0;
  773. border_width[2] = 0;
  774. border_width[3] = 0;
  775. expand_margin[0] = 0;
  776. expand_margin[1] = 0;
  777. expand_margin[2] = 0;
  778. expand_margin[3] = 0;
  779. corner_radius[0] = 0;
  780. corner_radius[1] = 0;
  781. corner_radius[2] = 0;
  782. corner_radius[3] = 0;
  783. }
  784. StyleBoxFlat::~StyleBoxFlat() {
  785. }
  786. void StyleBoxLine::set_color(const Color &p_color) {
  787. color = p_color;
  788. emit_changed();
  789. }
  790. Color StyleBoxLine::get_color() const {
  791. return color;
  792. }
  793. void StyleBoxLine::set_thickness(int p_thickness) {
  794. thickness = p_thickness;
  795. emit_changed();
  796. }
  797. int StyleBoxLine::get_thickness() const {
  798. return thickness;
  799. }
  800. void StyleBoxLine::set_vertical(bool p_vertical) {
  801. vertical = p_vertical;
  802. emit_changed();
  803. }
  804. bool StyleBoxLine::is_vertical() const {
  805. return vertical;
  806. }
  807. void StyleBoxLine::set_grow_end(float p_grow_end) {
  808. grow_end = p_grow_end;
  809. emit_changed();
  810. }
  811. float StyleBoxLine::get_grow_end() const {
  812. return grow_end;
  813. }
  814. void StyleBoxLine::set_grow_begin(float p_grow_begin) {
  815. grow_begin = p_grow_begin;
  816. emit_changed();
  817. }
  818. float StyleBoxLine::get_grow_begin() const {
  819. return grow_begin;
  820. }
  821. void StyleBoxLine::_bind_methods() {
  822. ClassDB::bind_method(D_METHOD("set_color", "color"), &StyleBoxLine::set_color);
  823. ClassDB::bind_method(D_METHOD("get_color"), &StyleBoxLine::get_color);
  824. ClassDB::bind_method(D_METHOD("set_thickness", "thickness"), &StyleBoxLine::set_thickness);
  825. ClassDB::bind_method(D_METHOD("get_thickness"), &StyleBoxLine::get_thickness);
  826. ClassDB::bind_method(D_METHOD("set_grow_begin", "offset"), &StyleBoxLine::set_grow_begin);
  827. ClassDB::bind_method(D_METHOD("get_grow_begin"), &StyleBoxLine::get_grow_begin);
  828. ClassDB::bind_method(D_METHOD("set_grow_end", "offset"), &StyleBoxLine::set_grow_end);
  829. ClassDB::bind_method(D_METHOD("get_grow_end"), &StyleBoxLine::get_grow_end);
  830. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &StyleBoxLine::set_vertical);
  831. ClassDB::bind_method(D_METHOD("is_vertical"), &StyleBoxLine::is_vertical);
  832. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  833. ADD_PROPERTY(PropertyInfo(Variant::REAL, "grow_begin", PROPERTY_HINT_RANGE, "-300,300,1"), "set_grow_begin", "get_grow_begin");
  834. ADD_PROPERTY(PropertyInfo(Variant::REAL, "grow_end", PROPERTY_HINT_RANGE, "-300,300,1"), "set_grow_end", "get_grow_end");
  835. ADD_PROPERTY(PropertyInfo(Variant::INT, "thickness", PROPERTY_HINT_RANGE, "0,10"), "set_thickness", "get_thickness");
  836. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  837. }
  838. float StyleBoxLine::get_style_margin(Margin p_margin) const {
  839. ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
  840. if (vertical) {
  841. if (p_margin == MARGIN_LEFT || p_margin == MARGIN_RIGHT) {
  842. return thickness / 2.0;
  843. }
  844. } else if (p_margin == MARGIN_TOP || p_margin == MARGIN_BOTTOM) {
  845. return thickness / 2.0;
  846. }
  847. return 0;
  848. }
  849. Size2 StyleBoxLine::get_center_size() const {
  850. return Size2();
  851. }
  852. void StyleBoxLine::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  853. VisualServer *vs = VisualServer::get_singleton();
  854. Rect2i r = p_rect;
  855. if (vertical) {
  856. r.position.y -= grow_begin;
  857. r.size.y += (grow_begin + grow_end);
  858. r.size.x = thickness;
  859. } else {
  860. r.position.x -= grow_begin;
  861. r.size.x += (grow_begin + grow_end);
  862. r.size.y = thickness;
  863. }
  864. vs->canvas_item_add_rect(p_canvas_item, r, color);
  865. }
  866. StyleBoxLine::StyleBoxLine() {
  867. grow_begin = 1.0;
  868. grow_end = 1.0;
  869. thickness = 1;
  870. color = Color(0.0, 0.0, 0.0);
  871. vertical = false;
  872. }
  873. StyleBoxLine::~StyleBoxLine() {}