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