| 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 | #include "split_container.compat.inc" |
| 33 | |
| 34 | #include "scene/gui/texture_rect.h" |
| 35 | #include "scene/main/viewport.h" |
| 36 | #include "scene/theme/theme_db.h" |
| 37 | |
| 38 | void SplitContainerDragger::gui_input(const Ref<InputEvent> &p_event) { |
| 39 | ERR_FAIL_COND(p_event.is_null()); |
| 40 | |
| 41 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 42 | ERR_FAIL_NULL(sc); |
| 43 | |
| 44 | if (sc->collapsed || sc->valid_children.size() < 2u || !sc->dragging_enabled) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if ((sc->vertical && p_event->is_action_pressed("ui_up", true)) || (!sc->vertical && !is_layout_rtl() && p_event->is_action_pressed("ui_left", true)) || (!sc->vertical && is_layout_rtl() && p_event->is_action_pressed("ui_right", true))) { |
| 49 | sc->set_split_offset(sc->get_split_offset(dragger_index) - (sc->vertical ? sc->get_size().height : sc->get_size().width) * 0.1, dragger_index); |
| 50 | sc->clamp_split_offset(dragger_index); |
| 51 | accept_event(); |
| 52 | return; |
| 53 | } else if ((sc->vertical && p_event->is_action_pressed("ui_down", true)) || (!sc->vertical && !is_layout_rtl() && p_event->is_action_pressed("ui_right", true)) || (!sc->vertical && is_layout_rtl() && p_event->is_action_pressed("ui_left", true))) { |
| 54 | sc->set_split_offset(sc->get_split_offset(dragger_index) + (sc->vertical ? sc->get_size().height : sc->get_size().width) * 0.1, dragger_index); |
| 55 | sc->clamp_split_offset(dragger_index); |
| 56 | accept_event(); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | Ref<InputEventMouseButton> mb = p_event; |
| 61 | |
| 62 | if (mb.is_valid()) { |
| 63 | if (mb->get_button_index() == MouseButton::LEFT) { |
| 64 | if (mb->is_pressed()) { |
| 65 | // To match the visual position, clamp on the first split. |
| 66 | sc->_update_dragger_positions(0); |
| 67 | dragging = true; |
| 68 | sc->emit_signal(SNAME("drag_started")); |
| 69 | start_drag_split_offset = sc->get_split_offset(dragger_index); |
| 70 | if (sc->vertical) { |
| 71 | drag_from = (int)get_transform().xform(mb->get_position()).y; |
| 72 | } else { |
| 73 | drag_from = (int)get_transform().xform(mb->get_position()).x; |
| 74 | } |
| 75 | } else { |
| 76 | dragging = false; |
| 77 | queue_redraw(); |
| 78 | sc->emit_signal(SNAME("drag_ended")); |
| 79 | } |
| 80 | accept_event(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Ref<InputEventMouseMotion> mm = p_event; |
| 85 | |
| 86 | if (mm.is_valid()) { |
| 87 | if (!dragging) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | Vector2i in_parent_pos = get_transform().xform(mm->get_position()); |
| 92 | int new_drag_offset; |
| 93 | if (!sc->vertical && is_layout_rtl()) { |
| 94 | new_drag_offset = start_drag_split_offset - (in_parent_pos.x - drag_from); |
| 95 | } else { |
| 96 | new_drag_offset = start_drag_split_offset + ((sc->vertical ? in_parent_pos.y : in_parent_pos.x) - drag_from); |
| 97 | } |
| 98 | sc->set_split_offset(new_drag_offset, dragger_index); |
| 99 | sc->_update_dragger_positions(dragger_index); |
| 100 | sc->queue_sort(); |
| 101 | sc->emit_signal(SNAME("dragged"), sc->get_split_offset(dragger_index)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | Control::CursorShape SplitContainerDragger::get_cursor_shape(const Point2 &p_pos) const { |
| 106 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 107 | if (!sc->collapsed && sc->dragging_enabled) { |
| 108 | return (sc->vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT); |
| 109 | } |
| 110 | return Control::get_cursor_shape(p_pos); |
| 111 | } |
| 112 | |
| 113 | void SplitContainerDragger::_accessibility_action_inc(const Variant &p_data) { |
| 114 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 115 | |
| 116 | if (sc->collapsed || sc->valid_children.size() < 2u || !sc->dragging_enabled) { |
| 117 | return; |
| 118 | } |
| 119 | sc->set_split_offset(sc->get_split_offset(dragger_index) - (sc->vertical ? sc->get_size().height : sc->get_size().width) * 0.1, dragger_index); |
| 120 | sc->clamp_split_offset(dragger_index); |
| 121 | } |
| 122 | |
| 123 | void SplitContainerDragger::_accessibility_action_dec(const Variant &p_data) { |
| 124 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 125 | |
| 126 | if (sc->collapsed || sc->valid_children.size() < 2u || !sc->dragging_enabled) { |
| 127 | return; |
| 128 | } |
| 129 | sc->set_split_offset(sc->get_split_offset(dragger_index) + (sc->vertical ? sc->get_size().height : sc->get_size().width) * 0.1, dragger_index); |
| 130 | sc->clamp_split_offset(dragger_index); |
| 131 | } |
| 132 | |
| 133 | void SplitContainerDragger::_accessibility_action_set_value(const Variant &p_data) { |
| 134 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 135 | |
| 136 | if (sc->collapsed || sc->valid_children.size() < 2u || !sc->dragging_enabled) { |
| 137 | return; |
| 138 | } |
| 139 | sc->set_split_offset(p_data, dragger_index); |
| 140 | sc->clamp_split_offset(dragger_index); |
| 141 | } |
| 142 | |
| 143 | void SplitContainerDragger::_touch_dragger_mouse_exited() { |
| 144 | if (!dragging) { |
| 145 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 146 | touch_dragger->set_modulate(sc->theme_cache.touch_dragger_color); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void SplitContainerDragger::_touch_dragger_gui_input(const Ref<InputEvent> &p_event) { |
| 151 | if (!touch_dragger) { |
| 152 | return; |
| 153 | } |
| 154 | Ref<InputEventMouseMotion> mm = p_event; |
| 155 | Ref<InputEventMouseButton> mb = p_event; |
| 156 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 157 | |
| 158 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) { |
| 159 | if (mb->is_pressed()) { |
| 160 | touch_dragger->set_modulate(sc->theme_cache.touch_dragger_pressed_color); |
| 161 | } else { |
| 162 | touch_dragger->set_modulate(sc->theme_cache.touch_dragger_color); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (mm.is_valid() && !dragging) { |
| 167 | touch_dragger->set_modulate(sc->theme_cache.touch_dragger_hover_color); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void SplitContainerDragger::set_touch_dragger_enabled(bool p_enabled) { |
| 172 | if (p_enabled) { |
| 173 | touch_dragger = memnew(TextureRect); |
| 174 | update_touch_dragger(); |
| 175 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 176 | touch_dragger->set_modulate(sc->theme_cache.touch_dragger_color); |
| 177 | touch_dragger->connect(SceneStringName(gui_input), callable_mp(this, &SplitContainerDragger::_touch_dragger_gui_input)); |
| 178 | touch_dragger->connect(SceneStringName(mouse_exited), callable_mp(this, &SplitContainerDragger::_touch_dragger_mouse_exited)); |
| 179 | add_child(touch_dragger, false, Node::INTERNAL_MODE_FRONT); |
| 180 | } else { |
| 181 | if (touch_dragger) { |
| 182 | touch_dragger->queue_free(); |
| 183 | touch_dragger = nullptr; |
| 184 | } |
| 185 | } |
| 186 | queue_redraw(); |
| 187 | } |
| 188 | |
| 189 | void SplitContainerDragger::update_touch_dragger() { |
| 190 | if (!touch_dragger) { |
| 191 | return; |
| 192 | } |
| 193 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 194 | touch_dragger->set_texture(sc->_get_touch_dragger_icon()); |
| 195 | touch_dragger->set_anchors_and_offsets_preset(Control::PRESET_CENTER); |
| 196 | touch_dragger->set_default_cursor_shape(sc->vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT); |
| 197 | } |
| 198 | |
| 199 | void SplitContainerDragger::_notification(int p_what) { |
| 200 | switch (p_what) { |
| 201 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 202 | if (dragger_index < 0) { |
| 203 | return; |
| 204 | } |
| 205 | RID ae = get_accessibility_element(); |
| 206 | ERR_FAIL_COND(ae.is_null()); |
| 207 | |
| 208 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SPLITTER); |
| 209 | DisplayServer::get_singleton()->accessibility_update_set_name(ae, RTR("Drag to resize")); |
| 210 | |
| 211 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 212 | if (sc->collapsed || sc->valid_children.size() < 2u || !sc->dragging_enabled) { |
| 213 | return; |
| 214 | } |
| 215 | sc->clamp_split_offset(dragger_index); |
| 216 | DisplayServer::get_singleton()->accessibility_update_set_num_value(ae, sc->get_split_offset(dragger_index)); |
| 217 | |
| 218 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &SplitContainerDragger::_accessibility_action_dec)); |
| 219 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &SplitContainerDragger::_accessibility_action_inc)); |
| 220 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &SplitContainerDragger::_accessibility_action_set_value)); |
| 221 | } break; |
| 222 | |
| 223 | case NOTIFICATION_THEME_CHANGED: { |
| 224 | if (touch_dragger) { |
| 225 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 226 | touch_dragger->set_modulate(sc->theme_cache.touch_dragger_color); |
| 227 | touch_dragger->set_texture(sc->_get_touch_dragger_icon()); |
| 228 | } |
| 229 | } break; |
| 230 | |
| 231 | case NOTIFICATION_MOUSE_ENTER: { |
| 232 | mouse_inside = true; |
| 233 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 234 | if (sc->theme_cache.autohide) { |
| 235 | queue_redraw(); |
| 236 | } |
| 237 | } break; |
| 238 | |
| 239 | case NOTIFICATION_MOUSE_EXIT: { |
| 240 | mouse_inside = false; |
| 241 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 242 | if (sc->theme_cache.autohide) { |
| 243 | queue_redraw(); |
| 244 | } |
| 245 | } break; |
| 246 | |
| 247 | case NOTIFICATION_FOCUS_EXIT: { |
| 248 | if (dragging) { |
| 249 | dragging = false; |
| 250 | queue_redraw(); |
| 251 | } |
| 252 | } break; |
| 253 | |
| 254 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 255 | if (dragging && !is_visible_in_tree()) { |
| 256 | dragging = false; |
| 257 | } |
| 258 | } break; |
| 259 | |
| 260 | case NOTIFICATION_DRAW: { |
| 261 | SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent()); |
| 262 | draw_style_box(sc->theme_cache.split_bar_background, split_bar_rect); |
| 263 | if (sc->dragger_visibility == SplitContainer::DRAGGER_VISIBLE && (dragging || mouse_inside || !sc->theme_cache.autohide) && !sc->touch_dragger_enabled) { |
| 264 | Ref<Texture2D> tex = sc->_get_grabber_icon(); |
| 265 | float available_size = sc->vertical ? (sc->get_size().x - tex->get_size().x) : (sc->get_size().y - tex->get_size().y); |
| 266 | if (available_size - sc->drag_area_margin_begin - sc->drag_area_margin_end > 0) { // Draw the grabber only if it fits. |
| 267 | draw_texture(tex, (split_bar_rect.get_position() + (split_bar_rect.get_size() - tex->get_size()) * 0.5)); |
| 268 | } |
| 269 | } |
| 270 | if (sc->show_drag_area && Engine::get_singleton()->is_editor_hint()) { |
| 271 | draw_rect(Rect2(Vector2(0, 0), get_size()), sc->dragging_enabled ? Color(1, 1, 0, 0.3) : Color(1, 0, 0, 0.3)); |
| 272 | } |
| 273 | } break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | SplitContainerDragger::SplitContainerDragger() { |
| 278 | set_focus_mode(FOCUS_ACCESSIBILITY); |
| 279 | } |
| 280 | |
| 281 | Ref<Texture2D> SplitContainer::_get_grabber_icon() const { |
| 282 | if (is_fixed) { |
| 283 | return theme_cache.grabber_icon; |
| 284 | } else { |
| 285 | if (vertical) { |
| 286 | return theme_cache.grabber_icon_v; |
| 287 | } else { |
| 288 | return theme_cache.grabber_icon_h; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | Ref<Texture2D> SplitContainer::_get_touch_dragger_icon() const { |
| 294 | if (is_fixed) { |
| 295 | return theme_cache.touch_dragger_icon; |
| 296 | } else { |
| 297 | if (vertical) { |
| 298 | return theme_cache.touch_dragger_icon_v; |
| 299 | } else { |
| 300 | return theme_cache.touch_dragger_icon_h; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | int SplitContainer::_get_separation() const { |
| 306 | if (dragger_visibility == DRAGGER_HIDDEN_COLLAPSED) { |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | if (touch_dragger_enabled) { |
| 311 | return theme_cache.separation; |
| 312 | } |
| 313 | // DRAGGER_VISIBLE or DRAGGER_HIDDEN. |
| 314 | Ref<Texture2D> g = _get_grabber_icon(); |
| 315 | return MAX(theme_cache.separation, vertical ? g->get_height() : g->get_width()); |
| 316 | } |
| 317 | |
| 318 | Point2i SplitContainer::_get_valid_range(int p_dragger_index) const { |
| 319 | ERR_FAIL_INDEX_V(p_dragger_index, (int)dragger_positions.size(), Point2i()); |
| 320 | const int axis = vertical ? 1 : 0; |
| 321 | const int sep = _get_separation(); |
| 322 | |
| 323 | // Sum the minimum sizes on the left and right sides of the dragger. |
| 324 | Point2i position_range = Point2i(0, (int)get_size()[axis]); |
| 325 | position_range.x += sep * p_dragger_index; |
| 326 | position_range.y -= sep * ((int)dragger_positions.size() - p_dragger_index); |
| 327 | |
| 328 | for (int i = 0; i < (int)valid_children.size(); i++) { |
| 329 | Control *child = valid_children[i]; |
| 330 | ERR_FAIL_NULL_V(child, Point2i()); |
| 331 | if (i <= p_dragger_index) { |
| 332 | position_range.x += (int)child->get_combined_minimum_size()[axis]; |
| 333 | } else if (i > p_dragger_index) { |
| 334 | position_range.y -= (int)child->get_combined_minimum_size()[axis]; |
| 335 | } |
| 336 | } |
| 337 | return position_range; |
| 338 | } |
| 339 | |
| 340 | PackedInt32Array SplitContainer::_get_desired_sizes() const { |
| 341 | ERR_FAIL_COND_V((int)default_dragger_positions.size() != split_offsets.size() || (int)valid_children.size() - 1 != split_offsets.size(), PackedInt32Array()); |
| 342 | PackedInt32Array desired_sizes; |
| 343 | desired_sizes.resize_uninitialized((int)valid_children.size()); |
| 344 | |
| 345 | const int sep = _get_separation(); |
| 346 | const int axis = vertical ? 1 : 0; |
| 347 | |
| 348 | int desired_start_pos = 0; |
| 349 | for (int i = 0; i < (int)valid_children.size() - 1; i++) { |
| 350 | const int desired_end_pos = default_dragger_positions[i] + split_offsets[i]; |
| 351 | desired_sizes.write[i] = desired_end_pos - desired_start_pos; |
| 352 | desired_start_pos = desired_end_pos + sep; |
| 353 | } |
| 354 | desired_sizes.write[(int)valid_children.size() - 1] = (int)get_size()[axis] - desired_start_pos; |
| 355 | |
| 356 | return desired_sizes; |
| 357 | } |
| 358 | |
| 359 | void SplitContainer::_set_desired_sizes(const PackedInt32Array &p_desired_sizes, int p_priority_index) { |
| 360 | const int sep = _get_separation(); |
| 361 | const int axis = vertical ? 1 : 0; |
| 362 | const real_t size = get_size()[axis]; |
| 363 | |
| 364 | real_t total_desired_size = 0; |
| 365 | if (!p_desired_sizes.is_empty()) { |
| 366 | ERR_FAIL_COND((int)valid_children.size() != p_desired_sizes.size()); |
| 367 | total_desired_size += sep * (p_desired_sizes.size() - 1); |
| 368 | } |
| 369 | |
| 370 | struct StretchData { |
| 371 | real_t min_size = 0; |
| 372 | real_t stretch_ratio = 0.0; |
| 373 | real_t final_size = 0; |
| 374 | bool priority = false; |
| 375 | }; |
| 376 | |
| 377 | // First pass, determine the total stretch amount. |
| 378 | real_t stretch_total = 0; |
| 379 | LocalVector<StretchData> stretch_data; |
| 380 | for (int i = 0; i < (int)valid_children.size(); i++) { |
| 381 | Control *child = valid_children[i]; |
| 382 | StretchData sdata; |
| 383 | sdata.min_size = child->get_combined_minimum_size()[axis]; |
| 384 | sdata.final_size = MAX(sdata.min_size, p_desired_sizes.is_empty() ? 0 : p_desired_sizes[i]); |
| 385 | total_desired_size += sdata.final_size; |
| 386 | sdata.priority = i == p_priority_index; |
| 387 | // Treat the priority child as not expanded, so it doesn't shrink with other expanded children. |
| 388 | if (i != p_priority_index && child->get_stretch_ratio() > 0 && (vertical ? child->get_v_size_flags() : child->get_h_size_flags()).has_flag(SIZE_EXPAND)) { |
| 389 | sdata.stretch_ratio = child->get_stretch_ratio(); |
| 390 | stretch_total += sdata.stretch_ratio; |
| 391 | } |
| 392 | stretch_data.push_back(sdata); |
| 393 | } |
| 394 | |
| 395 | real_t available_space = size - total_desired_size; |
| 396 | |
| 397 | // Grow expanding children. |
| 398 | if (available_space > 0) { |
| 399 | const real_t grow_amount = available_space / stretch_total; |
| 400 | for (StretchData &sdata : stretch_data) { |
| 401 | if (sdata.stretch_ratio <= 0) { |
| 402 | continue; |
| 403 | } |
| 404 | const real_t prev_size = sdata.final_size; |
| 405 | sdata.final_size = prev_size + grow_amount * sdata.stretch_ratio; |
| 406 | const real_t size_diff = prev_size - sdata.final_size; |
| 407 | available_space += size_diff; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Shrink expanding children. |
| 412 | while (available_space < 0) { |
| 413 | real_t shrinkable_stretch_ratio = 0.0; |
| 414 | real_t shrinkable_amount = 0.0; |
| 415 | for (const StretchData &sdata : stretch_data) { |
| 416 | if (sdata.stretch_ratio <= 0 || sdata.final_size <= sdata.min_size) { |
| 417 | continue; |
| 418 | } |
| 419 | shrinkable_stretch_ratio += sdata.stretch_ratio; |
| 420 | shrinkable_amount += sdata.final_size - sdata.min_size; |
| 421 | } |
| 422 | if (shrinkable_stretch_ratio == 0) { |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | const real_t shrink_amount = MIN(-available_space, shrinkable_amount) / shrinkable_stretch_ratio; |
| 427 | if (Math::is_zero_approx(shrink_amount)) { |
| 428 | break; |
| 429 | } |
| 430 | const real_t prev_available_space = available_space; |
| 431 | for (StretchData &sdata : stretch_data) { |
| 432 | if (sdata.stretch_ratio <= 0 || sdata.final_size <= sdata.min_size) { |
| 433 | continue; |
| 434 | } |
| 435 | const real_t prev_size = sdata.final_size; |
| 436 | sdata.final_size = CLAMP(prev_size - shrink_amount * sdata.stretch_ratio, sdata.min_size, sdata.final_size); |
| 437 | const real_t size_diff = prev_size - sdata.final_size; |
| 438 | available_space += size_diff; |
| 439 | } |
| 440 | if (Math::is_equal_approx(available_space, prev_available_space)) { |
| 441 | // Shrinking can fail due to values being too small to have an effect but too large for `is_zero_approx`. |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // Shrink non-expanding children. |
| 447 | bool skip_priority_child = true; |
| 448 | while (available_space < 0) { |
| 449 | // Get largest and target sizes. The target size is the second largest size. |
| 450 | real_t largest_size = 0; |
| 451 | real_t target_size = 0; |
| 452 | int largest_count = 0; |
| 453 | for (const StretchData &sdata : stretch_data) { |
| 454 | if (sdata.final_size <= sdata.min_size || (skip_priority_child && sdata.priority)) { |
| 455 | continue; |
| 456 | } |
| 457 | if (sdata.final_size > largest_size) { |
| 458 | target_size = largest_size; |
| 459 | largest_size = sdata.final_size; |
| 460 | largest_count = 1; |
| 461 | } else if (sdata.final_size == largest_size) { |
| 462 | largest_count++; |
| 463 | } else if (sdata.final_size < largest_size && sdata.final_size > target_size) { |
| 464 | target_size = sdata.final_size; |
| 465 | } |
| 466 | } |
| 467 | if (largest_size <= 0) { |
| 468 | if (skip_priority_child) { |
| 469 | // Retry with priority child. |
| 470 | skip_priority_child = false; |
| 471 | continue; |
| 472 | } else { |
| 473 | // No more children to shrink. |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | // Don't shrink smaller than needed. |
| 478 | target_size = MAX(target_size, largest_size + available_space / largest_count); |
| 479 | const real_t prev_available_space = available_space; |
| 480 | for (StretchData &sdata : stretch_data) { |
| 481 | if (sdata.final_size <= sdata.min_size || (skip_priority_child && sdata.priority)) { |
| 482 | continue; |
| 483 | } |
| 484 | // Shrink all largest elements. |
| 485 | if (sdata.final_size == largest_size) { |
| 486 | sdata.final_size = CLAMP(target_size, sdata.min_size, sdata.final_size); |
| 487 | const real_t size_diff = largest_size - sdata.final_size; |
| 488 | available_space += size_diff; |
| 489 | } |
| 490 | } |
| 491 | if (Math::is_zero_approx(available_space) || Math::is_equal_approx(available_space, prev_available_space)) { |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | ERR_FAIL_COND((int)default_dragger_positions.size() != (int)stretch_data.size() - 1); |
| 497 | |
| 498 | // Update the split offsets to match the desired sizes. |
| 499 | split_offsets.resize(MAX(1, (int)default_dragger_positions.size())); |
| 500 | int pos = 0; |
| 501 | real_t error_accumulator = 0.0; |
| 502 | for (int i = 0; i < (int)default_dragger_positions.size(); i++) { |
| 503 | int final_size = (int)stretch_data[i].final_size; |
| 504 | if (final_size == stretch_data[i].final_size) { |
| 505 | error_accumulator += stretch_data[i].final_size - final_size; |
| 506 | if (error_accumulator > 1.0) { |
| 507 | error_accumulator -= 1.0; |
| 508 | final_size += 1; |
| 509 | } |
| 510 | } |
| 511 | pos += final_size; |
| 512 | split_offsets.write[i] = pos - default_dragger_positions[i]; |
| 513 | pos += sep; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void SplitContainer::_update_default_dragger_positions() { |
| 518 | if (valid_children.size() <= 1u) { |
| 519 | default_dragger_positions.clear(); |
| 520 | return; |
| 521 | } |
| 522 | default_dragger_positions.resize((int)valid_children.size() - 1); |
| 523 | |
| 524 | const int sep = _get_separation(); |
| 525 | const int axis = vertical ? 1 : 0; |
| 526 | const int size = (int)get_size()[axis]; |
| 527 | |
| 528 | struct StretchData { |
| 529 | int min_size = 0; |
| 530 | real_t stretch_ratio = 0.0; |
| 531 | int final_size = 0; |
| 532 | bool expand_flag = false; |
| 533 | bool will_stretch = false; |
| 534 | }; |
| 535 | |
| 536 | // First pass, determine the total stretch amount. |
| 537 | real_t stretchable_space = size - sep * ((int)valid_children.size() - 1); |
| 538 | real_t stretch_total = 0; |
| 539 | int expand_count = 0; |
| 540 | LocalVector<StretchData> stretch_data; |
| 541 | for (const Control *child : valid_children) { |
| 542 | StretchData sdata; |
| 543 | sdata.min_size = (int)child->get_combined_minimum_size()[axis]; |
| 544 | sdata.final_size = sdata.min_size; |
| 545 | if ((vertical ? child->get_v_size_flags() : child->get_h_size_flags()).has_flag(SIZE_EXPAND) && child->get_stretch_ratio() > 0) { |
| 546 | sdata.stretch_ratio = child->get_stretch_ratio(); |
| 547 | stretch_total += sdata.stretch_ratio; |
| 548 | sdata.expand_flag = true; |
| 549 | sdata.will_stretch = true; |
| 550 | expand_count++; |
| 551 | } else { |
| 552 | stretchable_space -= sdata.min_size; |
| 553 | } |
| 554 | stretch_data.push_back(sdata); |
| 555 | } |
| 556 | |
| 557 | #ifndef DISABLE_DEPRECATED |
| 558 | if (expand_count == 2 && valid_children.size() == 2u) { |
| 559 | // Special case when there are 2 expanded children, ignore minimum sizes. |
| 560 | const real_t ratio = stretch_data[0].stretch_ratio / (stretch_data[0].stretch_ratio + stretch_data[1].stretch_ratio); |
| 561 | default_dragger_positions[0] = (int)(size * ratio - sep * 0.5); |
| 562 | return; |
| 563 | } |
| 564 | #endif // DISABLE_DEPRECATED |
| 565 | |
| 566 | // Determine final sizes if stretching. |
| 567 | while (stretch_total > 0.0 && stretchable_space > 0.0) { |
| 568 | bool refit_successful = true; |
| 569 | // Keep track of accumulated error in pixels. |
| 570 | float error = 0.0; |
| 571 | for (StretchData &sdata : stretch_data) { |
| 572 | if (!sdata.will_stretch) { |
| 573 | continue; |
| 574 | } |
| 575 | // Check if it reaches its minimum size. |
| 576 | const float desired_stretch_size = sdata.stretch_ratio / stretch_total * stretchable_space; |
| 577 | error += desired_stretch_size - (int)desired_stretch_size; |
| 578 | if (desired_stretch_size < sdata.min_size) { |
| 579 | // Will not be stretched, remove and retry. |
| 580 | stretch_total -= sdata.stretch_ratio; |
| 581 | stretchable_space -= sdata.min_size; |
| 582 | sdata.will_stretch = false; |
| 583 | sdata.final_size = sdata.min_size; |
| 584 | refit_successful = false; |
| 585 | break; |
| 586 | } else { |
| 587 | sdata.final_size = (int)desired_stretch_size; |
| 588 | // Dump accumulated error if one pixel or more. |
| 589 | if (error >= 1.0) { |
| 590 | sdata.final_size += 1; |
| 591 | error -= 1; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | if (refit_successful) { |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | // Set the default positions. |
| 601 | int pos = 0; |
| 602 | int expands_seen = 0; |
| 603 | for (int i = 0; i < (int)default_dragger_positions.size(); i++) { |
| 604 | pos += stretch_data[i].final_size; |
| 605 | if (stretch_data[i].expand_flag) { |
| 606 | expands_seen += 1; |
| 607 | } |
| 608 | if (expands_seen == 0) { |
| 609 | // Before all expand flags. |
| 610 | default_dragger_positions[i] = 0; |
| 611 | } else if (expands_seen >= expand_count) { |
| 612 | // After all expand flags. |
| 613 | default_dragger_positions[i] = size - sep; |
| 614 | } else { |
| 615 | default_dragger_positions[i] = pos; |
| 616 | } |
| 617 | pos += sep; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | void SplitContainer::_update_dragger_positions(int p_clamp_index) { |
| 622 | if (p_clamp_index != -1) { |
| 623 | ERR_FAIL_INDEX(p_clamp_index, (int)dragger_positions.size()); |
| 624 | } |
| 625 | |
| 626 | const int sep = _get_separation(); |
| 627 | const int axis = vertical ? 1 : 0; |
| 628 | const int size = (int)get_size()[axis]; |
| 629 | |
| 630 | dragger_positions.resize(default_dragger_positions.size()); |
| 631 | |
| 632 | if (split_offsets.size() < (int)default_dragger_positions.size() || split_offsets.is_empty()) { |
| 633 | split_offsets.resize_initialized(MAX(1, (int)default_dragger_positions.size())); |
| 634 | } |
| 635 | |
| 636 | if (collapsed) { |
| 637 | for (int i = 0; i < (int)dragger_positions.size(); i++) { |
| 638 | dragger_positions[i] = default_dragger_positions[i]; |
| 639 | const Point2i valid_range = _get_valid_range(i); |
| 640 | dragger_positions[i] = CLAMP(dragger_positions[i], valid_range.x, valid_range.y); |
| 641 | if (p_clamp_index != -1) { |
| 642 | split_offsets.write[i] = dragger_positions[i] - default_dragger_positions[i]; |
| 643 | } |
| 644 | if (!vertical && is_layout_rtl()) { |
| 645 | dragger_positions[i] = size - dragger_positions[i] - sep; |
| 646 | } |
| 647 | } |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | // Use split_offsets to find the desired dragger positions. |
| 652 | for (int i = 0; i < (int)dragger_positions.size(); i++) { |
| 653 | // Clamp the desired position to acceptable values. |
| 654 | const Point2i valid_range = _get_valid_range(i); |
| 655 | dragger_positions[i] = CLAMP(default_dragger_positions[i] + split_offsets[i], valid_range.x, valid_range.y); |
| 656 | } |
| 657 | |
| 658 | // Prevent overlaps. |
| 659 | if (p_clamp_index == -1) { |
| 660 | // Check each dragger with the one to the right of it. |
| 661 | for (int i = 0; i < (int)dragger_positions.size() - 1; i++) { |
| 662 | const int check_min_size = (int)valid_children[i + 1]->get_combined_minimum_size()[axis]; |
| 663 | const int push_pos = dragger_positions[i] + sep + check_min_size; |
| 664 | if (dragger_positions[i + 1] < push_pos) { |
| 665 | dragger_positions[i + 1] = push_pos; |
| 666 | const Point2i valid_range = _get_valid_range(i); |
| 667 | dragger_positions[i] = CLAMP(dragger_positions[i], valid_range.x, valid_range.y); |
| 668 | } |
| 669 | } |
| 670 | } else { |
| 671 | // Prioritize the active dragger. |
| 672 | const int dragging_position = dragger_positions[p_clamp_index]; |
| 673 | |
| 674 | // Push overlapping draggers to the left. |
| 675 | int accumulated_min_size = (int)valid_children[p_clamp_index]->get_combined_minimum_size()[axis]; |
| 676 | for (int i = p_clamp_index - 1; i >= 0; i--) { |
| 677 | const int push_pos = dragging_position - sep * (p_clamp_index - i) - accumulated_min_size; |
| 678 | if (dragger_positions[i] > push_pos) { |
| 679 | dragger_positions[i] = push_pos; |
| 680 | } |
| 681 | accumulated_min_size += (int)valid_children[i]->get_combined_minimum_size()[axis]; |
| 682 | } |
| 683 | |
| 684 | // Push overlapping draggers to the right. |
| 685 | accumulated_min_size = 0; |
| 686 | for (int i = p_clamp_index + 1; i < (int)dragger_positions.size(); i++) { |
| 687 | accumulated_min_size += (int)valid_children[i]->get_combined_minimum_size()[axis]; |
| 688 | const int push_pos = dragging_position + sep * (i - p_clamp_index) + accumulated_min_size; |
| 689 | if (dragger_positions[i] < push_pos) { |
| 690 | dragger_positions[i] = push_pos; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | // Clamp the split_offset if requested. |
| 696 | if (p_clamp_index != -1) { |
| 697 | for (int i = 0; i < (int)dragger_positions.size(); i++) { |
| 698 | split_offsets.write[i] = dragger_positions[i] - default_dragger_positions[i]; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // Invert if rtl. |
| 703 | if (!vertical && is_layout_rtl()) { |
| 704 | for (int i = 0; i < (int)dragger_positions.size(); i++) { |
| 705 | dragger_positions[i] = size - dragger_positions[i] - sep; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | void SplitContainer::_resort() { |
| 711 | if (!is_visible_in_tree()) { |
| 712 | return; |
| 713 | } |
| 714 | if (valid_children.size() < 2u) { |
| 715 | if (valid_children.size() == 1u) { |
| 716 | // Only one valid child. |
| 717 | Control *child = valid_children[0]; |
| 718 | fit_child_in_rect(child, Rect2(Point2(), get_size())); |
| 719 | } |
| 720 | for (SplitContainerDragger *dragger : dragging_area_controls) { |
| 721 | dragger->hide(); |
| 722 | } |
| 723 | return; |
| 724 | } |
| 725 | for (SplitContainerDragger *dragger : dragging_area_controls) { |
| 726 | dragger->set_visible(!collapsed); |
| 727 | if (touch_dragger_enabled) { |
| 728 | dragger->touch_dragger->set_visible(dragging_enabled); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | _update_default_dragger_positions(); |
| 733 | _update_dragger_positions(); |
| 734 | |
| 735 | const int sep = _get_separation(); |
| 736 | const int axis = vertical ? 1 : 0; |
| 737 | const Size2i new_size = get_size(); |
| 738 | const bool rtl = is_layout_rtl(); |
| 739 | |
| 740 | // Move the children. |
| 741 | for (int i = 0; i < (int)valid_children.size(); i++) { |
| 742 | Control *child = valid_children[i]; |
| 743 | int start_pos; |
| 744 | int end_pos; |
| 745 | if (!vertical && rtl) { |
| 746 | start_pos = i >= (int)dragger_positions.size() ? 0 : dragger_positions[i] + sep; |
| 747 | end_pos = i == 0 ? new_size[axis] : dragger_positions[i - 1]; |
| 748 | } else { |
| 749 | start_pos = i == 0 ? 0 : dragger_positions[i - 1] + sep; |
| 750 | end_pos = i >= (int)dragger_positions.size() ? new_size[axis] : dragger_positions[i]; |
| 751 | } |
| 752 | int size = end_pos - start_pos; |
| 753 | |
| 754 | if (vertical) { |
| 755 | fit_child_in_rect(child, Rect2(Point2(0, start_pos), Size2(new_size.width, size))); |
| 756 | } else { |
| 757 | fit_child_in_rect(child, Rect2(Point2(start_pos, 0), Size2(size, new_size.height))); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | _update_draggers(); |
| 762 | |
| 763 | // Update dragger positions. |
| 764 | const int dragger_ctrl_size = MAX(sep, theme_cache.minimum_grab_thickness); |
| 765 | const float split_bar_offset = (dragger_ctrl_size - sep) * 0.5; |
| 766 | |
| 767 | ERR_FAIL_COND(dragging_area_controls.size() != dragger_positions.size()); |
| 768 | for (int i = 0; i < (int)dragger_positions.size(); i++) { |
| 769 | dragging_area_controls[i]->set_mouse_filter(dragging_enabled ? MOUSE_FILTER_STOP : MOUSE_FILTER_IGNORE); |
| 770 | if (vertical) { |
| 771 | const Rect2 split_bar_rect = Rect2(rtl ? drag_area_margin_end : drag_area_margin_begin, dragger_positions[i], new_size.width - drag_area_margin_begin - drag_area_margin_end, sep); |
| 772 | dragging_area_controls[i]->set_rect(Rect2(split_bar_rect.position.x, split_bar_rect.position.y - split_bar_offset + drag_area_offset, split_bar_rect.size.x, dragger_ctrl_size)); |
| 773 | dragging_area_controls[i]->split_bar_rect = Rect2(Vector2(0.0, int(split_bar_offset) - drag_area_offset), split_bar_rect.size); |
| 774 | } else { |
| 775 | const Rect2 split_bar_rect = Rect2(dragger_positions[i], drag_area_margin_begin, sep, new_size.height - drag_area_margin_begin - drag_area_margin_end); |
| 776 | dragging_area_controls[i]->set_rect(Rect2(split_bar_rect.position.x - split_bar_offset + drag_area_offset * (rtl ? -1 : 1), split_bar_rect.position.y, dragger_ctrl_size, split_bar_rect.size.y)); |
| 777 | dragging_area_controls[i]->split_bar_rect = Rect2(Vector2(int(split_bar_offset) - drag_area_offset * (rtl ? -1 : 1), 0.0), split_bar_rect.size); |
| 778 | } |
| 779 | dragging_area_controls[i]->queue_redraw(); |
| 780 | } |
| 781 | queue_redraw(); |
| 782 | } |
| 783 | |
| 784 | void SplitContainer::_update_draggers() { |
| 785 | if (!is_visible_in_tree()) { |
| 786 | return; |
| 787 | } |
| 788 | const int valid_child_count = (int)valid_children.size(); |
| 789 | const int dragger_count = MAX(valid_child_count - 1, 1); |
| 790 | const int draggers_size_diff = dragger_count - (int)dragging_area_controls.size(); |
| 791 | |
| 792 | // Add new draggers. |
| 793 | for (int i = 0; i < draggers_size_diff; i++) { |
| 794 | SplitContainerDragger *dragger = memnew(SplitContainerDragger); |
| 795 | dragging_area_controls.push_back(dragger); |
| 796 | add_child(dragger, false, Node::INTERNAL_MODE_BACK); |
| 797 | if (touch_dragger_enabled) { |
| 798 | dragger->set_touch_dragger_enabled(true); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | // Remove extra draggers. |
| 803 | for (int i = 0; i < -draggers_size_diff; i++) { |
| 804 | const int remove_at = (int)dragging_area_controls.size() - 1; |
| 805 | SplitContainerDragger *dragger = dragging_area_controls[remove_at]; |
| 806 | dragging_area_controls.remove_at(remove_at); |
| 807 | // replace_by removes all children, so make sure it is a child before removing. |
| 808 | if (dragger->get_parent() == this) { |
| 809 | remove_child(dragger); |
| 810 | } |
| 811 | memdelete(dragger); |
| 812 | } |
| 813 | |
| 814 | // Make sure draggers have the correct index. |
| 815 | for (int i = 0; i < (int)dragging_area_controls.size(); i++) { |
| 816 | dragging_area_controls[i]->dragger_index = i; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | Size2 SplitContainer::get_minimum_size() const { |
| 821 | const int sep = _get_separation(); |
| 822 | const int axis = vertical ? 1 : 0; |
| 823 | const int other_axis = vertical ? 0 : 1; |
| 824 | |
| 825 | Size2i minimum; |
| 826 | |
| 827 | if (valid_children.size() >= 2u) { |
| 828 | minimum[axis] += sep * ((int)valid_children.size() - 1); |
| 829 | } |
| 830 | |
| 831 | for (const Control *child : valid_children) { |
| 832 | const Size2 min_size = child->get_combined_minimum_size(); |
| 833 | minimum[axis] += (int)min_size[axis]; |
| 834 | minimum[other_axis] = (int)MAX(minimum[other_axis], min_size[other_axis]); |
| 835 | } |
| 836 | |
| 837 | return minimum; |
| 838 | } |
| 839 | |
| 840 | void SplitContainer::_validate_property(PropertyInfo &p_property) const { |
| 841 | if (is_fixed && p_property.name == "vertical") { |
| 842 | p_property.usage = PROPERTY_USAGE_NONE; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | void SplitContainer::_notification(int p_what) { |
| 847 | switch (p_what) { |
| 848 | case NOTIFICATION_TRANSLATION_CHANGED: |
| 849 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { |
| 850 | queue_sort(); |
| 851 | } break; |
| 852 | case NOTIFICATION_POSTINITIALIZE: { |
| 853 | initialized = true; |
| 854 | } break; |
| 855 | case NOTIFICATION_SORT_CHILDREN: { |
| 856 | _resort(); |
| 857 | } break; |
| 858 | case NOTIFICATION_THEME_CHANGED: { |
| 859 | update_minimum_size(); |
| 860 | } break; |
| 861 | case NOTIFICATION_PREDELETE: { |
| 862 | valid_children.clear(); |
| 863 | dragging_area_controls.clear(); |
| 864 | } break; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | void SplitContainer::add_child_notify(Node *p_child) { |
| 869 | Container::add_child_notify(p_child); |
| 870 | |
| 871 | if (p_child->is_internal()) { |
| 872 | return; |
| 873 | } |
| 874 | Control *child = as_sortable_control(p_child, SortableVisibilityMode::IGNORE); |
| 875 | if (!child) { |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | child->connect(SceneStringName(visibility_changed), callable_mp(this, &SplitContainer::_on_child_visibility_changed).bind(child)); |
| 880 | if (child->is_visible()) { |
| 881 | _add_valid_child(child); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | void SplitContainer::remove_child_notify(Node *p_child) { |
| 886 | Container::remove_child_notify(p_child); |
| 887 | |
| 888 | if (p_child->is_internal()) { |
| 889 | return; |
| 890 | } |
| 891 | Control *child = as_sortable_control(p_child, SortableVisibilityMode::IGNORE); |
| 892 | if (!child) { |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | child->disconnect(SceneStringName(visibility_changed), callable_mp(this, &SplitContainer::_on_child_visibility_changed)); |
| 897 | if (child->is_visible()) { |
| 898 | _remove_valid_child(child); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | void SplitContainer::move_child_notify(Node *p_child) { |
| 903 | Container::move_child_notify(p_child); |
| 904 | |
| 905 | Control *moved_child = as_sortable_control(p_child, SortableVisibilityMode::IGNORE); |
| 906 | const int prev_index = valid_children.find(moved_child); |
| 907 | if (prev_index == -1) { |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | PackedInt32Array desired_sizes; |
| 912 | if (initialized && !split_offset_pending && valid_children.size() > 2u && split_offsets.size() == (int)default_dragger_positions.size()) { |
| 913 | desired_sizes = _get_desired_sizes(); |
| 914 | } |
| 915 | |
| 916 | valid_children.remove_at(prev_index); |
| 917 | |
| 918 | // Get new index. |
| 919 | int index = 0; |
| 920 | for (int i = 0; i < get_child_count(false); i++) { |
| 921 | Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE); |
| 922 | if (!child) { |
| 923 | continue; |
| 924 | } |
| 925 | if (child == moved_child) { |
| 926 | break; |
| 927 | } |
| 928 | if (valid_children.has(child)) { |
| 929 | index++; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | valid_children.insert(index, moved_child); |
| 934 | |
| 935 | if (desired_sizes.is_empty()) { |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | const int prev_desired_size = desired_sizes[prev_index]; |
| 940 | desired_sizes.remove_at(prev_index); |
| 941 | desired_sizes.insert(index, prev_desired_size); |
| 942 | _set_desired_sizes(desired_sizes, index); |
| 943 | } |
| 944 | |
| 945 | void SplitContainer::_on_child_visibility_changed(Control *p_control) { |
| 946 | if (p_control->is_visible()) { |
| 947 | _add_valid_child(p_control); |
| 948 | } else { |
| 949 | _remove_valid_child(p_control); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | void SplitContainer::_add_valid_child(Control *p_control) { |
| 954 | if (valid_children.has(p_control)) { |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | // Get index to insert. |
| 959 | bool child_is_valid = false; |
| 960 | int index = 0; |
| 961 | for (int i = 0; i < get_child_count(false); i++) { |
| 962 | Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE); |
| 963 | if (!child) { |
| 964 | continue; |
| 965 | } |
| 966 | if (child == p_control) { |
| 967 | if (child->is_visible()) { |
| 968 | child_is_valid = true; |
| 969 | } |
| 970 | break; |
| 971 | } |
| 972 | if (valid_children.has(child)) { |
| 973 | index++; |
| 974 | } |
| 975 | } |
| 976 | if (!child_is_valid) { |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | PackedInt32Array desired_sizes; |
| 981 | if (initialized && can_use_desired_sizes && !split_offset_pending && valid_children.size() >= 2u && split_offsets.size() == (int)default_dragger_positions.size()) { |
| 982 | desired_sizes = _get_desired_sizes(); |
| 983 | } |
| 984 | |
| 985 | valid_children.insert(index, p_control); |
| 986 | if (!initialized) { |
| 987 | // If not initialized, the theme cache isn't ready yet so return early. |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | _update_default_dragger_positions(); |
| 992 | queue_sort(); |
| 993 | |
| 994 | if (valid_children.size() <= 2u) { |
| 995 | // Already have first dragger. |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | // Call deferred in case already adding or removing children. |
| 1000 | callable_mp(this, &SplitContainer::_update_draggers).call_deferred(); |
| 1001 | |
| 1002 | if (split_offset_pending && split_offsets.size() == (int)valid_children.size() - 1) { |
| 1003 | split_offset_pending = false; |
| 1004 | } |
| 1005 | if (desired_sizes.is_empty()) { |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | // Use the child's existing size as it's desired size. |
| 1010 | const int axis = vertical ? 1 : 0; |
| 1011 | desired_sizes.insert(index, (int)p_control->get_size()[axis]); |
| 1012 | _set_desired_sizes(desired_sizes, index); |
| 1013 | } |
| 1014 | |
| 1015 | void SplitContainer::_remove_valid_child(Control *p_control) { |
| 1016 | const int index = valid_children.find(p_control); |
| 1017 | if (index == -1) { |
| 1018 | return; |
| 1019 | } |
| 1020 | |
| 1021 | PackedInt32Array desired_sizes; |
| 1022 | if (initialized && !split_offset_pending && valid_children.size() > 2u && split_offsets.size() == (int)default_dragger_positions.size()) { |
| 1023 | desired_sizes = _get_desired_sizes(); |
| 1024 | } |
| 1025 | |
| 1026 | valid_children.remove_at(index); |
| 1027 | if (!initialized) { |
| 1028 | return; |
| 1029 | } |
| 1030 | // Only use desired sizes to change the split offset after the first time a child is removed. |
| 1031 | // This allows adding children to not affect the split offsets when creating. |
| 1032 | can_use_desired_sizes = true; |
| 1033 | |
| 1034 | _update_default_dragger_positions(); |
| 1035 | queue_sort(); |
| 1036 | |
| 1037 | if (valid_children.size() <= 1u) { |
| 1038 | // Don't remove last dragger. |
| 1039 | return; |
| 1040 | } |
| 1041 | |
| 1042 | // Call deferred in case already adding or removing children. |
| 1043 | callable_mp(this, &SplitContainer::_update_draggers).call_deferred(); |
| 1044 | |
| 1045 | if (split_offset_pending && split_offsets.size() == (int)valid_children.size() - 2) { |
| 1046 | split_offset_pending = false; |
| 1047 | } |
| 1048 | if (desired_sizes.is_empty()) { |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | desired_sizes.remove_at(index); |
| 1053 | _set_desired_sizes(desired_sizes); |
| 1054 | } |
| 1055 | |
| 1056 | void SplitContainer::set_split_offset(int p_offset, int p_index) { |
| 1057 | ERR_FAIL_INDEX(p_index, split_offsets.size()); |
| 1058 | if (split_offsets[p_index] == p_offset) { |
| 1059 | return; |
| 1060 | } |
| 1061 | |
| 1062 | split_offsets.write[p_index] = p_offset; |
| 1063 | queue_sort(); |
| 1064 | } |
| 1065 | |
| 1066 | int SplitContainer::get_split_offset(int p_index) const { |
| 1067 | ERR_FAIL_INDEX_V(p_index, split_offsets.size(), 0); |
| 1068 | return split_offsets[p_index]; |
| 1069 | } |
| 1070 | |
| 1071 | void SplitContainer::set_split_offsets(const PackedInt32Array &p_offsets) { |
| 1072 | if (split_offsets == p_offsets) { |
| 1073 | return; |
| 1074 | } |
| 1075 | split_offsets = p_offsets; |
| 1076 | split_offset_pending = split_offsets.size() > 1 && (int)valid_children.size() - 1 != split_offsets.size(); |
| 1077 | queue_sort(); |
| 1078 | } |
| 1079 | |
| 1080 | PackedInt32Array SplitContainer::get_split_offsets() const { |
| 1081 | return split_offsets; |
| 1082 | } |
| 1083 | |
| 1084 | void SplitContainer::clamp_split_offset(int p_priority_index) { |
| 1085 | ERR_FAIL_INDEX(p_priority_index, split_offsets.size()); |
| 1086 | if (valid_children.size() < 2u) { |
| 1087 | // Needs at least two children. |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | _update_dragger_positions(p_priority_index); |
| 1092 | queue_sort(); |
| 1093 | } |
| 1094 | |
| 1095 | void SplitContainer::set_collapsed(bool p_collapsed) { |
| 1096 | if (collapsed == p_collapsed) { |
| 1097 | return; |
| 1098 | } |
| 1099 | collapsed = p_collapsed; |
| 1100 | queue_sort(); |
| 1101 | } |
| 1102 | |
| 1103 | void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) { |
| 1104 | if (dragger_visibility == p_visibility) { |
| 1105 | return; |
| 1106 | } |
| 1107 | dragger_visibility = p_visibility; |
| 1108 | queue_sort(); |
| 1109 | } |
| 1110 | |
| 1111 | SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const { |
| 1112 | return dragger_visibility; |
| 1113 | } |
| 1114 | |
| 1115 | bool SplitContainer::is_collapsed() const { |
| 1116 | return collapsed; |
| 1117 | } |
| 1118 | |
| 1119 | void SplitContainer::set_vertical(bool p_vertical) { |
| 1120 | ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + "."); |
| 1121 | if (vertical == p_vertical) { |
| 1122 | return; |
| 1123 | } |
| 1124 | vertical = p_vertical; |
| 1125 | for (SplitContainerDragger *dragger : dragging_area_controls) { |
| 1126 | dragger->update_touch_dragger(); |
| 1127 | } |
| 1128 | |
| 1129 | update_minimum_size(); |
| 1130 | _resort(); |
| 1131 | } |
| 1132 | |
| 1133 | bool SplitContainer::is_vertical() const { |
| 1134 | return vertical; |
| 1135 | } |
| 1136 | |
| 1137 | void SplitContainer::set_dragging_enabled(bool p_enabled) { |
| 1138 | if (dragging_enabled == p_enabled) { |
| 1139 | return; |
| 1140 | } |
| 1141 | dragging_enabled = p_enabled; |
| 1142 | if (!dragging_enabled) { |
| 1143 | bool was_dragging = false; |
| 1144 | for (SplitContainerDragger *dragger : dragging_area_controls) { |
| 1145 | was_dragging |= dragger->dragging; |
| 1146 | dragger->dragging = false; |
| 1147 | } |
| 1148 | if (was_dragging) { |
| 1149 | emit_signal(SNAME("drag_ended")); |
| 1150 | } |
| 1151 | } |
| 1152 | if (get_viewport()) { |
| 1153 | get_viewport()->update_mouse_cursor_state(); |
| 1154 | } |
| 1155 | _resort(); |
| 1156 | } |
| 1157 | |
| 1158 | bool SplitContainer::is_dragging_enabled() const { |
| 1159 | return dragging_enabled; |
| 1160 | } |
| 1161 | |
| 1162 | Vector<int> SplitContainer::get_allowed_size_flags_horizontal() const { |
| 1163 | Vector<int> flags; |
| 1164 | flags.append(SIZE_FILL); |
| 1165 | if (!vertical) { |
| 1166 | flags.append(SIZE_EXPAND); |
| 1167 | } |
| 1168 | flags.append(SIZE_SHRINK_BEGIN); |
| 1169 | flags.append(SIZE_SHRINK_CENTER); |
| 1170 | flags.append(SIZE_SHRINK_END); |
| 1171 | return flags; |
| 1172 | } |
| 1173 | |
| 1174 | Vector<int> SplitContainer::get_allowed_size_flags_vertical() const { |
| 1175 | Vector<int> flags; |
| 1176 | flags.append(SIZE_FILL); |
| 1177 | if (vertical) { |
| 1178 | flags.append(SIZE_EXPAND); |
| 1179 | } |
| 1180 | flags.append(SIZE_SHRINK_BEGIN); |
| 1181 | flags.append(SIZE_SHRINK_CENTER); |
| 1182 | flags.append(SIZE_SHRINK_END); |
| 1183 | return flags; |
| 1184 | } |
| 1185 | |
| 1186 | void SplitContainer::set_drag_area_margin_begin(int p_margin) { |
| 1187 | if (drag_area_margin_begin == p_margin) { |
| 1188 | return; |
| 1189 | } |
| 1190 | drag_area_margin_begin = p_margin; |
| 1191 | queue_sort(); |
| 1192 | } |
| 1193 | |
| 1194 | int SplitContainer::get_drag_area_margin_begin() const { |
| 1195 | return drag_area_margin_begin; |
| 1196 | } |
| 1197 | |
| 1198 | void SplitContainer::set_drag_area_margin_end(int p_margin) { |
| 1199 | if (drag_area_margin_end == p_margin) { |
| 1200 | return; |
| 1201 | } |
| 1202 | drag_area_margin_end = p_margin; |
| 1203 | queue_sort(); |
| 1204 | } |
| 1205 | |
| 1206 | int SplitContainer::get_drag_area_margin_end() const { |
| 1207 | return drag_area_margin_end; |
| 1208 | } |
| 1209 | |
| 1210 | void SplitContainer::set_drag_area_offset(int p_offset) { |
| 1211 | if (drag_area_offset == p_offset) { |
| 1212 | return; |
| 1213 | } |
| 1214 | drag_area_offset = p_offset; |
| 1215 | queue_sort(); |
| 1216 | } |
| 1217 | |
| 1218 | int SplitContainer::get_drag_area_offset() const { |
| 1219 | return drag_area_offset; |
| 1220 | } |
| 1221 | |
| 1222 | void SplitContainer::set_show_drag_area_enabled(bool p_enabled) { |
| 1223 | show_drag_area = p_enabled; |
| 1224 | for (SplitContainerDragger *dragger : dragging_area_controls) { |
| 1225 | dragger->queue_redraw(); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | bool SplitContainer::is_show_drag_area_enabled() const { |
| 1230 | return show_drag_area; |
| 1231 | } |
| 1232 | |
| 1233 | TypedArray<Control> SplitContainer::get_drag_area_controls() { |
| 1234 | TypedArray<Control> controls; |
| 1235 | controls.resize((int)dragging_area_controls.size()); |
| 1236 | for (int i = 0; i < (int)dragging_area_controls.size(); i++) { |
| 1237 | controls[i] = dragging_area_controls[i]; |
| 1238 | } |
| 1239 | return controls; |
| 1240 | } |
| 1241 | |
| 1242 | void SplitContainer::set_touch_dragger_enabled(bool p_enabled) { |
| 1243 | if (touch_dragger_enabled == p_enabled) { |
| 1244 | return; |
| 1245 | } |
| 1246 | touch_dragger_enabled = p_enabled; |
| 1247 | for (SplitContainerDragger *dragger : dragging_area_controls) { |
| 1248 | dragger->set_touch_dragger_enabled(p_enabled); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | bool SplitContainer::is_touch_dragger_enabled() const { |
| 1253 | return touch_dragger_enabled; |
| 1254 | } |
| 1255 | |
| 1256 | void SplitContainer::_bind_methods() { |
| 1257 | ClassDB::bind_method(D_METHOD("set_split_offsets", "offsets"), &SplitContainer::set_split_offsets); |
| 1258 | ClassDB::bind_method(D_METHOD("get_split_offsets"), &SplitContainer::get_split_offsets); |
| 1259 | |
| 1260 | ClassDB::bind_method(D_METHOD("clamp_split_offset", "priority_index"), &SplitContainer::clamp_split_offset, DEFVAL(0)); |
| 1261 | |
| 1262 | ClassDB::bind_method(D_METHOD("set_collapsed", "collapsed"), &SplitContainer::set_collapsed); |
| 1263 | ClassDB::bind_method(D_METHOD("is_collapsed"), &SplitContainer::is_collapsed); |
| 1264 | |
| 1265 | ClassDB::bind_method(D_METHOD("set_dragger_visibility", "mode"), &SplitContainer::set_dragger_visibility); |
| 1266 | ClassDB::bind_method(D_METHOD("get_dragger_visibility"), &SplitContainer::get_dragger_visibility); |
| 1267 | |
| 1268 | ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &SplitContainer::set_vertical); |
| 1269 | ClassDB::bind_method(D_METHOD("is_vertical"), &SplitContainer::is_vertical); |
| 1270 | |
| 1271 | ClassDB::bind_method(D_METHOD("set_dragging_enabled", "dragging_enabled"), &SplitContainer::set_dragging_enabled); |
| 1272 | ClassDB::bind_method(D_METHOD("is_dragging_enabled"), &SplitContainer::is_dragging_enabled); |
| 1273 | |
| 1274 | ClassDB::bind_method(D_METHOD("set_drag_area_margin_begin", "margin"), &SplitContainer::set_drag_area_margin_begin); |
| 1275 | ClassDB::bind_method(D_METHOD("get_drag_area_margin_begin"), &SplitContainer::get_drag_area_margin_begin); |
| 1276 | |
| 1277 | ClassDB::bind_method(D_METHOD("set_drag_area_margin_end", "margin"), &SplitContainer::set_drag_area_margin_end); |
| 1278 | ClassDB::bind_method(D_METHOD("get_drag_area_margin_end"), &SplitContainer::get_drag_area_margin_end); |
| 1279 | |
| 1280 | ClassDB::bind_method(D_METHOD("set_drag_area_offset", "offset"), &SplitContainer::set_drag_area_offset); |
| 1281 | ClassDB::bind_method(D_METHOD("get_drag_area_offset"), &SplitContainer::get_drag_area_offset); |
| 1282 | |
| 1283 | ClassDB::bind_method(D_METHOD("set_drag_area_highlight_in_editor", "drag_area_highlight_in_editor"), &SplitContainer::set_show_drag_area_enabled); |
| 1284 | ClassDB::bind_method(D_METHOD("is_drag_area_highlight_in_editor_enabled"), &SplitContainer::is_show_drag_area_enabled); |
| 1285 | |
| 1286 | ClassDB::bind_method(D_METHOD("get_drag_area_controls"), &SplitContainer::get_drag_area_controls); |
| 1287 | |
| 1288 | ClassDB::bind_method(D_METHOD("set_touch_dragger_enabled", "enabled"), &SplitContainer::set_touch_dragger_enabled); |
| 1289 | ClassDB::bind_method(D_METHOD("is_touch_dragger_enabled"), &SplitContainer::is_touch_dragger_enabled); |
| 1290 | |
| 1291 | ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::INT, "offset"))); |
| 1292 | ADD_SIGNAL(MethodInfo("drag_started")); |
| 1293 | ADD_SIGNAL(MethodInfo("drag_ended")); |
| 1294 | |
| 1295 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "split_offsets", PROPERTY_HINT_NONE, "suffix:px"), "set_split_offsets", "get_split_offsets"); |
| 1296 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed"); |
| 1297 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dragging_enabled"), "set_dragging_enabled", "is_dragging_enabled"); |
| 1298 | ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden and Collapsed"), "set_dragger_visibility", "get_dragger_visibility"); |
| 1299 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical"); |
| 1300 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "touch_dragger_enabled"), "set_touch_dragger_enabled", "is_touch_dragger_enabled"); |
| 1301 | |
| 1302 | ADD_GROUP("Drag Area", "drag_area_"); |
| 1303 | ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_margin_begin", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_margin_begin", "get_drag_area_margin_begin"); |
| 1304 | ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_margin_end", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_margin_end", "get_drag_area_margin_end"); |
| 1305 | ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_offset", "get_drag_area_offset"); |
| 1306 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_area_highlight_in_editor"), "set_drag_area_highlight_in_editor", "is_drag_area_highlight_in_editor_enabled"); |
| 1307 | |
| 1308 | BIND_ENUM_CONSTANT(DRAGGER_VISIBLE); |
| 1309 | BIND_ENUM_CONSTANT(DRAGGER_HIDDEN); |
| 1310 | BIND_ENUM_CONSTANT(DRAGGER_HIDDEN_COLLAPSED); |
| 1311 | |
| 1312 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, SplitContainer, touch_dragger_color); |
| 1313 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, SplitContainer, touch_dragger_pressed_color); |
| 1314 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, SplitContainer, touch_dragger_hover_color); |
| 1315 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, separation); |
| 1316 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, minimum_grab_thickness); |
| 1317 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, autohide); |
| 1318 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon, "touch_dragger"); |
| 1319 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon_h, "h_touch_dragger"); |
| 1320 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon_v, "v_touch_dragger"); |
| 1321 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon, "grabber"); |
| 1322 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon_h, "h_grabber"); |
| 1323 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon_v, "v_grabber"); |
| 1324 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SplitContainer, split_bar_background, "split_bar_background"); |
| 1325 | |
| 1326 | #ifndef DISABLE_DEPRECATED |
| 1327 | ClassDB::bind_method(D_METHOD("get_drag_area_control"), &SplitContainer::get_drag_area_control); |
| 1328 | ClassDB::bind_method(D_METHOD("set_split_offset", "offset"), &SplitContainer::_set_split_offset_first); |
| 1329 | ClassDB::bind_method(D_METHOD("get_split_offset"), &SplitContainer::_get_split_offset_first); |
| 1330 | ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_NO_EDITOR), "set_split_offset", "get_split_offset"); |
| 1331 | #endif // DISABLE_DEPRECATED |
| 1332 | } |
| 1333 | |
| 1334 | SplitContainer::SplitContainer(bool p_vertical) { |
| 1335 | vertical = p_vertical; |
| 1336 | split_offsets.push_back(0); |
| 1337 | |
| 1338 | SplitContainerDragger *dragger = memnew(SplitContainerDragger); |
| 1339 | dragging_area_controls.push_back(dragger); |
| 1340 | add_child(dragger, false, Node::INTERNAL_MODE_BACK); |
| 1341 | } |