summaryrefslogtreecommitdiff
path: root/lib/nes/app/ui/screen-confirm-quit.cc
blob: 3c7a337f75f5cde522e747ea78b9c4228d8aae13 (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
#include "nes/app/ui/screen-confirm-quit.hh"
#include "nes/app/graphics/renderer.hh"
#include "nes/app/graphics/color.hh"
#include "nes/app/input/input-device-keyboard.hh"

namespace nes::app
{
	screen_confirm_quit::screen_confirm_quit(input_device_keyboard& keyboard)
		: keyboard_{ keyboard }
	{
	}

	auto screen_confirm_quit::render(renderer& renderer) -> void
	{
		renderer.render_rect(2, 8, 28, 10, color::background_secondary);
		renderer.render_border(2, 8, 28, 10, color::foreground_primary);

		auto const attrs = text_attributes{}
			.set_alignment(text_alignment::center)
			.set_ellipsize_mode(ellipsize_mode::end)
			.set_max_width(26);
		renderer.render_text(16, 10, "Are you sure?", color::foreground_primary, attrs);
		renderer.render_text(16, 12, "All progress is lost.", color::foreground_primary, attrs);
		if (confirm_)
		{
			renderer.render_text(10, 15, "Cancel", color::foreground_secondary);
			renderer.render_text(18, 15, "> OK <", color::accent_primary);
		}
		else
		{
			renderer.render_text(8, 15, "> Cancel <", color::accent_primary);
			renderer.render_text(20, 15, "OK", color::foreground_secondary);
		}
	}

	auto screen_confirm_quit::process_events() -> action
	{
		while (auto const event = keyboard_.poll_event())
		{
			// Use key-up so the enter key is not immediately captured by the game.
			if (event == input_event::key_up(key::enter))
			{
				if (confirm_)
				{
					return action::confirm_quit();
				}
				else
				{
					return action::cancel_quit();
				}
			}
			else if (event == input_event::key_down(key::space))
			{
				confirm_ = !confirm_;
			}
			else if (event == input_event::key_down(key::arrow_left))
			{
				confirm_ = false;
			}
			else if (event == input_event::key_down(key::arrow_right))
			{
				confirm_ = true;
			}
		}

		return action{};
	}
} // namespace nes::app