void GuessMyNumber::Run() { sf::RenderWindow render_window( sf::VideoMode( 1024, 768, 32 ), TITLE, sf::Style::Titlebar | sf::Style::Close ); sf::Event event; // We have to do this because we don't use SFML to draw. render_window.resetGLStates(); // Create widgets. sfg::Window::Ptr window( sfg::Window::Create() ); window->SetTitle( TITLE ); sfg::Button::Ptr new_game_button( sfg::Button::Create( "New game" ) ); new_game_button->GetSignal( sfg::Widget::OnLeftClick ).Connect( &GuessMyNumber::OnNewGameClick, this ); m_guess_button->SetId( "guess" ); m_guess_button->GetSignal( sfg::Widget::OnLeftClick ).Connect( &GuessMyNumber::OnGuessClick, this ); // Layout. sfg::Table::Ptr table( sfg::Table::Create() ); table->Attach( sfg::Label::Create( "Your guess:" ), sf::Rect<sf::Uint32>( 0, 0, 1, 1 ), sfg::Table::FILL, sfg::Table::FILL ); table->Attach( m_number_entry, sf::Rect<sf::Uint32>( 1, 0, 1, 1 ) ); table->Attach( sfg::Label::Create( "Tries:" ), sf::Rect<sf::Uint32>( 0, 1, 1, 1 ), sfg::Table::FILL, sfg::Table::FILL ); table->Attach( m_tries_label, sf::Rect<sf::Uint32>( 1, 1, 1, 1 ) ); table->Attach( sfg::Label::Create( "Hint:" ), sf::Rect<sf::Uint32>( 0, 2, 1, 1 ), sfg::Table::FILL, sfg::Table::FILL ); table->Attach( m_hint_label, sf::Rect<sf::Uint32>( 1, 2, 1, 1 ) ); table->SetColumnSpacings( 5.f ); table->SetRowSpacings( 5.f ); sfg::Box::Ptr buttons_box( sfg::Box::Create( sfg::Box::HORIZONTAL, 5.f ) ); buttons_box->Pack( new_game_button ); buttons_box->Pack( m_guess_button ); sfg::Box::Ptr content_vbox( sfg::Box::Create( sfg::Box::VERTICAL, 5.f ) ); content_vbox->Pack( sfg::Label::Create( "Guess my number, it's from 1 to 100!" ) ); content_vbox->Pack( table ); content_vbox->Pack( buttons_box ); window->Add( content_vbox ); ResetGame(); window->SetPosition( sf::Vector2f( static_cast<float>( render_window.getSize().x / 2 ) - window->GetAllocation().width / 2.f, static_cast<float>( render_window.getSize().y / 2 ) - window->GetAllocation().height / 2.f ) ); // Custom properties. sfg::Context::Get().GetEngine().SetProperty( "Button#guess", "BackgroundColor", sf::Color( 0, 100, 0 ) ); sfg::Context::Get().GetEngine().SetProperty( "Button#guess", "BorderColor", sf::Color( 0, 100, 0 ) ); sfg::Context::Get().GetEngine().SetProperty( "Button#guess:Prelight", "BackgroundColor", sf::Color( 0, 130, 0 ) ); sfg::Context::Get().GetEngine().SetProperty( "Button#guess:Prelight", "BorderColor", sf::Color( 0, 130, 0 ) ); sfg::Context::Get().GetEngine().SetProperty( "Button#guess > Label", "FontSize", 20.f ); // Make sure all properties are applied. window->Refresh(); while( render_window.isOpen() ) { while( render_window.pollEvent( event ) ) { if( (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) || event.type == sf::Event::Closed ) { render_window.close(); } window->HandleEvent( event ); } window->Update( 0.f ); render_window.clear(); m_sfgui.Display( render_window ); render_window.display(); } }
void ButtonsExample::Run() { // Create the main SFML window sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "SFGUI Buttons 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 m_window = sfg::Window::Create(); m_window->SetTitle( "Title" ); // Create a Box to contain all our fun buttons ;) sfg::Box::Ptr box = sfg::Box::Create( sfg::Box::VERTICAL, 5.f ); // Create the Button itself. m_button = sfg::Button::Create( "Click me" ); // Add the Button to the Box box->Pack( m_button ); // So that our Button 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. m_button->GetSignal( sfg::Widget::OnLeftClick ).Connect( &ButtonsExample::ButtonClick, this ); // If attempting to connect to a class method you need to provide // a pointer to it as the second parameter after the function address. // Refer to the Signals example for more information. // Create the ToggleButton itself. m_toggle_button = sfg::ToggleButton::Create( "Toggle me" ); // Connect the OnToggle signal to our handler. m_toggle_button->GetSignal( sfg::ToggleButton::OnToggle ).Connect( &ButtonsExample::ButtonToggle, this ); // Add the ToggleButton to the Box box->Pack( m_toggle_button ); // Create the CheckButton itself. m_check_button = sfg::CheckButton::Create( "Check me" ); // Since a CheckButton is also a ToggleButton we can use // ToggleButton signals to handle events for CheckButtons. m_check_button->GetSignal( sfg::ToggleButton::OnToggle ).Connect( &ButtonsExample::ButtonCheck, this ); // Add the CheckButton to the Box box->Pack( m_check_button ); // Just to keep things tidy ;) box->Pack( sfg::Separator::Create() ); // Create our RadioButtons. // RadioButtons each have a group they belong to. If not specified, // a new group is created by default for each RadioButton. You can // then use RadioButton::SetGroup() to set the group of a RadioButton // after you create them. If you already know which buttons will belong // to the same group you can just pass the group of the first button // to the following buttons when you construct them as we have done here. m_radio_button1 = sfg::RadioButton::Create( "Either this" ); m_radio_button2 = sfg::RadioButton::Create( "Or this", m_radio_button1->GetGroup() ); m_radio_button3 = sfg::RadioButton::Create( "Or maybe even this", m_radio_button1->GetGroup() ); // Set the third RadioButton to be the active one. // By default none of the RadioButtons are active at start. m_radio_button3->SetActive( true ); // Here we use the same handler for all three RadioButtons. // RadioButtons are CheckButtons and therefore also ToggleButtons, // hence we can use ToggleButton signals with RadioButtons as well. m_radio_button1->GetSignal( sfg::ToggleButton::OnToggle ).Connect( &ButtonsExample::ButtonSelect, this ); m_radio_button2->GetSignal( sfg::ToggleButton::OnToggle ).Connect( &ButtonsExample::ButtonSelect, this ); m_radio_button3->GetSignal( sfg::ToggleButton::OnToggle ).Connect( &ButtonsExample::ButtonSelect, this ); // Add the RadioButtons to the Box box->Pack( m_radio_button1 ); box->Pack( m_radio_button2 ); box->Pack( m_radio_button3 ); // Finally add the Box to the window. m_window->Add( box ); // Start the game loop while ( app_window.isOpen() ) { // Process events sf::Event event; while ( app_window.pollEvent( event ) ) { // Handle events m_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. m_window->Update( 0.f ); // Clear screen app_window.clear(); // Draw the GUI m_sfgui.Display( app_window ); // Update the window app_window.display(); } }