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