This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatGUI.h
204 lines (185 loc) · 5.12 KB
/
ChatGUI.h
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#pragma once
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
WORD defColor;
void SetCurPos(int x, int y) {
SetConsoleCursorPosition(hStdOut, { (SHORT)x, (SHORT)y });
}
class GUI {
public:
int drawWindow(int x, int y, int width, int height, string title, WORD color = defColor, WORD borderColor = defColor) {
if (title.size() + 4 > width) width = title.size() + 4;
if (height < 2) height = 2;
SetConsoleTextAttribute(hStdOut, borderColor);
SetCurPos(x, y);
cout << "Ú";
SetCurPos(x + width - 1, y);
cout << "¿";
SetCurPos(x, y + height - 1);
cout << "À";
SetCurPos(x + width - 1, y + height - 1);
cout << "Ù";
SetCurPos(x + 3 + title.size(), y);
for (int i = 3 + title.size(); i < width - 1; i++) cout << "Ä";
SetCurPos(x + 1, y + height - 1);
for (int i = 1; i < width - 1; i++) cout << "Ä";
for (int i = 1; i < height - 1; i++) {
SetCurPos(x, y + i);
cout << "³";
SetCurPos(x + width - 1, y + i);
cout << "³";
}
SetCurPos(x + 2 + title.size(), y);
cout << "Ã";
SetConsoleTextAttribute(hStdOut, color);
SetCurPos(x + 2, y);
cout << title;
elements.push_back({ 0, x, y, width, height, title, "", "", NULL, color, borderColor });
SetConsoleTextAttribute(hStdOut, defColor);
return elements.size() - 1;
}
int drawButton(int x, int y, string caption, string key, WORD color = defColor, WORD borderColor = defColor) {
SetConsoleTextAttribute(hStdOut, borderColor);
SetCurPos(x, y);
cout << "[";
SetCurPos(x + 2 + key.size() - 1, y);
cout << "]";
SetConsoleTextAttribute(hStdOut, color);
SetCurPos(x + 1, y);
cout << key;
SetCurPos(x + 3 + key.size(), y);
cout << caption;
elements.push_back({ 1, x, y, 0, 0, "", caption, key, NULL, color, borderColor });
SetConsoleTextAttribute(hStdOut, defColor);
return elements.size() - 1;
}
int drawButtonO(int x, int y, string caption, string key, WORD color = defColor, WORD borderColor = defColor) {
int id = drawButton(x, y, caption, key, color, borderColor);
SetConsoleTextAttribute(hStdOut, FOREGROUND_RED);
SetCurPos(x + 4 + key.size() + caption.size(), y);
cout << "[®]";
SetConsoleTextAttribute(hStdOut, defColor);
elements[id].active = false;
return id;
}
int buttonOupd(int id) {
int _x = elements[id].x,
_y = elements[id].y;
string _key = elements[id].key,
_caption = elements[id].caption;
bool active = elements[id].active;
active = !active;
WORD color;
if (active) color = FOREGROUND_GREEN; else color = FOREGROUND_RED;
SetConsoleTextAttribute(hStdOut, color);
SetCurPos(_x + 4 + _key.size() + _caption.size(), _y);
cout << "[®]";
SetConsoleTextAttribute(hStdOut, defColor);
elements[id].active = active;
elements[id].type = 2;
return id;
}
int startInput(int x, int y, int width, int height, int key, int id = -1, WORD color = defColor) {
string caption;
int _x = x,
_y = y;
char ch = NULL;
if (id != -1) {
x = elements[id].x;
y = elements[id].y;
width = elements[id].width;
height = elements[id].height;
color = elements[id].color;
caption = elements[id].caption;
key = stoi(elements[id].key);
_x = x + caption.size() % width;
_y = y + caption.size() / width;
}
SetConsoleTextAttribute(hStdOut, color);
SetCurPos(_x, _y);
while (ch != key) {
ch = _getch();
if (ch >= 32 && ch <= 126) {
if (_x > x + width - 1) {
_y++;
_x = x;
}
if (_y < y + height) {
SetCurPos(_x, _y);
cout << ch;
caption += ch;
}
else {
_y--;
_x = x + width - 1;
}
_x++;
}
if (ch == 8) {
if (_x > x) {
SetCurPos(_x - 1, _y);
_x--;
}
else if (_y > y) {
SetCurPos(x + width - 1, _y - 1);
_x = x + width - 1;
_y--;
}
else ch = -1;
if (ch != -1) {
cout << " ";
caption = caption.substr(0, caption.size() - 1);
}
SetCurPos(_x, _y);
}
}
if (id == -1) {
elements.push_back({ 3, x, y, width, height, "", caption, to_string(key), NULL, color, defColor });
SetConsoleTextAttribute(hStdOut, defColor);
id = elements.size() - 1;
}
else {
elements[id].caption = caption;
}
return id;
}
private:
struct Element {
int type;
int x;
int y;
int width;
int height;
string title;
string caption;
string key;
bool active;
WORD color;
WORD borderColor;
};
vector<Element> elements;
};
VOID DrawChatGUI(VOID) {
GetConsoleScreenBufferInfo(hStdOut, &csbi);
defColor = csbi.wAttributes;
GUI gui;
system("cls");
gui.drawWindow(0, 0, 30, 10, "Buttons", FOREGROUND_GREEN, FOREGROUND_RED);
gui.drawWindow(30, 0, 30, 15, "Input", FOREGROUND_GREEN, FOREGROUND_RED);
gui.drawWindow(0, 10, 30, 5, "Window Name", FOREGROUND_GREEN, FOREGROUND_RED);
gui.drawButton(2, 3, "Exit", "ECS", defColor, FOREGROUND_RED);
int button = gui.drawButtonO(2, 4, "Input", "CTRL + I", defColor, FOREGROUND_RED);
int input = -1;
SetCurPos(0, 15);
char res = NULL;
while (res != 27) {
res = _getch();
if (res == 9) {
gui.buttonOupd(button);
if (input != -1) gui.startInput(0, 0, 0, 0, 0, input); else input = gui.startInput(31, 1, 28, 13, 9);
gui.buttonOupd(button);
SetCurPos(0, 15);
}
}
system("cls");
}