|
| 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(800, 600), "ListBox Example"); |
| 12 | + window.setVerticalSyncEnabled(true); |
| 13 | + window.setFramerateLimit(30); |
| 14 | + |
| 15 | + sf::Image firstImage; |
| 16 | + firstImage.loadFromFile("data/add.png"); |
| 17 | + |
| 18 | + sf::Image secondImage; |
| 19 | + secondImage.loadFromFile("data/delete.png"); |
| 20 | + |
| 21 | + sfg::Desktop desktop; |
| 22 | + |
| 23 | + auto window1 = sfg::Window::Create(); |
| 24 | + window1->SetTitle( "ListBox with ItemTextPolicy::RESIZE_LISTBOX" ); |
| 25 | + |
| 26 | + auto box1 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL ); |
| 27 | + box1->SetSpacing( 5.f ); |
| 28 | + box1->PackEnd( sfg::Label::Create( "The minimum width\nof this ListBox\ncorresponds to the largest\nitem's text width." ), false, true ); |
| 29 | + |
| 30 | + auto listbox1 = sfg::ListBox::Create(); |
| 31 | + listbox1->AppendItem( "This is the first item" ); |
| 32 | + listbox1->AppendItem( "Second item" ); |
| 33 | + listbox1->AppendItem( "Third item which is a bit large" ); |
| 34 | + listbox1->AppendItem( "Fourth item" ); |
| 35 | + listbox1->AppendItem( "Fifth item" ); |
| 36 | + listbox1->AppendItem( "Sixth item" ); |
| 37 | + listbox1->AppendItem( "Last one !" ); |
| 38 | + box1->PackEnd( listbox1 ); |
| 39 | + listbox1->SetImagesSize(sf::Vector2f(32.f, 32.f)); |
| 40 | + |
| 41 | + window1->Add( box1 ); |
| 42 | + |
| 43 | + auto window2 = sfg::Window::Create(); |
| 44 | + window2->SetTitle( "ListBox with ItemTextPolicy::SHRINK" ); |
| 45 | + |
| 46 | + auto box2 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL ); |
| 47 | + box2->SetSpacing( 5.f ); |
| 48 | + auto label2 = sfg::Label::Create( "The items' texts\nare shrinked if the\nListBox is not big\nenough." ); |
| 49 | + box2->PackEnd( label2, false, true ); |
| 50 | + |
| 51 | + auto listbox2 = sfg::ListBox::Create(); |
| 52 | + listbox2->AppendItem( "This is the first item (long text)" ); |
| 53 | + listbox2->AppendItem( "Second item", firstImage ); |
| 54 | + listbox2->AppendItem( "Third item which is very long !", secondImage ); |
| 55 | + listbox2->AppendItem( "Fourth item" ); |
| 56 | + listbox2->AppendItem( "Fifth item" ); |
| 57 | + listbox2->AppendItem( "Sixth item, again it's too large !" ); |
| 58 | + listbox2->AppendItem( "Last one !" ); |
| 59 | + listbox2->SetItemTextPolicy( sfg::ListBox::ItemTextPolicy::SHRINK ); |
| 60 | + box2->PackEnd( listbox2 ); |
| 61 | + listbox2->SetImagesSize(sf::Vector2f(32.f, 32.f)); |
| 62 | + |
| 63 | + window2->Add( box2 ); |
| 64 | + |
| 65 | + auto window3 = sfg::Window::Create(); |
| 66 | + window3->SetTitle( "ListBox with ItemTextPolicy::SHRINK" ); |
| 67 | + |
| 68 | + auto box3 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL ); |
| 69 | + box3->SetSpacing( 5.f ); |
| 70 | + auto label3 = sfg::Label::Create( "You can select multiple\nitems in this ListBox." ); |
| 71 | + box3->PackEnd( label3, false, true ); |
| 72 | + |
| 73 | + auto listbox3 = sfg::ListBox::Create(); |
| 74 | + listbox3->AppendItem( "First item" ); |
| 75 | + listbox3->AppendItem( "Second item" ); |
| 76 | + listbox3->AppendItem( "Third item" ); |
| 77 | + listbox3->AppendItem( "Fourth item" ); |
| 78 | + listbox3->AppendItem( "Fifth item" ); |
| 79 | + listbox3->AppendItem( "Sixth item" ); |
| 80 | + listbox3->AppendItem( "Last one !" ); |
| 81 | + listbox3->SetSelectionMode( sfg::ListBox::SelectionMode::MULTI_SELECTION ); |
| 82 | + listbox3->SetSelection( {1, 3, 4, 5} ); |
| 83 | + box3->PackEnd( listbox3 ); |
| 84 | + |
| 85 | + window3->Add( box3 ); |
| 86 | + |
| 87 | + desktop.Add( window1 ); |
| 88 | + desktop.Add( window2 ); |
| 89 | + desktop.Add( window3 ); |
| 90 | + |
| 91 | + sf::Vector2f windowSize( static_cast<float>( window.getSize().x ), static_cast<float>( window.getSize().y ) ); |
| 92 | + |
| 93 | + window2->SetPosition(sf::Vector2f(windowSize.x/2.f - window2->GetRequisition().x/2.f, windowSize.y/2.f - window2->GetRequisition().y/2.f)); |
| 94 | + window3->SetPosition(sf::Vector2f(windowSize.x - window3->GetRequisition().x - 100.f, windowSize.y - window3->GetRequisition().y - 100.f)); |
| 95 | + |
| 96 | + sf::Event event; |
| 97 | + sf::Clock clock; |
| 98 | + |
| 99 | + window.resetGLStates(); |
| 100 | + |
| 101 | + int i = 0; |
| 102 | + |
| 103 | + while (window.isOpen()) |
| 104 | + { |
| 105 | + while (window.pollEvent(event)) |
| 106 | + { |
| 107 | + desktop.HandleEvent( event ); |
| 108 | + switch(event.type) |
| 109 | + { |
| 110 | + case sf::Event::Closed: |
| 111 | + window.close(); |
| 112 | + break; |
| 113 | + case sf::Event::KeyPressed: |
| 114 | + if( event.key.code == sf::Keyboard::R ) { |
| 115 | + listbox3->RemoveItem(2); |
| 116 | + } else if( event.key.code == sf::Keyboard::I ) { |
| 117 | + listbox3->InsertItem(3, "Inserted item #" + std::to_string(i)); |
| 118 | + ++i; |
| 119 | + } else if( event.key.code == sf::Keyboard::A) { |
| 120 | + listbox3->AppendItem("Appended item #" + std::to_string(i)); |
| 121 | + ++i; |
| 122 | + } else if( event.key.code == sf::Keyboard::P) { |
| 123 | + listbox3->PrependItem("Prepended item #" + std::to_string(i)); |
| 124 | + ++i; |
| 125 | + } |
| 126 | + break; |
| 127 | + default: |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + desktop.Update( clock.restart().asSeconds() ); |
| 132 | + window.clear(); |
| 133 | + sfgui.Display( window ); |
| 134 | + window.display(); |
| 135 | + } |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
0 commit comments