Fixes a bas resizing with SplitContainer

This commit is contained in:
Gilles Roudiere 2018-01-20 21:03:20 +01:00
parent d1f32708b7
commit 30e1e42175
2 changed files with 44 additions and 102 deletions

View File

@ -61,127 +61,69 @@ Control *SplitContainer::_getch(int p_idx) const {
} }
void SplitContainer::_resort() { void SplitContainer::_resort() {
/* First pass, determine minimum size AND amount of stretchable elements */
int axis = vertical ? 1 : 0; int axis = vertical ? 1 : 0;
bool has_first = _getch(0);
bool has_second = _getch(1);
if (!has_first && !has_second) {
return;
} else if (!(has_first && has_second)) {
if (has_first)
fit_child_in_rect(_getch(0), Rect2(Point2(), get_size()));
else
fit_child_in_rect(_getch(1), Rect2(Point2(), get_size()));
return;
}
Control *first = _getch(0); Control *first = _getch(0);
Control *second = _getch(1); Control *second = _getch(1);
bool ratiomode = false; // If we have only one element
bool expand_first_mode = false; if (!first || !second) {
if (first) {
if (vertical) { fit_child_in_rect(_getch(0), Rect2(Point2(), get_size()));
} else if (second) {
ratiomode = first->get_v_size_flags() & SIZE_EXPAND && second->get_v_size_flags() & SIZE_EXPAND; fit_child_in_rect(_getch(1), Rect2(Point2(), get_size()));
expand_first_mode = first->get_v_size_flags() & SIZE_EXPAND && !(second->get_v_size_flags() & SIZE_EXPAND); }
} else { return;
ratiomode = first->get_h_size_flags() & SIZE_EXPAND && second->get_h_size_flags() & SIZE_EXPAND;
expand_first_mode = first->get_h_size_flags() & SIZE_EXPAND && !(second->get_h_size_flags() & SIZE_EXPAND);
} }
int sep = get_constant("separation"); // Determine expanded children
Ref<Texture> g = get_icon("grabber"); bool first_expanded = false;
bool second_expanded = false;
if (vertical) {
first_expanded = first->get_v_size_flags() & SIZE_EXPAND;
second_expanded = second->get_v_size_flags() & SIZE_EXPAND;
} else {
first_expanded = first->get_h_size_flags() & SIZE_EXPAND;
second_expanded = second->get_h_size_flags() & SIZE_EXPAND;
}
// Determine the separation between items
Ref<Texture> g = get_icon("grabber");
int sep = get_constant("separation");
if (dragger_visibility == DRAGGER_HIDDEN_COLLAPSED) { if (dragger_visibility == DRAGGER_HIDDEN_COLLAPSED) {
sep = 0; sep = 0;
} else { } else {
sep = MAX(sep, vertical ? g->get_height() : g->get_width()); sep = MAX(sep, vertical ? g->get_height() : g->get_width());
} }
int total = vertical ? get_size().height : get_size().width; // Compute the minimum size
total -= sep;
int minimum = 0;
Size2 ms_first = first->get_combined_minimum_size(); Size2 ms_first = first->get_combined_minimum_size();
Size2 ms_second = second->get_combined_minimum_size(); Size2 ms_second = second->get_combined_minimum_size();
if (vertical) { float ratio = first->get_stretch_ratio() / (first->get_stretch_ratio() + second->get_stretch_ratio());
minimum = ms_first.height + ms_second.height;
int no_offset_middle_sep = 0;
if (first_expanded && second_expanded) {
no_offset_middle_sep = get_size()[axis] * ratio - sep / 2;
} else if (first_expanded) {
no_offset_middle_sep = get_size()[axis] - ms_second[axis] - sep;
} else { } else {
minimum = ms_first.width + ms_second.width; no_offset_middle_sep = ms_first[axis];
} }
int available = total - minimum; middle_sep = no_offset_middle_sep;
if (available < 0) middle_sep += (collapsed) ? 0 : split_offset;
available = 0; middle_sep = MIN(middle_sep, get_size()[axis] - ms_second[axis] - sep);
middle_sep = MAX(middle_sep, ms_first[axis]);
middle_sep = 0; if (!collapsed) {
split_offset = middle_sep - no_offset_middle_sep;
if (collapsed) {
if (ratiomode) {
int first_ratio = first->get_stretch_ratio();
int second_ratio = second->get_stretch_ratio();
float ratio = float(first_ratio) / (first_ratio + second_ratio);
middle_sep = ms_first[axis] + available * ratio;
} else if (expand_first_mode) {
middle_sep = get_size()[axis] - ms_second[axis] - sep;
} else {
middle_sep = ms_first[axis];
}
} else if (ratiomode) {
int first_ratio = first->get_stretch_ratio();
int second_ratio = second->get_stretch_ratio();
float ratio = float(first_ratio) / (first_ratio + second_ratio);
if (expand_ofs < -(available * ratio))
expand_ofs = -(available * ratio);
else if (expand_ofs > (available * (1.0 - ratio)))
expand_ofs = (available * (1.0 - ratio));
middle_sep = ms_first[axis] + available * ratio + expand_ofs;
} else if (expand_first_mode) {
if (expand_ofs > 0)
expand_ofs = 0;
else if (expand_ofs < -available)
expand_ofs = -available;
middle_sep = get_size()[axis] - ms_second[axis] - sep + expand_ofs;
} else {
if (expand_ofs < 0)
expand_ofs = 0;
else if (expand_ofs > available)
expand_ofs = available;
middle_sep = ms_first[axis] + expand_ofs;
} }
if (vertical) { if (vertical) {
fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(get_size().width, middle_sep))); fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(get_size().width, middle_sep)));
int sofs = middle_sep + sep; int sofs = middle_sep + sep;
fit_child_in_rect(second, Rect2(Point2(0, sofs), Size2(get_size().width, get_size().height - sofs))); fit_child_in_rect(second, Rect2(Point2(0, sofs), Size2(get_size().width, get_size().height - sofs)));
} else { } else {
fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height))); fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height)));
int sofs = middle_sep + sep; int sofs = middle_sep + sep;
fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height))); fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height)));
@ -290,7 +232,7 @@ void SplitContainer::_gui_input(const Ref<InputEvent> &p_event) {
dragging = true; dragging = true;
drag_from = mb->get_position().y; drag_from = mb->get_position().y;
drag_ofs = expand_ofs; drag_ofs = split_offset;
} }
} else { } else {
@ -298,7 +240,7 @@ void SplitContainer::_gui_input(const Ref<InputEvent> &p_event) {
dragging = true; dragging = true;
drag_from = mb->get_position().x; drag_from = mb->get_position().x;
drag_ofs = expand_ofs; drag_ofs = split_offset;
} }
} }
} else { } else {
@ -312,7 +254,7 @@ void SplitContainer::_gui_input(const Ref<InputEvent> &p_event) {
if (mm.is_valid() && dragging) { if (mm.is_valid() && dragging) {
expand_ofs = drag_ofs + ((vertical ? mm->get_position().y : mm->get_position().x) - drag_from); split_offset = drag_ofs + ((vertical ? mm->get_position().y : mm->get_position().x) - drag_from);
queue_sort(); queue_sort();
emit_signal("dragged", get_split_offset()); emit_signal("dragged", get_split_offset());
} }
@ -343,16 +285,16 @@ Control::CursorShape SplitContainer::get_cursor_shape(const Point2 &p_pos) const
void SplitContainer::set_split_offset(int p_offset) { void SplitContainer::set_split_offset(int p_offset) {
if (expand_ofs == p_offset) if (split_offset == p_offset)
return; return;
expand_ofs = p_offset; split_offset = p_offset;
queue_sort(); queue_sort();
} }
int SplitContainer::get_split_offset() const { int SplitContainer::get_split_offset() const {
return expand_ofs; return split_offset;
} }
void SplitContainer::set_collapsed(bool p_collapsed) { void SplitContainer::set_collapsed(bool p_collapsed) {
@ -407,7 +349,7 @@ void SplitContainer::_bind_methods() {
SplitContainer::SplitContainer(bool p_vertical) { SplitContainer::SplitContainer(bool p_vertical) {
mouse_inside = false; mouse_inside = false;
expand_ofs = 0; split_offset = 0;
middle_sep = 0; middle_sep = 0;
vertical = p_vertical; vertical = p_vertical;
dragging = false; dragging = false;

View File

@ -46,7 +46,7 @@ public:
private: private:
bool vertical; bool vertical;
int expand_ofs; int split_offset;
int middle_sep; int middle_sep;
bool dragging; bool dragging;
int drag_from; int drag_from;