summaryrefslogtreecommitdiff
path: root/src/gui/position.hpp
blob: a905348cd0ed019f969c5268f374e3ab5db5b3be (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
////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018 Leo Tenenbaum
// This file is part of GraphColoring.
//
// GraphColoring 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.
//
// GraphColoring 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 GraphColoring.  If not, see <https://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////


// This position class can be used for relative positions, like
// (30% of parent width + 5 pixels, 20% of parent height - 20 pixels)

#ifndef GRAPHCOLORING_GUI_POSITION_H_
#define GRAPHCOLORING_GUI_POSITION_H_

#include <string>

namespace gui {

class Position {
public:
	typedef Position Size; // "Size" alias for Positions that really refer to sizes.
	enum class Alignment
	{
		LEFT,
		CENTER,
		RIGHT,
		TOP = LEFT,
		BOTTOM = RIGHT
	};
	Position(int x = 0, int y = 0, double relx = 0, double rely = 0,
		const Size* parent_size = nullptr, const Position* parent_pos
		= nullptr);
	virtual ~Position() {}
	int X() const;
	int Y() const;
	void SetX(int x);
	void SetY(int y);
	void SetPos(int x, int y);
	void SetRel(double relx, double rely);
	void SetParent(const Size* parent_size = nullptr,
		const Position* parent_pos = nullptr);
	int AlignedX(Alignment horizontal_align, int width)  const;
	int AlignedY(Alignment vertical_align,   int height) const;
	int x;
	int y;
	double relx;
	double rely;
	const Size* parent_size;
	const Position* parent_pos;
private:
	static int AlignAxis(Alignment alignment, int val, int size);
	friend std::ostream& operator<<(std::ostream& os, Position pos);
};

extern std::ostream& operator<<(std::ostream& os, Position pos);

typedef Position::Alignment Alignment;
typedef Position::Size Size;

} // namespace gui

#endif // GRAPHCOLORING_GUI_POSITION_H_