-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.cpp
More file actions
190 lines (148 loc) · 3.45 KB
/
UI.cpp
File metadata and controls
190 lines (148 loc) · 3.45 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
183
184
185
186
187
188
189
190
#include "UI.h"
#include <conio.h> //input polling
#include <iostream>
#include <utility> //pairs
#include <map>
using namespace std;
#define UP "up"
#define DOWN "down"
#define LEFT "left"
#define RIGHT "right"
#define ENTER "enter"
#define ESCAPE "escape"
#define COLORSCREEN "colorscreen"
#define DEFAULT "default"
#define QUIT "quit"
#define CONTINUE "continue"
map<string, pair<int, int>> dirMap {
{ UP, {-1, 0} }, // Moving up decreases row (Y-axis) I always get this shit fucked up god dammit
{ RIGHT, {0, 1} }, // Moving right increases column (X-axis)
{ DOWN, {1, 0} }, // Moving down increases row (Y-axis)
{ LEFT, {0, -1} } // Moving left decreases column (X-axis)
};
map <int, string> inputArrowMap = { //key scancode maps to direction of arrow key
{72, UP},
{77, RIGHT},
{80, DOWN},
{75, LEFT}
};
map <int, string> colorMap = {
{0, RED},
{1, ORANGE},
{2, YELLOW },
{3, GREEN},
{4, BLUE},
{5, MAGENTA},
{6, WHITE},
{7, BLACK},
{8, CYAN},
{9, GREY}
};
string UI::inputPoll() { //stops program and waits for input currently. Most likely this will be intended behavior.
int key = _getch();
if (key == 27) {
return ESCAPE;
}
if (key >= 48 && key <= 57) {
int value = key - 49;
return to_string(value);
}
int scanCode = _getch();
if (key == 224) { //arrow key processing
for (map<int, string>::iterator it = inputArrowMap.begin(); it != inputArrowMap.end(); ++it) {
if (it->first == scanCode) //224
return it->second;
}
}
else {
throw UI::InputError();
}
}
void UI::beginProgram()
{
bool quitting = false;
string input;
int anyKey;
while (!quitting)
{
system("cls");
printScreen();
try
{
input = inputPoll();
}
catch (UI::InputError e)
{
cout << e.what() << endl << "Press any key to continue..." << endl;;
anyKey = _getch();
continue;
}
try
{
processInput(input, quitting);
}
catch (Board::BoundsError e)
{
cout << e.what() << endl << "Press any key to continue..." << endl;
anyKey = _getch();
continue;
}
}
}
void UI::processInput(string inputEvent, bool& quitting)
{
if (inputEvent == ESCAPE) {
quit();
return;
}
if (inputEvent == UP || inputEvent == DOWN || inputEvent == LEFT || inputEvent == RIGHT) {
for (map<string, pair<int, int>>::iterator it = dirMap.begin(); it != dirMap.end(); ++it) {
if (inputEvent == it->first) {
pair<int, int> dir = it->second;
pixelBoard.moveCursor(dir.first, dir.second);
return;
}
}
}
else if (isdigit(inputEvent[0])) {
int keyVal = stoi(inputEvent);
auto it = colorMap.find(keyVal);
if (it != colorMap.end()) {
pixelBoard.getCurrPixel()->setColor(it->second);
}
else {
cout << "Error: color code nto found";
}
}
}
void UI::printScreen() {
//system("cls");
TextStructs.HeaderStruct.print();
cout << endl;
pixelBoard.printBoard();
cout << endl;
TextStructs.DefaultStruct.print();
}
bool UI::quit() {
system("cls");
TextStructs.QuitStruct.print();
bool inputValid = false;
while(!inputValid) {
string input;
try {
input = inputPoll();
}
catch (UI::InputError e) {
cout << e.what() << endl;
continue;
}
if (input == CONTINUE) {
return false;
}
else if (input == QUIT) {
return true;
}
inputValid = true;
}
return false;
}