Skip to content

Commit

Permalink
ListBox widget added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zax37 committed Nov 25, 2015
1 parent c01dd76 commit d4f439c
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ set(
"${INCLUDE_PATH}/SFGUI/Frame.hpp"
"${INCLUDE_PATH}/SFGUI/Image.hpp"
"${INCLUDE_PATH}/SFGUI/Label.hpp"
"${INCLUDE_PATH}/SFGUI/ListBox.hpp"
"${INCLUDE_PATH}/SFGUI/Misc.hpp"
"${INCLUDE_PATH}/SFGUI/Notebook.hpp"
"${INCLUDE_PATH}/SFGUI/Object.hpp"
Expand Down Expand Up @@ -135,6 +136,7 @@ set(
"${SOURCE_PATH}/SFGUI/GLLoader.hpp"
"${SOURCE_PATH}/SFGUI/Image.cpp"
"${SOURCE_PATH}/SFGUI/Label.cpp"
"${SOURCE_PATH}/SFGUI/ListBox.cpp"
"${SOURCE_PATH}/SFGUI/Misc.cpp"
"${SOURCE_PATH}/SFGUI/Notebook.cpp"
"${SOURCE_PATH}/SFGUI/Object.cpp"
Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ build_example( "ProgressBar" "ProgressBar.cpp" )
build_example( "SpinButton" "SpinButton.cpp" )
build_example( "Canvas" "Canvas.cpp" )
build_example( "CustomWidget" "CustomWidget.cpp" )
build_example( "ListBox" "ListBox.cpp" )
build_example( "SFGUI-Test" "Test.cpp" )

# Copy data directory to build cache directory to be able to run examples from
Expand Down
91 changes: 91 additions & 0 deletions examples/ListBox.cpp
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;
}
60 changes: 60 additions & 0 deletions include/SFGUI/ListBox.hpp
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;
};

}
1 change: 1 addition & 0 deletions include/SFGUI/Widgets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <SFGUI/Frame.hpp>
#include <SFGUI/Image.hpp>
#include <SFGUI/Label.hpp>
#include <SFGUI/ListBox.hpp>
#include <SFGUI/Notebook.hpp>
#include <SFGUI/ProgressBar.hpp>
#include <SFGUI/RadioButton.hpp>
Expand Down
Loading

0 comments on commit d4f439c

Please sign in to comment.