1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_CLIENT_UI_LISTBOX_H
4#define GAME_CLIENT_UI_LISTBOX_H
5
6#include "ui_scrollregion.h"
7
8struct CListboxItem
9{
10 bool m_Visible;
11 bool m_Selected;
12 CUIRect m_Rect;
13};
14
15// Instances of CListBox must be static, as member addresses are used as UI item IDs
16class CListBox : private CUIElementBase
17{
18private:
19 CUIRect m_ListBoxView;
20 CUIRect m_RowView;
21 float m_ListBoxRowHeight;
22 int m_ListBoxItemIndex;
23 int m_ListBoxSelectedIndex;
24 int m_ListBoxNewSelected;
25 int m_ListBoxNewSelOffset;
26 bool m_ListBoxUpdateScroll;
27 int m_ListBoxNumItems;
28 int m_ListBoxItemsPerRow;
29 bool m_ListBoxItemSelected;
30 bool m_ListBoxItemActivated;
31 bool m_ScrollbarShown;
32 const char *m_pBottomText;
33 float m_FooterHeight;
34 float m_AutoSpacing;
35 CScrollRegion m_ScrollRegion;
36 vec2 m_ScrollOffset;
37 int m_BackgroundCorners;
38 float m_ScrollbarWidth;
39 float m_ScrollbarMargin;
40 bool m_HasHeader;
41 bool m_Active;
42
43protected:
44 CListboxItem DoNextRow();
45
46public:
47 CListBox();
48
49 void DoBegin(const CUIRect *pRect);
50 void DoHeader(const CUIRect *pRect, const char *pTitle, float HeaderHeight = 20.0f, float Spacing = 2.0f);
51 void DoAutoSpacing(float Spacing = 20.0f) { m_AutoSpacing = Spacing; }
52 void DoSpacing(float Spacing = 20.0f);
53 void DoFooter(const char *pBottomText, float FooterHeight = 20.0f); // call before DoStart to create a footer
54 void DoStart(float RowHeight, int NumItems, int ItemsPerRow, int RowsPerScroll, int SelectedIndex, const CUIRect *pRect = nullptr, bool Background = true, int BackgroundCorners = IGraphics::CORNER_ALL, bool ForceShowScrollbar = false);
55 void ScrollToSelected() { m_ListBoxUpdateScroll = true; }
56 CListboxItem DoNextItem(const void *pId, bool Selected = false, float CornerRadius = 5.0f);
57 CListboxItem DoSubheader();
58 int DoEnd();
59
60 // Active state must be set before calling DoStart.
61 bool Active() const { return m_Active; }
62 void SetActive(bool Active) { m_Active = Active; }
63
64 bool WasItemSelected() const { return m_ListBoxItemSelected; }
65 bool WasItemActivated() const { return m_ListBoxItemActivated; }
66
67 bool ScrollbarShown() const { return m_ScrollbarShown; }
68 float ScrollbarWidth() const { return ScrollbarShown() ? ScrollbarWidthMax() : 0.0f; }
69 float ScrollbarWidthMax() const { return m_ScrollbarWidth; }
70 void SetScrollbarWidth(float Width) { m_ScrollbarWidth = Width; }
71 float ScrollbarMargin() const { return m_ScrollbarMargin; }
72 void SetScrollbarMargin(float Margin) { m_ScrollbarMargin = Margin; }
73};
74
75#endif
76