| 7 | #include <SFML/Graphics.hpp> |
| 8 | |
| 9 | int main() { |
| 10 | // Create the main SFML window |
| 11 | sf::RenderWindow app_window( sf::VideoMode( { 800, 600 } ), "SFGUI Buttons Example", sf::Style::Titlebar | sf::Style::Close ); |
| 12 | |
| 13 | // We have to do this because we don't use SFML to draw. |
| 14 | app_window.resetGLStates(); |
| 15 | |
| 16 | // Create an SFGUI. This is required before doing anything with SFGUI. |
| 17 | sfg::SFGUI sfgui; |
| 18 | |
| 19 | // Create our main SFGUI window |
| 20 | auto window = sfg::Window::Create(); |
| 21 | window->SetTitle( "Title" ); |
| 22 | |
| 23 | // Create a Box to contain all our fun buttons ;) |
| 24 | auto box = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.f ); |
| 25 | |
| 26 | // Create the Button itself. |
| 27 | auto button = sfg::Button::Create( "Click me" ); |
| 28 | |
| 29 | // Add the Button to the Box |
| 30 | box->Pack( button ); |
| 31 | |
| 32 | // So that our Button has a meaningful purpose |
| 33 | // (besides just looking awesome :P) we need to tell it to connect |
| 34 | // to a callback of our choosing to notify us when it is clicked. |
| 35 | button->GetSignal( sfg::Widget::OnLeftClick ).Connect( [&button] { |
| 36 | // When the Button is clicked it's label should change. |
| 37 | button->SetLabel( "I was clicked" ); |
| 38 | } ); |
| 39 | |
| 40 | // If attempting to connect to a class method you need to provide |
| 41 | // a pointer to it as the second parameter after the function address. |
| 42 | // Refer to the Signals example for more information. |
| 43 | |
| 44 | // Create the ToggleButton itself. |
| 45 | auto toggle_button = sfg::ToggleButton::Create( "Toggle me" ); |
| 46 | |
| 47 | // Connect the OnToggle signal to our handler. |
| 48 | toggle_button->GetSignal( sfg::ToggleButton::OnToggle ).Connect( [&toggle_button, &window] { |
| 49 | // When the ToggleButton is active hide the window's titlebar. |
| 50 | if( toggle_button->IsActive() ) { |
| 51 | window->SetStyle( window->GetStyle() ^ sfg::Window::TITLEBAR ); |
| 52 | } |
| 53 | else { |
| 54 | window->SetStyle( window->GetStyle() | sfg::Window::TITLEBAR ); |
| 55 | } |
| 56 | } ); |
| 57 | |
| 58 | // Add the ToggleButton to the Box |
| 59 | box->Pack( toggle_button ); |
| 60 | |
| 61 | // Create the CheckButton itself. |
| 62 | auto check_button = sfg::CheckButton::Create( "Check me" ); |
| 63 | |
| 64 | // Since a CheckButton is also a ToggleButton we can use |
| 65 | // ToggleButton signals to handle events for CheckButtons. |
| 66 | check_button->GetSignal( sfg::ToggleButton::OnToggle ).Connect( [&check_button, &window] { |
nothing calls this directly
no test coverage detected