summaryrefslogtreecommitdiff
path: root/src/gui/menu.cpp
blob: 18bacb29378dd4e3a3ef3fb406436e7df2a52e49 (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
////////////////////////////////////////////////////////////////////////////////
// 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/>.
////////////////////////////////////////////////////////////////////////////////

#include "menu.hpp"

#include <iostream>

namespace gui {

Menu::Menu(Window* window,
	const std::vector<std::shared_ptr<Button>>& buttons_,
	Position position_, Size size_)
	: window(window), buttons(buttons_), position(position_), size(size_)
{
	int total_button_height = 0;
	for (std::shared_ptr<Button>& button : buttons)
	{
		total_button_height += button->GetHeight();
	}

	int number_of_buttons = buttons.size();
	double spacing_rel = 1.0 / (number_of_buttons + 1);
	double spacing_abs = (-total_button_height) / (number_of_buttons + 1);
	if (spacing_rel * size.Y() + spacing_abs < 0) // Window is very short; just give up.
	{
		spacing_rel = 0;
		spacing_abs = 0;
	}
	double y_abs = spacing_abs;
	double y_rel = spacing_rel;

	for (std::shared_ptr<Button>& button : buttons)
	{
		button->SetPosition(Position(0, y_abs, 0.5, y_rel, &size, &position));
		button->SetAlignment(Alignment::CENTER, Alignment::TOP);
		y_abs += spacing_abs + button->GetHeight();
		y_rel += spacing_rel;
	}
}

void Menu::SetCommand(int button, Button::callback_t command)
{
	buttons[button]->SetCommand(command);
}

void Menu::SetCommands(std::vector<Button::callback_t> commands)
{
	for (unsigned i = 0; i < buttons.size(); i++)
		SetCommand((int)i, commands[i]);
}


void Menu::Render()
{
	for (std::shared_ptr<Button>& button : buttons)
		button->Render();
}

} // namespace gui