TGUI  0.8.7
ListBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 
73  static ListBox::Ptr copy(ListBox::ConstPtr listBox);
74 
75 
80  ListBoxRenderer* getSharedRenderer();
81  const ListBoxRenderer* getSharedRenderer() const;
82 
88  ListBoxRenderer* getRenderer();
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 
236  void removeAllItems();
237 
238 
249  sf::String getItemById(const sf::String& id) const;
250 
251 
259  sf::String getItemByIndex(std::size_t index) const;
260 
261 
269  sf::String getSelectedItem() const;
270 
271 
279  sf::String getSelectedItemId() const;
280 
281 
288  int getSelectedItemIndex() const;
289 
290 
304  bool changeItem(const sf::String& originalValue, const sf::String& newValue);
305 
306 
320  bool changeItemById(const sf::String& id, const sf::String& newValue);
321 
322 
334  bool changeItemByIndex(std::size_t index, const sf::String& newValue);
335 
336 
343  std::size_t getItemCount() const;
344 
345 
352  std::vector<sf::String> getItems() const;
353 
354 
363  const std::vector<sf::String>& getItemIds() const;
364 
365 
374  void setItemHeight(unsigned int itemHeight);
375 
376 
383  unsigned int getItemHeight() const;
384 
385 
397  void setTextSize(unsigned int textSize) override;
398 
399 
409  void setMaximumItems(std::size_t maximumItems = 0);
410 
411 
419  std::size_t getMaximumItems() const;
420 
421 
430  void setAutoScroll(bool autoScroll);
431 
432 
437  bool getAutoScroll() const;
438 
439 
447  bool contains(const sf::String& item) const;
448 
449 
457  bool containsId(const sf::String& id) const;
458 
459 
465  void setScrollbarValue(unsigned int value);
466 
467 
473  unsigned int getScrollbarValue() const;
474 
475 
482  bool mouseOnWidget(Vector2f pos) const override;
483 
487  void leftMousePressed(Vector2f pos) override;
488 
492  void leftMouseReleased(Vector2f pos) override;
493 
497  void mouseMoved(Vector2f pos) override;
498 
502  bool mouseWheelScrolled(float delta, Vector2f pos) override;
503 
507  void mouseNoLongerOnWidget() override;
508 
512  void leftMouseButtonNoLongerDown() override;
513 
514 
522  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
523 
524 
526  protected:
527 
537  Signal& getSignal(std::string signalName) override;
538 
539 
546  void rendererChanged(const std::string& property) override;
547 
548 
552  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
553 
554 
558  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
559 
560 
562  // Returns the size without the borders
564  Vector2f getInnerSize() const;
565 
566 
568  // Update the colors and text style of the selected and hovered items
570  void updateSelectedAndHoveringItemColorsAndStyle();
571 
572 
574  // Update the color and text style of all the items
576  void updateItemColorsAndStyle();
577 
578 
580  // Update on which item the mouse is standing
582  void updateHoveringItem(int item);
583 
584 
586  // Update which item is selected
588  void updateSelectedItem(int item);
589 
590 
592  // This function is called every frame with the time passed since the last frame.
594  void update(sf::Time elapsedTime) override;
595 
596 
598  // Makes a copy of the widget
600  Widget::Ptr clone() const override
601  {
602  return std::make_shared<ListBox>(*this);
603  }
604 
605 
607  public:
608 
609  SignalItem onItemSelect = {"ItemSelected"};
610  SignalItem onMousePress = {"MousePressed"};
611  SignalItem onMouseRelease = {"MouseReleased"};
612  SignalItem onDoubleClick = {"DoubleClicked"};
613 
614 
616  protected:
617 
618  // This contains the different items in the list box
619  std::vector<Text> m_items;
620  std::vector<sf::String> m_itemIds;
621 
622  // What is the index of the selected item?
623  // This is also used by combo box, so it can't just be changed to a pointer!
624  int m_selectedItem = -1;
625 
626  int m_hoveringItem = -1;
627 
628  // The size must be stored
629  unsigned int m_itemHeight = 0;
630  unsigned int m_requestedTextSize = 0;
631 
632  // This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
633  std::size_t m_maxItems = 0;
634 
635  // When there are too many items a scrollbar will be shown
637 
638  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
639  bool m_possibleDoubleClick = false;
640 
641  bool m_autoScroll = true;
642 
643  Sprite m_spriteBackground;
644 
645  // Cached renderer properties
646  Borders m_bordersCached;
647  Borders m_paddingCached;
648  Color m_borderColorCached;
649  Color m_backgroundColorCached;
650  Color m_backgroundColorHoverCached;
651  Color m_selectedBackgroundColorCached;
652  Color m_selectedBackgroundColorHoverCached;
653  Color m_textColorCached;
654  Color m_textColorHoverCached;
655  Color m_selectedTextColorCached;
656  Color m_selectedTextColorHoverCached;
657  TextStyle m_textStyleCached;
658  TextStyle m_selectedTextStyleCached;
659 
661  };
662 
664 }
665 
667 
668 #endif // TGUI_LIST_BOX_HPP
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:220
Definition: ListBoxRenderer.hpp:36
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:48
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:563
List box widget.
Definition: ListBox.hpp:42
Definition: Sprite.hpp:45
Definition: Vector2f.hpp:38
Definition: Outline.hpp:38
The parent class for every widget.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< ListBox > Ptr
Shared widget pointer.
Definition: ListBox.hpp:46
std::shared_ptr< const ListBox > ConstPtr
Shared constant widget pointer.
Definition: ListBox.hpp:47
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ListBox.hpp:600
Wrapper for text styles.
Definition: TextStyle.hpp:46