style_box.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
  32. return true;
  33. }
  34. void StyleBox::set_default_margin(Margin p_margin, float p_value) {
  35. margin[p_margin] = p_value;
  36. emit_changed();
  37. }
  38. float StyleBox::get_default_margin(Margin p_margin) const {
  39. return margin[p_margin];
  40. }
  41. float StyleBox::get_margin(Margin p_margin) const {
  42. if (margin[p_margin] < 0)
  43. return get_style_margin(p_margin);
  44. else
  45. return margin[p_margin];
  46. }
  47. Size2 StyleBox::get_minimum_size() const {
  48. return Size2(get_margin(MARGIN_LEFT) + get_margin(MARGIN_RIGHT), get_margin(MARGIN_TOP) + get_margin(MARGIN_BOTTOM));
  49. }
  50. Point2 StyleBox::get_offset() const {
  51. return Point2(get_margin(MARGIN_LEFT), get_margin(MARGIN_TOP));
  52. }
  53. Size2 StyleBox::get_center_size() const {
  54. return Size2();
  55. }
  56. void StyleBox::_bind_methods() {
  57. ObjectTypeDB::bind_method(_MD("test_mask", "point", "rect"), &StyleBox::test_mask);
  58. ObjectTypeDB::bind_method(_MD("set_default_margin", "margin", "offset"), &StyleBox::set_default_margin);
  59. ObjectTypeDB::bind_method(_MD("get_default_margin", "margin"), &StyleBox::get_default_margin);
  60. // ObjectTypeDB::bind_method(_MD("set_default_margin"),&StyleBox::set_default_margin);
  61. // ObjectTypeDB::bind_method(_MD("get_default_margin"),&StyleBox::get_default_margin);
  62. ObjectTypeDB::bind_method(_MD("get_margin", "margin"), &StyleBox::get_margin);
  63. ObjectTypeDB::bind_method(_MD("get_minimum_size"), &StyleBox::get_minimum_size);
  64. ObjectTypeDB::bind_method(_MD("get_center_size"), &StyleBox::get_center_size);
  65. ObjectTypeDB::bind_method(_MD("get_offset"), &StyleBox::get_offset);
  66. ObjectTypeDB::bind_method(_MD("draw", "canvas_item", "rect"), &StyleBox::draw);
  67. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin/left", PROPERTY_HINT_RANGE, "-1,2048,1"), _SCS("set_default_margin"), _SCS("get_default_margin"), MARGIN_LEFT);
  68. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin/right", PROPERTY_HINT_RANGE, "-1,2048,1"), _SCS("set_default_margin"), _SCS("get_default_margin"), MARGIN_RIGHT);
  69. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin/top", PROPERTY_HINT_RANGE, "-1,2048,1"), _SCS("set_default_margin"), _SCS("get_default_margin"), MARGIN_TOP);
  70. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin/bottom", PROPERTY_HINT_RANGE, "-1,2048,1"), _SCS("set_default_margin"), _SCS("get_default_margin"), MARGIN_BOTTOM);
  71. }
  72. StyleBox::StyleBox() {
  73. for (int i = 0; i < 4; i++) {
  74. margin[i] = -1;
  75. }
  76. }
  77. void StyleBoxTexture::set_texture(RES p_texture) {
  78. if (texture == p_texture)
  79. return;
  80. texture = p_texture;
  81. emit_signal("texture_changed");
  82. emit_changed();
  83. }
  84. RES StyleBoxTexture::get_texture() const {
  85. return texture;
  86. }
  87. void StyleBoxTexture::set_margin_size(Margin p_margin, float p_size) {
  88. margin[p_margin] = p_size;
  89. emit_changed();
  90. }
  91. float StyleBoxTexture::get_margin_size(Margin p_margin) const {
  92. return margin[p_margin];
  93. }
  94. float StyleBoxTexture::get_style_margin(Margin p_margin) const {
  95. return margin[p_margin];
  96. }
  97. void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  98. if (texture.is_null())
  99. return;
  100. Rect2 r = p_rect;
  101. r.pos.x -= expand_margin[MARGIN_LEFT];
  102. r.pos.y -= expand_margin[MARGIN_TOP];
  103. r.size.x += expand_margin[MARGIN_LEFT] + expand_margin[MARGIN_RIGHT];
  104. r.size.y += expand_margin[MARGIN_TOP] + expand_margin[MARGIN_BOTTOM];
  105. VisualServer::get_singleton()->canvas_item_add_style_box(p_canvas_item, r, region_rect, texture->get_rid(), Vector2(margin[MARGIN_LEFT], margin[MARGIN_TOP]), Vector2(margin[MARGIN_RIGHT], margin[MARGIN_BOTTOM]), draw_center);
  106. }
  107. void StyleBoxTexture::set_draw_center(bool p_draw) {
  108. draw_center = p_draw;
  109. emit_changed();
  110. }
  111. bool StyleBoxTexture::get_draw_center() const {
  112. return draw_center;
  113. }
  114. Size2 StyleBoxTexture::get_center_size() const {
  115. if (texture.is_null())
  116. return Size2();
  117. return texture->get_size() - get_minimum_size();
  118. }
  119. void StyleBoxTexture::set_expand_margin_size(Margin p_expand_margin, float p_size) {
  120. ERR_FAIL_INDEX(p_expand_margin, 4);
  121. expand_margin[p_expand_margin] = p_size;
  122. emit_changed();
  123. }
  124. float StyleBoxTexture::get_expand_margin_size(Margin p_expand_margin) const {
  125. ERR_FAIL_INDEX_V(p_expand_margin, 4, 0);
  126. return expand_margin[p_expand_margin];
  127. }
  128. void StyleBoxTexture::set_region_rect(const Rect2 &p_region_rect) {
  129. if (region_rect == p_region_rect)
  130. return;
  131. region_rect = p_region_rect;
  132. emit_changed();
  133. }
  134. Rect2 StyleBoxTexture::get_region_rect() const {
  135. return region_rect;
  136. }
  137. void StyleBoxTexture::_bind_methods() {
  138. ObjectTypeDB::bind_method(_MD("set_texture", "texture:Texture"), &StyleBoxTexture::set_texture);
  139. ObjectTypeDB::bind_method(_MD("get_texture:Texture"), &StyleBoxTexture::get_texture);
  140. ObjectTypeDB::bind_method(_MD("set_margin_size", "margin", "size"), &StyleBoxTexture::set_margin_size);
  141. ObjectTypeDB::bind_method(_MD("get_margin_size", "margin"), &StyleBoxTexture::get_margin_size);
  142. ObjectTypeDB::bind_method(_MD("set_expand_margin_size", "margin", "size"), &StyleBoxTexture::set_expand_margin_size);
  143. ObjectTypeDB::bind_method(_MD("get_expand_margin_size", "margin"), &StyleBoxTexture::get_expand_margin_size);
  144. ObjectTypeDB::bind_method(_MD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect);
  145. ObjectTypeDB::bind_method(_MD("get_region_rect"), &StyleBoxTexture::get_region_rect);
  146. ObjectTypeDB::bind_method(_MD("set_draw_center", "enable"), &StyleBoxTexture::set_draw_center);
  147. ObjectTypeDB::bind_method(_MD("get_draw_center"), &StyleBoxTexture::get_draw_center);
  148. ADD_SIGNAL(MethodInfo("texture_changed"));
  149. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"), _SCS("get_texture"));
  150. ADD_PROPERTYNZ(PropertyInfo(Variant::RECT2, "region_rect"), _SCS("set_region_rect"), _SCS("get_region_rect"));
  151. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin/left", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_margin_size"), _SCS("get_margin_size"), MARGIN_LEFT);
  152. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin/right", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_margin_size"), _SCS("get_margin_size"), MARGIN_RIGHT);
  153. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin/top", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_margin_size"), _SCS("get_margin_size"), MARGIN_TOP);
  154. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin/bottom", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_margin_size"), _SCS("get_margin_size"), MARGIN_BOTTOM);
  155. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin/left", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_expand_margin_size"), _SCS("get_expand_margin_size"), MARGIN_LEFT);
  156. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin/right", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_expand_margin_size"), _SCS("get_expand_margin_size"), MARGIN_RIGHT);
  157. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin/top", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_expand_margin_size"), _SCS("get_expand_margin_size"), MARGIN_TOP);
  158. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin/bottom", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_expand_margin_size"), _SCS("get_expand_margin_size"), MARGIN_BOTTOM);
  159. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), _SCS("set_draw_center"), _SCS("get_draw_center"));
  160. }
  161. StyleBoxTexture::StyleBoxTexture() {
  162. for (int i = 0; i < 4; i++) {
  163. margin[i] = 0;
  164. expand_margin[i] = 0;
  165. }
  166. draw_center = true;
  167. }
  168. StyleBoxTexture::~StyleBoxTexture() {
  169. }
  170. ////////////////
  171. void StyleBoxFlat::set_bg_color(const Color &p_color) {
  172. bg_color = p_color;
  173. emit_changed();
  174. }
  175. void StyleBoxFlat::set_light_color(const Color &p_color) {
  176. light_color = p_color;
  177. emit_changed();
  178. }
  179. void StyleBoxFlat::set_dark_color(const Color &p_color) {
  180. dark_color = p_color;
  181. emit_changed();
  182. }
  183. Color StyleBoxFlat::get_bg_color() const {
  184. return bg_color;
  185. }
  186. Color StyleBoxFlat::get_light_color() const {
  187. return light_color;
  188. }
  189. Color StyleBoxFlat::get_dark_color() const {
  190. return dark_color;
  191. }
  192. void StyleBoxFlat::set_border_size(int p_size) {
  193. border_size = p_size;
  194. emit_changed();
  195. }
  196. int StyleBoxFlat::get_border_size() const {
  197. return border_size;
  198. }
  199. void StyleBoxFlat::set_border_blend(bool p_blend) {
  200. blend = p_blend;
  201. emit_changed();
  202. }
  203. bool StyleBoxFlat::get_border_blend() const {
  204. return blend;
  205. }
  206. void StyleBoxFlat::set_draw_center(bool p_draw) {
  207. draw_center = p_draw;
  208. emit_changed();
  209. }
  210. bool StyleBoxFlat::get_draw_center() const {
  211. return draw_center;
  212. }
  213. Size2 StyleBoxFlat::get_center_size() const {
  214. return Size2();
  215. }
  216. void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  217. VisualServer *vs = VisualServer::get_singleton();
  218. Rect2i r = p_rect;
  219. for (int i = 0; i < border_size; i++) {
  220. Color color_upleft = light_color;
  221. Color color_downright = dark_color;
  222. if (blend) {
  223. color_upleft.r = (border_size - i) * color_upleft.r / border_size + i * bg_color.r / border_size;
  224. color_upleft.g = (border_size - i) * color_upleft.g / border_size + i * bg_color.g / border_size;
  225. color_upleft.b = (border_size - i) * color_upleft.b / border_size + i * bg_color.b / border_size;
  226. color_downright.r = (border_size - i) * color_downright.r / border_size + i * bg_color.r / border_size;
  227. color_downright.g = (border_size - i) * color_downright.g / border_size + i * bg_color.g / border_size;
  228. color_downright.b = (border_size - i) * color_downright.b / border_size + i * bg_color.b / border_size;
  229. }
  230. vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r.pos.x, r.pos.y + r.size.y - 1), Size2(r.size.x, 1)), color_downright);
  231. vs->canvas_item_add_rect(p_canvas_item, Rect2(Point2i(r.pos.x + r.size.x - 1, r.pos.y), Size2(1, r.size.y)), color_downright);
  232. vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, Size2(r.size.x, 1)), color_upleft);
  233. vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, Size2(1, r.size.y)), color_upleft);
  234. r.pos.x++;
  235. r.pos.y++;
  236. r.size.x -= 2;
  237. r.size.y -= 2;
  238. }
  239. if (draw_center)
  240. vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, r.size), bg_color);
  241. }
  242. float StyleBoxFlat::get_style_margin(Margin p_margin) const {
  243. return border_size;
  244. }
  245. void StyleBoxFlat::_bind_methods() {
  246. ObjectTypeDB::bind_method(_MD("set_bg_color", "color"), &StyleBoxFlat::set_bg_color);
  247. ObjectTypeDB::bind_method(_MD("get_bg_color"), &StyleBoxFlat::get_bg_color);
  248. ObjectTypeDB::bind_method(_MD("set_light_color", "color"), &StyleBoxFlat::set_light_color);
  249. ObjectTypeDB::bind_method(_MD("get_light_color"), &StyleBoxFlat::get_light_color);
  250. ObjectTypeDB::bind_method(_MD("set_dark_color", "color"), &StyleBoxFlat::set_dark_color);
  251. ObjectTypeDB::bind_method(_MD("get_dark_color"), &StyleBoxFlat::get_dark_color);
  252. ObjectTypeDB::bind_method(_MD("set_border_size", "size"), &StyleBoxFlat::set_border_size);
  253. ObjectTypeDB::bind_method(_MD("get_border_size"), &StyleBoxFlat::get_border_size);
  254. ObjectTypeDB::bind_method(_MD("set_border_blend", "blend"), &StyleBoxFlat::set_border_blend);
  255. ObjectTypeDB::bind_method(_MD("get_border_blend"), &StyleBoxFlat::get_border_blend);
  256. ObjectTypeDB::bind_method(_MD("set_draw_center", "size"), &StyleBoxFlat::set_draw_center);
  257. ObjectTypeDB::bind_method(_MD("get_draw_center"), &StyleBoxFlat::get_draw_center);
  258. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), _SCS("set_bg_color"), _SCS("get_bg_color"));
  259. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "light_color"), _SCS("set_light_color"), _SCS("get_light_color"));
  260. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "dark_color"), _SCS("set_dark_color"), _SCS("get_dark_color"));
  261. ADD_PROPERTY(PropertyInfo(Variant::INT, "border_size", PROPERTY_HINT_RANGE, "0,4096"), _SCS("set_border_size"), _SCS("get_border_size"));
  262. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "border_blend"), _SCS("set_border_blend"), _SCS("get_border_blend"));
  263. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_bg"), _SCS("set_draw_center"), _SCS("get_draw_center"));
  264. }
  265. StyleBoxFlat::StyleBoxFlat() {
  266. bg_color = Color(0.6, 0.6, 0.6);
  267. light_color = Color(0.8, 0.8, 0.8);
  268. dark_color = Color(0.8, 0.8, 0.8);
  269. draw_center = true;
  270. blend = true;
  271. border_size = 0;
  272. }
  273. StyleBoxFlat::~StyleBoxFlat() {
  274. }
  275. ////////////////
  276. void StyleBoxImageMask::_bind_methods() {
  277. ObjectTypeDB::bind_method(_MD("set_image", "image"), &StyleBoxImageMask::set_image);
  278. ObjectTypeDB::bind_method(_MD("get_image"), &StyleBoxImageMask::get_image);
  279. ObjectTypeDB::bind_method(_MD("set_expand", "expand"), &StyleBoxImageMask::set_expand);
  280. ObjectTypeDB::bind_method(_MD("get_expand"), &StyleBoxImageMask::get_expand);
  281. ObjectTypeDB::bind_method(_MD("set_expand_margin_size", "margin", "size"), &StyleBoxImageMask::set_expand_margin_size);
  282. ObjectTypeDB::bind_method(_MD("get_expand_margin_size", "margin"), &StyleBoxImageMask::get_expand_margin_size);
  283. ADD_PROPERTY(PropertyInfo(Variant::IMAGE, "image"), _SCS("set_image"), _SCS("get_image"));
  284. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), _SCS("set_expand"), _SCS("get_expand"));
  285. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin/left", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_expand_margin_size"), _SCS("get_expand_margin_size"), MARGIN_LEFT);
  286. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin/right", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_expand_margin_size"), _SCS("get_expand_margin_size"), MARGIN_RIGHT);
  287. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin/top", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_expand_margin_size"), _SCS("get_expand_margin_size"), MARGIN_TOP);
  288. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "expand_margin/bottom", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_expand_margin_size"), _SCS("get_expand_margin_size"), MARGIN_BOTTOM);
  289. }
  290. bool StyleBoxImageMask::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
  291. if (image.empty())
  292. return false;
  293. if (p_rect.size.x < 1)
  294. return false;
  295. if (p_rect.size.y < 1)
  296. return false;
  297. Size2i imgsize(image.get_width(), image.get_height());
  298. if (imgsize.x <= 0 || imgsize.y <= 0)
  299. return false;
  300. Point2i img_expand_size(imgsize.x - expand_margin[MARGIN_LEFT] - expand_margin[MARGIN_RIGHT], imgsize.y - expand_margin[MARGIN_TOP] - expand_margin[MARGIN_BOTTOM]);
  301. Point2i rect_expand_size(p_rect.size.x - expand_margin[MARGIN_LEFT] - expand_margin[MARGIN_RIGHT], p_rect.size.y - expand_margin[MARGIN_TOP] - expand_margin[MARGIN_BOTTOM]);
  302. if (rect_expand_size.x < 1)
  303. rect_expand_size.x = 1;
  304. if (rect_expand_size.y < 1)
  305. rect_expand_size.y = 1;
  306. Point2i click_pos;
  307. //treat x
  308. if (p_point.x < p_rect.pos.x)
  309. click_pos.x = 0;
  310. else if (expand) {
  311. if (p_point.x >= p_rect.pos.x + p_rect.size.x)
  312. click_pos.x = imgsize.x - 1;
  313. else if ((p_point.x - p_rect.pos.x) < expand_margin[MARGIN_LEFT])
  314. click_pos.x = p_point.x;
  315. else if ((p_point.x - (p_rect.pos.x + p_rect.size.x)) < expand_margin[MARGIN_RIGHT])
  316. click_pos.x = imgsize.x - (p_point.x - (p_rect.pos.x + p_rect.size.x));
  317. else //expand
  318. click_pos.x = (p_point.x - p_rect.pos.x - expand_margin[MARGIN_LEFT]) * img_expand_size.x / rect_expand_size.x;
  319. } else if ((p_point.x - p_rect.pos.x) > imgsize.x)
  320. click_pos.x = imgsize.x;
  321. //treat y
  322. if (p_point.y < p_rect.pos.y)
  323. click_pos.y = 0;
  324. else if (expand) {
  325. if (p_point.y >= p_rect.pos.y + p_rect.size.y)
  326. click_pos.y = imgsize.y - 1;
  327. else if ((p_point.y - p_rect.pos.y) < expand_margin[MARGIN_TOP])
  328. click_pos.y = p_point.y;
  329. else if ((p_point.y - (p_rect.pos.y + p_rect.size.y)) < expand_margin[MARGIN_BOTTOM])
  330. click_pos.y = imgsize.y - (p_point.y - (p_rect.pos.y + p_rect.size.y));
  331. else //expand
  332. click_pos.y = (p_point.y - p_rect.pos.y - expand_margin[MARGIN_TOP]) * img_expand_size.y / rect_expand_size.y;
  333. } else if ((p_point.y - p_rect.pos.y) > imgsize.y)
  334. click_pos.y = imgsize.y;
  335. return image.get_pixel(click_pos.x, click_pos.y).gray() > 0.5;
  336. }
  337. void StyleBoxImageMask::set_image(const Image &p_image) {
  338. image = p_image;
  339. }
  340. Image StyleBoxImageMask::get_image() const {
  341. return image;
  342. }
  343. void StyleBoxImageMask::set_expand(bool p_expand) {
  344. expand = p_expand;
  345. }
  346. bool StyleBoxImageMask::get_expand() const {
  347. return expand;
  348. }
  349. void StyleBoxImageMask::set_expand_margin_size(Margin p_expand_margin, float p_size) {
  350. ERR_FAIL_INDEX(p_expand_margin, 4);
  351. expand_margin[p_expand_margin] = p_size;
  352. }
  353. float StyleBoxImageMask::get_expand_margin_size(Margin p_expand_margin) const {
  354. ERR_FAIL_INDEX_V(p_expand_margin, 4, 0);
  355. return expand_margin[p_expand_margin];
  356. }
  357. StyleBoxImageMask::StyleBoxImageMask() {
  358. for (int i = 0; i < 4; i++) {
  359. expand_margin[i] = 0;
  360. }
  361. expand = true;
  362. }