Luanti 5.16.0-dev
Loading...
Searching...
No Matches
guiTable.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <map>
8#include <set>
9#include <string>
10#include <vector>
11
12#include <IGUIElement.h>
13#include <SColor.h>
14
15namespace gui {
16 class IGUIFont;
17}
18
19namespace video {
20 class ITexture;
21}
22
24class GUIScrollBar;
25
26/*
27 A table GUI element for GUIFormSpecMenu.
28
29 Sends a EGET_TABLE_CHANGED event to the parent when
30 an item is selected or double-clicked.
31 Call checkEvent() to get info.
32
33 Credits: The interface and implementation of this class are (very)
34 loosely based on the Irrlicht classes CGUITable and CGUIListBox.
35 CGUITable and CGUIListBox are licensed under the Irrlicht license;
36 they are Copyright (C) 2002-2012 Nikolaus Gebhardt
37*/
38class GUITable : public gui::IGUIElement
39{
40public:
41 /*
42 Stores dynamic data that should be preserved
43 when updating a formspec
44 */
46 {
47 s32 selected = 0;
48 s32 scrollpos = 0;
49 s32 keynav_time = 0;
50 core::stringw keynav_buffer;
51 std::set<s32> opened_trees;
52 };
53
54 /*
55 An option of the form <name>=<value>
56 */
57 struct Option
58 {
59 std::string name;
60 std::string value;
61
62 Option(const std::string &name_, const std::string &value_) :
63 name(name_),
64 value(value_)
65 {}
66 };
67
68 /*
69 A list of options that concern the entire table
70 */
71 typedef std::vector<Option> TableOptions;
72
73 /*
74 A column with options
75 */
77 {
78 std::string type;
79 std::vector<Option> options;
80 };
81 typedef std::vector<TableColumn> TableColumns;
82
83
84 GUITable(gui::IGUIEnvironment *env,
85 gui::IGUIElement *parent, s32 id,
86 core::rect<s32> rectangle,
88
89 virtual ~GUITable();
90
91 /* Split a string of the form "name=value" into name and value */
92 static Option splitOption(const std::string &str);
93
94 /* Set textlist-like options, columns and data */
95 void setTextList(const std::vector<std::string> &content,
96 bool transparent);
97
99 // Adds empty strings to end of content if there is an incomplete row
100 void setTable(const TableOptions &options,
101 const TableColumns &columns,
102 std::vector<std::string> &content);
103
104 /* Clear the table */
105 void clear();
106
107 /* Get info about last event (string such as "CHG:1:2") */
108 // Call this after EGET_TABLE_CHANGED
109 std::string checkEvent();
110
111 /* Get index of currently selected row (first=1; 0 if none selected) */
112 s32 getSelected() const;
113
114 /* Set currently selected row (first=1; 0 if none selected) */
115 // If given index is not visible at the moment, select its parent
116 // Autoscroll to make the selected row fully visible
117 void setSelected(s32 index);
118
120 virtual void setOverrideFont(gui::IGUIFont *font = nullptr);
121
123 virtual gui::IGUIFont *getOverrideFont() const;
124
125 /* Get selection, scroll position and opened (sub)trees */
127
128 /* Set selection, scroll position and opened (sub)trees */
129 void setDynamicData(const DynamicData &dyndata);
130
131 /* Must be called when position or size changes */
132 virtual void updateAbsolutePosition();
133
134 /* Irrlicht draw method */
135 virtual void draw();
136
137 /* Irrlicht event handler */
138 virtual bool OnEvent(const SEvent &event);
139
140protected:
148
149 struct Cell {
150 s32 xmin;
151 s32 xmax;
152 s32 xpos;
156 video::SColor color;
159 f32 image_scale; // only for "image" type columns
160 };
161
162 struct Row {
166 // visible_index >= 0: is index of row in m_visible_rows
167 // visible_index == -1: parent open but other ancestor closed
168 // visible_index == -2: parent closed
170 };
171
172 // Texture source
174
175 // Table content (including hidden rows)
176 std::vector<Row> m_rows;
177 // Table content (only visible; indices into m_rows)
178 std::vector<s32> m_visible_rows;
179 bool m_is_textlist = false;
180 bool m_has_tree_column = false;
181
182 // Selection status
183 s32 m_selected = -1; // index of row (1...n), or 0 if none selected
185 bool m_sel_doubleclick = false;
186
187 // Keyboard navigation stuff
189 core::stringw m_keynav_buffer = L"";
190
191 // Drawing and geometry information
192 bool m_border = true;
193 video::SColor m_color = video::SColor(255, 255, 255, 255);
194 video::SColor m_background = video::SColor(255, 0, 0, 0);
195 video::SColor m_highlight = video::SColor(255, 70, 100, 50);
196 video::SColor m_highlight_text = video::SColor(255, 255, 255, 255);
197 s32 m_rowheight = 1;
198 gui::IGUIFont *m_font = nullptr;
200
201 // Allocated strings and images
202 std::vector<core::stringw> m_strings;
203 std::vector<video::ITexture*> m_images;
204 std::map<std::string, s32> m_alloc_strings;
205 std::map<std::string, s32> m_alloc_images;
206
207 s32 allocString(const std::string &text);
208 s32 allocImage(const std::string &imagename);
209 void allocationComplete();
210
211 // Helper for draw() that draws a single cell
212 void drawCell(const Cell *cell, video::SColor color,
213 const core::rect<s32> &rowrect,
214 const core::rect<s32> &client_clip);
215
216 // Returns the i-th visible row (NULL if i is invalid)
217 const Row *getRow(s32 i) const;
218
219 // Key navigation helper
220 bool doesRowStartWith(const Row *row, const core::stringw &str) const;
221
222 // Returns the row at a given screen Y coordinate
223 // Returns index i such that m_rows[i] is valid (or -1 on error)
224 s32 getRowAt(s32 y, bool &really_hovering) const;
225
226 // Returns the cell at a given screen X coordinate within m_rows[row_i]
227 // Returns index j such that m_rows[row_i].cells[j] is valid
228 // (or -1 on error)
229 s32 getCellAt(s32 x, s32 row_i) const;
230
231 // Make the selected row fully visible
232 void autoScroll();
233
234 // Should be called when m_rowcount or m_rowheight changes
235 void updateScrollBar();
236
237 // Sends EET_GUI_EVENT / EGET_TABLE_CHANGED to parent
238 void sendTableEvent(s32 column, bool doubleclick);
239
240 // Functions that help deal with hidden rows
241 // The following functions take raw row indices (hidden rows not skipped)
242 void getOpenedTrees(std::set<s32> &opened_trees) const;
243 void setOpenedTrees(const std::set<s32> &opened_trees);
244 void openTree(s32 to_open);
245 void closeTree(s32 to_close);
246 // The following function takes a visible row index (hidden rows skipped)
247 // dir: -1 = left (close), 0 = auto (toggle), 1 = right (open)
248 void toggleVisibleTree(s32 row_i, int dir, bool move_selection);
249
250 // Aligns cell content in column according to alignment specification
251 // align = 0: left aligned, 1: centered, 2: right aligned, 3: inline
252 static void alignContent(Cell *cell, s32 xmax, s32 content_width,
253 s32 align);
254};
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:205
Definition guiScrollBar.h:16
void openTree(s32 to_open)
Definition guiTable.cpp:1175
virtual void setOverrideFont(gui::IGUIFont *font=nullptr)
Sets another skin independent font. If this is set to zero, the button uses the font of the skin.
Definition guiTable.cpp:583
s32 m_rowheight
Definition guiTable.h:197
GUIScrollBar * m_scrollbar
Definition guiTable.h:199
void setDynamicData(const DynamicData &dyndata)
Definition guiTable.cpp:620
gui::IGUIFont * m_font
Definition guiTable.h:198
std::map< std::string, s32 > m_alloc_images
Definition guiTable.h:205
bool doesRowStartWith(const Row *row, const core::stringw &str) const
Definition guiTable.cpp:994
void drawCell(const Cell *cell, video::SColor color, const core::rect< s32 > &rowrect, const core::rect< s32 > &client_clip)
Definition guiTable.cpp:708
void sendTableEvent(s32 column, bool doubleclick)
Definition guiTable.cpp:1089
s32 getRowAt(s32 y, bool &really_hovering) const
Definition guiTable.cpp:1011
s32 allocImage(const std::string &imagename)
Definition guiTable.cpp:965
static Option splitOption(const std::string &str)
Definition guiTable.cpp:77
virtual void updateAbsolutePosition()
Definition guiTable.cpp:635
core::stringw m_keynav_buffer
Definition guiTable.h:189
void autoScroll()
Definition guiTable.cpp:1065
DynamicData getDynamicData() const
Definition guiTable.cpp:608
std::vector< core::stringw > m_strings
Definition guiTable.h:202
virtual bool OnEvent(const SEvent &event)
Definition guiTable.cpp:765
void allocationComplete()
Definition guiTable.cpp:978
bool m_has_tree_column
Definition guiTable.h:180
void setTextList(const std::vector< std::string > &content, bool transparent)
Definition guiTable.cpp:87
s32 m_sel_column
Definition guiTable.h:184
ColumnType
Definition guiTable.h:141
@ COLUMN_TYPE_COLOR
Definition guiTable.h:144
@ COLUMN_TYPE_INDENT
Definition guiTable.h:145
@ COLUMN_TYPE_TEXT
Definition guiTable.h:142
@ COLUMN_TYPE_TREE
Definition guiTable.h:146
@ COLUMN_TYPE_IMAGE
Definition guiTable.h:143
std::map< std::string, s32 > m_alloc_strings
Definition guiTable.h:204
virtual ~GUITable()
Definition guiTable.cpp:65
s32 getCellAt(s32 x, s32 row_i) const
Definition guiTable.cpp:1033
void toggleVisibleTree(s32 row_i, int dir, bool move_selection)
Definition guiTable.cpp:1193
video::SColor m_highlight
Definition guiTable.h:195
video::SColor m_color
Definition guiTable.h:193
std::vector< Option > TableOptions
Definition guiTable.h:71
u64 m_keynav_time
Definition guiTable.h:188
std::vector< TableColumn > TableColumns
Definition guiTable.h:81
std::vector< Row > m_rows
Definition guiTable.h:176
s32 getSelected() const
Definition guiTable.cpp:530
s32 allocString(const std::string &text)
Definition guiTable.cpp:951
bool m_sel_doubleclick
Definition guiTable.h:185
bool m_border
Definition guiTable.h:192
GUITable(gui::IGUIEnvironment *env, gui::IGUIElement *parent, s32 id, core::rect< s32 > rectangle, ISimpleTextureSource *tsrc)
Definition guiTable.cpp:28
void setTable(const TableOptions &options, const TableColumns &columns, std::vector< std::string > &content)
Set generic table options, columns and content, calculate cell sizes.
Definition guiTable.cpp:147
std::vector< video::ITexture * > m_images
Definition guiTable.h:203
video::SColor m_background
Definition guiTable.h:194
void closeTree(s32 to_close)
Definition guiTable.cpp:1183
virtual gui::IGUIFont * getOverrideFont() const
Gets the override font (if any).
Definition guiTable.cpp:603
video::SColor m_highlight_text
Definition guiTable.h:196
std::vector< s32 > m_visible_rows
Definition guiTable.h:178
const Row * getRow(s32 i) const
Definition guiTable.cpp:986
s32 m_selected
Definition guiTable.h:183
void setOpenedTrees(const std::set< s32 > &opened_trees)
Definition guiTable.cpp:1114
static void alignContent(Cell *cell, s32 xmax, s32 content_width, s32 align)
Definition guiTable.cpp:1248
std::string checkEvent()
Definition guiTable.cpp:506
void setSelected(s32 index)
Definition guiTable.cpp:539
virtual void draw()
Definition guiTable.cpp:641
void updateScrollBar()
Definition guiTable.cpp:1078
bool m_is_textlist
Definition guiTable.h:179
ISimpleTextureSource * m_tsrc
Definition guiTable.h:173
void getOpenedTrees(std::set< s32 > &opened_trees) const
Definition guiTable.cpp:1103
void clear()
Definition guiTable.cpp:476
Definition texturesource.h:34
Definition fontengine.h:15
Definition clientmap.h:36
Definition guiTable.h:149
s32 content_index
Definition guiTable.h:154
bool color_defined
Definition guiTable.h:157
s32 reported_column
Definition guiTable.h:158
s32 xmax
Definition guiTable.h:151
video::SColor color
Definition guiTable.h:156
s32 xpos
Definition guiTable.h:152
f32 image_scale
Definition guiTable.h:159
s32 xmin
Definition guiTable.h:150
ColumnType content_type
Definition guiTable.h:153
s32 tooltip_index
Definition guiTable.h:155
Definition guiTable.h:46
s32 scrollpos
Definition guiTable.h:48
s32 keynav_time
Definition guiTable.h:49
s32 selected
Definition guiTable.h:47
core::stringw keynav_buffer
Definition guiTable.h:50
std::set< s32 > opened_trees
Definition guiTable.h:51
Definition guiTable.h:58
std::string name
Definition guiTable.h:59
Option(const std::string &name_, const std::string &value_)
Definition guiTable.h:62
std::string value
Definition guiTable.h:60
Definition guiTable.h:162
Cell * cells
Definition guiTable.h:163
s32 visible_index
Definition guiTable.h:169
s32 indent
Definition guiTable.h:165
s32 cellcount
Definition guiTable.h:164
Definition guiTable.h:77
std::string type
Definition guiTable.h:78
std::vector< Option > options
Definition guiTable.h:79
constexpr v3f x
Definition test_irr_matrix4.cpp:18
constexpr v3f y
Definition test_irr_matrix4.cpp:19