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