4 #include "system/stacktrace.h"
7 #include "game/level.h"
8 #include "sdl/renderer.h"
9 #include "system/log.h"
10 #include "system/lt.h"
11 #include "system/nth_alloc.h"
12 #include "ui/console.h"
13 #include "ui/console_log.h"
14 #include "ui/edit_field.h"
15 #include "ui/history.h"
16 #include "math/extrema.h"
18 #define FONT_WIDTH_SCALE 3.0f
19 #define FONT_HEIGHT_SCALE 3.0f
21 #define CONSOLE_LOG_CAPACITY 10
22 #define HISTORY_CAPACITY 20
23 #define PROMPT_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT)
24 #define CONSOLE_LOG_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT * CONSOLE_LOG_CAPACITY)
26 #define CONSOLE_HEIGHT (CONSOLE_LOG_HEIGHT + PROMPT_HEIGHT)
28 #define SLIDE_DOWN_TIME 0.4f
30 #define CONSOLE_ALPHA (0.80f)
31 #define CONSOLE_BACKGROUND (rgba(0.20f, 0.20f, 0.20f, CONSOLE_ALPHA))
32 #define CONSOLE_FOREGROUND (rgba(0.80f, 0.80f, 0.80f, CONSOLE_ALPHA))
33 #define CONSOLE_ERROR (rgba(0.80f, 0.50f, 0.50f, CONSOLE_ALPHA))
42 Token token(const char *begin, const char *end)
44 Token t = {begin, end};
49 int token_equals_str(Token t, const char *s)
51 trace_assert(t.begin <= t.end);
52 size_t n1 = (size_t) (t.end - t.begin);
53 size_t n2 = strlen(s);
54 if (n1 != n2) return 0;
55 return memcmp(t.begin, s, n1) == 0;
59 Token token_nt(const char *s)
61 return token(s, s + strlen(s));
67 while (t->begin < t->end && isspace(*t->begin)) {
73 Token chop_word(Token *t)
76 const char *end = t->begin;
77 while (end < t->end && !isspace(*end)) {
80 Token result = token(t->begin, end);
88 Edit_field *edit_field;
89 Console_Log *console_log;
95 /* TODO(#356): Console does not support autocompletion */
97 Console *create_console(Game *game)
101 Console *console = PUSH_LT(lt, nth_calloc(1, sizeof(Console)), free);
102 if (console == NULL) {
107 console->edit_field = PUSH_LT(
110 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
113 if (console->edit_field == NULL) {
117 console->console_log = PUSH_LT(
120 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
121 CONSOLE_LOG_CAPACITY),
122 destroy_console_log);
126 console->history = PUSH_LT(
128 create_history(HISTORY_CAPACITY),
130 if (console->history == NULL) {
134 console->game = game;
139 void destroy_console(Console *console)
141 trace_assert(console);
142 RETURN_LT0(console->lt);
145 static int console_eval_input(Console *console)
147 const char *input_text = edit_field_as_text(console->edit_field);
149 Token input = token_nt(input_text);
150 Token command = chop_word(&input);
152 if (token_equals_str(command, "")) {
153 edit_field_clean(console->edit_field);
157 if (history_push(console->history, input_text) < 0) {
161 if (console_log_push_line(console->console_log, input_text, NULL, CONSOLE_FOREGROUND) < 0) {
165 if (token_equals_str(command, "load")) {
166 Token level = chop_word(&input);
167 console_log_push_line(console->console_log, "Loading level:", NULL, CONSOLE_FOREGROUND);
168 console_log_push_line(console->console_log, level.begin, level.end, CONSOLE_FOREGROUND);
169 char level_name[256];
170 memset(level_name, 0, 256);
171 memcpy(level_name, level.begin, min_size_t((size_t)(level.end - level.begin), 255));
173 if (game_load_level(console->game, level_name) < 0) {
174 console_log_push_line(console->console_log, "Could not load level", NULL, CONSOLE_ERROR);
176 } else if (token_equals_str(command, "menu")) {
177 console_log_push_line(console->console_log, "Loading menu", NULL, CONSOLE_FOREGROUND);
178 game_switch_state(console->game, GAME_STATE_LEVEL_PICKER);
180 console_log_push_line(console->console_log, "Unknown command", NULL, CONSOLE_ERROR);
183 edit_field_clean(console->edit_field);
188 int console_handle_event(Console *console,
189 const SDL_Event *event)
191 switch(event->type) {
193 switch(event->key.keysym.sym) {
195 return console_eval_input(console);
200 history_current(console->history));
201 history_prev(console->history);
205 if (event->key.keysym.mod & KMOD_CTRL) {
207 console->edit_field, history_current(console->history));
208 history_prev(console->history);
214 if (event->key.keysym.mod & KMOD_CTRL) {
215 console_log_clear(console->console_log);
223 history_current(console->history));
224 history_next(console->history);
228 if (event->key.keysym.mod & KMOD_CTRL) {
230 console->edit_field, history_current(console->history));
231 history_next(console->history);
239 return edit_field_event(console->edit_field, event);
242 int console_render(const Console *console,
243 const Camera *camera)
245 /* TODO(#364): console doesn't have any padding around the edit fields */
247 SDL_RenderGetViewport(camera->renderer, &view_port);
249 const float e = console->a * (2 - console->a);
250 const float y = -(1.0f - e) * CONSOLE_HEIGHT;
252 if (camera_fill_rect_screen(
257 CONSOLE_BACKGROUND) < 0) {
261 console_log_render(console->console_log,
265 if (edit_field_render_screen(console->edit_field,
267 vec(0.0f, y + CONSOLE_LOG_HEIGHT)) < 0) {
274 int console_update(Console *console, float delta_time)
276 trace_assert(console);
278 if (console->a < 1.0f) {
279 console->a += 1.0f / SLIDE_DOWN_TIME * delta_time;
281 if (console->a > 1.0f) {
289 void console_slide_down(Console *console)
291 trace_assert(console);