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