TGUI 1.13
Loading...
Searching...
No Matches
PanelListBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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#ifndef TGUI_PANEL_LIST_BOX_HPP
26#define TGUI_PANEL_LIST_BOX_HPP
27
28#include <TGUI/Renderers/PanelListBoxRenderer.hpp>
29#include <TGUI/Widgets/ScrollablePanel.hpp>
30
32
33namespace tgui
34{
38 class TGUI_API PanelListBox : public ScrollablePanel
39 {
40 public:
41 using Ptr = std::shared_ptr<PanelListBox>;
42 using ConstPtr = std::shared_ptr<const PanelListBox>;
43
44 static constexpr char StaticWidgetType[] = "PanelListBox";
45
53 explicit PanelListBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
54
59 static Ptr create();
60
66 static Ptr copy(const ConstPtr& panelListBox);
67
72 [[nodiscard]] PanelListBoxRenderer* getSharedRenderer() override;
73 [[nodiscard]] const PanelListBoxRenderer* getSharedRenderer() const override;
74
80 [[nodiscard]] PanelListBoxRenderer* getRenderer() override;
81
86 void setSize(const Layout2d& size) override;
88
95 Panel::Ptr addItem(const String& id = {}, int index = -1);
96
102 [[nodiscard]] Panel::Ptr getPanelTemplate() const;
103
109 [[nodiscard]] Layout getItemsWidth() const;
110
116 void setItemsHeight(const Layout& height);
117
123 [[nodiscard]] Layout getItemsHeight() const;
124
138 bool setSelectedItem(const Panel::Ptr& panelPtr);
139
154
167 bool setSelectedItemByIndex(std::size_t index);
168
173
185 bool removeItem(const Panel::Ptr& panelPtr);
186
198 bool removeItemById(const String& id);
199
212 bool removeItemByIndex(std::size_t index);
213
218
228 [[nodiscard]] Panel::Ptr getItemById(const String& id) const;
229
237 [[nodiscard]] Panel::Ptr getItemByIndex(std::size_t index) const;
238
248 int getIndexById(const String& id) const;
249
257 int getIndexByItem(const Panel::Ptr& panelPtr) const;
258
266 String getIdByIndex(std::size_t index) const;
267
274 [[nodiscard]] Panel::Ptr getSelectedItem() const;
275
282 [[nodiscard]] String getSelectedItemId() const;
283
289 [[nodiscard]] int getSelectedItemIndex() const;
290
296 [[nodiscard]] int getHoveredItemIndex() const;
297
303 [[nodiscard]] std::size_t getItemCount() const;
304
310 [[nodiscard]] std::vector<Panel::Ptr> getItems() const;
311
317 [[nodiscard]] std::vector<String> getItemIds() const;
318
325 void setMaximumItems(std::size_t maximumItems = 0);
326
333 [[nodiscard]] std::size_t getMaximumItems() const;
334
342 bool contains(const Panel::Ptr& panelPtr) const;
343
351 bool containsId(const String& id) const;
352
356 void mouseMoved(Vector2f pos) override;
357
361 void mouseNoLongerOnWidget() override;
362
366 bool leftMousePressed(Vector2f pos) override;
367
369
370 protected:
380 [[nodiscard]] Signal& getSignal(String signalName) override;
381
387 void rendererChanged(const String& property) override;
388
392 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
393
397 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
398
400 // Updates the position of the items
402 void updateItemsPositions() const;
403
405 // Updates the size of the items
407 void updateItemsSize() const;
408
410 // Returns all items height
412 [[nodiscard]] float getAllItemsHeight() const;
413
415 // Returns height of the items
416 //
419 [[nodiscard]] float getItemsHeightUpToIndex(std::size_t index) const;
420
422 // Update which item is selected
424 void updateSelectedItem(int item);
425
427 // Update on which item the mouse is standing
429 void updateHoveringItem(int item);
430
432 // Update the colors and style of the selected and hovered items
434 void updateSelectedAndHoveringItemColorsAndStyle() const;
435
437 // Clear item style
438 //
441 void clearItemStyle(int item) const;
442
444 // Clear all items style
446 void clearAllItemsStyle() const;
447
449 // Makes a copy of the widget
451 [[nodiscard]] Widget::Ptr clone() const override;
452
454
455 public:
457 "ItemSelected"};
458
460
461 protected:
462 struct Item
463 {
464 Panel::Ptr panel;
465 String id;
466 };
467
468 std::vector<Item> m_items;
469 std::size_t m_maxItems = 0;
470 Panel::Ptr m_panelTemplate = Panel::create();
471
472 int m_selectedItem = -1;
473 int m_hoveringItem = -1;
474
475 // Cached renderer properties
476 Color m_itemsBackgroundColorCached;
477 Color m_itemsBackgroundColorHoverCached;
478 Color m_selectedItemsBackgroundColorCached;
479 Color m_selectedItemsBackgroundColorHoverCached;
480 };
481} // namespace tgui
482
483#endif // TGUI_PANEL_LIST_BOX_HPP
Wrapper for colors.
Definition Color.hpp:63
Class to store the position or size of a widget.
Definition Layout.hpp:320
Class to store the left, top, width or height of a widget.
Definition Layout.hpp:100
Definition PanelListBoxRenderer.hpp:35
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition PanelListBox.hpp:44
bool removeItemById(const String &id)
Removes the item that were added with the given id.
bool setSelectedItemById(const String &id)
Selects an item in the panel list box.
PanelListBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< PanelListBox > Ptr
Shared widget pointer.
Definition PanelListBox.hpp:41
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
int getSelectedItemIndex() const
Gets the index of the selected item.
bool setSelectedItem(const Panel::Ptr &panelPtr)
Selects an item in the panel list box.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
Layout getItemsWidth() const
Returns the width of the items in the list box.
String getIdByIndex(std::size_t index) const
Returns the id of the item at the given index.
std::vector< String > getItemIds() const
Returns a vector of stored items ids.
Panel::Ptr addItem(const String &id={}, int index=-1)
Adds an item to the list.
Panel::Ptr getItemByIndex(std::size_t index) const
Returns the item name of the item at the given index.
bool containsId(const String &id) const
Returns whether the panel list box contains an item with the given id.
PanelListBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static Ptr copy(const ConstPtr &panelListBox)
Makes a copy of another panel list box.
Panel::Ptr getPanelTemplate() const
Get panel template from which new elements are created.
Panel::Ptr getSelectedItem() const
Returns the currently selected item.
std::size_t getItemCount() const
Returns the amount of items in the panel list box.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void setItemsHeight(const Layout &height)
Changes the height of the items in the list box.
std::size_t getMaximumItems() const
Returns the maximum items that the list box can contain.
Layout getItemsHeight() const
Returns the height of the items in the list box.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void clearItemStyle(int item) const
std::vector< Panel::Ptr > getItems() const
Returns a copy of the items in the list box.
SignalPanelListBoxItem onItemSelect
An item was selected in the list box. Optional parameter: selected item or its index.
Definition PanelListBox.hpp:456
void setMaximumItems(std::size_t maximumItems=0)
Changes the maximum items that the list box can contain.
std::shared_ptr< const PanelListBox > ConstPtr
Shared constant widget pointer.
Definition PanelListBox.hpp:42
String getSelectedItemId() const
Gets the id of the selected item.
static Ptr create()
Creates a new panel list box widget.
float getItemsHeightUpToIndex(std::size_t index) const
void deselectItem()
Deselects the selected item.
Panel::Ptr getItemById(const String &id) const
Returns the item name of the item with the given id.
bool removeItem(const Panel::Ptr &panelPtr)
Removes the item from the panel list with the given pointer.
bool removeItemByIndex(std::size_t index)
Removes the item from the panel list box.
bool contains(const Panel::Ptr &panelPtr) const
Returns whether the panel list box contains the given item.
int getIndexByItem(const Panel::Ptr &panelPtr) const
Returns the index of the given item.
int getHoveredItemIndex() const
Gets the index of the item below the mouse cursor.
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.
void setSize(const Layout2d &size) override
Changes the size of the panel list box.
void removeAllItems()
Removes all items from the list.
bool setSelectedItemByIndex(std::size_t index)
Selects an item in the panel list box.
int getIndexById(const String &id) const
Returns the index of the item with the given id.
std::shared_ptr< Panel > Ptr
Shared widget pointer.
Definition Panel.hpp:41
static Panel::Ptr create(const Layout2d &size={"100%", "100%"})
Creates a new panel widget.
void setSize(const Layout2d &size) override
Changes the size of the panel.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:627
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:94
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Definition PanelListBox.hpp:463