4 #include "ebisp/interpreter.h"
5 #include "ebisp/parser.h"
6 #include "ebisp/scope.h"
7 #include "game/level.h"
8 #include "game/level/player/rigid_rect.h"
9 #include "game/level_script.h"
10 #include "sdl/renderer.h"
11 #include "system/log.h"
12 #include "system/log_script.h"
13 #include "system/lt.h"
14 #include "system/nth_alloc.h"
15 #include "ui/console.h"
16 #include "ui/console_log.h"
17 #include "ui/edit_field.h"
18 #include "ui/history.h"
20 #define FONT_WIDTH_SCALE 3.0f
21 #define FONT_HEIGHT_SCALE 3.0f
23 #define CONSOLE_LOG_CAPACITY 10
24 #define HISTORY_CAPACITY 20
25 #define PROMPT_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT)
26 #define CONSOLE_LOG_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT * CONSOLE_LOG_CAPACITY)
28 #define CONSOLE_HEIGHT (CONSOLE_LOG_HEIGHT + PROMPT_HEIGHT)
30 #define SLIDE_DOWN_TIME 0.4f
32 #define CONSOLE_ALPHA (0.80f)
33 #define CONSOLE_BACKGROUND (rgba(0.20f, 0.20f, 0.20f, CONSOLE_ALPHA))
34 #define CONSOLE_FOREGROUND (rgba(0.80f, 0.80f, 0.80f, CONSOLE_ALPHA))
35 #define CONSOLE_ERROR (rgba(0.80f, 0.50f, 0.50f, CONSOLE_ALPHA))
37 #define CONSOLE_EVAL_RESULT_SIZE 256
44 Edit_field *edit_field;
45 Console_Log *console_log;
52 /* TODO(#355): Console does not support Emacs keybindings */
53 /* TODO(#356): Console does not support autocompletion */
54 /* TODO(#357): Console does not show the state of the GC of the script */
55 /* TODO(#358): Console does not support copy, cut, paste operations */
57 Console *create_console(Level *level,
58 const Sprite_font *font)
66 Console *console = PUSH_LT(lt, nth_alloc(sizeof(Console)), free);
67 if (console == NULL) {
72 console->gc = PUSH_LT(lt, create_gc(), destroy_gc);
73 if (console->gc == NULL) {
77 console->scope.expr = CONS(console->gc,
81 load_std_library(console->gc, &console->scope);
82 load_log_library(console->gc, &console->scope);
83 load_level_library(console->gc, &console->scope, level);
85 console->edit_field = PUSH_LT(
89 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
92 if (console->edit_field == NULL) {
96 console->console_log = PUSH_LT(
100 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
101 CONSOLE_LOG_CAPACITY),
102 destroy_console_log);
104 console->level = level;
107 console->eval_result = PUSH_LT(
109 nth_alloc(sizeof(char) * CONSOLE_EVAL_RESULT_SIZE),
111 if (console->eval_result == NULL) {
114 memset(console->eval_result, 0, sizeof(char) * CONSOLE_EVAL_RESULT_SIZE);
116 console->history = PUSH_LT(
118 create_history(HISTORY_CAPACITY),
120 if (console->history == NULL) {
127 void destroy_console(Console *console)
130 RETURN_LT0(console->lt);
133 static int console_eval_input(Console *console)
135 const char *source_code = edit_field_as_text(console->edit_field);
137 /* TODO(#387): console pushes empty strings to the history */
138 if (history_push(console->history, source_code) < 0) {
142 if (console_log_push_line(console->console_log, source_code, CONSOLE_FOREGROUND) < 0) {
146 while (*source_code != 0) {
147 struct ParseResult parse_result = read_expr_from_string(console->gc,
150 if (parse_result.is_error) {
151 if (console_log_push_line(console->console_log, parse_result.error_message, CONSOLE_ERROR)) {
155 edit_field_clean(console->edit_field);
160 struct EvalResult eval_result = eval(
167 console->eval_result,
168 CONSOLE_EVAL_RESULT_SIZE) < 0) {
172 if (console_log_push_line(console->console_log,
173 console->eval_result,
174 eval_result.is_error ?
176 CONSOLE_FOREGROUND)) {
180 source_code = next_token(parse_result.end).begin;
183 gc_collect(console->gc, console->scope.expr);
184 edit_field_clean(console->edit_field);
189 int console_handle_event(Console *console,
190 const SDL_Event *event)
192 switch(event->type) {
194 switch(event->key.keysym.sym) {
196 return console_eval_input(console);
201 history_current(console->history));
202 history_prev(console->history);
208 history_current(console->history));
209 history_next(console->history);
215 return edit_field_handle_event(console->edit_field, event);
218 int console_render(const Console *console,
219 SDL_Renderer *renderer)
221 /* TODO(#364): console doesn't have any padding around the edit fields */
223 SDL_RenderGetViewport(renderer, &view_port);
225 const float e = console->a * (2 - console->a);
226 const float y = -(1.0f - e) * CONSOLE_HEIGHT;
228 if (fill_rect(renderer,
232 CONSOLE_BACKGROUND) < 0) {
236 if (console_log_render(console->console_log,
242 if (edit_field_render(console->edit_field,
244 vec(0.0f, y + CONSOLE_LOG_HEIGHT)) < 0) {
251 int console_update(Console *console, float delta_time)
255 if (console->a < 1.0f) {
256 console->a += 1.0f / SLIDE_DOWN_TIME * delta_time;
258 if (console->a > 1.0f) {
266 void console_slide_down(Console *console)