nsnake
Classic snake game for the terminal
Dialog.cpp
1 #include <Interface/Dialog.hpp>
2 #include <Interface/Layout.hpp>
3 #include <Interface/Menu/Menu.hpp>
4 #include <Interface/Ncurses.hpp>
5 #include <Config/Globals.hpp>
6 #include <Flow/InputManager.hpp>
7 #include <Misc/Utils.hpp>
8 
9 #include <vector>
10 #include <algorithm>
11 
12 void Dialog::show(std::string message, bool pressAnyKey)
13 {
14  std::vector<std::string> message_lines = Utils::String::split(message, '\n');
15 
16  // The dialog needs to wrap around this text. So we need...
17  int message_width = 0; // ...the char count of the widest line and...
18  int message_height = 0; // ...the number of lines of the whole message
19 
20  message_height = message_lines.size();
21 
22  for (size_t i = 0; i < message_lines.size(); i++)
23  message_width = std::max(message_width, (int)message_lines[i].size());
24 
25  // Now, to the size and position of the actual Dialog.
26  // Making it centered on the screen
27  int window_x = Layout::screenWidth /2 - (message_width + 2)/2;
28  int window_y = Layout::screenHeight/2 - (message_height) /2;
29  int window_width = message_width + 2; // left/right borders
30  int window_height = message_height + 2; // top/bottom borders
31 
32  Window dialog(window_x, window_y, window_width, window_height);
33 
34  if (Globals::Screen::show_borders)
35  {
36  dialog.borders(Globals::Screen::fancy_borders ?
37  Window::BORDER_FANCY :
38  Window::BORDER_REGULAR);
39  }
40 
41  // Before showing anything on the screen we must
42  // call `refresh()`, to... well, refresh the
43  // main screen buffer
44  refresh();
45 
46  // Show all lines, starting from (1, 1)
47  for (size_t i = 0; i < message_lines.size(); i++)
48  dialog.print(message_lines[i], 1, i + 1);
49 
50  dialog.refresh();
51  refresh();
52 
53  // Wait forever to get any key...
54  if (pressAnyKey)
56 }
57 
58 bool Dialog::askBool(std::string question, std::string title, bool default_value)
59 {
60  int windowx = Layout::screenWidth/2 - (question.size() + 12)/2;
61  int windowy = Layout::screenHeight/2 - 5/2;
62 
63  Window dialog(windowx,
64  windowy,
65  question.size() + 2 + 10, // borders + empty space
66  5);
67 
68  if (Globals::Screen::show_borders)
69  {
70  dialog.borders(Globals::Screen::fancy_borders ?
71  Window::BORDER_FANCY :
72  Window::BORDER_REGULAR);
73  }
74  if (! title.empty())
75  dialog.setTitle(title);
76 
77  dialog.refresh();
78 
79  Menu menu(1, 2, question.size() + 10, 2);
80 
81  std::vector<std::string> options;
82  options.push_back("Yes");
83  options.push_back("No");
84 
85  MenuItemTextlist* list = new MenuItemTextlist(question,
86  0,
87  options,
88  (default_value ?
89  "Yes" :
90  "No"));
91  menu.add(list);
92 
93  while (true)
94  {
95  // Drawing things
96  dialog.clear();
97 
98  menu.draw(&dialog);
99 
100  dialog.refresh();
101  refresh();
102 
103  // Getting input (waiting infinitely for it)
104  InputManager::update(-1);
105 
106  if (InputManager::isPressed("quit")) // user-defined
107  return false;
108 
109  menu.handleInput();
110 
111  if (InputManager::isPressed('\n') ||
112  InputManager::isPressed(KEY_ENTER))
113  {
114  std::string str(menu.getString(0));
115  return (str == "Yes");
116  }
117  }
118 
119  // Will never get here
120  return false;
121 }
122 
A segment of the terminal screen (2D char matrix).
Definition: Window.hpp:16
static int screenWidth
Full width of the terminal right now.
Definition: Layout.hpp:19
int getInput(int delay_ms=-1)
Returns a pressed character within a timespan of delay_ms (milliseconds).
Definition: Ncurses.cpp:37
A list of selectable text.
List of selectable items.
Definition: Menu.hpp:28
bool askBool(std::string question, std::string title="", bool default_value=false)
Spawns a Dialog box asking for a yes-or-no #question.
Definition: Dialog.cpp:58
void show(std::string message, bool pressAnyKey=false)
Shows a message on the screen.
Definition: Dialog.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
static int screenHeight
Full height of the terminal right now.
Definition: Layout.hpp:22