4 #include "ebisp/interpreter.h"
5 #include "ebisp/parser.h"
6 #include "ebisp/scope.h"
8 #include "game/level.h"
9 #include "game/level/player/rigid_rect.h"
10 #include "game/level_script.h"
11 #include "sdl/renderer.h"
12 #include "system/log.h"
13 #include "system/log_script.h"
14 #include "system/lt.h"
15 #include "system/nth_alloc.h"
16 #include "ui/console.h"
17 #include "ui/console_log.h"
18 #include "ui/edit_field.h"
19 #include "ui/history.h"
21 #define FONT_WIDTH_SCALE 3.0f
22 #define FONT_HEIGHT_SCALE 3.0f
24 #define CONSOLE_LOG_CAPACITY 10
25 #define HISTORY_CAPACITY 20
26 #define PROMPT_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT)
27 #define CONSOLE_LOG_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT * CONSOLE_LOG_CAPACITY)
29 #define CONSOLE_HEIGHT (CONSOLE_LOG_HEIGHT + PROMPT_HEIGHT)
31 #define SLIDE_DOWN_TIME 0.4f
33 #define CONSOLE_ALPHA (0.80f)
34 #define CONSOLE_BACKGROUND (rgba(0.20f, 0.20f, 0.20f, CONSOLE_ALPHA))
35 #define CONSOLE_FOREGROUND (rgba(0.80f, 0.80f, 0.80f, CONSOLE_ALPHA))
36 #define CONSOLE_ERROR (rgba(0.80f, 0.50f, 0.50f, CONSOLE_ALPHA))
38 #define CONSOLE_EVAL_RESULT_SIZE 256
45 Edit_field *edit_field;
46 Console_Log *console_log;
53 /* TODO(#355): Console does not support Emacs keybindings */
54 /* TODO(#356): Console does not support autocompletion */
55 /* TODO(#357): Console does not show the state of the GC of the script */
56 /* TODO(#358): Console does not support copy, cut, paste operations */
58 Console *create_console(Level *level,
59 const Sprite_font *font)
67 Console *console = PUSH_LT(lt, nth_alloc(sizeof(Console)), free);
68 if (console == NULL) {
73 console->gc = PUSH_LT(lt, create_gc(), destroy_gc);
74 if (console->gc == NULL) {
78 console->scope.expr = CONS(console->gc,
82 load_std_library(console->gc, &console->scope);
83 load_log_library(console->gc, &console->scope);
84 load_level_library(console->gc, &console->scope, level);
86 console->edit_field = PUSH_LT(
90 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
93 if (console->edit_field == NULL) {
97 console->console_log = PUSH_LT(
101 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
102 CONSOLE_LOG_CAPACITY),
103 destroy_console_log);
105 console->level = level;
108 console->eval_result = PUSH_LT(
110 nth_alloc(sizeof(char) * CONSOLE_EVAL_RESULT_SIZE),
112 if (console->eval_result == NULL) {
115 memset(console->eval_result, 0, sizeof(char) * CONSOLE_EVAL_RESULT_SIZE);
117 console->history = PUSH_LT(
119 create_history(HISTORY_CAPACITY),
121 if (console->history == NULL) {
128 void destroy_console(Console *console)
131 RETURN_LT0(console->lt);
134 static int console_eval_input(Console *console)
136 const char *source_code = edit_field_as_text(console->edit_field);
138 /* TODO(#387): console pushes empty strings to the history */
139 if (history_push(console->history, source_code) < 0) {
143 if (console_log_push_line(console->console_log, source_code, CONSOLE_FOREGROUND) < 0) {
147 while (*source_code != 0) {
148 struct ParseResult parse_result = read_expr_from_string(console->gc,
151 if (parse_result.is_error) {
152 if (console_log_push_line(console->console_log, parse_result.error_message, CONSOLE_ERROR)) {
156 edit_field_clean(console->edit_field);
161 struct EvalResult eval_result = eval(
168 console->eval_result,
169 CONSOLE_EVAL_RESULT_SIZE) < 0) {
173 if (console_log_push_line(console->console_log,
174 console->eval_result,
175 eval_result.is_error ?
177 CONSOLE_FOREGROUND)) {
181 source_code = next_token(parse_result.end).begin;
184 gc_collect(console->gc, console->scope.expr);
185 edit_field_clean(console->edit_field);
190 int console_handle_event(Console *console,
191 const SDL_Event *event)
193 switch(event->type) {
195 switch(event->key.keysym.sym) {
197 return console_eval_input(console);
202 history_current(console->history));
203 history_prev(console->history);
209 history_current(console->history));
210 history_next(console->history);
216 return edit_field_handle_event(console->edit_field, event);
219 int console_render(const Console *console,
220 SDL_Renderer *renderer)
222 /* TODO(#364): console doesn't have any padding around the edit fields */
224 SDL_RenderGetViewport(renderer, &view_port);
226 const float e = console->a * (2 - console->a);
227 const float y = -(1.0f - e) * CONSOLE_HEIGHT;
229 if (fill_rect(renderer,
233 CONSOLE_BACKGROUND) < 0) {
237 if (console_log_render(console->console_log,
243 if (edit_field_render(console->edit_field,
245 vec(0.0f, y + CONSOLE_LOG_HEIGHT)) < 0) {
252 int console_update(Console *console, float delta_time)
256 if (console->a < 1.0f) {
257 console->a += 1.0f / SLIDE_DOWN_TIME * delta_time;
259 if (console->a > 1.0f) {
267 void console_slide_down(Console *console)