1 /*************************************************************************/
2 /*  style_box.cpp                                                        */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 
test_mask(const Point2 & p_point,const Rect2 & p_rect) const32 bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
33 
34 	return true;
35 }
36 
set_default_margin(Margin p_margin,float p_value)37 void StyleBox::set_default_margin(Margin p_margin, float p_value) {
38 
39 	margin[p_margin] = p_value;
40 	emit_changed();
41 }
get_default_margin(Margin p_margin) const42 float StyleBox::get_default_margin(Margin p_margin) const {
43 
44 	return margin[p_margin];
45 }
46 
get_margin(Margin p_margin) const47 float StyleBox::get_margin(Margin p_margin) const {
48 
49 	if (margin[p_margin] < 0)
50 		return get_style_margin(p_margin);
51 	else
52 		return margin[p_margin];
53 }
54 
get_minimum_size() const55 Size2 StyleBox::get_minimum_size() const {
56 
57 	return Size2(get_margin(MARGIN_LEFT) + get_margin(MARGIN_RIGHT), get_margin(MARGIN_TOP) + get_margin(MARGIN_BOTTOM));
58 }
59 
get_offset() const60 Point2 StyleBox::get_offset() const {
61 
62 	return Point2(get_margin(MARGIN_LEFT), get_margin(MARGIN_TOP));
63 }
64 
get_center_size() const65 Size2 StyleBox::get_center_size() const {
66 
67 	return Size2();
68 }
69 
_bind_methods()70 void StyleBox::_bind_methods() {
71 
72 	ObjectTypeDB::bind_method(_MD("test_mask", "point", "rect"), &StyleBox::test_mask);
73 
74 	ObjectTypeDB::bind_method(_MD("set_default_margin", "margin", "offset"), &StyleBox::set_default_margin);
75 	ObjectTypeDB::bind_method(_MD("get_default_margin", "margin"), &StyleBox::get_default_margin);
76 
77 	//	ObjectTypeDB::bind_method(_MD("set_default_margin"),&StyleBox::set_default_margin);
78 	//	ObjectTypeDB::bind_method(_MD("get_default_margin"),&StyleBox::get_default_margin);
79 
80 	ObjectTypeDB::bind_method(_MD("get_margin", "margin"), &StyleBox::get_margin);
81 	ObjectTypeDB::bind_method(_MD("get_minimum_size"), &StyleBox::get_minimum_size);
82 	ObjectTypeDB::bind_method(_MD("get_center_size"), &StyleBox::get_center_size);
83 	ObjectTypeDB::bind_method(_MD("get_offset"), &StyleBox::get_offset);
84 
85 	ObjectTypeDB::bind_method(_MD("draw", "canvas_item", "rect"), &StyleBox::draw);
86 
87 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin/left", PROPERTY_HINT_RANGE, "-1,2048,1"), _SCS("set_default_margin"), _SCS("get_default_margin"), MARGIN_LEFT);
88 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin/right", PROPERTY_HINT_RANGE, "-1,2048,1"), _SCS("set_default_margin"), _SCS("get_default_margin"), MARGIN_RIGHT);
89 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin/top", PROPERTY_HINT_RANGE, "-1,2048,1"), _SCS("set_default_margin"), _SCS("get_default_margin"), MARGIN_TOP);
90 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "content_margin/bottom", PROPERTY_HINT_RANGE, "-1,2048,1"), _SCS("set_default_margin"), _SCS("get_default_margin"), MARGIN_BOTTOM);
91 }
92 
StyleBox()93 StyleBox::StyleBox() {
94 
95 	for (int i = 0; i < 4; i++) {
96 
97 		margin[i] = -1;
98 	}
99 }
100 
set_texture(RES p_texture)101 void StyleBoxTexture::set_texture(RES p_texture) {
102 
103 	if (texture == p_texture)
104 		return;
105 	texture = p_texture;
106 	emit_signal("texture_changed");
107 	emit_changed();
108 }
get_texture() const109 RES StyleBoxTexture::get_texture() const {
110 
111 	return texture;
112 }
113 
set_margin_size(Margin p_margin,float p_size)114 void StyleBoxTexture::set_margin_size(Margin p_margin, float p_size) {
115 
116 	margin[p_margin] = p_size;
117 	emit_changed();
118 }
get_margin_size(Margin p_margin) const119 float StyleBoxTexture::get_margin_size(Margin p_margin) const {
120 
121 	return margin[p_margin];
122 }
123 
get_style_margin(Margin p_margin) const124 float StyleBoxTexture::get_style_margin(Margin p_margin) const {
125 
126 	return margin[p_margin];
127 }
128 
draw(RID p_canvas_item,const Rect2 & p_rect) const129 void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
130 	if (texture.is_null())
131 		return;
132 
133 	Rect2 r = p_rect;
134 	r.pos.x -= expand_margin[MARGIN_LEFT];
135 	r.pos.y -= expand_margin[MARGIN_TOP];
136 	r.size.x += expand_margin[MARGIN_LEFT] + expand_margin[MARGIN_RIGHT];
137 	r.size.y += expand_margin[MARGIN_TOP] + expand_margin[MARGIN_BOTTOM];
138 	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);
139 }
140 
set_draw_center(bool p_draw)141 void StyleBoxTexture::set_draw_center(bool p_draw) {
142 
143 	draw_center = p_draw;
144 	emit_changed();
145 }
146 
get_draw_center() const147 bool StyleBoxTexture::get_draw_center() const {
148 
149 	return draw_center;
150 }
151 
get_center_size() const152 Size2 StyleBoxTexture::get_center_size() const {
153 
154 	if (texture.is_null())
155 		return Size2();
156 
157 	return texture->get_size() - get_minimum_size();
158 }
159 
set_expand_margin_size(Margin p_expand_margin,float p_size)160 void StyleBoxTexture::set_expand_margin_size(Margin p_expand_margin, float p_size) {
161 
162 	ERR_FAIL_INDEX(p_expand_margin, 4);
163 	expand_margin[p_expand_margin] = p_size;
164 	emit_changed();
165 }
166 
get_expand_margin_size(Margin p_expand_margin) const167 float StyleBoxTexture::get_expand_margin_size(Margin p_expand_margin) const {
168 
169 	ERR_FAIL_INDEX_V(p_expand_margin, 4, 0);
170 	return expand_margin[p_expand_margin];
171 }
172 
set_region_rect(const Rect2 & p_region_rect)173 void StyleBoxTexture::set_region_rect(const Rect2 &p_region_rect) {
174 
175 	if (region_rect == p_region_rect)
176 		return;
177 
178 	region_rect = p_region_rect;
179 	emit_changed();
180 }
181 
get_region_rect() const182 Rect2 StyleBoxTexture::get_region_rect() const {
183 
184 	return region_rect;
185 }
186 
_bind_methods()187 void StyleBoxTexture::_bind_methods() {
188 
189 	ObjectTypeDB::bind_method(_MD("set_texture", "texture:Texture"), &StyleBoxTexture::set_texture);
190 	ObjectTypeDB::bind_method(_MD("get_texture:Texture"), &StyleBoxTexture::get_texture);
191 
192 	ObjectTypeDB::bind_method(_MD("set_margin_size", "margin", "size"), &StyleBoxTexture::set_margin_size);
193 	ObjectTypeDB::bind_method(_MD("get_margin_size", "margin"), &StyleBoxTexture::get_margin_size);
194 
195 	ObjectTypeDB::bind_method(_MD("set_expand_margin_size", "margin", "size"), &StyleBoxTexture::set_expand_margin_size);
196 	ObjectTypeDB::bind_method(_MD("get_expand_margin_size", "margin"), &StyleBoxTexture::get_expand_margin_size);
197 
198 	ObjectTypeDB::bind_method(_MD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect);
199 	ObjectTypeDB::bind_method(_MD("get_region_rect"), &StyleBoxTexture::get_region_rect);
200 
201 	ObjectTypeDB::bind_method(_MD("set_draw_center", "enable"), &StyleBoxTexture::set_draw_center);
202 	ObjectTypeDB::bind_method(_MD("get_draw_center"), &StyleBoxTexture::get_draw_center);
203 
204 	ADD_SIGNAL(MethodInfo("texture_changed"));
205 
206 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"), _SCS("get_texture"));
207 	ADD_PROPERTYNZ(PropertyInfo(Variant::RECT2, "region_rect"), _SCS("set_region_rect"), _SCS("get_region_rect"));
208 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin/left", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_margin_size"), _SCS("get_margin_size"), MARGIN_LEFT);
209 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin/right", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_margin_size"), _SCS("get_margin_size"), MARGIN_RIGHT);
210 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin/top", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_margin_size"), _SCS("get_margin_size"), MARGIN_TOP);
211 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "margin/bottom", PROPERTY_HINT_RANGE, "0,2048,1"), _SCS("set_margin_size"), _SCS("get_margin_size"), MARGIN_BOTTOM);
212 	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);
213 	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);
214 	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);
215 	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);
216 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), _SCS("set_draw_center"), _SCS("get_draw_center"));
217 }
218 
StyleBoxTexture()219 StyleBoxTexture::StyleBoxTexture() {
220 
221 	for (int i = 0; i < 4; i++) {
222 		margin[i] = 0;
223 		expand_margin[i] = 0;
224 	}
225 	draw_center = true;
226 }
~StyleBoxTexture()227 StyleBoxTexture::~StyleBoxTexture() {
228 }
229 
230 ////////////////
231 
set_bg_color(const Color & p_color)232 void StyleBoxFlat::set_bg_color(const Color &p_color) {
233 
234 	bg_color = p_color;
235 	emit_changed();
236 }
237 
set_light_color(const Color & p_color)238 void StyleBoxFlat::set_light_color(const Color &p_color) {
239 
240 	light_color = p_color;
241 	emit_changed();
242 }
set_dark_color(const Color & p_color)243 void StyleBoxFlat::set_dark_color(const Color &p_color) {
244 
245 	dark_color = p_color;
246 	emit_changed();
247 }
248 
get_bg_color() const249 Color StyleBoxFlat::get_bg_color() const {
250 
251 	return bg_color;
252 }
get_light_color() const253 Color StyleBoxFlat::get_light_color() const {
254 
255 	return light_color;
256 }
get_dark_color() const257 Color StyleBoxFlat::get_dark_color() const {
258 
259 	return dark_color;
260 }
261 
set_border_size(int p_size)262 void StyleBoxFlat::set_border_size(int p_size) {
263 
264 	border_size = p_size;
265 	emit_changed();
266 }
get_border_size() const267 int StyleBoxFlat::get_border_size() const {
268 
269 	return border_size;
270 }
271 
set_border_blend(bool p_blend)272 void StyleBoxFlat::set_border_blend(bool p_blend) {
273 
274 	blend = p_blend;
275 	emit_changed();
276 }
277 
get_border_blend() const278 bool StyleBoxFlat::get_border_blend() const {
279 
280 	return blend;
281 }
282 
set_draw_center(bool p_draw)283 void StyleBoxFlat::set_draw_center(bool p_draw) {
284 
285 	draw_center = p_draw;
286 	emit_changed();
287 }
get_draw_center() const288 bool StyleBoxFlat::get_draw_center() const {
289 
290 	return draw_center;
291 }
get_center_size() const292 Size2 StyleBoxFlat::get_center_size() const {
293 
294 	return Size2();
295 }
296 
draw(RID p_canvas_item,const Rect2 & p_rect) const297 void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
298 
299 	VisualServer *vs = VisualServer::get_singleton();
300 	Rect2i r = p_rect;
301 
302 	for (int i = 0; i < border_size; i++) {
303 
304 		Color color_upleft = light_color;
305 		Color color_downright = dark_color;
306 
307 		if (blend) {
308 
309 			color_upleft.r = (border_size - i) * color_upleft.r / border_size + i * bg_color.r / border_size;
310 			color_upleft.g = (border_size - i) * color_upleft.g / border_size + i * bg_color.g / border_size;
311 			color_upleft.b = (border_size - i) * color_upleft.b / border_size + i * bg_color.b / border_size;
312 
313 			color_downright.r = (border_size - i) * color_downright.r / border_size + i * bg_color.r / border_size;
314 			color_downright.g = (border_size - i) * color_downright.g / border_size + i * bg_color.g / border_size;
315 			color_downright.b = (border_size - i) * color_downright.b / border_size + i * bg_color.b / border_size;
316 		}
317 
318 		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);
319 		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);
320 
321 		vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, Size2(r.size.x, 1)), color_upleft);
322 		vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, Size2(1, r.size.y)), color_upleft);
323 
324 		r.pos.x++;
325 		r.pos.y++;
326 		r.size.x -= 2;
327 		r.size.y -= 2;
328 	}
329 
330 	if (draw_center)
331 		vs->canvas_item_add_rect(p_canvas_item, Rect2(r.pos, r.size), bg_color);
332 }
333 
get_style_margin(Margin p_margin) const334 float StyleBoxFlat::get_style_margin(Margin p_margin) const {
335 
336 	return border_size;
337 }
_bind_methods()338 void StyleBoxFlat::_bind_methods() {
339 
340 	ObjectTypeDB::bind_method(_MD("set_bg_color", "color"), &StyleBoxFlat::set_bg_color);
341 	ObjectTypeDB::bind_method(_MD("get_bg_color"), &StyleBoxFlat::get_bg_color);
342 	ObjectTypeDB::bind_method(_MD("set_light_color", "color"), &StyleBoxFlat::set_light_color);
343 	ObjectTypeDB::bind_method(_MD("get_light_color"), &StyleBoxFlat::get_light_color);
344 	ObjectTypeDB::bind_method(_MD("set_dark_color", "color"), &StyleBoxFlat::set_dark_color);
345 	ObjectTypeDB::bind_method(_MD("get_dark_color"), &StyleBoxFlat::get_dark_color);
346 	ObjectTypeDB::bind_method(_MD("set_border_size", "size"), &StyleBoxFlat::set_border_size);
347 	ObjectTypeDB::bind_method(_MD("get_border_size"), &StyleBoxFlat::get_border_size);
348 	ObjectTypeDB::bind_method(_MD("set_border_blend", "blend"), &StyleBoxFlat::set_border_blend);
349 	ObjectTypeDB::bind_method(_MD("get_border_blend"), &StyleBoxFlat::get_border_blend);
350 	ObjectTypeDB::bind_method(_MD("set_draw_center", "size"), &StyleBoxFlat::set_draw_center);
351 	ObjectTypeDB::bind_method(_MD("get_draw_center"), &StyleBoxFlat::get_draw_center);
352 
353 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), _SCS("set_bg_color"), _SCS("get_bg_color"));
354 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "light_color"), _SCS("set_light_color"), _SCS("get_light_color"));
355 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "dark_color"), _SCS("set_dark_color"), _SCS("get_dark_color"));
356 	ADD_PROPERTY(PropertyInfo(Variant::INT, "border_size", PROPERTY_HINT_RANGE, "0,4096"), _SCS("set_border_size"), _SCS("get_border_size"));
357 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "border_blend"), _SCS("set_border_blend"), _SCS("get_border_blend"));
358 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_bg"), _SCS("set_draw_center"), _SCS("get_draw_center"));
359 }
360 
StyleBoxFlat()361 StyleBoxFlat::StyleBoxFlat() {
362 
363 	bg_color = Color(0.6, 0.6, 0.6);
364 	light_color = Color(0.8, 0.8, 0.8);
365 	dark_color = Color(0.8, 0.8, 0.8);
366 	draw_center = true;
367 	blend = true;
368 	border_size = 0;
369 }
~StyleBoxFlat()370 StyleBoxFlat::~StyleBoxFlat() {
371 }
372 
373 ////////////////
374 
_bind_methods()375 void StyleBoxImageMask::_bind_methods() {
376 
377 	ObjectTypeDB::bind_method(_MD("set_image", "image"), &StyleBoxImageMask::set_image);
378 	ObjectTypeDB::bind_method(_MD("get_image"), &StyleBoxImageMask::get_image);
379 	ObjectTypeDB::bind_method(_MD("set_expand", "expand"), &StyleBoxImageMask::set_expand);
380 	ObjectTypeDB::bind_method(_MD("get_expand"), &StyleBoxImageMask::get_expand);
381 	ObjectTypeDB::bind_method(_MD("set_expand_margin_size", "margin", "size"), &StyleBoxImageMask::set_expand_margin_size);
382 	ObjectTypeDB::bind_method(_MD("get_expand_margin_size", "margin"), &StyleBoxImageMask::get_expand_margin_size);
383 
384 	ADD_PROPERTY(PropertyInfo(Variant::IMAGE, "image"), _SCS("set_image"), _SCS("get_image"));
385 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), _SCS("set_expand"), _SCS("get_expand"));
386 	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);
387 	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);
388 	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);
389 	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);
390 }
391 
test_mask(const Point2 & p_point,const Rect2 & p_rect) const392 bool StyleBoxImageMask::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
393 
394 	if (image.empty())
395 		return false;
396 	if (p_rect.size.x < 1)
397 		return false;
398 	if (p_rect.size.y < 1)
399 		return false;
400 
401 	Size2i imgsize(image.get_width(), image.get_height());
402 	if (imgsize.x <= 0 || imgsize.y <= 0)
403 		return false;
404 
405 	Point2i img_expand_size(imgsize.x - expand_margin[MARGIN_LEFT] - expand_margin[MARGIN_RIGHT], imgsize.y - expand_margin[MARGIN_TOP] - expand_margin[MARGIN_BOTTOM]);
406 	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]);
407 	if (rect_expand_size.x < 1)
408 		rect_expand_size.x = 1;
409 	if (rect_expand_size.y < 1)
410 		rect_expand_size.y = 1;
411 
412 	Point2i click_pos;
413 
414 	//treat x
415 
416 	if (p_point.x < p_rect.pos.x)
417 		click_pos.x = 0;
418 	else if (expand) {
419 
420 		if (p_point.x >= p_rect.pos.x + p_rect.size.x)
421 			click_pos.x = imgsize.x - 1;
422 		else if ((p_point.x - p_rect.pos.x) < expand_margin[MARGIN_LEFT])
423 			click_pos.x = p_point.x;
424 		else if ((p_point.x - (p_rect.pos.x + p_rect.size.x)) < expand_margin[MARGIN_RIGHT])
425 			click_pos.x = imgsize.x - (p_point.x - (p_rect.pos.x + p_rect.size.x));
426 		else //expand
427 			click_pos.x = (p_point.x - p_rect.pos.x - expand_margin[MARGIN_LEFT]) * img_expand_size.x / rect_expand_size.x;
428 	} else if ((p_point.x - p_rect.pos.x) > imgsize.x)
429 		click_pos.x = imgsize.x;
430 
431 	//treat y
432 
433 	if (p_point.y < p_rect.pos.y)
434 		click_pos.y = 0;
435 	else if (expand) {
436 
437 		if (p_point.y >= p_rect.pos.y + p_rect.size.y)
438 			click_pos.y = imgsize.y - 1;
439 		else if ((p_point.y - p_rect.pos.y) < expand_margin[MARGIN_TOP])
440 			click_pos.y = p_point.y;
441 		else if ((p_point.y - (p_rect.pos.y + p_rect.size.y)) < expand_margin[MARGIN_BOTTOM])
442 			click_pos.y = imgsize.y - (p_point.y - (p_rect.pos.y + p_rect.size.y));
443 		else //expand
444 			click_pos.y = (p_point.y - p_rect.pos.y - expand_margin[MARGIN_TOP]) * img_expand_size.y / rect_expand_size.y;
445 	} else if ((p_point.y - p_rect.pos.y) > imgsize.y)
446 		click_pos.y = imgsize.y;
447 
448 	return image.get_pixel(click_pos.x, click_pos.y).gray() > 0.5;
449 }
450 
set_image(const Image & p_image)451 void StyleBoxImageMask::set_image(const Image &p_image) {
452 
453 	image = p_image;
454 }
get_image() const455 Image StyleBoxImageMask::get_image() const {
456 
457 	return image;
458 }
459 
set_expand(bool p_expand)460 void StyleBoxImageMask::set_expand(bool p_expand) {
461 
462 	expand = p_expand;
463 }
get_expand() const464 bool StyleBoxImageMask::get_expand() const {
465 
466 	return expand;
467 }
set_expand_margin_size(Margin p_expand_margin,float p_size)468 void StyleBoxImageMask::set_expand_margin_size(Margin p_expand_margin, float p_size) {
469 
470 	ERR_FAIL_INDEX(p_expand_margin, 4);
471 	expand_margin[p_expand_margin] = p_size;
472 }
473 
get_expand_margin_size(Margin p_expand_margin) const474 float StyleBoxImageMask::get_expand_margin_size(Margin p_expand_margin) const {
475 
476 	ERR_FAIL_INDEX_V(p_expand_margin, 4, 0);
477 	return expand_margin[p_expand_margin];
478 }
479 
StyleBoxImageMask()480 StyleBoxImageMask::StyleBoxImageMask() {
481 
482 	for (int i = 0; i < 4; i++) {
483 		expand_margin[i] = 0;
484 	}
485 	expand = true;
486 }
487