diff options
Diffstat (limited to 'scene/gui/box_container.cpp')
-rw-r--r-- | scene/gui/box_container.cpp | 181 |
1 files changed, 84 insertions, 97 deletions
diff --git a/scene/gui/box_container.cpp b/scene/gui/box_container.cpp index 1f6e0392d8..7c1487c42f 100644 --- a/scene/gui/box_container.cpp +++ b/scene/gui/box_container.cpp @@ -27,8 +27,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "box_container.h" -#include "margin_container.h" #include "label.h" +#include "margin_container.h" struct _MinSizeCache { @@ -41,110 +41,105 @@ void BoxContainer::_resort() { /** First pass, determine minimum size AND amount of stretchable elements */ + Size2i new_size = get_size(); - Size2i new_size=get_size(); + int sep = get_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer"); - int sep=get_constant("separation");//,vertical?"VBoxContainer":"HBoxContainer"); + bool first = true; + int children_count = 0; + int stretch_min = 0; + int stretch_avail = 0; + float stretch_ratio_total = 0; + Map<Control *, _MinSizeCache> min_size_cache; - bool first=true; - int children_count=0; - int stretch_min=0; - int stretch_avail=0; - float stretch_ratio_total=0; - Map<Control*,_MinSizeCache> min_size_cache; - - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) continue; if (c->is_set_as_toplevel()) continue; - Size2i size=c->get_combined_minimum_size(); + Size2i size = c->get_combined_minimum_size(); _MinSizeCache msc; if (vertical) { /* VERTICAL */ - stretch_min+=size.height; - msc.min_size=size.height; - msc.will_stretch=c->get_v_size_flags() & SIZE_EXPAND; + stretch_min += size.height; + msc.min_size = size.height; + msc.will_stretch = c->get_v_size_flags() & SIZE_EXPAND; } else { /* HORIZONTAL */ - stretch_min+=size.width; - msc.min_size=size.width; - msc.will_stretch=c->get_h_size_flags() & SIZE_EXPAND; + stretch_min += size.width; + msc.min_size = size.width; + msc.will_stretch = c->get_h_size_flags() & SIZE_EXPAND; } if (msc.will_stretch) { - stretch_avail+=msc.min_size; - stretch_ratio_total+=c->get_stretch_ratio(); + stretch_avail += msc.min_size; + stretch_ratio_total += c->get_stretch_ratio(); } - msc.final_size=msc.min_size; - min_size_cache[c]=msc; + msc.final_size = msc.min_size; + min_size_cache[c] = msc; children_count++; } - if (children_count==0) + if (children_count == 0) return; - int stretch_max = (vertical? new_size.height : new_size.width ) - (children_count-1) * sep; + int stretch_max = (vertical ? new_size.height : new_size.width) - (children_count - 1) * sep; int stretch_diff = stretch_max - stretch_min; - if (stretch_diff<0) { + if (stretch_diff < 0) { //avoid negative stretch space - stretch_max=stretch_min; - stretch_diff=0; + stretch_max = stretch_min; + stretch_diff = 0; } - stretch_avail+=stretch_diff; //available stretch space. + stretch_avail += stretch_diff; //available stretch space. /** Second, pass sucessively to discard elements that can't be stretched, this will run while stretchable elements exist */ - bool has_stretched = false; - while(stretch_ratio_total>0) { // first of all, dont even be here if no stretchable objects exist + while (stretch_ratio_total > 0) { // first of all, dont even be here if no stretchable objects exist has_stretched = true; - bool refit_successful=true; //assume refit-test will go well + bool refit_successful = true; //assume refit-test will go well - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { - Control *c=get_child(i)->cast_to<Control>(); + Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) continue; if (c->is_set_as_toplevel()) continue; ERR_FAIL_COND(!min_size_cache.has(c)); - _MinSizeCache &msc=min_size_cache[c]; + _MinSizeCache &msc = min_size_cache[c]; if (msc.will_stretch) { //wants to stretch //let's see if it can really stretch - int final_pixel_size=stretch_avail * c->get_stretch_ratio() / stretch_ratio_total; - if (final_pixel_size<msc.min_size) { + int final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total; + if (final_pixel_size < msc.min_size) { //if available stretching area is too small for widget, //then remove it from stretching area - msc.will_stretch=false; - stretch_ratio_total-=c->get_stretch_ratio(); - refit_successful=false; - stretch_avail-=msc.min_size; - msc.final_size=msc.min_size; + msc.will_stretch = false; + stretch_ratio_total -= c->get_stretch_ratio(); + refit_successful = false; + stretch_avail -= msc.min_size; + msc.final_size = msc.min_size; break; } else { - msc.final_size=final_pixel_size; + msc.final_size = final_pixel_size; } } } if (refit_successful) //uf refit went well, break break; - } - /** Final pass, draw and stretch elements **/ - - int ofs=0; + int ofs = 0; if (!has_stretched) { switch (align) { case ALIGN_BEGIN: @@ -158,70 +153,64 @@ void BoxContainer::_resort() { } } - first=true; - int idx=0; + first = true; + int idx = 0; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { - Control *c=get_child(i)->cast_to<Control>(); + Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) continue; if (c->is_set_as_toplevel()) continue; - _MinSizeCache &msc=min_size_cache[c]; - + _MinSizeCache &msc = min_size_cache[c]; if (first) - first=false; + first = false; else - ofs+=sep; + ofs += sep; - int from=ofs; - int to=ofs+msc.final_size; + int from = ofs; + int to = ofs + msc.final_size; - - if (msc.will_stretch && idx==children_count-1) { + if (msc.will_stretch && idx == children_count - 1) { //adjust so the last one always fits perfect //compensating for numerical imprecision - to=vertical?new_size.height:new_size.width; - + to = vertical ? new_size.height : new_size.width; } - int size=to-from; + int size = to - from; Rect2 rect; if (vertical) { - rect=Rect2(0,from,new_size.width,size); + rect = Rect2(0, from, new_size.width, size); } else { - rect=Rect2(from,0,size,new_size.height); - + rect = Rect2(from, 0, size, new_size.height); } - fit_child_in_rect(c,rect); + fit_child_in_rect(c, rect); - ofs=to; + ofs = to; idx++; } - } Size2 BoxContainer::get_minimum_size() const { - /* Calculate MINIMUM SIZE */ Size2i minimum; - int sep=get_constant("separation");//,vertical?"VBoxContainer":"HBoxContainer"); + int sep = get_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer"); - bool first=true; + bool first = true; - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; if (c->is_set_as_toplevel()) @@ -231,27 +220,26 @@ Size2 BoxContainer::get_minimum_size() const { continue; } - Size2i size=c->get_combined_minimum_size(); + Size2i size = c->get_combined_minimum_size(); if (vertical) { /* VERTICAL */ - if ( size.width > minimum.width ) { - minimum.width=size.width; + if (size.width > minimum.width) { + minimum.width = size.width; } - minimum.height+=size.height+(first?0:sep); + minimum.height += size.height + (first ? 0 : sep); } else { /* HORIZONTAL */ - if ( size.height > minimum.height ) { - minimum.height=size.height; + if (size.height > minimum.height) { + minimum.height = size.height; } - minimum.width+=size.width+(first?0:sep); - + minimum.width += size.width + (first ? 0 : sep); } - first=false; + first = false; } return minimum; @@ -259,7 +247,7 @@ Size2 BoxContainer::get_minimum_size() const { void BoxContainer::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_SORT_CHILDREN: { @@ -279,7 +267,7 @@ BoxContainer::AlignMode BoxContainer::get_alignment() const { void BoxContainer::add_spacer(bool p_begin) { - Control *c = memnew( Control ); + Control *c = memnew(Control); c->set_mouse_filter(MOUSE_FILTER_PASS); //allow spacer to pass mouse events if (vertical) @@ -289,12 +277,12 @@ void BoxContainer::add_spacer(bool p_begin) { add_child(c); if (p_begin) - move_child(c,0); + move_child(c, 0); } BoxContainer::BoxContainer(bool p_vertical) { - vertical=p_vertical; + vertical = p_vertical; align = ALIGN_BEGIN; //set_ignore_mouse(true); set_mouse_filter(MOUSE_FILTER_PASS); @@ -302,24 +290,23 @@ BoxContainer::BoxContainer(bool p_vertical) { void BoxContainer::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_spacer","begin"),&BoxContainer::add_spacer); - ClassDB::bind_method(D_METHOD("get_alignment"),&BoxContainer::get_alignment); - ClassDB::bind_method(D_METHOD("set_alignment","alignment"),&BoxContainer::set_alignment); - - BIND_CONSTANT( ALIGN_BEGIN ); - BIND_CONSTANT( ALIGN_CENTER ); - BIND_CONSTANT( ALIGN_END ); + ClassDB::bind_method(D_METHOD("add_spacer", "begin"), &BoxContainer::add_spacer); + ClassDB::bind_method(D_METHOD("get_alignment"), &BoxContainer::get_alignment); + ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &BoxContainer::set_alignment); - ADD_PROPERTY( PropertyInfo(Variant::INT,"alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment","get_alignment") ; + BIND_CONSTANT(ALIGN_BEGIN); + BIND_CONSTANT(ALIGN_CENTER); + BIND_CONSTANT(ALIGN_END); + ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment", "get_alignment"); } -MarginContainer* VBoxContainer::add_margin_child(const String& p_label,Control *p_control,bool p_expand) { +MarginContainer *VBoxContainer::add_margin_child(const String &p_label, Control *p_control, bool p_expand) { - Label *l = memnew( Label ); + Label *l = memnew(Label); l->set_text(p_label); add_child(l); - MarginContainer *mc = memnew( MarginContainer ); + MarginContainer *mc = memnew(MarginContainer); mc->add_child(p_control); add_child(mc); if (p_expand) |