3 #include "game/level.h"
4 #include "game/level/player/rigid_rect.h"
6 #include "script/interpreter.h"
7 #include "script/parser.h"
8 #include "script/scope.h"
9 #include "sdl/renderer.h"
10 #include "system/error.h"
11 #include "system/lt.h"
12 #include "ui/console.h"
13 #include "ui/edit_field.h"
16 #define FONT_WIDTH_SCALE 3.0f
17 #define FONT_HEIGHT_SCALE 3.0f
19 #define LOG_CAPACITY 10
20 #define PROMPT_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT)
21 #define LOG_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT * LOG_CAPACITY)
23 #define CONSOLE_HEIGHT (LOG_HEIGHT + PROMPT_HEIGHT)
25 #define SLIDE_DOWN_TIME 0.4f
27 #define CONSOLE_BACKGROUND (color(0.20f, 0.20f, 0.20f, 1.0f))
28 #define CONSOLE_FOREGROUND (color(0.80f, 0.80f, 0.80f, 1.0f))
29 #define CONSOLE_ERROR (color(0.80f, 0.50f, 0.50f, 1.0f))
36 Edit_field *edit_field;
42 /* TODO(#354): Console does not allow to travel the history by pressing up and down */
43 /* TODO(#355): Console does not support Emacs keybindings */
44 /* TODO(#356): Console does not support autocompletion */
45 /* TODO(#357): Console does not show the state of the GC of the script */
46 /* TODO(#358): Console does not support copy, cut, paste operations */
48 static struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
54 Level *level = (Level*) param;
55 const char *rect_id = CAR(args).atom->str;
56 struct Expr vector_force_expr = CAR(CDR(args));
57 const float force_x = (float) CAR(vector_force_expr).atom->num;
58 const float force_y = (float) CDR(vector_force_expr).atom->num;
60 print_expr_as_sexpr(args); printf("\n");
62 Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
63 if (rigid_rect != NULL) {
64 printf("Found rect `%s`\n", rect_id);
65 printf("Applying force (%f, %f)\n", force_x, force_y);
66 rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
68 fprintf(stderr, "Couldn't find rigid_rect `%s`", rect_id);
71 return eval_success(NIL(gc));
74 Console *create_console(Level *level,
75 const Sprite_font *font)
83 Console *console = PUSH_LT(lt, malloc(sizeof(Console)), free);
84 if (console == NULL) {
85 throw_error(ERROR_TYPE_LIBC);
90 console->gc = PUSH_LT(lt, create_gc(), destroy_gc);
91 if (console->gc == NULL) {
95 console->scope.expr = CONS(console->gc,
101 SYMBOL(console->gc, "rect-apply-force"),
102 NATIVE(console->gc, rect_apply_force, level));
104 console->edit_field = PUSH_LT(
108 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
111 if (console->edit_field == NULL) {
115 console->log = PUSH_LT(
119 vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
123 console->level = level;
129 void destroy_console(Console *console)
132 RETURN_LT0(console->lt);
135 int console_handle_event(Console *console,
136 const SDL_Event *event)
138 switch(event->type) {
140 switch(event->key.keysym.sym) {
142 const char *source_code = edit_field_as_text(console->edit_field);
143 struct ParseResult parse_result = read_expr_from_string(console->gc,
145 if (parse_result.is_error) {
146 if (log_push_line(console->log, source_code, CONSOLE_ERROR) < 0) {
150 if (log_push_line(console->log, parse_result.error_message, CONSOLE_ERROR)) {
154 edit_field_clean(console->edit_field);
159 struct EvalResult eval_result = eval(
164 if (eval_result.is_error) {
165 /* TODO(#360): Console doesn't report any eval errors visually */
167 print_expr_as_sexpr(eval_result.expr);
171 gc_collect(console->gc, console->scope.expr);
173 if (log_push_line(console->log,
174 edit_field_as_text(console->edit_field),
175 CONSOLE_FOREGROUND) < 0) {
178 edit_field_clean(console->edit_field);
185 return edit_field_handle_event(console->edit_field, event);
188 int console_render(const Console *console,
189 SDL_Renderer *renderer)
191 /* TODO(#364): console doesn't have any padding around the edit fields */
193 SDL_RenderGetViewport(renderer, &view_port);
195 const float e = console->a * (2 - console->a);
196 const float y = -(1.0f - e) * CONSOLE_HEIGHT;
198 if (fill_rect(renderer,
202 CONSOLE_BACKGROUND) < 0) {
206 if (log_render(console->log,
212 if (edit_field_render(console->edit_field,
214 vec(0.0f, y + LOG_HEIGHT)) < 0) {
221 int console_update(Console *console, float delta_time)
225 /* TODO(#366): console slide down animation doesn't have any easing */
227 if (console->a < 1.0f) {
228 console->a += 1.0f / SLIDE_DOWN_TIME * delta_time;
230 if (console->a > 1.0f) {
238 void console_slide_down(Console *console)