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))
38 Edit_field *edit_field;
39 Console_Log *console_log;
45 /* TODO(#356): Console does not support autocompletion */
47 Console *create_console(Game *game)
51 Console *console = PUSH_LT(lt, nth_calloc(1, sizeof(Console)), free);
52 if (console == NULL) {
57 console->edit_field = PUSH_LT(
60 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
63 if (console->edit_field == NULL) {
67 console->console_log = PUSH_LT(
70 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
71 CONSOLE_LOG_CAPACITY),
76 console->history = PUSH_LT(
78 create_history(HISTORY_CAPACITY),
80 if (console->history == NULL) {
89 void destroy_console(Console *console)
91 trace_assert(console);
92 RETURN_LT0(console->lt);
95 static int console_eval_input(Console *console)
97 const char *input_text = edit_field_as_text(console->edit_field);
99 String input = string_nt(input_text);
100 String command = chop_word(&input);
102 if (string_equal(command, STRING_LIT(""))) {
103 edit_field_clean(console->edit_field);
107 if (history_push(console->history, input_text) < 0) {
111 if (console_log_push_line(console->console_log, input_text, NULL, CONSOLE_FOREGROUND) < 0) {
115 if (string_equal(command, STRING_LIT("load"))) {
116 String level = chop_word(&input);
117 console_log_push_line(console->console_log, "Loading level:", NULL, CONSOLE_FOREGROUND);
118 console_log_push_line(console->console_log, level.data, level.data + level.count, CONSOLE_FOREGROUND);
119 char level_name[256];
120 memset(level_name, 0, 256);
121 memcpy(level_name, level.data, min_size_t(level.count, 255));
123 if (game_load_level(console->game, level_name) < 0) {
124 console_log_push_line(console->console_log, "Could not load level", NULL, CONSOLE_ERROR);
126 } else if (string_equal(command, STRING_LIT("menu"))) {
127 console_log_push_line(console->console_log, "Loading menu", NULL, CONSOLE_FOREGROUND);
128 game_switch_state(console->game, GAME_STATE_LEVEL_PICKER);
130 console_log_push_line(console->console_log, "Unknown command", NULL, CONSOLE_ERROR);
133 edit_field_clean(console->edit_field);
138 int console_handle_event(Console *console,
139 const SDL_Event *event)
141 switch(event->type) {
143 switch(event->key.keysym.sym) {
145 return console_eval_input(console);
150 history_current(console->history));
151 history_prev(console->history);
155 if (event->key.keysym.mod & KMOD_CTRL) {
157 console->edit_field, history_current(console->history));
158 history_prev(console->history);
164 if (event->key.keysym.mod & KMOD_CTRL) {
165 console_log_clear(console->console_log);
173 history_current(console->history));
174 history_next(console->history);
178 if (event->key.keysym.mod & KMOD_CTRL) {
180 console->edit_field, history_current(console->history));
181 history_next(console->history);
189 return edit_field_event(console->edit_field, event);
192 int console_render(const Console *console,
193 const Camera *camera)
195 /* TODO(#364): console doesn't have any padding around the edit fields */
197 SDL_RenderGetViewport(camera->renderer, &view_port);
199 const float e = console->a * (2 - console->a);
200 const float y = -(1.0f - e) * CONSOLE_HEIGHT;
202 if (camera_fill_rect_screen(
207 CONSOLE_BACKGROUND) < 0) {
211 console_log_render(console->console_log,
215 if (edit_field_render_screen(console->edit_field,
217 vec(0.0f, y + CONSOLE_LOG_HEIGHT)) < 0) {
224 int console_update(Console *console, float delta_time)
226 trace_assert(console);
228 if (console->a < 1.0f) {
229 console->a += 1.0f / SLIDE_DOWN_TIME * delta_time;
231 if (console->a > 1.0f) {
239 void console_slide_down(Console *console)
241 trace_assert(console);