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