-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.cpp
176 lines (144 loc) · 3.38 KB
/
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
#include "mastrix.hpp"
ClientConsoleVar<float> consoleSpeed("console_speed", 600);
ClientConsoleVar<float> consoleHeight("console_height", 200);
Console console;
const float console_margin_left = 3;
const float console_margin_bottom = 10;
const float fade_height = 10;
const float bottom_margin = 3;
const float text_height = 12;
const float line_margin = 4;
Color console_bgcolor(77,77,77, 180);
Color console_transparent(77,77,77, 0);
Color console_fade(100, 100, 100, 100); //fix this
Color console_cheap_line(255,255,255);
CLIENT_CONSOLE_COMMAND(toggleconsole)
{
console.toggle();
}
Console::Console()
{
cursor_pos = 0;
isDown = false;
position = 0;
pending = "";
}
void Console::toggle(void)
{
isDown = !isDown;
line = "";
if(isDown) this->enable();
}
void Console::println(const char *str)
{
history.push_front( std::string(str) );
}
void Console::printf(const char *fmt, ...)
{
char buf[512] = "";
va_list args;
va_start(args, fmt);
vsnprintf(buf, 512, fmt, args);
va_end(args);
char *pos = strtok(buf, "\n");
do {
if(pos)
history.push_front( std::string(pos) );
} while((pos = strtok(NULL, "\n")));
}
bool Console::timepass(void)
{
char k = 0;
if(isDown && position < consoleHeight) {
position += consoleSpeed*getRealDt();
} else if(!isDown && position > -fade_height) {
position -= consoleSpeed*getRealDt();
}
return false;
}
bool Console::handleEvent(SDL_Event *ev)
{
if(!isDown)
return false;
switch (ev->type)
{
case SDL_KEYDOWN:
if(ev->key.keysym.unicode > 0 && ev->key.keysym.unicode < 256)
keypress( ev->key.keysym.unicode );
return true;
case SDL_KEYUP:
return true;
default:
return false;
}
return false;
}
void Console::keypress(char key)
{
switch(key)
{
case '`': case'~':
toggle();
break;
case '\b':
line = line.substr(0, line.length()-1);
break;
case '\n': case '\r':
history.push_front(line);
pending += line;
line = "";
if(clientScripting.isCompleteCommand(pending)) {
command(pending);
pending = "";
} else {
pending += "\n";
}
break;
default:
line += key;
break;
}
}
void Console::command(std::string cmd)
{
clientScripting.command(cmd.c_str());
}
ClientConsoleVar<bool> cheapConsole("cheapconsole", false);
void Console::redraw(void)
{
// if(position <= 0) remove();
float draw_y = position - bottom_margin;
// Draw background
if(cheapConsole)
graphics.drawLine(
console_cheap_line,
0,
position,
graphics.getScreenWidth(),
position + fade_height);
else
{
glBegin(GL_QUADS);
graphics.setColor(console_bgcolor);
glVertex2f(0, 0);
glVertex2f(graphics.getScreenWidth(), 0);
glVertex2f(graphics.getScreenWidth(), position);
glVertex2f(0, position);
glVertex2f(0, position);
glVertex2f(graphics.getScreenWidth(), position);
graphics.setColor(console_transparent);
glVertex2f(graphics.getScreenWidth(), position+fade_height);
glVertex2f(0, position+fade_height);
glEnd();
}
// Draw current line
graphics.drawText(line.c_str(), console_margin_left, draw_y);
draw_y -= (text_height+line_margin);
// Draw history lines
for(historyList::iterator ii=history.begin(); ii!=history.end(); ii++) {
//GameX.DrawText(console_margin_left, draw_y, (char*)(*ii).c_str(), 230, 230, 255);
graphics.drawText((*ii).c_str(), console_margin_left, draw_y);
if(draw_y < 0) break;
draw_y -= text_height;
}
}