Neo  0.5.0
Developer Documentation
Tree.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014 (C) Yannick Pflanzer <neo-engine.de>
3  *
4  * This file is part of Neo2D.
5  *
6  * Neo2D is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Neo2D is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with Neo2D. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Diese Datei ist Teil von Neo2D.
20  *
21  * Neo2D ist Freie Software: Sie können es unter den Bedingungen
22  * der GNU Lesser General Public License, wie von der Free Software Foundation,
23  * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
24  * veröffentlichten Version, weiterverbreiten und/oder modifizieren.
25  *
26  * Neo2D wird in der Hoffnung, dass es nützlich sein wird, aber
27  * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
28  * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
29  * Siehe die GNU Lesser General Public License für weitere Details.
30  *
31  * Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit
32  *diesem
33  * Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
34  */
35 
36 #ifndef __TREE_H__
37 #define __TREE_H__
38 
39 #include <string>
40 #include <NeoEngine.h>
41 #include <Widget.h>
42 #include <Button.h>
43 #include <Label.h>
44 #include <vector>
45 
46 namespace Neo2D
47 {
48 namespace Gui
49 {
50 
55 template<class T> class TreeNode
56 {
57 protected:
58  T m_data;
59  bool m_opened;
60 
61  std::vector<TreeNode<T>*> m_children;
62 
63 public:
64 
70  TreeNode<T>* getChild(int idx) { return m_children[idx]; }
71 
78  {
79  TreeNode<T>* t = new TreeNode<T>(data);
80  m_children.push_back(t);
81  return t;
82  }
83 
88  size_t getNumChildren(){ return m_children.size(); }
89 
94  void clearChildren() { m_children.clear(); }
95 
100  T getData() { return m_data; }
101 
108  void setOpen(bool v) { m_opened = v; }
109 
114  bool isOpen() { return m_opened; }
115 
116  TreeNode(T d) : m_data(d), m_opened(false){}
117  TreeNode() {}
119 };
120 
121 #ifdef SWIG
122 // Some templates
123 %template(TreeModel) TreeNode<std::string>;
124 #endif
125 
126 using namespace Neo;
127 
134 class NEO2D_EXPORT TreeView : public Widget
135 {
136  friend class TreeNode<std::string>;
137 protected:
138 
142 
144 
157  void updateTree(TreeNode<std::string>* root, Vector2* level);
158 
170  void drawTree(TreeNode<std::string>* root, Vector2* level);
171 
172  TreeNode<std::string>* find(const char* name, TreeNode<std::string>* root);
173 
174 public:
175  TreeView(unsigned int x, unsigned int y, unsigned int width,
176  unsigned int height, const char* label)
177  : Widget(x, y, width, height, label),
178  m_label(nullptr),
179  m_selected(nullptr),
180  m_autoSize(true)
181  {
182  m_root.setOpen(true);
183  }
184 
185  ~TreeView() { clear(); }
186 
193  bool getAutoSize() { return m_autoSize; }
194 
199  void setAutoSize(bool b) { m_autoSize = b; }
200 
204  void show() { m_visible = true; }
205 
217  void clear(TreeNode<std::string>* root);
218 
223  void clear() { clear(&m_root); }
224 
229  TreeNode<std::string>* getTreeModel() { return &m_root; }
230 
235  const char* getSelected() { if(m_selected) return m_selected->getData().c_str(); return nullptr; }
236 
237  void selectEntry(const char* name);
238  void selectEntry(TreeNode<std::string>* node) { m_selected = node; }
239 
240  TreeNode<std::string>* find(const char* name);
241 
242  void draw(Vector2 offset);
243  void update();
244 
245  void setSize(Vector2 sz) { m_width = sz.x; if(!m_autoSize) m_height = sz.y; }
246 };
247 }
248 }
249 #endif
void clearChildren()
Clears the list of children. Attention: Does not delete any child data!
Definition: Tree.h:94
TreeNode< T > * getChild(int idx)
Returns the child that has the given index.
Definition: Tree.h:70
~TreeNode()
Definition: Tree.h:118
const char * getSelected()
Returns the currently selected node or nullptr if none is selected.
Definition: Tree.h:235
TreeView(unsigned int x, unsigned int y, unsigned int width, unsigned int height, const char *label)
Definition: Tree.h:175
Represents a single tree node.
Definition: Tree.h:55
void show()
Shows the tree.
Definition: Tree.h:204
float y
Definition: Vector2.h:36
void setAutoSize(bool b)
Configures the tree widget to resize itself according to its content.
Definition: Tree.h:199
The Label class displays a string on the screen.
Definition: Label.h:55
float x
Definition: Vector2.h:35
The Widget class contains all information that is common to all GUI widgets.
Definition: Widget.h:72
TreeNode< std::string > m_root
Definition: Tree.h:140
TreeNode()
Definition: Tree.h:117
bool m_autoSize
Definition: Tree.h:139
~TreeView()
Definition: Tree.h:185
Definition: Vector2.h:31
Definition: Button.h:42
T m_data
Definition: Tree.h:58
size_t getNumChildren()
Returns the number of children this node has.
Definition: Tree.h:88
void selectEntry(TreeNode< std::string > *node)
Definition: Tree.h:238
TreeNode< T > * addChild(T data)
Adds a child to the node.
Definition: Tree.h:77
TreeNode< std::string > * m_selected
Definition: Tree.h:141
Label * m_label
Definition: Tree.h:143
void clear()
Clears everything starting at the root node.
Definition: Tree.h:223
T getData()
Returns the data of this node.
Definition: Tree.h:100
bool m_opened
Definition: Tree.h:59
Implements a tree view which displays a multi-way tree of strings while providing callbacks for the s...
Definition: Tree.h:134
bool getAutoSize()
Checks if the tree widget is configured to resize itself according to its content.
Definition: Tree.h:193
void setSize(Vector2 sz)
Definition: Tree.h:245
std::vector< TreeNode< T > * > m_children
Definition: Tree.h:61
Definition: Color.h:29
TreeNode< std::string > * getTreeModel()
Returns the root node of this tree.
Definition: Tree.h:229
bool isOpen()
Checks if this node is open.
Definition: Tree.h:114
TreeNode(T d)
Definition: Tree.h:116
void setOpen(bool v)
Configures if this node is considered to be opened up. Important for the tree view to decide whether ...
Definition: Tree.h:108