|
| 1 | +// Always include the necessary header files. |
| 2 | +// Including SFGUI/Widgets.hpp includes everything |
| 3 | +// you can possibly need automatically. |
| 4 | +#include <SFGUI/SFGUI.hpp> |
| 5 | +#include <SFGUI/Widgets.hpp> |
| 6 | + |
| 7 | +#include <SFML/Graphics.hpp> |
| 8 | + |
| 9 | +int main() { |
| 10 | + sfg::SFGUI sfgui; |
| 11 | + sf::RenderWindow window(sf::VideoMode(640, 480), "ListBox Example"); |
| 12 | + window.setVerticalSyncEnabled(true); |
| 13 | + window.setFramerateLimit(30); |
| 14 | + |
| 15 | + sfg::Desktop desktop; |
| 16 | + |
| 17 | + auto sfg_window = sfg::Window::Create(); |
| 18 | + sfg_window->SetTitle( "ListBoxExample" ); |
| 19 | + |
| 20 | + auto box_outer = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL, 15.0f ); |
| 21 | + auto box_inner1 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.0f ); |
| 22 | + auto box_inner2 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.0f ); |
| 23 | + |
| 24 | + auto label1 = sfg::Label::Create("I'm single-select list."); |
| 25 | + auto label2 = sfg::Label::Create("I'm multi-select list."); |
| 26 | + |
| 27 | + auto input1 = sfg::Entry::Create(""); |
| 28 | + auto input2 = sfg::Entry::Create(""); |
| 29 | + |
| 30 | + auto list1 = sfg::ListBox::Create(); |
| 31 | + list1->AppendItem( "Item1" ); |
| 32 | + list1->AppendItem( "Item2" ); |
| 33 | + list1->AppendItem( "Item3" ); |
| 34 | + list1->GetSignal( sfg::ListBox::OnSelect ).Connect( std::bind( [list1, input1](){ if(list1->GetSelectedItemsCount()) input1->SetText(list1->GetSelectedItemText()); else input1->SetText(""); } ) ); |
| 35 | + |
| 36 | + auto list2 = sfg::ListBox::Create(); |
| 37 | + list2->AppendItem( "Item1" ); |
| 38 | + list2->AppendItem( "Item2" ); |
| 39 | + list2->AppendItem( "Item3" ); |
| 40 | + list2->multiselect = true; |
| 41 | + list2->GetSignal( sfg::ListBox::OnSelect ).Connect( std::bind( [list2, input2](){ |
| 42 | + std::string str = ""; |
| 43 | + for(unsigned i=0; i<list2->GetSelectedItemsCount(); i++) |
| 44 | + { |
| 45 | + str += list2->GetSelectedItemText(i); |
| 46 | + str += ' '; |
| 47 | + } |
| 48 | + input2->SetText(str); |
| 49 | + } ) ); |
| 50 | + |
| 51 | + box_outer->Pack(box_inner1); |
| 52 | + box_outer->Pack(box_inner2); |
| 53 | + |
| 54 | + box_inner1->Pack(label1); |
| 55 | + box_inner1->Pack(list1); |
| 56 | + box_inner1->Pack(input1); |
| 57 | + |
| 58 | + box_inner2->Pack(label2); |
| 59 | + box_inner2->Pack(list2); |
| 60 | + box_inner2->Pack(input2); |
| 61 | + |
| 62 | + sfg_window->Add( box_outer ); |
| 63 | + desktop.Add( sfg_window ); |
| 64 | + |
| 65 | + sfg_window->SetPosition(sf::Vector2f(window.getSize().x/2-sfg_window->GetRequisition().x/2, window.getSize().y/2-sfg_window->GetRequisition().y/2)); |
| 66 | + |
| 67 | + sf::Event event; |
| 68 | + sf::Clock clock; |
| 69 | + |
| 70 | + window.resetGLStates(); |
| 71 | + |
| 72 | + while (window.isOpen()) |
| 73 | + { |
| 74 | + while (window.pollEvent(event)) |
| 75 | + { |
| 76 | + desktop.HandleEvent( event ); |
| 77 | + switch(event.type) |
| 78 | + { |
| 79 | + case sf::Event::Closed: |
| 80 | + window.close(); |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + desktop.Update( clock.restart().asSeconds() ); |
| 85 | + window.clear(); |
| 86 | + sfgui.Display( window ); |
| 87 | + window.display(); |
| 88 | + } |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
0 commit comments