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 bool m_ListBoxDoneEvents;
28 int m_ListBoxNumItems;
29 int m_ListBoxItemsPerRow;
30 bool m_ListBoxItemSelected;
31 bool m_ListBoxItemActivated;
32 bool m_ScrollbarShown;
33 const char *m_pBottomText;
34 float m_FooterHeight;
35 float m_AutoSpacing;
36 CScrollRegion m_ScrollRegion;
37 vec2 m_ScrollOffset;
38 int m_BackgroundCorners;
39 float m_ScrollbarWidth;
40 float m_ScrollbarMargin;
41 bool m_HasHeader;
42 bool m_Active;
43
44protected:
45 CListboxItem DoNextRow();
46
47public:
48 CListBox();
49
50 void DoBegin(const CUIRect *pRect);
51 void DoHeader(const CUIRect *pRect, const char *pTitle, float HeaderHeight = 20.0f, float Spacing = 2.0f);
52 void DoAutoSpacing(float Spacing = 20.0f) { m_AutoSpacing = Spacing; }
53 void DoSpacing(float Spacing = 20.0f);
54 void DoFooter(const char *pBottomText, float FooterHeight = 20.0f); // call before DoStart to create a footer
55 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);
56 void ScrollToSelected() { m_ListBoxUpdateScroll = true; }
57 CListboxItem DoNextItem(const void *pId, bool Selected = false, float CornerRadius = 5.0f);
58 CListboxItem DoSubheader();
59 int DoEnd();
60
61 // Active state must be set before calling DoStart.
62 bool Active() const { return m_Active; }
63 void SetActive(bool Active) { m_Active = Active; }
64
65 bool WasItemSelected() const { return m_ListBoxItemSelected; }
66 bool WasItemActivated() const { return m_ListBoxItemActivated; }
67
68 bool ScrollbarShown() const { return m_ScrollbarShown; }
69 float ScrollbarWidth() const { return ScrollbarShown() ? ScrollbarWidthMax() : 0.0f; }
70 float ScrollbarWidthMax() const { return m_ScrollbarWidth; }
71 void SetScrollbarWidth(float Width) { m_ScrollbarWidth = Width; }
72 float ScrollbarMargin() const { return m_ScrollbarMargin; }
73 void SetScrollbarMargin(float Margin) { m_ScrollbarMargin = Margin; }
74};
75
76#endif
77