GlistEngine
Loading...
Searching...
No Matches
gGUITreelist.h
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (c) 2014 Nitra Games Ltd., Istanbul, Turkey *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice should not be *
13 * deleted from the source form of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28
29/****************************************************************************
30 * Author: Sevval Bulburu, Aynur Dogan 2022-08-09 *
31 ****************************************************************************/
32
33#ifndef UI_GGUITREELIST_H_
34#define UI_GGUITREELIST_H_
35
36#include "gGUIScrollable.h"
37#include <deque>
38#include <iostream>
39#include "gImage.h"
40
41/*
42 * This class creats a tree list. Tree list is kind of a list that includes sub
43 * titles. All sub titles can have their own sub titles. There is not any edge
44 * for the numbers of sub titles. Uses a struct and its vector attribute which
45 * includes struct type of elements for storing the sub elements.
46 *
47 * The devolopers can make operations on the tree list.This operations are basically
48 * adding an object to the list, removing an object from the list, updating titles
49 * of the objects from the list.
50 *
51 * Developers can choose a title on the list. If they clicked on '>' symbol and
52 * the sub titles are not on the draw list, the parent element's sub vector's
53 * titles adding to the list and the list will be updating, sub titles will be
54 * shown. If the sub titles are shown on the list, draw list will be updating
55 * and sub titles will be disabled.
56 *
57 * Also there is an icon mode for the tree list. If developer uses the setIconType()
58 * function and actived the icons, '>' and '-' symbols will be 'Folder' and 'File'
59 * icons. This icons can change for all elements with using gGUIResources class
60 * or getting an image from outside.
61 *
62 * Developers can get the struct's attributes with getter functions.
63 */
65public:
66 struct Element {
68 std::string title;
69 std::deque<Element*> sub;
73 bool isicon;
77
79 parentlist = nullptr;
80 isicon = false;
81 isexpanded = false;
82 orderno = 0;
83 parent = nullptr;
84 isparent = false;
85 icon = nullptr;
86 isiconchanged = false;
87 title = "";
88 }
89
90 /*
91 * Used for print all the objects to the console in the struct.
92 */
93 void logTitle() {
94 if(parent!= nullptr) gLogi("Element") << title << (" iconid") << res.getIconNum();
95 for(int i = 0; i < sub.size(); i++) {
96 sub[i]->logTitle();
97 }
98 }
99
100 /*
101 * Used for clear the list which used for draw the titles before updating
102 * list.
103 */
105 parentlist->allsubtitles.clear();
106 parentlist->allorderno.clear();
107 parentlist->icons.clear();
108 }
109
110 /*
111 * Makes a list for drawing recursively. Updates the list when it's called.
112 * Operates when
113 * an element added to the list,
114 * an element removed from the list,
115 * making an update to the element's title,
116 * the user clicked to parent's title and opened the sub titles.
117 */
119 std::string linetext = "";
120 if(isicon == false) {
121 for(int i = 0; i < orderno; i++) linetext = " " + linetext;
122 if(sub.size() > 0) {
123 linetext = linetext + "> ";
124 isparent = true;
125 }
126 else linetext = linetext + "- ";
127 }
128 else{
129 for(int i = 0; i < orderno + 1; i++) linetext = " " + linetext;
130 parentlist->allorderno.push_back(orderno + 1);
131 parentlist->icons.push_back(this->icon);
132 if(sub.size() > 0) isparent = true;
133 }
134
135 linetext += title;
136 parentlist->allsubtitles.push_back(linetext);
137 if(isexpanded) for(int i = 0; i < sub.size(); i++) sub[i]->addSelfToList();
138 if(title == "Top") {
139 parentlist->allsubtitles.erase(parentlist->allsubtitles.begin() + 0);
140 if(parentlist->allorderno.size() > 0) parentlist->allorderno.erase(parentlist->allorderno.begin() + 0);
141 if(parentlist->icons.size() > 0) parentlist->icons.erase(parentlist->icons.begin() + 0);
142 }
143 }
144
145 /*
146 * Finds an element according to the titles. Compares the given title with
147 * the struct object's titles. When the titles are matched it returns the
148 * struct object which has the same title with the given. If there is no
149 * match then returns null.
150 *
151 * @param title is the string value which we want to compare with the
152 * struct object's title.
153 */
154 Element* findElement(std::string title) {
155 Element* tmp;
156 if(title == this->title) return this;
157 for(int i = 0; i < sub.size(); i++) {
158 tmp = sub[i]->findElement(title);
159 if(tmp) return tmp;
160 }
161 return nullptr;
162 }
163
164 /*
165 * Delete all the sub struct objects in the parent element's sub vectors.
166 * When it erase the sub vectors, it updates the number of total elements
167 * in the struct.
168 *
169 * @param element is the struct object which deleted from the list.
170 */
171 void removeSubElement(Element* element) {
172 if(element->sub.size() != 0) {
173 parentlist->nodenum -= element->sub.size();
174 for(int i = 0; i < element->sub.size(); i++) removeSubElement(element->sub[i]);
175 }
176 element->sub.clear();
177 }
178
179 /*
180 * Delete the given struct object from the list. Before it erase the
181 * element, it calls the removeSubElement function and erase all the sub
182 * elements then delete the given element.
183 *
184 * @param element is the struct object which deleted from the list.
185 */
186 void removeElement(Element* element) {
187 removeSubElement(element);
188 parentlist->nodenum -= 1;
189 int index = 0;
190 for(auto i: element->parent->sub) {
191 if(i!= element) {
192 index++;
193 }
194 else break;
195 }
196 element->parent->sub.erase(element->parent->sub.begin() + index);
197 }
198
199 /*
200 * Sets the isicon attributes of all elements.
201 */
202 void setIconType(bool isicon) {
203 this->isicon = isicon;
204 for(int i = 0; i < sub.size(); i++) sub[i]->setIconType(isicon);
205 }
206
207 /*
208 * Sets the icon attributes of all elements. This function uses when
209 * isicon value of the elements is true.
210 * Sets the icons according to their isparent attributes. If the element
211 * has got a sub vector, it's icon will be the 'Folder icon'. If it has
212 * not got a sub vector, it's icon will be the 'File icon'.
213 *
214 */
215 void setIcon() {
216 if(sub.size() > 0) this->icon = res.getIconImage(gGUIResources::ICON_FOLDER);
217 else this->icon = res.getIconImage(gGUIResources::ICON_FILE);
218 for(int i = 0; i < sub.size(); i++) sub[i]->setIcon();
219 }
220 };
221
222
224 virtual ~gGUITreelist();
225
226
227 void set(gBaseApp* root, gBaseGUIObject* topParentGUIObject, gBaseGUIObject* parentGUIObject, int parentSlotLineNo, int parentSlotColumnNo, int x, int y, int w, int h);
228
229 /*
230 * Delete the given struct object from the tree. Uses the struct's remove
231 * functions.
232 *
233 * @param element is the struct object which deleted from the list.
234 */
235 void removeElement(Element* element);
236
237 /*
238 * Used for add a struct object to the tree's sub vector.
239 * Developer should initialize the title first.
240 *
241 * @param element is the struct object which includes element's attributes.
242 */
243 void addElement(Element* element);
244
245 /*
246 * Used for add a sub element into the parent element's sub vector.
247 * Developer should initialize the title first. Element's orderno and the
248 * nodenum will be updates in the function.
249 *
250 * @param element is the struct object which includes element's attributes.
251 *
252 * @param parent is the struct object which element will be added to its
253 * sub vector.
254 */
256
257 /*
258 * Renamed title of the given element with the given text.
259 *
260 * @param element is the struct object that renamed on the list.
261 * @param newtitle is the string value which updated to the element's title.
262 */
263 void insertData(Element* element, std::string newtitle);
264
265 /*
266 * Uses for update the draw list and some values about the draw box.
267 */
269
270 void clear();
271
273
274 void mousePressed(int x, int y, int button);
275 void mouseReleased(int x, int y, int button);
276
277 /*
278 * Sets the color of the row which developer click and select, with the
279 * given color. Colors consist of RGB values float between 0.0f-1.0f.
280 *
281 * @param color The given color consist of (r, g, b) float values.
282 */
283 void setChosenColor(float r, float g, float b);
284
285 /*
286 * Sets the number of the lines that visible on the tree list. This function
287 * should be use before Treelist added to the panel with setControl function.
288 *
289 * @param linenumber is an integer value which define the number of minimum
290 * lines. Developer should give a number bigger than 0.
291 */
292 void setVisibleLineNumber(int linenumber);
293
294 /*
295 * Sets the icon type at the top of the titles. If developer wants to use new
296 * icons, should sets the isicon attribute. If the attribute is not setted,
297 * default icons will be '>' and '-'.
298 *
299 * @param isicon is the boolean value that sets the icon types. For using new
300 * icons, it should be 'true'.
301 */
302 void setIconType(bool isicon);
303
304 /*
305 * Sets the given icon to the given object of struct. Developer can give any
306 * image from outside. The image must be loaded before using as a parameter
307 * for the function. Image's width and height will be arrange automatically.
308 *
309 * @param icon is the image that developer wants to use.
310 *
311 * @param element is the struct object which the icon will be changed.
312 */
313 void setIcon(gImage* icon, Element* element);
314
315
316 /*
317 * Sets the given icon to the given object of the struct. Uses the icons in
318 * the gGUIResources class. This icons are storing in the base64.
319 *
320 * @param iconid is the icon number of the images in the gGUIResources class.
321 * This numbers are defined with enumeration method.
322 *
323 * @param element is the struct object which the icon will be changed.
324 */
325 void setIcon(int iconid, Element* element);
326
327 /*
328 * Sets the color of the icons with the given color. Colors consist of RGB
329 * values float between 0.0f-1.0f.
330 *
331 * @param color The given color consist of (r, g, b) float values.
332 */
333 void setIconsColor(float r, float g, float b);
334
335
336 /*
337 * Returns given struct object's title.
338 *
339 * @param element is the struct object which returned title's node.
340 */
341 std::string getTitle(Element* element);
342
343
344 /*
345 * Returns given struct object's title.
346 *
347 * @param No of the element to get it's title
348 */
349 std::string getTitle(int elementNo);
350
351
352 /*
353 * Returns selected struct object's title.
354 *
355 */
356 std::string getSelectedTitle();
357
358 /*
359 * Returns given struct object's node number. Node number is a static integer
360 * value. It contains the total number of the elements.
361 *
362 * @param element is the struct object which has the node number value.
363 */
365
366 /*
367 * Returns given struct object's orderno. Orderno is used for calculating the
368 * sub elements locations on the x axis on the list.
369 *
370 * @param element is the struct object which returned the orderno's node.
371 */
372 int getOrderno(Element* element);
373
374 /*
375 * Returns the given struct object's isexpanded value. If the sub titles of
376 * the element is on the list, it returns true. If is not, it returns false.
377 *
378 * @param element is the struct object which if it is isexpanded or not.
379 */
380 bool isExpanded(Element* element);
381
382 /*
383 * Returns the given struct object's isparent value. If given element is a
384 * parent, it returns true. If is not, it returns false.
385 *
386 * @param element is the struct object which if it is isparent or not.
387 */
388 bool isParent(Element* element);
389
390 /*
391 * Returns the isicon value of the struct object.
392 */
394
395 /*
396 * Returns the pointer of the image that uses for icons to the given struct
397 * object.
398 *
399 * @param element is the struct object which returned icon image.
400 */
402
403 /*
404 * Returns the color that when an element is chosen.
405 */
407
408 /*
409 * Returns the icons color.
410 */
412
413 /*
414 * Returns the number of the lines which is visible in the box.
415 */
417
418 /*
419 * Returns root element of the struct object.
420 */
422
423 void setSelectedLineNumber(int lineNo);
425
427 std::vector<std::string> allsubtitles;
428 std::vector<int> allorderno;
429 std::deque<gTexture*> icons;
430
431private:
432 int listboxh;
433 int lineh;
434 int visibilelinenum, minboxh;
435 int fldy, flno;
436 float textoffset;
437 int firstlineno;
438 int selectedno;
439 float arrowsize, spacesize;
440 bool mousepressedonlist;
441 Element topelement;
442 gColor chosencolor, iconcolor;
443 int iconx, iconw, iconh;
444
445private:
446 void updateTotalHeight();
447};
448
449#endif /* UI_GGUITREELIST_H_ */
Definition gBaseApp.h:16
Definition gBaseGUIObject.h:18
gBaseApp * root
Definition gBaseGUIObject.h:147
static gGUIResources res
Definition gBaseGUIObject.h:143
gBaseGUIObject * parent
Definition gBaseGUIObject.h:149
Definition gColor.h:17
gTexture * getIconImage(int iconId, bool isIconBig=false)
@ ICON_FOLDER
Definition gGUIResources.h:75
@ ICON_FILE
Definition gGUIResources.h:74
Definition gGUIScrollable.h:68
Definition gGUITreelist.h:64
void setVisibleLineNumber(int linenumber)
gTexture * getIcon(Element *element)
void setIconsColor(float r, float g, float b)
std::string getTitle(int elementNo)
std::vector< std::string > allsubtitles
Definition gGUITreelist.h:427
void addElement(Element *element, Element *parent)
int getNodenum()
void set(gBaseApp *root, gBaseGUIObject *topParentGUIObject, gBaseGUIObject *parentGUIObject, int parentSlotLineNo, int parentSlotColumnNo, int x, int y, int w, int h)
void setChosenColor(float r, float g, float b)
void mousePressed(int x, int y, int button)
void setIcon(gImage *icon, Element *element)
void drawContent()
int getVisibleLineNumber()
std::deque< gTexture * > icons
Definition gGUITreelist.h:429
Element * getRootElement()
int getSelectedLineNumber()
void setIcon(int iconid, Element *element)
void setIconType(bool isicon)
gColor getIconsColor()
void refreshList()
void mouseReleased(int x, int y, int button)
int nodenum
Definition gGUITreelist.h:426
void insertData(Element *element, std::string newtitle)
void setSelectedLineNumber(int lineNo)
bool isParent(Element *element)
void addElement(Element *element)
bool isExpanded(Element *element)
int getOrderno(Element *element)
void removeElement(Element *element)
bool getIconType()
std::string getSelectedTitle()
gColor getChosenColor()
virtual ~gGUITreelist()
std::string getTitle(Element *element)
std::vector< int > allorderno
Definition gGUITreelist.h:428
Definition gImage.h:34
Definition gUtils.h:271
Definition gTexture.h:17
float r
Definition gColor.h:22
float b
Definition gColor.h:22
float g
Definition gColor.h:22
Definition gGUITreelist.h:66
void logTitle()
Definition gGUITreelist.h:93
void setIconType(bool isicon)
Definition gGUITreelist.h:202
gGUITreelist * parentlist
Definition gGUITreelist.h:67
int orderno
Definition gGUITreelist.h:75
std::string title
Definition gGUITreelist.h:68
void removeElement(Element *element)
Definition gGUITreelist.h:186
void setIcon()
Definition gGUITreelist.h:215
bool isparent
Definition gGUITreelist.h:72
void removeSubElement(Element *element)
Definition gGUITreelist.h:171
Element * findElement(std::string title)
Definition gGUITreelist.h:154
bool isexpanded
Definition gGUITreelist.h:71
std::deque< Element * > sub
Definition gGUITreelist.h:69
bool isiconchanged
Definition gGUITreelist.h:74
void addSelfToList()
Definition gGUITreelist.h:118
Element()
Definition gGUITreelist.h:78
bool isicon
Definition gGUITreelist.h:73
gTexture * icon
Definition gGUITreelist.h:76
Element * parent
Definition gGUITreelist.h:70
void clearAllSubTitlesList()
Definition gGUITreelist.h:104