style_box.cpp 40 KB

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