nsnake
Classic snake game for the terminal
MenuItemCheckbox.cpp
1 #include <Interface/Menu/MenuItemCheckbox.hpp>
2 #include <Config/Globals.hpp>
3 #include <Flow/InputManager.hpp>
4 
5 MenuItemCheckbox::MenuItemCheckbox(std::string label, int id, bool initial):
6  MenuItem(label, id),
7  checked(initial)
8 {
9  this->type = MenuItem::CHECKBOX; // placing it above wont work
10 }
11 
12 void MenuItemCheckbox::draw(Window* window, int x, int y, int width, bool hilite)
13 {
14  // Will draw
15  // label text
16  // If not hilite.
17  // If hilite:
18  // label < text >
19  MenuItem::draw(window, x, y, width - 9, hilite); // button width
20 
21  int posx = x + width;
22 
23  window->print(((hilite)?
24  "<":
25  "["),
26  posx - 8, y,
27  ((hilite)?
28  Globals::Theme::hilite_text:
29  Globals::Theme::text));
30 
31  window->print("ON", posx - 7, y, ((this->checked) ?
32  Globals::Theme::hilite_text:
33  Globals::Theme::text));
34 
35  window->print("|", posx - 5, y, Globals::Theme::text);
36 
37  window->print("OFF", posx - 4, y, ((this->checked) ?
38  Globals::Theme::text :
39  Globals::Theme::hilite_text));
40 
41  window->print(((hilite)?
42  ">":
43  "]"),
44  posx - 1, y,
45  ((hilite)?
46  Globals::Theme::hilite_text:
47  Globals::Theme::text));
48 }
50 {
51  if (InputManager::noKeyPressed())
52  return;
53 
54  if (InputManager::isPressed("left") || // user-defined
55  InputManager::isPressed(KEY_LEFT))
56  this->check(true);
57 
58  else if (InputManager::isPressed("right") || // user-defined
59  InputManager::isPressed(KEY_RIGHT))
60  this->check(false);
61 
62  else if (InputManager::isPressed(' ') ||
63  InputManager::isPressed('\n') ||
64  InputManager::isPressed(KEY_ENTER))
65  this->toggle();
66 }
67 void MenuItemCheckbox::check(bool option)
68 {
69  this->checked = option;
70 }
71 void MenuItemCheckbox::toggle()
72 {
73  this->checked = !(this->checked);
74 }
75 bool MenuItemCheckbox::isChecked()
76 {
77  return this->checked;
78 }
79 
MenuItemType type
Specific type of this widget.
Definition: MenuItem.hpp:51
A segment of the terminal screen (2D char matrix).
Definition: Window.hpp:16
void handleInput()
Makes the menu item react to input, as seen on the global InputManager.
Simplest type of item possible, with a label and user-defined id.
Definition: MenuItem.hpp:11
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
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
void draw(Window *window, int x, int y, int width, bool hilite=false)
Shows this item at #x, #y with #width.