TGUI  0.8.9
ListBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_LIST_BOX_HPP
27 #define TGUI_LIST_BOX_HPP
28 
29 
30 #include <TGUI/CopiedSharedPtr.hpp>
31 #include <TGUI/Widgets/Scrollbar.hpp>
32 #include <TGUI/Renderers/ListBoxRenderer.hpp>
33 #include <TGUI/Text.hpp>
34 
36 
37 namespace tgui
38 {
42  class TGUI_API ListBox : public Widget
43  {
44  public:
45 
46  typedef std::shared_ptr<ListBox> Ptr;
47  typedef std::shared_ptr<const ListBox> ConstPtr;
48 
49 
51  // Default constructor
53  ListBox();
54 
55 
62  static ListBox::Ptr create();
63 
64 
74 
75 
81  const ListBoxRenderer* getSharedRenderer() const;
82 
89  const ListBoxRenderer* getRenderer() const;
90 
91 
98  void setPosition(const Layout2d& position) override;
99  using Widget::setPosition;
100 
101 
108  void setSize(const Layout2d& size) override;
109  using Widget::setSize;
110 
111 
126  bool addItem(const sf::String& itemName, const sf::String& id = "");
127 
128 
143  bool setSelectedItem(const sf::String& itemName);
144 
145 
160  bool setSelectedItemById(const sf::String& id);
161 
162 
176  bool setSelectedItemByIndex(std::size_t index);
177 
178 
183  void deselectItem();
184 
185 
198  bool removeItem(const sf::String& itemName);
199 
200 
213  bool removeItemById(const sf::String& id);
214 
215 
229  bool removeItemByIndex(std::size_t index);
230 
231 
237 
238 
249  sf::String getItemById(const sf::String& id) const;
250 
251 
259  sf::String getItemByIndex(std::size_t index) const;
260 
261 
271  int getIndexById(const sf::String& id) const;
272 
273 
281  sf::String getIdByIndex(std::size_t index) const;
282 
283 
291  sf::String getSelectedItem() const;
292 
293 
301  sf::String getSelectedItemId() const;
302 
303 
310  int getSelectedItemIndex() const;
311 
312 
326  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
327 
328 
342  bool changeItemById(const sf::String& id, const sf::String& newValue);
343 
344 
356  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
357 
358 
365  std::size_t getItemCount() const;
366 
367 
374  std::vector<sf::String> getItems() const;
375 
376 
385  const std::vector<sf::String>& getItemIds() const;
386 
387 
396  void setItemHeight(unsigned int itemHeight);
397 
398 
405  unsigned int getItemHeight() const;
406 
407 
419  void setTextSize(unsigned int textSize) override;
420 
421 
431  void setMaximumItems(std::size_t maximumItems = 0);
432 
433 
441  std::size_t getMaximumItems() const;
442 
443 
452  void setAutoScroll(bool autoScroll);
453 
454 
459  bool getAutoScroll() const;
460 
461 
469  bool contains(const sf::String& item) const;
470 
471 
479  bool containsId(const sf::String& id) const;
480 
481 
487  void setScrollbarValue(unsigned int value);
488 
489 
495  unsigned int getScrollbarValue() const;
496 
497 
504  bool mouseOnWidget(Vector2f pos) const override;
505 
509  void leftMousePressed(Vector2f pos) override;
510 
514  void leftMouseReleased(Vector2f pos) override;
515 
519  void mouseMoved(Vector2f pos) override;
520 
524  bool mouseWheelScrolled(float delta, Vector2f pos) override;
525 
529  void mouseNoLongerOnWidget() override;
530 
534  void leftMouseButtonNoLongerDown() override;
535 
536 
544  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
545 
546 
548  protected:
549 
559  Signal& getSignal(std::string signalName) override;
560 
561 
568  void rendererChanged(const std::string& property) override;
569 
570 
574  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
575 
576 
580  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
581 
582 
584  // Returns the size without the borders
586  Vector2f getInnerSize() const;
587 
588 
590  // Update the colors and text style of the selected and hovered items
592  void updateSelectedAndHoveringItemColorsAndStyle();
593 
594 
596  // Update the color and text style of all the items
598  void updateItemColorsAndStyle();
599 
600 
602  // Update on which item the mouse is standing
604  void updateHoveringItem(int item);
605 
606 
608  // Update which item is selected
610  void updateSelectedItem(int item);
611 
612 
614  // This function is called every frame with the time passed since the last frame.
616  bool update(sf::Time elapsedTime) override;
617 
618 
620  // Makes a copy of the widget
622  Widget::Ptr clone() const override
623  {
624  return std::make_shared<ListBox>(*this);
625  }
626 
627 
629  public:
630 
631  SignalItem onItemSelect = {"ItemSelected"};
632  SignalItem onMousePress = {"MousePressed"};
633  SignalItem onMouseRelease = {"MouseReleased"};
634  SignalItem onDoubleClick = {"DoubleClicked"};
635 
636 
638  protected:
639 
640  // This contains the different items in the list box
641  std::vector<Text> m_items;
642  std::vector<sf::String> m_itemIds;
643 
644  // What is the index of the selected item?
645  // This is also used by combo box, so it can't just be changed to a pointer!
646  int m_selectedItem = -1;
647 
648  int m_hoveringItem = -1;
649 
650  // The size must be stored
651  unsigned int m_itemHeight = 0;
652  unsigned int m_requestedTextSize = 0;
653 
654  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
655  std::size_t m_maxItems = 0;
656 
657  // When there are too many items a scrollbar will be shown
659 
660  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
661  bool m_possibleDoubleClick = false;
662 
663  bool m_autoScroll = true;
664 
665  Sprite m_spriteBackground;
666 
667  // Cached renderer properties
668  Borders m_bordersCached;
669  Borders m_paddingCached;
670  Color m_borderColorCached;
671  Color m_backgroundColorCached;
672  Color m_backgroundColorHoverCached;
673  Color m_selectedBackgroundColorCached;
674  Color m_selectedBackgroundColorHoverCached;
675  Color m_textColorCached;
676  Color m_textColorHoverCached;
677  Color m_selectedTextColorCached;
678  Color m_selectedTextColorHoverCached;
679  TextStyle m_textStyleCached;
680  TextStyle m_selectedTextStyleCached;
681 
683  };
684 
686 }
687 
689 
690 #endif // TGUI_LIST_BOX_HPP
Wrapper for colors.
Definition: Color.hpp:49
Definition: CopiedSharedPtr.hpp:40
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Definition: ListBoxRenderer.hpp:37
List box widget.
Definition: ListBox.hpp:43
void setItemHeight(unsigned int itemHeight)
Changes the height of the items in the list box.
std::size_t getItemCount() const
Returns the amount of items in the list box.
void setTextSize(unsigned int textSize) override
Changes the text size of the items.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:46
bool changeItemById(const sf::String &id, const sf::String &newValue)
Changes the name of an item with the given id to newValue.
unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
unsigned int getItemHeight() const
Returns the height of the items in the list box.
const std::vector< sf::String > & getItemIds() const
Returns a copy of the item ids in the list box.
void deselectItem()
Deselects the selected item.
int getIndexById(const sf::String &id) const
Returns the index of the item with the given id.
bool addItem(const sf::String &itemName, const sf::String &id="")
Adds an item to the list.
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition: ListBox.hpp:47
bool removeItem(const sf::String &itemName)
Removes the item from the list with the given name.
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the list box can contain.
bool containsId(const sf::String &id) const
Returns whether the list box contains an item with the given id.
bool setSelectedItem(const sf::String &itemName)
Selects an item in the list box.
std::vector< sf::String > getItems() const
Returns a copy of the items in the list box.
static ListBox::Ptr copy(ListBox::ConstPtr listBox)
Makes a copy of another list box.
static ListBox::Ptr create()
Creates a new list box widget.
bool removeItemById(const sf::String &id)
Removes the item that were added with the given id.
void removeAllItems()
Removes all items from the list.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
sf::String getItemByIndex(std::size_t index) const
Returns the item name of the item at the given index.
bool removeItemByIndex(std::size_t index)
Removes the item from the list box.
bool contains(const sf::String &item) const
Returns whether the list box contains the given item.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ListBox.hpp:622
void setSize(const Layout2d &size) override
Changes the size of the list box.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
ListBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
sf::String getIdByIndex(std::size_t index) const
Returns the id of the item at the given index.
bool getAutoScroll() const
Returns whether the list box scrolls to the bottom when a new item is added.
bool setSelectedItemById(const sf::String &id)
Selects an item in the list box.
sf::String getItemById(const sf::String &id) const
Returns the item name of the item with the given id.
int getSelectedItemIndex() const
Gets the index of the selected item.
sf::String getSelectedItem() const
Returns the currently selected item.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
void setAutoScroll(bool autoScroll)
Changes whether the list box scrolls to the bottom when a new item is added.
std::size_t getMaximumItems() const
Returns the maximum items that the list box can contain.
bool changeItemByIndex(std::size_t index, const sf::String &newValue)
Changes the name of an item at the given index to newValue.
void setPosition(const Layout2d &position) override
Sets the position of the widget.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the list box.
sf::String getSelectedItemId() const
Gets the id of the selected item.
ListBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool changeItem(const sf::String &originalValue, const sf::String &newValue)
Changes an item with name originalValue to newValue.
Definition: Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:575
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:231
Definition: Sprite.hpp:46
Wrapper for text styles.
Definition: TextStyle.hpp:47
Definition: Vector2f.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37