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