void Box::AllocateChildren() const { ChildrenCont::const_iterator iter( m_children.begin() ); ChildrenCont::const_iterator iterend( m_children.end() ); unsigned int num_expand( 0 ); unsigned int num_visible( 0 ); // Count number of visible and expanded children. for( ; iter != iterend; ++iter ) { if( !IsChildInteresting( iter->widget ) ) { continue; } ++num_visible; if( iter->expand ) { ++num_expand; } } // Calculate extra width pre expanded widget. float extra( 0.f ); if( num_expand > 0 ) { if( m_orientation == HORIZONTAL ) { extra = std::max( 0.f, GetAllocation().width - GetRequisition().x ) / static_cast<float>( num_expand ); } else { extra = std::max( 0.f, GetAllocation().height - GetRequisition().y ) / static_cast<float>( num_expand ); } } // Allocate children. float gap( Context::Get().GetEngine().GetProperty<float>( "Gap", shared_from_this() ) ); sf::Vector2f allocation( 0.f, 0.f ); sf::Vector2f position( gap, gap ); for( iter = m_children.begin(); iter != iterend; ++iter ) { if( !IsChildInteresting( iter->widget ) ) { continue; } if( m_orientation == HORIZONTAL ) { allocation.x = iter->widget->GetRequisition().x + (iter->expand ? extra : 0.f); allocation.y = GetAllocation().height - 2 * gap; iter->widget->SetAllocation( sf::FloatRect( position.x, position.y, allocation.x - (iter->expand && !iter->fill ? extra : 0.f), allocation.y ) ); position.x += allocation.x + GetSpacing(); } else { allocation.x = GetAllocation().width - 2 * gap; allocation.y = iter->widget->GetRequisition().y + (iter->expand ? extra : 0.f); iter->widget->SetAllocation( sf::FloatRect( position.x, position.y, allocation.x, allocation.y - (iter->expand && !iter->fill ? extra : 0.f) ) ); position.y += allocation.y + GetSpacing(); } --num_visible; } }
void Alignment::UpdateChild() { auto child = GetChild(); if( !child ) { return; } sf::FloatRect allocation( GetAllocation() ); sf::Vector2f spare_space( allocation.width, allocation.height ); spare_space -= child->GetRequisition(); spare_space.x *= 1.f - GetScale().x; spare_space.y *= 1.f - GetScale().y; if( ( spare_space.x < 0 ) || ( spare_space.y < 0 ) ) { #if defined( SFGUI_DEBUG ) std::cerr << "SFGUI warning: Alignment got a smaller allocation than it requested.\n"; return; #endif } allocation.left = spare_space.x * GetAlignment().x; allocation.top = spare_space.y * GetAlignment().y; allocation.width -= spare_space.x; allocation.height -= spare_space.y; child->SetAllocation( allocation ); }
sf::Vector2f Alignment::CalculateRequisition() { auto child = GetChild(); if( !child ) { return sf::Vector2f( 0.f, 0.f ); } return child->GetRequisition(); }
void Window::HandleMouseMoveEvent( int x, int y ) { if( m_dragging ) { SetPosition( sf::Vector2f( static_cast<float>( x ) - m_drag_offset.x, static_cast<float>( y ) - m_drag_offset.y ) ); } else if( m_resizing ) { AllocateSize( sf::FloatRect( GetAllocation().Left, GetAllocation().Top, std::max( GetRequisition().x, static_cast<float>( x ) + m_drag_offset.x - GetAllocation().Left ), std::max( GetRequisition().y, static_cast<float>( y ) + m_drag_offset.y - GetAllocation().Top ) ) ); } }
void Window::HandleMouseMoveEvent( int x, int y ) { if( ( x == std::numeric_limits<int>::min() ) || ( y == std::numeric_limits<int>::min() ) ) { return; } if( m_dragging ) { SetPosition( sf::Vector2f( static_cast<float>( x ) - m_drag_offset.x, static_cast<float>( y ) - m_drag_offset.y ) ); } else if( m_resizing && (GetStyle() & RESIZE) == RESIZE ) { SetAllocation( sf::FloatRect( GetAllocation().left, GetAllocation().top, std::max( GetRequisition().x, static_cast<float>( x ) + m_drag_offset.x - GetAllocation().left ), std::max( GetRequisition().y, static_cast<float>( y ) + m_drag_offset.y - GetAllocation().top ) ) ); } }
void Widget::RequestSize() const { m_recalc_requisition = true; Container::Ptr parent = m_parent.lock(); // Notify observers. OnSizeRequest(); if( parent ) { parent->RequestSize(); } else { sf::Vector2f requisition( GetRequisition() ); sf::FloatRect allocation( GetAllocation().Left, GetAllocation().Top, std::max( GetAllocation().Width, requisition.x ), std::max( GetAllocation().Height, requisition.y ) ); AllocateSize( allocation ); } }
void Button::AllocateChild() { auto child = GetChild(); if( !child ) { return; } float padding( Context::Get().GetEngine().GetProperty<float>( "Padding", shared_from_this() ) ); float border_width( Context::Get().GetEngine().GetProperty<float>( "BorderWidth", shared_from_this() ) ); sf::FloatRect allocation( GetAllocation() ); allocation.left = padding + border_width; allocation.top = padding + border_width; allocation.width = child->GetRequisition().x; allocation.height -= border_width * 2.f + padding * 2.f; if( GetState() == State::ACTIVE ) { allocation.left += border_width; allocation.top += border_width; } child->SetAllocation( allocation ); }
int main() { sfg::SFGUI sfgui; sf::RenderWindow window(sf::VideoMode(640, 480), "ListBox Example"); window.setVerticalSyncEnabled(true); window.setFramerateLimit(30); sfg::Desktop desktop; auto sfg_window = sfg::Window::Create(); sfg_window->SetTitle( "ListBoxExample" ); auto box_outer = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL, 15.0f ); auto box_inner1 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.0f ); auto box_inner2 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.0f ); auto label1 = sfg::Label::Create("I'm single-select list."); auto label2 = sfg::Label::Create("I'm multi-select list."); auto input1 = sfg::Entry::Create(""); auto input2 = sfg::Entry::Create(""); auto list1 = sfg::ListBox::Create(); list1->AppendItem( "Item1" ); list1->AppendItem( "Item2" ); list1->AppendItem( "Item3" ); list1->GetSignal( sfg::ListBox::OnSelect ).Connect( std::bind( [list1, input1](){ if(list1->GetSelectedItemsCount()) input1->SetText(list1->GetSelectedItemText()); else input1->SetText(""); } ) ); auto list2 = sfg::ListBox::Create(); list2->AppendItem( "Item1" ); list2->AppendItem( "Item2" ); list2->AppendItem( "Item3" ); list2->multiselect = true; list2->GetSignal( sfg::ListBox::OnSelect ).Connect( std::bind( [list2, input2](){ std::string str = ""; for(unsigned i=0; i<list2->GetSelectedItemsCount(); i++) { str += list2->GetSelectedItemText(i); str += ' '; } input2->SetText(str); } ) ); box_outer->Pack(box_inner1); box_outer->Pack(box_inner2); box_inner1->Pack(label1); box_inner1->Pack(list1); box_inner1->Pack(input1); box_inner2->Pack(label2); box_inner2->Pack(list2); box_inner2->Pack(input2); sfg_window->Add( box_outer ); desktop.Add( sfg_window ); sfg_window->SetPosition(sf::Vector2f(window.getSize().x/2-sfg_window->GetRequisition().x/2, window.getSize().y/2-sfg_window->GetRequisition().y/2)); sf::Event event; sf::Clock clock; window.resetGLStates(); while (window.isOpen()) { while (window.pollEvent(event)) { desktop.HandleEvent( event ); switch(event.type) { case sf::Event::Closed: window.close(); break; } } desktop.Update( clock.restart().asSeconds() ); window.clear(); sfgui.Display( window ); window.display(); } return 0; }