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 | |
31 | #include "style_box.h" |
32 | |
33 | #include "scene/main/canvas_item.h" |
34 | |
35 | Size2 StyleBox::get_minimum_size() const { |
36 | Size2 min_size = Size2(get_margin(SIDE_LEFT) + get_margin(SIDE_RIGHT), get_margin(SIDE_TOP) + get_margin(SIDE_BOTTOM)); |
37 | Size2 custom_size; |
38 | GDVIRTUAL_CALL(_get_minimum_size, custom_size); |
39 | |
40 | if (min_size.x < custom_size.x) { |
41 | min_size.x = custom_size.x; |
42 | } |
43 | if (min_size.y < custom_size.y) { |
44 | min_size.y = custom_size.y; |
45 | } |
46 | |
47 | return min_size; |
48 | } |
49 | |
50 | void StyleBox::set_content_margin(Side p_side, float p_value) { |
51 | ERR_FAIL_INDEX((int)p_side, 4); |
52 | |
53 | content_margin[p_side] = p_value; |
54 | emit_changed(); |
55 | } |
56 | |
57 | void StyleBox::set_content_margin_all(float p_value) { |
58 | for (int i = 0; i < 4; i++) { |
59 | content_margin[i] = p_value; |
60 | } |
61 | emit_changed(); |
62 | } |
63 | |
64 | void StyleBox::set_content_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { |
65 | content_margin[SIDE_LEFT] = p_left; |
66 | content_margin[SIDE_TOP] = p_top; |
67 | content_margin[SIDE_RIGHT] = p_right; |
68 | content_margin[SIDE_BOTTOM] = p_bottom; |
69 | emit_changed(); |
70 | } |
71 | |
72 | float StyleBox::get_content_margin(Side p_side) const { |
73 | ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); |
74 | |
75 | return content_margin[p_side]; |
76 | } |
77 | |
78 | float StyleBox::get_margin(Side p_side) const { |
79 | ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); |
80 | |
81 | if (content_margin[p_side] < 0) { |
82 | return get_style_margin(p_side); |
83 | } else { |
84 | return content_margin[p_side]; |
85 | } |
86 | } |
87 | |
88 | Point2 StyleBox::get_offset() const { |
89 | return Point2(get_margin(SIDE_LEFT), get_margin(SIDE_TOP)); |
90 | } |
91 | |
92 | void StyleBox::draw(RID p_canvas_item, const Rect2 &p_rect) const { |
93 | GDVIRTUAL_REQUIRED_CALL(_draw, p_canvas_item, p_rect); |
94 | } |
95 | |
96 | Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const { |
97 | Rect2 ret; |
98 | if (GDVIRTUAL_CALL(_get_draw_rect, p_rect, ret)) { |
99 | return ret; |
100 | } |
101 | return p_rect; |
102 | } |
103 | |
104 | CanvasItem *StyleBox::get_current_item_drawn() const { |
105 | return CanvasItem::get_current_item_drawn(); |
106 | } |
107 | |
108 | bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const { |
109 | bool ret = true; |
110 | GDVIRTUAL_CALL(_test_mask, p_point, p_rect, ret); |
111 | return ret; |
112 | } |
113 | |
114 | void StyleBox::_bind_methods() { |
115 | ClassDB::bind_method(D_METHOD("get_minimum_size" ), &StyleBox::get_minimum_size); |
116 | |
117 | ClassDB::bind_method(D_METHOD("set_content_margin" , "margin" , "offset" ), &StyleBox::set_content_margin); |
118 | ClassDB::bind_method(D_METHOD("set_content_margin_all" , "offset" ), &StyleBox::set_content_margin_all); |
119 | ClassDB::bind_method(D_METHOD("get_content_margin" , "margin" ), &StyleBox::get_content_margin); |
120 | |
121 | ClassDB::bind_method(D_METHOD("get_margin" , "margin" ), &StyleBox::get_margin); |
122 | ClassDB::bind_method(D_METHOD("get_offset" ), &StyleBox::get_offset); |
123 | |
124 | ClassDB::bind_method(D_METHOD("draw" , "canvas_item" , "rect" ), &StyleBox::draw); |
125 | ClassDB::bind_method(D_METHOD("get_current_item_drawn" ), &StyleBox::get_current_item_drawn); |
126 | |
127 | ClassDB::bind_method(D_METHOD("test_mask" , "point" , "rect" ), &StyleBox::test_mask); |
128 | |
129 | ADD_GROUP("Content Margins" , "content_margin_" ); |
130 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_left" , PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px" ), "set_content_margin" , "get_content_margin" , SIDE_LEFT); |
131 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_top" , PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px" ), "set_content_margin" , "get_content_margin" , SIDE_TOP); |
132 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_right" , PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px" ), "set_content_margin" , "get_content_margin" , SIDE_RIGHT); |
133 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_bottom" , PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px" ), "set_content_margin" , "get_content_margin" , SIDE_BOTTOM); |
134 | |
135 | GDVIRTUAL_BIND(_draw, "to_canvas_item" , "rect" ) |
136 | GDVIRTUAL_BIND(_get_draw_rect, "rect" ) |
137 | GDVIRTUAL_BIND(_get_minimum_size) |
138 | GDVIRTUAL_BIND(_test_mask, "point" , "rect" ) |
139 | } |
140 | |
141 | StyleBox::StyleBox() { |
142 | for (int i = 0; i < 4; i++) { |
143 | content_margin[i] = -1; |
144 | } |
145 | } |
146 | |