nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
MenuItemTextlist.cpp
1#include <Interface/Menu/MenuItemTextlist.hpp>
2#include <Misc/Utils.hpp>
3#include <Config/Globals.hpp>
4#include <Flow/InputManager.hpp>
5
6#include <stdexcept>
7
8MenuItemTextlist::MenuItemTextlist(std::string label, int id, std::vector<std::string> options, std::string initial):
9 MenuItem(label, id),
10 options(options),
11 currentIndex(-1)
12{
13 this->type = MenuItem::TEXTLIST;
14
15 for (unsigned int i = 0; i < options.size(); ++i)
16 {
17 if (options[i] == initial)
18 {
19 this->currentIndex = i;
20 break;
21 }
22 }
23 if ((this->currentIndex == -1) || (options.empty()))
24 throw std::runtime_error("Invalid initial value for MenuItemTextlist");
25}
26void MenuItemTextlist::draw(Window* window, int x, int y, int width, bool hilite)
27{
28 std::string text = this->options[this->currentIndex];
29
30 // Will draw
31 // label text
32 // If not hilite.
33 // If hilite:
34 // label < text >
35 MenuItem::draw(window,
36 x,
37 y,
38 (width - text.size() - 5),
39 hilite);
40
41 int rightmost = x + width;
42
43 window->print(((hilite)?
44 "<":
45 "["),
46 rightmost - text.size() - 2,
47 y,
48 ((hilite)?
49 Globals::Theme::hilite_text:
50 Globals::Theme::text));
51
52 window->print(((hilite)?
53 ">":
54 "]"),
55 rightmost - 1,
56 y,
57 ((hilite)?
58 Globals::Theme::hilite_text:
59 Globals::Theme::text));
60
61 window->print(text,
62 rightmost - text.size() - 1,
63 y,
64 Globals::Theme::hilite_text);
65}
67{
68 if (InputManager::noKeyPressed())
69 return;
70
71 if (InputManager::isPressed("left") || // user-defined
72 InputManager::isPressed(KEY_LEFT))
73 this->decrease();
74
75 else if (InputManager::isPressed("right") ||
76 InputManager::isPressed(KEY_RIGHT))
77 this->increase();
78
79 else if (InputManager::isPressed('r') ||
80 InputManager::isPressed('R') ||
81 InputManager::isPressed(' '))
82 this->reset();
83}
84void MenuItemTextlist::increase()
85{
86 this->currentIndex++;
87
88 if (this->currentIndex >= (int)this->options.size())
89 this->currentIndex = 0;
90}
91
92void MenuItemTextlist::decrease()
93{
94 this->currentIndex--;
95
96 if (this->currentIndex < 0)
97 this->currentIndex = (this->options.size() - 1);
98}
99void MenuItemTextlist::reset()
100{
101 this->currentIndex = 0;
102}
103std::string MenuItemTextlist::currentText()
104{
105 return this->options[this->currentIndex];
106}
107
void handleInput()
Makes the menu item react to input, as seen on the global InputManager.
void draw(Window *window, int x, int y, int width, bool hilite)
Shows this item at #x, #y with #width.
A segment of the terminal screen (2D char matrix).
Definition Window.hpp:17
void print(std::string str, int x, int y, ColorPair pair=0)
Shows text #str at x y on the window with color #pair.
Definition Window.cpp:94
Simplest type of item possible, with a label and user-defined id.
Definition MenuItem.hpp:12
virtual void draw(Window *window, int x, int y, int width, bool hilite=false)
Shows this item at #x, #y with #width.
Definition MenuItem.cpp:12