-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathui_console.cpp
220 lines (188 loc) · 5.3 KB
/
ui_console.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// DyLua: SimpleGraphic
// (c) David Gowor, 2014
//
// Module: UI Console
//
#include "ui_local.h"
#include <fmt/core.h>
#include <string_view>
// =============
// Configuration
// =============
#define UI_CONMOVETIME 100.0f // Movement time
// =====================
// ui_IConsole Interface
// =====================
class ui_console_c: public ui_IConsole, public conInputHandler_c {
public:
// Interface
void Toggle();
void Hide();
bool KeyEvent(int key, int type);
void Render();
// Encapsulated
ui_console_c(ui_main_c* ui);
ui_main_c* ui = nullptr;
sys_IMain* sys = nullptr;
r_IRenderer* renderer = nullptr;
conVar_c* con_fontSize = nullptr;
enum {
MODE_UP, // Up (hidden)
MODE_MV_DOWN, // Moving down
MODE_DOWN, // Down (shown)
MODE_MV_UP // Moving up
} mode;
int moveStart = 0;
char input[1024] = {};
int caret = 0;
void SetConInput(char* newInput, int newCaret);
};
ui_IConsole* ui_IConsole::GetHandle(ui_main_c* ui)
{
return new ui_console_c(ui);
}
void ui_IConsole::FreeHandle(ui_IConsole* hnd)
{
delete (ui_console_c*)hnd;
}
ui_console_c::ui_console_c(ui_main_c* ui)
: conInputHandler_c(ui->sys->con), ui(ui), sys(ui->sys), renderer(ui->renderer)
{
con_fontSize = sys->con->Cvar_Add("con_fontSize", CV_ARCHIVE|CV_CLAMP, "14", 12, 32);
mode = MODE_UP;
RefreshConInput();
}
// ===============
// Console Handler
// ===============
void ui_console_c::Toggle()
{
switch (mode) {
case MODE_UP:
// Start moving down
mode = MODE_MV_DOWN;
moveStart = sys->GetTime();
break;
case MODE_DOWN:
// Start moving up and reset buffer scroll position
mode = MODE_MV_UP;
moveStart = sys->GetTime();
sys->con->Scroll(CBSC_BOTTOM);
break;
default:
break;
}
}
void ui_console_c::Hide()
{
mode = MODE_UP;
}
bool ui_console_c::KeyEvent(int key, int type)
{
if (type == KE_KEYDOWN && (key == '`' || key == KEY_F1) && sys->IsKeyDown(KEY_CTRL)) {
Toggle();
return true;
}
if (mode == MODE_MV_DOWN || mode == MODE_DOWN) {
ConInputKeyEvent(key, type);
return true;
}
return false;
}
void ui_console_c::Render()
{
float uiw = (float)renderer->VirtualScreenWidth();
float uih = (float)renderer->VirtualScreenHeight();
// Find y position
float y = (float)floor(uih/2.0f);
int movetime = sys->GetTime() - moveStart;
switch (mode) {
case MODE_UP:
// Don't render when not visible at all
return;
case MODE_MV_DOWN:
if (movetime >= UI_CONMOVETIME) {
// Reached bottom
mode = MODE_DOWN;
} else {
// Still moving
y*= movetime / UI_CONMOVETIME;
}
break;
case MODE_MV_UP:
if (movetime >= UI_CONMOVETIME) {
// Reached top
mode = MODE_UP;
ClearConInput();
return;
} else {
// Still moving
y*= 1 - movetime / UI_CONMOVETIME;
}
break;
default:
break;
}
int fontSize = con_fontSize->intVal;
float backHeight = y + fontSize;
// Draw the background of the console
renderer->DrawColor(colorBlack);
renderer->DrawImage(NULL, 0, 0, uiw, backHeight);
renderer->DrawColor(0xFF007700);
renderer->DrawImage(NULL, 0, backHeight - fontSize / 2.0f, uiw, fontSize / 2.0f);
float basey = y - (float)fontSize;
float liney;
// Draw info strings
liney = basey;
renderer->DrawString(0, liney, F_RIGHT, fontSize, colorGreen, F_FIXED, CFG_VERSION);
liney-= fontSize;
int memTotal = lua_gc(ui->L, LUA_GCCOUNT, 0);
for (dword i = 0; i < ui->subScriptSize; i++) {
if (ui->subScriptList[i]) {
memTotal+= ui->subScriptList[i]->GetScriptMemory();
}
}
renderer->DrawStringFormat(0, liney, F_RIGHT, fontSize, colorWhite, F_FIXED, "%dkB used by Lua", memTotal);
#ifdef _MEMTRAK_H
liney-= fontSize;
renderer->DrawStringFormat(0, liney, F_RIGHT, fontSize, colorWhite, F_FIXED, "%dkB tracking overhead", _memTrak_getOverhead()>>10);
liney-= fontSize;
renderer->DrawStringFormat(0, liney, F_RIGHT, fontSize, colorWhite, F_FIXED, "%d allocations", _memTrak_getAllocCount());
liney-= fontSize;
renderer->DrawStringFormat(0, liney, F_RIGHT, fontSize, colorWhite, F_FIXED, "%dkB used by engine", _memTrak_getAllocSize()>>10);
#endif
// Draw the text lines
liney = basey - fontSize;
int index = -1;
const char* l;
while (liney >= 0 && (l = sys->con->EnumLines(&index))) {
renderer->DrawString(0, liney, F_LEFT, fontSize, colorWhite, F_FIXED, l);
liney-= fontSize;
}
// Generate input caret string
size_t caretPad = 0;
for (int c = 0; c < caret; c++) {
int escLen = IsColorEscape(&input[c]);
if (escLen) {
c+= escLen - 1;
} else {
++caretPad;
}
}
std::string caretStr(caretPad, ' ');
caretStr += "_";
// Draw prompt, input text, and caret
renderer->DrawString(0, basey, F_LEFT, fontSize, colorWhite, F_FIXED, "]");
renderer->DrawStringFormat(fontSize * 0.66f, basey, F_LEFT, fontSize, colorWhite, F_FIXED, "%s", input);
renderer->DrawString(fontSize * 0.66f, basey, F_LEFT, fontSize, colorWhite, F_FIXED, caretStr.c_str());
}
void ui_console_c::SetConInput(char* newInput, int newCaret)
{
std::string_view nextInput = newInput;
if (nextInput.size() >= 1024) {
nextInput = nextInput.substr(0, 1023);
}
memcpy(input, nextInput.data(), nextInput.size());
input[nextInput.size()] = '\0';
caret = newCaret; // TOOD(LV): bounds-check new caret?
}