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