void GuessMyNumber::OnGuessClick() { // Validate number. unsigned int buf_number( 0 ); std::stringstream sstr( static_cast<std::string>( m_number_entry->GetText() ) ); sstr >> buf_number; if( buf_number < 1 || buf_number > 100 ) { m_hint_label->SetText( "Enter a number from 1 to 100." ); return; } ++m_tries; UpdateUI(); unsigned char number( static_cast<unsigned char>( buf_number ) ); if( number < m_number ) { m_hint_label->SetText( "My number is higher." ); } else if( number > m_number ) { m_hint_label->SetText( "My number is lower." ); } else { m_hint_label->SetText( "Correct!" ); m_guess_button->Show( false ); } m_number_entry->SetText( "" ); }
void GuessMyNumber::ResetGame() { m_tries = 0; m_number = static_cast<unsigned char>( std::rand() % 100 + 1 ); m_hint_label->SetText( "-" ); UpdateUI(); m_guess_button->Show( true ); }
void GuessMyNumber::UpdateUI() { std::stringstream sstr; sstr << m_tries; m_tries_label->SetText( sstr.str() ); }
int main() { // Create the main SFML window sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "SFGUI Combo Box Example", sf::Style::Titlebar | sf::Style::Close ); // Construct our SFML guard // See http://sfgui.sfml-dev.de/forum/topic52-crash-on-close.html for more info. sfg::SFGUI sfgui; // Create our main SFGUI window sfg::Window::Ptr window; window = sfg::Window::Create(); window->SetTitle( "Title" ); // Create the combo box itself. combo_box = sfg::ComboBox::Create(); // Set the entries of the combo box. combo_box->AppendItem( "Bar" ); combo_box->PrependItem( "Foo" ); sel_label = sfg::Label::Create( L"Please select an item!" ); sfg::Button::Ptr button( sfg::Button::Create( L"Add item" ) ); sfg::Box::Ptr hbox( sfg::Box::Create( sfg::Box::HORIZONTAL, 5 ) ); hbox->Pack( combo_box ); hbox->Pack( button, false ); sfg::Box::Ptr vbox( sfg::Box::Create( sfg::Box::VERTICAL, 5 ) ); vbox->Pack( hbox, false ); vbox->Pack( sel_label, true ); // Add the combo box to the window window->Add( vbox ); // So that our combo box has a meaningful purpose (besides just looking // awesome :P) we need to tell it to connect to a callback of our choosing to // notify us when it is clicked. combo_box->OnSelect.Connect( &OnComboSelect ); button->OnClick.Connect( &OnAddItemClick ); // If attempting to connect to a class method you need to provide // a pointer to it as the second parameter after the function address. // Start the game loop while ( app_window.isOpen() ) { // Process events sf::Event event; while ( app_window.pollEvent( event ) ) { // Handle events window->HandleEvent( event ); // Close window : exit if ( event.type == sf::Event::Closed ) { app_window.close(); } } // Update the GUI, note that you shouldn't normally // pass 0 seconds to the update method. window->Update( 0.f ); // Clear screen app_window.clear(); // Draw the GUI sfg::Renderer::Get().Display( app_window ); // Update the window app_window.display(); } // If you have any global or static widgets, // you need to reset their pointers before your // application exits. combo_box.reset(); sel_label.reset(); return EXIT_SUCCESS; }
void OnComboSelect() { std::stringstream sstr; sstr << "Item " << combo_box->GetSelectedItem() << " selected with text \"" << static_cast<std::string>( combo_box->GetSelectedText() ) << "\""; sel_label->SetText( sstr.str() ); }
void EntryExample::ButtonClick() { // When the button is clicked set the contents of the label // to the contents of the entry widget. m_label->SetText( m_entry->GetText() ); }
void EntryExample::Run() { // Create the main SFML window sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "SFGUI Entry Example", sf::Style::Titlebar | sf::Style::Close ); // We have to do this because we don't use SFML to draw. app_window.resetGLStates(); // Create our main SFGUI window auto window = sfg::Window::Create(); window->SetTitle( "Title" ); // Create our box. auto box = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL ); // Create a button. auto button = sfg::Button::Create(); button->SetLabel( "Set" ); // Connect the button. button->GetSignal( sfg::Widget::OnLeftClick ).Connect( std::bind( &EntryExample::ButtonClick, this ) ); // Create a label. m_label = sfg::Label::Create(); m_label->SetText( "no text yet" ); // Create our entry widget itself. m_entry = sfg::Entry::Create(); // Until now all widgets only expanded to fit the text inside of it. // This is not the case with the entry widget which can be empty // but still has to have a reasonable size. // To disable the automatic sizing of widgets in general you can // use the SetRequisition() method. it takes an sf::Vector as it's // parameter. Depending on which side you want to have a minimum size, // you set the corresponding value in the vector. // Here we chose to set the minimum x size of the widget to 80. m_entry->SetRequisition( sf::Vector2f( 80.f, 0.f ) ); // Setting sizing back to automatic is as easy as setting // x and y sizes to 0. // Pack into box box->Pack( m_entry ); box->Pack( button ); box->Pack( m_label ); // Set box spacing box->SetSpacing( 5.f ); // Add our box to the window window->Add( box ); sf::Clock clock; // Update an initial time to construct the GUI before drawing begins. // This makes sure that there are no frames in which no GUI is visible. window->Update( 0.f ); // Start the game loop while ( app_window.isOpen() ) { // Process events sf::Event event; while ( app_window.pollEvent( event ) ) { // Handle events window->HandleEvent( event ); // Close window : exit if ( event.type == sf::Event::Closed ) { app_window.close(); } } // Update the GUI every 5ms if( clock.getElapsedTime().asMicroseconds() >= 5000 ) { // Update() takes the elapsed time in seconds. window->Update( static_cast<float>( clock.getElapsedTime().asMicroseconds() ) / 1000000.f ); clock.restart(); } // Clear screen app_window.clear(); // Draw the GUI m_sfgui.Display( app_window ); // Update the window app_window.display(); } }
int main() { // Create the main SFML window sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "SFGUI Entry Example" ); // Create our main SFGUI window sfg::Window::Ptr window; window = sfg::Window::Create(); window->SetTitle( "Title" ); // Create our box. sfg::Box::Ptr box = sfg::Box::Create( sfg::Box::Horizontal ); // Create a button. sfg::Button::Ptr button = sfg::Button::Create(); button->SetLabel( "Set" ); // Connect the button. button->OnClick.Connect( &ButtonClick ); // Create a label. label = sfg::Label::Create(); label->SetText( "no text yet" ); // Create our entry widget itself. entry = sfg::Entry::Create(); // Until now all widgets only expanded to fit the text inside of it. // This is not the case with the entry widget which can be empty // but still has to have a reasonable size. // To disable the automatic sizing of widgets in general you can // use the SetRequisition() method. it takes an sf::Vector as it's // parameter. Depending on which side you want to have a minimum size, // you set the corresponding value in the vector. // Here we chose to set the minimum x size of the widget to 80. entry->SetRequisition( sf::Vector2f( 80.f, 0.f ) ); // Setting sizing back to automatic is as easy as setting // x and y sizes to 0. // Pack into box box->Pack( entry ); box->Pack( button ); box->Pack( label ); // Set box spacing box->SetSpacing( 5.f ); // Add our box to the window window->Add( box ); window->SetBorderWidth( 10.f ); // Start the game loop while ( app_window.IsOpened() ) { // Process events sf::Event event; while ( app_window.PollEvent( event ) ) { // Handle events window->HandleEvent( event ); // Close window : exit if( (event.Type == sf::Event::Closed) || (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape) ) { app_window.Close(); } } // Clear screen app_window.Clear(); // Draw the window window->Expose( app_window ); // Update the window app_window.Display(); } return EXIT_SUCCESS; }
void ButtonClick() { // When the button is clicked set the contents of the label // to the contents of the entry widget. label->SetText( entry->GetText() ); }
int main() { // Create the main SFML window sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "SFGUI Range Example" ); // Create our main SFGUI window sfg::Window::Ptr window; window = sfg::Window::Create(); window->SetTitle( "Title" ); // Create our box. sfg::Box::Ptr box = sfg::Box::Create( sfg::Box::Horizontal ); // Create a label. label = sfg::Label::Create(); label->SetText( "20" ); // Scale and Scrollbar widgets are subclasses of the Range class. // They have a common data representation object known as an // Adjustment. The Adjustment that each range widget is bound to // determines it's current state (where the slider is, what max // and min values are, how much to scroll per step etc.). Because // range widgets share this common data object they can also be // linked together by a common Adjustment instance. An Adjustment // is created automatically for you when you create a range widget. // Create our scale smart pointer. sfg::Scale::Ptr scale; // Create the scale itself. // We want a horizontal scale. scale = sfg::Scale::Create( sfg::Scale::Horizontal ); // Create our scrollbar smart pointer. sfg::Scrollbar::Ptr scrollbar; // Create the scrollbar itself. // We want a vertical scrollbar. scrollbar = sfg::Scrollbar::Create( sfg::Scrollbar::Vertical ); // We can link both widgets together by their adjustments. adjustment = scrollbar->GetAdjustment(); scale->SetAdjustment( adjustment ); // Tune the adjustment parameters. adjustment->SetLower( 20.f ); adjustment->SetUpper( 100.f ); // How much it should change when clicked on the stepper. adjustment->SetMinorStep( 3.f ); // How much it should change when clicked on the trough. adjustment->SetMajorStep( 12.f ); // CAUTION: // Normally you would only set the page size for scrollbar adjustments. // For demonstration purposes we do this for our scale widget too. // If page size isn't 0 a scale widget won't be able to be set to it's // maximum value. This is in fact also true for scrollbars, however // because they are used to scroll the page size must be subtracted from // the maximum. adjustment->SetPageSize( 20.f ); // Additionally you can connect to the OnChange signal of an adjustment // to get notified when any of it's parameters are changed. adjustment->OnChange.Connect( &AdjustmentChange ); // Just as with the entry widget we set custom requisitions for our // range widgets to make sure they don't look strange. scale->SetRequisition( sf::Vector2f( 80.f, 20.f ) ); scrollbar->SetRequisition( sf::Vector2f( 0.f, 80.f ) ); // To keep our scale's slider from expanding too much we use another box // set to verticle orientation. sfg::Box::Ptr scalebox = sfg::Box::Create( sfg::Box::Vertical ); scalebox->Pack( scale, false, false ); // Pack into box box->Pack( scalebox ); box->Pack( scrollbar ); box->Pack( label ); // Set box spacing box->SetSpacing( 5.f ); // Add our box to the window window->Add( box ); window->SetBorderWidth( 10.f ); // Start the game loop while ( app_window.IsOpened() ) { // Process events sf::Event event; while ( app_window.PollEvent( event ) ) { // Handle events window->HandleEvent( event ); // Close window : exit if( (event.Type == sf::Event::Closed) || (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape) ) { app_window.Close(); } } // Clear screen app_window.Clear(); // Draw the window window->Expose( app_window ); // Update the window app_window.Display(); } return EXIT_SUCCESS; }
void AdjustmentChange() { std::stringstream sstr; sstr << adjustment->GetValue(); label->SetText( sstr.str() ); }
void ComboBoxExample::OnComboSelect() { std::stringstream sstr; sstr << "Item " << m_combo_box->GetSelectedItem() << " selected with text \"" << static_cast<std::string>( m_combo_box->GetSelectedText() ) << "\""; m_sel_label->SetText( sstr.str() ); }