-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Always include the necessary header files. | ||
// Including SFGUI/Widgets.hpp includes everything | ||
// you can possibly need automatically. | ||
#include <SFGUI/SFGUI.hpp> | ||
#include <SFGUI/Widgets.hpp> | ||
|
||
#include <SFML/Graphics.hpp> | ||
|
||
int main() { | ||
sfg::SFGUI sfgui; | ||
sf::RenderWindow window(sf::VideoMode(640, 480), "ListBox Example"); | ||
window.setVerticalSyncEnabled(true); | ||
window.setFramerateLimit(30); | ||
|
||
sfg::Desktop desktop; | ||
|
||
auto sfg_window = sfg::Window::Create(); | ||
sfg_window->SetTitle( "ListBoxExample" ); | ||
|
||
auto box_outer = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL, 15.0f ); | ||
auto box_inner1 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.0f ); | ||
auto box_inner2 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.0f ); | ||
|
||
auto label1 = sfg::Label::Create("I'm single-select list."); | ||
auto label2 = sfg::Label::Create("I'm multi-select list."); | ||
|
||
auto input1 = sfg::Entry::Create(""); | ||
auto input2 = sfg::Entry::Create(""); | ||
|
||
auto list1 = sfg::ListBox::Create(); | ||
list1->AppendItem( "Item1" ); | ||
list1->AppendItem( "Item2" ); | ||
list1->AppendItem( "Item3" ); | ||
list1->GetSignal( sfg::ListBox::OnSelect ).Connect( std::bind( [list1, input1](){ if(list1->GetSelectedItemsCount()) input1->SetText(list1->GetSelectedItemText()); else input1->SetText(""); } ) ); | ||
|
||
auto list2 = sfg::ListBox::Create(); | ||
list2->AppendItem( "Item1" ); | ||
list2->AppendItem( "Item2" ); | ||
list2->AppendItem( "Item3" ); | ||
list2->multiselect = true; | ||
list2->GetSignal( sfg::ListBox::OnSelect ).Connect( std::bind( [list2, input2](){ | ||
std::string str = ""; | ||
for(unsigned i=0; i<list2->GetSelectedItemsCount(); i++) | ||
{ | ||
str += list2->GetSelectedItemText(i); | ||
str += ' '; | ||
} | ||
input2->SetText(str); | ||
} ) ); | ||
|
||
box_outer->Pack(box_inner1); | ||
box_outer->Pack(box_inner2); | ||
|
||
box_inner1->Pack(label1); | ||
box_inner1->Pack(list1); | ||
box_inner1->Pack(input1); | ||
|
||
box_inner2->Pack(label2); | ||
box_inner2->Pack(list2); | ||
box_inner2->Pack(input2); | ||
|
||
sfg_window->Add( box_outer ); | ||
desktop.Add( sfg_window ); | ||
|
||
sfg_window->SetPosition(sf::Vector2f(window.getSize().x/2-sfg_window->GetRequisition().x/2, window.getSize().y/2-sfg_window->GetRequisition().y/2)); | ||
|
||
sf::Event event; | ||
sf::Clock clock; | ||
|
||
window.resetGLStates(); | ||
|
||
while (window.isOpen()) | ||
{ | ||
while (window.pollEvent(event)) | ||
{ | ||
desktop.HandleEvent( event ); | ||
switch(event.type) | ||
{ | ||
case sf::Event::Closed: | ||
window.close(); | ||
break; | ||
} | ||
} | ||
desktop.Update( clock.restart().asSeconds() ); | ||
window.clear(); | ||
sfgui.Display( window ); | ||
window.display(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#pragma once | ||
|
||
#include <SFGUI/Bin.hpp> | ||
|
||
#include <SFML/System/String.hpp> | ||
#include <memory> | ||
#include <vector> | ||
|
||
namespace sfg { | ||
|
||
class SFGUI_API ListBox : public Widget { | ||
public: | ||
typedef std::shared_ptr<ListBox> Ptr; //!< Shared pointer. | ||
typedef std::shared_ptr<const ListBox> PtrConst; //!< Shared pointer. | ||
|
||
/** Create button. | ||
* @return ListBox. | ||
*/ | ||
static Ptr Create( ); | ||
|
||
const std::string& GetName() const override; | ||
|
||
unsigned GetSelectedItemsCount(); | ||
unsigned GetSelectedItemIndex(unsigned n=0); | ||
const std::string& GetSelectedItemText(unsigned n=0); | ||
|
||
void AppendItem(std::string str); | ||
void PrependItem(std::string str); | ||
void RemoveItem(unsigned index); | ||
void Clear(); | ||
|
||
// Signals. | ||
static Signal::SignalID OnSelect; //!< Fired when an entry is selected. | ||
|
||
bool resize_automatically = true; | ||
bool multiselect = false; | ||
|
||
protected: | ||
/** Ctor. | ||
*/ | ||
ListBox() = default; | ||
|
||
std::unique_ptr<RenderQueue> InvalidateImpl() const override; | ||
sf::Vector2f CalculateRequisition() override; | ||
|
||
private: | ||
void HandleMouseEnter( int x, int y ) override; | ||
void HandleMouseLeave( int x, int y ) override; | ||
void HandleMouseMoveEvent( int x, int y ) override; | ||
void HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int x, int y ) override; | ||
|
||
std::vector<std::string> m_entries; | ||
int selection_begin = -1, selection_end = -1; | ||
int hovered_element = -1; bool pressed_on_widget = false; | ||
std::vector<bool> selection_odds; | ||
|
||
bool widget_hover = false; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.