-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScreen.cpp
More file actions
182 lines (168 loc) · 5.04 KB
/
Screen.cpp
File metadata and controls
182 lines (168 loc) · 5.04 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Screen.cpp, Version 1.3 (9 Mar 2016)
#include<ncurses.h>
#include<panel.h>
#include<cstdlib>
#include<cstring>
#include<utility> //for std::pair
#include<stdexcept>
#include<unistd.h>
#include"goldchase.h"
#include"Screen.h"
// Utility function for fatal errors
void Screen::_two_second_error(const char* errstr)
{
attr_t attrs; //used in attr_(get|set)
short pair; //used in attr_(get|set)
attr_get(&attrs,&pair,NULL);//save terminal state
attron(COLOR_PAIR(c_error)|A_BLINK);
//Locate message in center of screen if room, else (0,0)
int maxlen,maxwid;
mvprintw(screenHeight>1 ? screenHeight/2 : 0, //y-coordinate for message
screenWidth>std::strlen(errstr) ? screenWidth/2-strlen(errstr)/2 : 0, //x-coordinate for message
errstr);
::refresh();
attr_set(attrs,pair,NULL);//restore terminal state
sleep(2);
}
//Screen ctor constructs a space with a box around it
Screen::Screen(int h, int w)
{
//First, call initialization functions
initscr(); // Start curses mode
start_color(); // We'll use color
cbreak();// Line buffering disabled, Pass everything to me
noecho();// Don't echo characters user types
curs_set(0); //make cursor invisible
keypad(stdscr, TRUE); //enable keypad arrows
//set up color schemes
init_pair(c_error, COLOR_WHITE, COLOR_RED);
init_pair(c_gold, COLOR_BLACK, COLOR_YELLOW);
// Interrogate our (physical) window's dimensions
int maxlen,maxwid;
std::pair<int,int> maxes=_getScreenSize();
screenHeight=maxes.first;
screenWidth=maxes.second;
// Abort if physical window is smaller than requested
// we subtract 2 because of border we need to draw
if(h > screenHeight-2 || w > screenWidth-2)
{
_two_second_error("WINDOW NOT LARGE ENOUGH!");
endwin();
throw std::runtime_error("window not large enough");
}
//outerWindow is +2 because it boxes L*W inner contents
WINDOW* outerWindow=newwin(h+2,w+2,0,0);;
new_panel(outerWindow);
box(outerWindow, 0, 0); //put frame around window (zeros mean use default chars)
innerWindow=newwin(h, w, 1, 1); //note it's offset by one to miss the outer box
panel=new_panel(innerWindow);
}
void Screen::notice(const char* msg)
{
const char* dismiss="Press spacebar to dismiss";
int greater=strlen(msg)>strlen(dismiss) ? strlen(msg) : strlen(dismiss);
int ycoord= screenHeight>1 ? screenHeight/2-2 : 0;
int xcoord=screenWidth>greater ? screenWidth/2-greater/2-1 : 0;
WINDOW* dialog=newwin(4,greater+2,ycoord,xcoord);
PANEL* dialog_panel=new_panel(dialog);
box(dialog,0,0);
mvwprintw(dialog,1,1+(greater-strlen(msg))/2,msg);
mvwprintw(dialog,2,1+(greater-strlen(dismiss))/2,dismiss);
panelRefresh();
do;
while(getch()!=' ');
del_panel(dialog_panel);
delwin(dialog);
panelRefresh();
}
int Screen::getOrdinal(const char* title, const std::vector<int>& nums)
{
if(nums.size() > screenHeight-2 || nums.size() > 10)
{
_two_second_error("too many numbers!");
return(nums.size() > 0 ? nums[0] : 0);
}
int titlewidth=strlen(title)+2 > 5 ? strlen(title)+2 : 5;
int ycoord= screenHeight/2-(nums.size()+2)/2;
int xcoord= screenWidth/2-titlewidth/2;
WINDOW* dialog=newwin(nums.size()+2,titlewidth,ycoord,xcoord);
PANEL* dialog_panel=new_panel(dialog);
box(dialog,0,0);
mvwprintw(dialog,0,titlewidth/2-strlen(title)/2,title);
for(int i=1; i<nums.size()+1; ++i)
{
char numAsStr[5];
sprintf(numAsStr,"%d",nums[i-1]);
mvwprintw(dialog,i,titlewidth/2-strlen(numAsStr)/2,numAsStr);
}
panelRefresh();
int keystroke;
bool valid=false;
do
{
keystroke=getch();
if(keystroke==KEY_BACKSPACE)
break;
int i=0;
for(int i=0; i<nums.size(); ++i)
if(nums[i]==keystroke-'0')
{
valid=true;
break;
}
} while(!valid);
del_panel(dialog_panel);
delwin(dialog);
panelRefresh();
return (keystroke==KEY_BACKSPACE) ? 0 : keystroke-'0';
}
std::string Screen::getText(void)
{
int greater=110<screenWidth-2 ? 110 : screenWidth-2;
int ycoord= screenHeight>1 ? screenHeight/2-2 : 0;
int xcoord=screenWidth>greater ? screenWidth/2-greater/2-1 : 0;
WINDOW* dialog=newwin(3,greater+2,ycoord,xcoord);
PANEL* dialog_panel=new_panel(dialog);
box(dialog,0,0);
wmove(dialog,1,1);
panelRefresh();
char str[111];
curs_set(1);
echo();
wgetnstr(dialog,str,greater);
noecho();
curs_set(0);
del_panel(dialog_panel);
delwin(dialog);
panelRefresh();
return(std::string(str));
}
Screen::~Screen()
{
notice("Exiting");
endwin();
}
void Screen::panelRefresh()
{
update_panels();
doupdate();
}
std::pair<int,int> Screen::_getScreenSize()
{
int x,y;
getmaxyx(stdscr,y,x);
return std::pair<int,int>(y,x);
}
void Screen::plot(int y, int x, chtype ch, unsigned int attr)
{
attr_t attr_save; //used in wattr_(get|set)
short pair; //used in wattr_(get|set)
wattr_get(innerWindow,&attr_save,&pair,NULL);//save terminal state
wattron(innerWindow,attr); //Turn on any attributes passed in
mvwaddch(innerWindow,y,x,ch); //Write out the character
wattr_set(innerWindow,attr_save,pair,NULL);//restore terminal state
}
int Screen::getKey()
{
return getch();
}