summaryrefslogtreecommitdiff
path: root/src/gui/components/GridTree.hpp
blob: 1b0a52a9e8cc801c71097679f65fc37c207a4ddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
    BasicWriter
    Copyright (C) 2014  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _GRID_TREE_H_
#define _GRID_TREE_H_

#include <vector>
#include <list>

namespace uigrid {

class GridTreeNode;

enum class Orientation
{
	none, horz, vert
};

struct Rect
{
	int x1, y1, x2, y2;

	Rect(int x1, int y1, int x2, int y2) :
		x1(x1), y1(y1), x2(x2), y2(y2)
	{
		// Do nothing here
	}

	static Rect bounds(int x, int y, int w, int h)
	{
		return Rect(x, y, x + w, y + h);
	}

	int w() const
	{
		return x2 - x1;
	}

	int h() const
	{
		return y2 - y1;
	}

};

struct FrameArea
{
	GridTreeNode *node;
	Rect r;

	FrameArea(GridTreeNode *node, const Rect &r) :
		node(node), r(r)
	{
		// Do nothing here
	}
};

struct Splitter
{
	GridTreeNode *node;
	Rect r;
	Orientation orientation;

	Splitter(GridTreeNode *node, const Rect &r, Orientation orientation) :
		node(node), r(r), orientation(orientation)
	{
		// Do nothing here
	}
};

class GridTreeNode
{

private:
	Orientation orientation;
	float relativeSize;
	GridTreeNode *parent;
	void *data;

	std::list<GridTreeNode*> children;

	void setParent(GridTreeNode *parent)
	{
		if (this->parent) {
			this->parent->removeChild(this);
		}
		if (parent) {
			parent->addChild(this);
		}
		this->parent = parent;
	}

	void removeChild(GridTreeNode *node)
	{
		for (auto it = children.begin(); it != children.end();) {
			if (*it == node) {
				it = children.erase(it);
			} else {
				it++;
			}
		}
	}

	void addChild(GridTreeNode *node)
	{
		children.push_back(node);
	}

public:

	/**
	 * Constructor of the GridTreeNode class.
	 *
	 * @param orientation describes the orientation of the children of
	 * this grid tree node.
	 * @param relativeSize contains the size of this node relative to the size
	 * of its parent node. The sum of the relativeSizes of all siblings has to
	 * be one.
	 * @param data is the data that should be attached to the node.
	 */
	GridTreeNode(Orientation orientation, float relativeSize = 1.0f,
			GridTreeNode *parent = nullptr, void *data = nullptr) :
		orientation(orientation), relativeSize(relativeSize), parent(nullptr),
		data(data)
	{
		setParent(parent);
	}

	~GridTreeNode()
	{
		// Delete all children
		for (auto c : children) {
			delete c;
		}
	}

	/**
	 * Returns true if this element of the grid tree is a "leaf" (e.g. this
	 * element has no children).
	 *
	 * @return true if the grid element is a leaf node, false otherwise.
	 */
	bool isLeaf() const
	{
		return children.empty();
	}

	/**
	 * Returns true if this element is the root node (has no parent).
	 *
	 * @return true if this element is the root node, false otherwise.
	 */
	bool isRoot() const
	{
		return parent == nullptr;
	}

	/**
	 * Sets the relative size of the node.
	 *
	 * @param relativeSize is the new relative size of the frame. Should be in
	 * an interval of [0, 1].
	 */
	void setRelativeSize(float relativeSize)
	{
		this->relativeSize = relativeSize;
	}

	/**
	 * Returns the current relative size of the node.
	 *
	 * @return the current relative size of the node.
	 */
	float getRelativeSize()
	{
		return relativeSize;
	}

	/**
	 * Returns the data that was attached to this grid tree node.
	 *
	 * @return the data that was attached to this grid tree node.
	 */
	void* getData()
	{
		return data;
	}

	/**
	 * Gathers the frame areas and the areas for which splitters should be
	 * drawn.
	 *
	 * @param areas is the list into which the frame area descriptors should be
	 * inserted.
	 * @param splitters is the list into which the splitter descriptors should
	 * be inserted. If nullptr is given, the list is not filled
	 * @param w is the width of the region for which the splitters should be
	 * gathered.
	 * @param h is the height of the region for which the splitters should be
	 * gathered.
	 */
	void gatherBoundingBoxes(std::vector<FrameArea> *areas,
			std::vector<Splitter> *splitters, const Rect &r,
			int splitterSize);

	/**
	 * Returns the position of the splitter with the given orientation for this
	 * element.
	 */
	Rect getSplitterPosition(Orientation orientation, const Rect &r);

};

}

#endif /* _GRID_TREE_H_ */