1 | /**************************************************************************/ |
2 | /* split_container.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 "split_container.h" |
32 | |
33 | #include "scene/gui/label.h" |
34 | #include "scene/gui/margin_container.h" |
35 | #include "scene/theme/theme_db.h" |
36 | |
37 | void SplitContainerDragger::gui_input(const Ref<InputEvent> &p_event) { |
38 | ERR_FAIL_COND(p_event.is_null()); |
39 | |
40 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
41 | |
42 | if (sc->collapsed || !sc->_getch(0) || !sc->_getch(1) || sc->dragger_visibility != SplitContainer::DRAGGER_VISIBLE) { |
43 | return; |
44 | } |
45 | |
46 | Ref<InputEventMouseButton> mb = p_event; |
47 | |
48 | if (mb.is_valid()) { |
49 | if (mb->get_button_index() == MouseButton::LEFT) { |
50 | if (mb->is_pressed()) { |
51 | sc->_compute_middle_sep(true); |
52 | dragging = true; |
53 | drag_ofs = sc->split_offset; |
54 | if (sc->vertical) { |
55 | drag_from = get_transform().xform(mb->get_position()).y; |
56 | } else { |
57 | drag_from = get_transform().xform(mb->get_position()).x; |
58 | } |
59 | } else { |
60 | dragging = false; |
61 | queue_redraw(); |
62 | } |
63 | } |
64 | } |
65 | |
66 | Ref<InputEventMouseMotion> mm = p_event; |
67 | |
68 | if (mm.is_valid()) { |
69 | if (!dragging) { |
70 | return; |
71 | } |
72 | |
73 | Vector2i in_parent_pos = get_transform().xform(mm->get_position()); |
74 | if (!sc->vertical && is_layout_rtl()) { |
75 | sc->split_offset = drag_ofs - (in_parent_pos.x - drag_from); |
76 | } else { |
77 | sc->split_offset = drag_ofs + ((sc->vertical ? in_parent_pos.y : in_parent_pos.x) - drag_from); |
78 | } |
79 | sc->_compute_middle_sep(true); |
80 | sc->queue_sort(); |
81 | sc->emit_signal(SNAME("dragged" ), sc->get_split_offset()); |
82 | } |
83 | } |
84 | |
85 | Control::CursorShape SplitContainerDragger::get_cursor_shape(const Point2 &p_pos) const { |
86 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
87 | |
88 | if (!sc->collapsed && sc->dragger_visibility == SplitContainer::DRAGGER_VISIBLE) { |
89 | return (sc->vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT); |
90 | } |
91 | |
92 | return Control::get_cursor_shape(p_pos); |
93 | } |
94 | |
95 | void SplitContainerDragger::_notification(int p_what) { |
96 | switch (p_what) { |
97 | case NOTIFICATION_MOUSE_ENTER: { |
98 | mouse_inside = true; |
99 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
100 | if (sc->theme_cache.autohide) { |
101 | queue_redraw(); |
102 | } |
103 | } break; |
104 | |
105 | case NOTIFICATION_MOUSE_EXIT: { |
106 | mouse_inside = false; |
107 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
108 | if (sc->theme_cache.autohide) { |
109 | queue_redraw(); |
110 | } |
111 | } break; |
112 | |
113 | case NOTIFICATION_DRAW: { |
114 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
115 | if (!dragging && !mouse_inside && sc->theme_cache.autohide) { |
116 | return; |
117 | } |
118 | |
119 | Ref<Texture2D> tex = sc->_get_grabber_icon(); |
120 | draw_texture(tex, (get_size() - tex->get_size()) / 2); |
121 | } break; |
122 | } |
123 | } |
124 | |
125 | Control *SplitContainer::_getch(int p_idx) const { |
126 | int idx = 0; |
127 | |
128 | for (int i = 0; i < get_child_count(false); i++) { |
129 | Control *c = Object::cast_to<Control>(get_child(i, false)); |
130 | if (!c || !c->is_visible()) { |
131 | continue; |
132 | } |
133 | if (c->is_set_as_top_level()) { |
134 | continue; |
135 | } |
136 | |
137 | if (idx == p_idx) { |
138 | return c; |
139 | } |
140 | |
141 | idx++; |
142 | } |
143 | |
144 | return nullptr; |
145 | } |
146 | |
147 | Ref<Texture2D> SplitContainer::_get_grabber_icon() const { |
148 | if (is_fixed) { |
149 | return theme_cache.grabber_icon; |
150 | } else { |
151 | if (vertical) { |
152 | return theme_cache.grabber_icon_v; |
153 | } else { |
154 | return theme_cache.grabber_icon_h; |
155 | } |
156 | } |
157 | } |
158 | |
159 | void SplitContainer::_compute_middle_sep(bool p_clamp) { |
160 | Control *first = _getch(0); |
161 | Control *second = _getch(1); |
162 | |
163 | // Determine expanded children. |
164 | bool first_expanded = (vertical ? first->get_v_size_flags() : first->get_h_size_flags()) & SIZE_EXPAND; |
165 | bool second_expanded = (vertical ? second->get_v_size_flags() : second->get_h_size_flags()) & SIZE_EXPAND; |
166 | |
167 | // Compute the minimum size. |
168 | int axis = vertical ? 1 : 0; |
169 | int size = get_size()[axis]; |
170 | int ms_first = first->get_combined_minimum_size()[axis]; |
171 | int ms_second = second->get_combined_minimum_size()[axis]; |
172 | |
173 | // Determine the separation between items. |
174 | Ref<Texture2D> g = _get_grabber_icon(); |
175 | int sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(theme_cache.separation, vertical ? g->get_height() : g->get_width()) : 0; |
176 | |
177 | // Compute the wished separation_point. |
178 | int wished_middle_sep = 0; |
179 | int split_offset_with_collapse = 0; |
180 | if (!collapsed) { |
181 | split_offset_with_collapse = split_offset; |
182 | } |
183 | if (first_expanded && second_expanded) { |
184 | float ratio = first->get_stretch_ratio() / (first->get_stretch_ratio() + second->get_stretch_ratio()); |
185 | wished_middle_sep = size * ratio - sep / 2 + split_offset_with_collapse; |
186 | } else if (first_expanded) { |
187 | wished_middle_sep = size - sep + split_offset_with_collapse; |
188 | } else { |
189 | wished_middle_sep = split_offset_with_collapse; |
190 | } |
191 | |
192 | // Clamp the middle sep to acceptatble values. |
193 | middle_sep = CLAMP(wished_middle_sep, ms_first, size - sep - ms_second); |
194 | |
195 | // Clamp the split_offset if requested. |
196 | if (p_clamp) { |
197 | split_offset -= wished_middle_sep - middle_sep; |
198 | } |
199 | } |
200 | |
201 | void SplitContainer::_resort() { |
202 | Control *first = _getch(0); |
203 | Control *second = _getch(1); |
204 | |
205 | // If we have only one element. |
206 | if (!first || !second) { |
207 | if (first) { |
208 | fit_child_in_rect(first, Rect2(Point2(), get_size())); |
209 | } else if (second) { |
210 | fit_child_in_rect(second, Rect2(Point2(), get_size())); |
211 | } |
212 | dragging_area_control->hide(); |
213 | return; |
214 | } |
215 | |
216 | // If we have more that one. |
217 | _compute_middle_sep(false); |
218 | |
219 | // Determine the separation between items. |
220 | Ref<Texture2D> g = _get_grabber_icon(); |
221 | int sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(theme_cache.separation, vertical ? g->get_height() : g->get_width()) : 0; |
222 | |
223 | // Move the children, including the dragger. |
224 | if (vertical) { |
225 | fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(get_size().width, middle_sep))); |
226 | int sofs = middle_sep + sep; |
227 | fit_child_in_rect(second, Rect2(Point2(0, sofs), Size2(get_size().width, get_size().height - sofs))); |
228 | } else { |
229 | if (is_layout_rtl()) { |
230 | middle_sep = get_size().width - middle_sep - sep; |
231 | fit_child_in_rect(second, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height))); |
232 | int sofs = middle_sep + sep; |
233 | fit_child_in_rect(first, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height))); |
234 | } else { |
235 | fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height))); |
236 | int sofs = middle_sep + sep; |
237 | fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height))); |
238 | } |
239 | } |
240 | |
241 | // Handle the dragger visibility and position. |
242 | if (dragger_visibility == DRAGGER_VISIBLE && !collapsed) { |
243 | dragging_area_control->show(); |
244 | |
245 | int dragger_ctrl_size = MAX(sep, theme_cache.minimum_grab_thickness); |
246 | if (vertical) { |
247 | dragging_area_control->set_rect(Rect2(Point2(0, middle_sep - (dragger_ctrl_size - sep) / 2), Size2(get_size().width, dragger_ctrl_size))); |
248 | } else { |
249 | dragging_area_control->set_rect(Rect2(Point2(middle_sep - (dragger_ctrl_size - sep) / 2, 0), Size2(dragger_ctrl_size, get_size().height))); |
250 | } |
251 | |
252 | dragging_area_control->queue_redraw(); |
253 | } else { |
254 | dragging_area_control->hide(); |
255 | } |
256 | } |
257 | |
258 | Size2 SplitContainer::get_minimum_size() const { |
259 | Size2i minimum; |
260 | Ref<Texture2D> g = _get_grabber_icon(); |
261 | int sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(theme_cache.separation, vertical ? g->get_height() : g->get_width()) : 0; |
262 | |
263 | for (int i = 0; i < 2; i++) { |
264 | if (!_getch(i)) { |
265 | break; |
266 | } |
267 | |
268 | if (i == 1) { |
269 | if (vertical) { |
270 | minimum.height += sep; |
271 | } else { |
272 | minimum.width += sep; |
273 | } |
274 | } |
275 | |
276 | Size2 ms = _getch(i)->get_combined_minimum_size(); |
277 | |
278 | if (vertical) { |
279 | minimum.height += ms.height; |
280 | minimum.width = MAX(minimum.width, ms.width); |
281 | } else { |
282 | minimum.width += ms.width; |
283 | minimum.height = MAX(minimum.height, ms.height); |
284 | } |
285 | } |
286 | |
287 | return minimum; |
288 | } |
289 | |
290 | void SplitContainer::_validate_property(PropertyInfo &p_property) const { |
291 | if (is_fixed && p_property.name == "vertical" ) { |
292 | p_property.usage = PROPERTY_USAGE_NONE; |
293 | } |
294 | } |
295 | |
296 | void SplitContainer::_notification(int p_what) { |
297 | switch (p_what) { |
298 | case NOTIFICATION_TRANSLATION_CHANGED: |
299 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { |
300 | queue_sort(); |
301 | } break; |
302 | |
303 | case NOTIFICATION_SORT_CHILDREN: { |
304 | _resort(); |
305 | } break; |
306 | |
307 | case NOTIFICATION_THEME_CHANGED: { |
308 | update_minimum_size(); |
309 | } break; |
310 | } |
311 | } |
312 | |
313 | void SplitContainer::set_split_offset(int p_offset) { |
314 | if (split_offset == p_offset) { |
315 | return; |
316 | } |
317 | |
318 | split_offset = p_offset; |
319 | |
320 | queue_sort(); |
321 | } |
322 | |
323 | int SplitContainer::get_split_offset() const { |
324 | return split_offset; |
325 | } |
326 | |
327 | void SplitContainer::clamp_split_offset() { |
328 | if (!_getch(0) || !_getch(1)) { |
329 | return; |
330 | } |
331 | |
332 | _compute_middle_sep(true); |
333 | queue_sort(); |
334 | } |
335 | |
336 | void SplitContainer::set_collapsed(bool p_collapsed) { |
337 | if (collapsed == p_collapsed) { |
338 | return; |
339 | } |
340 | |
341 | collapsed = p_collapsed; |
342 | queue_sort(); |
343 | } |
344 | |
345 | void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) { |
346 | if (dragger_visibility == p_visibility) { |
347 | return; |
348 | } |
349 | |
350 | dragger_visibility = p_visibility; |
351 | queue_sort(); |
352 | } |
353 | |
354 | SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const { |
355 | return dragger_visibility; |
356 | } |
357 | |
358 | bool SplitContainer::is_collapsed() const { |
359 | return collapsed; |
360 | } |
361 | |
362 | void SplitContainer::set_vertical(bool p_vertical) { |
363 | ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + "." ); |
364 | vertical = p_vertical; |
365 | update_minimum_size(); |
366 | _resort(); |
367 | } |
368 | |
369 | bool SplitContainer::is_vertical() const { |
370 | return vertical; |
371 | } |
372 | |
373 | Vector<int> SplitContainer::get_allowed_size_flags_horizontal() const { |
374 | Vector<int> flags; |
375 | flags.append(SIZE_FILL); |
376 | if (!vertical) { |
377 | flags.append(SIZE_EXPAND); |
378 | } |
379 | flags.append(SIZE_SHRINK_BEGIN); |
380 | flags.append(SIZE_SHRINK_CENTER); |
381 | flags.append(SIZE_SHRINK_END); |
382 | return flags; |
383 | } |
384 | |
385 | Vector<int> SplitContainer::get_allowed_size_flags_vertical() const { |
386 | Vector<int> flags; |
387 | flags.append(SIZE_FILL); |
388 | if (vertical) { |
389 | flags.append(SIZE_EXPAND); |
390 | } |
391 | flags.append(SIZE_SHRINK_BEGIN); |
392 | flags.append(SIZE_SHRINK_CENTER); |
393 | flags.append(SIZE_SHRINK_END); |
394 | return flags; |
395 | } |
396 | |
397 | void SplitContainer::_bind_methods() { |
398 | ClassDB::bind_method(D_METHOD("set_split_offset" , "offset" ), &SplitContainer::set_split_offset); |
399 | ClassDB::bind_method(D_METHOD("get_split_offset" ), &SplitContainer::get_split_offset); |
400 | ClassDB::bind_method(D_METHOD("clamp_split_offset" ), &SplitContainer::clamp_split_offset); |
401 | |
402 | ClassDB::bind_method(D_METHOD("set_collapsed" , "collapsed" ), &SplitContainer::set_collapsed); |
403 | ClassDB::bind_method(D_METHOD("is_collapsed" ), &SplitContainer::is_collapsed); |
404 | |
405 | ClassDB::bind_method(D_METHOD("set_dragger_visibility" , "mode" ), &SplitContainer::set_dragger_visibility); |
406 | ClassDB::bind_method(D_METHOD("get_dragger_visibility" ), &SplitContainer::get_dragger_visibility); |
407 | |
408 | ClassDB::bind_method(D_METHOD("set_vertical" , "vertical" ), &SplitContainer::set_vertical); |
409 | ClassDB::bind_method(D_METHOD("is_vertical" ), &SplitContainer::is_vertical); |
410 | |
411 | ADD_SIGNAL(MethodInfo("dragged" , PropertyInfo(Variant::INT, "offset" ))); |
412 | |
413 | ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset" , PROPERTY_HINT_NONE, "suffix:px" ), "set_split_offset" , "get_split_offset" ); |
414 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed" ), "set_collapsed" , "is_collapsed" ); |
415 | ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility" , PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden and Collapsed" ), "set_dragger_visibility" , "get_dragger_visibility" ); |
416 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical" ), "set_vertical" , "is_vertical" ); |
417 | |
418 | BIND_ENUM_CONSTANT(DRAGGER_VISIBLE); |
419 | BIND_ENUM_CONSTANT(DRAGGER_HIDDEN); |
420 | BIND_ENUM_CONSTANT(DRAGGER_HIDDEN_COLLAPSED); |
421 | |
422 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, separation); |
423 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, minimum_grab_thickness); |
424 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, autohide); |
425 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon, "grabber" ); |
426 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon_h, "h_grabber" ); |
427 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon_v, "v_grabber" ); |
428 | } |
429 | |
430 | SplitContainer::SplitContainer(bool p_vertical) { |
431 | vertical = p_vertical; |
432 | |
433 | dragging_area_control = memnew(SplitContainerDragger); |
434 | add_child(dragging_area_control, false, Node::INTERNAL_MODE_BACK); |
435 | } |
436 | |