forked from SFTtech/openage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.cpp
183 lines (148 loc) · 4.21 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
176
177
178
179
180
181
182
183
// Copyright 2013-2015 the openage authors. See copying.md for legal info.
#include "console.h"
#include "../log/log.h"
#include "../error/error.h"
#include "../util/strings.h"
#include "../util/unicode.h"
#include "draw.h"
namespace openage {
namespace console {
/*
* TODO:
* multiple terminals on screen
* resizable terminals
* scrollbars
* console input
* log console, command console
*/
Console::Console()
:
bottomleft{0, 0},
topright{1, 1},
charsize{1, 1},
visible(false),
buf{{80, 25}, 1337, 80},
font{{"DejaVu Sans Mono", "Book", 12}} {
termcolors.reserve(256);
// this better be representative for the width of all other characters
charsize.x = ceilf(font.get_advance_width("W"));
charsize.y = ceilf(font.get_line_height());
log::log(MSG(dbg) << "Console font character size: " << charsize.x << "x" << charsize.y);
// Adjust the corners of the console based on the default buffer size and char size
topright.x = charsize.x * buf.dims.x;
topright.y = charsize.y * buf.dims.y;
}
Console::~Console () {}
void Console::load_colors(std::vector<gamedata::palette_color> &colortable) {
for (auto &c : colortable) {
this->termcolors.emplace_back(c);
}
if (termcolors.size() != 256) {
throw Error(MSG(err) << "Exactly 256 terminal colors are required.");
}
}
void Console::register_to_engine(Engine *engine) {
engine->register_input_action(this);
engine->register_tick_action(this);
engine->register_drawhud_action(this);
engine->register_resize_action(this);
// TODO bind any needed input to InputContext
// Bind the console toggle key globally
auto &input = engine->get_input_manager();
auto &global = input.get_global_context();
global.bind(input::actions::TOGGLE_CONSOLE, [this, &input](const input::action_arg_t &) {
this->set_visible(!this->visible);
});
// toggle console will take highest priority
this->input_context.bind(input::actions::TOGGLE_CONSOLE, [this, &input](const input::action_arg_t &) {
this->set_visible(false);
});
this->input_context.bind(input::event_class::UTF8, [this](const input::action_arg_t &arg) {
// a single char typed into the console
std::string utf8 = arg.e.as_utf8();
this->buf.write(utf8.c_str());
command += utf8;
return true;
});
this->input_context.bind(input::event_class::NONPRINT, [this](const input::action_arg_t &arg) {
switch (arg.e.as_char()) {
case 8: // remove a single UTF-8 character
if (this->command.size() > 0) {
util::utf8_pop_back(this->command);
this->buf.pop_last_char();
}
return true;
case 13: // interpret command
this->buf.write('\n');
this->interpret(this->command);
this->command = "";
return true;
default:
return false;
}
});
this->input_context.utf8_mode = true;
}
void Console::set_visible(bool make_visible) {
Engine &e = Engine::get();
if (make_visible) {
e.get_input_manager().register_context(&this->input_context);
this->visible = true;
}
else {
e.get_input_manager().remove_context(&this->input_context);
this->visible = false;
}
}
void Console::write(const char *text) {
this->buf.write(text);
this->buf.write('\n');
}
void Console::interpret(const std::string &command) {
if (command == "exit") {
this->set_visible(false);
}
else if (command == "list") {
Engine &e = Engine::get();
for (auto &line : e.list_options()) {
this->write(line.c_str());
}
}
}
bool Console::on_tick() {
if (!this->visible) {
return true;
}
// TODO: handle stuff such as cursor blinking,
// repeating held-down keys
return true;
}
bool Console::on_drawhud() {
if (!this->visible) {
return true;
}
draw::to_opengl(this);
return true;
}
bool Console::on_input(SDL_Event *e) {
// only handle inputs if the console is visible
if (!this->visible) {
return true;
}
switch (e->type) {
case SDL_KEYDOWN:
//TODO handle key inputs
//do not allow anyone else to handle this input
return false;
break;
}
return true;
}
bool Console::on_resize(coord::window new_size) {
coord::pixel_t w = this->buf.dims.x * this->charsize.x;
coord::pixel_t h = this->buf.dims.y * this->charsize.y;
this->bottomleft = {(new_size.x - w) / 2, (new_size.y - h) / 2};
this->topright = {this->bottomleft.x + w, this->bottomleft.y - h};
return true;
}
}} // openage::console