nsnake
Classic snake game for the terminal
MenuItemTextbox.cpp
1 #include <Interface/Menu/MenuItemTextbox.hpp>
2 #include <Config/Globals.hpp>
3 #include <Flow/InputManager.hpp>
4 #include <Misc/Utils.hpp>
5 
6 MenuItemTextbox::MenuItemTextbox(std::string label, int id, int width, std::string initial):
7  MenuItem(label, id),
8  currentText(initial),
9  width(width)
10 {
11 
12 }
13 void MenuItemTextbox::draw(Window* window, int x, int y, int width, bool hilite)
14 {
15  // Drawing label before actual textbox
16  MenuItem::draw(window, x, y, width - (this->width) - 1, hilite);
17 
18  // Maximum x id
19  int posx = x + width;
20 
21  // Drawing the current text
22  if ((int)(this->currentText.size()) > (this->width))
23  window->print(this->currentText.substr(this->width), (posx - this->width), y, Globals::Theme::textbox);
24  else
25  window->print(this->currentText, (posx - this->width), y, Globals::Theme::textbox);
26 
27  int spacex = (posx - this->width) - this->currentText.size();
28 
29  // Drawing the rest of the spaces
30  for (unsigned int i = 0; i < (this->width - this->currentText.size()); i++)
31  window->printChar(' ', spacex + i, y, Globals::Theme::textbox);
32 }
33 
34 // Local function that tells in #input can be interpreted
35 // as a printable char (meaning no control character,
36 // as seen on the ASCII table).
37 bool isPrintable(int input)
38 {
39  return ((input > 32) && (input < 127));
40 }
41 
43 {
44  if (InputManager::noKeyPressed())
45  return;
46 
47  if (isPrintable(InputManager::pressedKey))
48  {
49  this->currentText += (char)(InputManager::pressedKey);
50  return;
51  }
52 
53  if (InputManager::isPressed(KEY_BACKSPACE))
54  Utils::String::pop_back(this->currentText);
55 }
56 
A segment of the terminal screen (2D char matrix).
Definition: Window.hpp:16
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 printChar(int c, int x, int y, ColorPair pair=0)
Shows #c at #x #y with color #pair.
Definition: Window.cpp:105
void handleInput()
Makes the menu item react to input, as seen on the global InputManager.
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.