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