summaryrefslogtreecommitdiff
path: root/Menu.cpp
blob: 1fd8fa59cc075ed0e2ee5d85e5a4a283c7307f79 (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
#include "Menu.hpp"
#include "lzr.h"
#include "cfg.hpp"
#include "input.h"
#include <stdlib.h>

Menu::Menu() : _was_down(false), _hovered(-1), _selected(1)
{
}

void Menu::update(const Cursor &cursor)
{
	if (input_pressed(K_O)) {
		_selected += 1;
		_selected %= _entries;
	}
	if (cursor.x < 0 || cursor.x >= CFG_DWIDTH ||
	    cursor.y < _ybegin || cursor.y > _yend) {
		_hovered = -1;
		_was_down = cursor.down;
		return;
	}
	int x = cursor.x / _wentry;
	if (x > _entries)
		x = _entries;
	_hovered = x;
	if (cursor.down && !_was_down)
		_selected = _hovered;
	_was_down = cursor.down;
}

void Menu::draw() const
{
	LZR_DrawSetColor(0, 0, 0, 1);
	LZR_DrawRectangle(true, 0, _ybegin, CFG_DWIDTH, _yend - _ybegin);
	LZR_DrawSetColor(rand() & 1, rand() & 1, rand() & 1, rand() & 1);
	if ((rand() & 31) == 0)
		LZR_DrawRectangle(true, _selected * _wentry, _ybegin,
		                  _wentry, _yend - _ybegin + 1);
	if (_hovered > -1) {
		LZR_DrawSetColor(rand() & 1, rand() & 1, rand() & 1, 1);
		LZR_DrawRectangle(true, _hovered * _wentry, _ybegin,
		                  _wentry, _yend - _ybegin + 1);
	}
}

int Menu::get_selection() const
{
	return _selected;
}