| 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 SpinButton 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 our SpinButton and progress bar. |
| 24 | auto box = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 10.f ); |
| 25 | |
| 26 | // Create the SpinButton itself. |
| 27 | auto spinbutton = sfg::SpinButton::Create( -2.f, 18.f, .4f ); |
| 28 | |
| 29 | // Just like an Entry, you need to specify a minimum width for the SpinButton. |
| 30 | spinbutton->SetRequisition( sf::Vector2f( 80.f, 0.f ) ); |
| 31 | |
| 32 | // Set the number of digits to display after the decimal point. |
| 33 | spinbutton->SetDigits( 2 ); |
| 34 | |
| 35 | // Add the SpinButton to the box. |
| 36 | box->Pack( spinbutton ); |
| 37 | |
| 38 | // Create our progress bar and set its size. |
| 39 | auto progress_bar = sfg::ProgressBar::Create(); |
| 40 | progress_bar->SetRequisition( sf::Vector2f( 80.f, 20.f ) ); |
| 41 | |
| 42 | // Connect the OnValueChanged signal so we get notified when the SpinButton's value changes. |
| 43 | spinbutton->GetSignal( sfg::SpinButton::OnValueChanged ).Connect( [&spinbutton, &progress_bar] { |
| 44 | const auto& adjustment = spinbutton->GetAdjustment(); |
| 45 | |
| 46 | auto range = adjustment->GetUpper() - adjustment->GetLower(); |
| 47 | auto inverse_fraction = 1.f - ( spinbutton->GetValue() - adjustment->GetLower() ) / range; |
| 48 | |
| 49 | progress_bar->SetFraction( inverse_fraction ); |
| 50 | } ); |
| 51 | |
| 52 | // Add the progress bar to the box. |
| 53 | box->Pack( progress_bar ); |
| 54 | |
| 55 | // Set the initial value of the SpinButton. |
| 56 | // We can only do this after we create our progress bar because the way we |
| 57 | // set it, it will try to update the progress bar when we change its value. |
| 58 | spinbutton->SetValue( 4.f ); |
| 59 | |
| 60 | // Add the box to the window. |
| 61 | window->Add( box ); |
| 62 | |
| 63 | // Our clock. |
| 64 | sf::Clock clock; |
| 65 | |
| 66 | // Update an initial time to construct the GUI before drawing begins. |
nothing calls this directly
no test coverage detected