]> git.lizzy.rs Git - nothing.git/blob - src/ui/console.c
Merge pull request #403 from tsoding/350
[nothing.git] / src / ui / console.c
1 #include <assert.h>
2
3 #include "game/level.h"
4 #include "game/level/player/rigid_rect.h"
5 #include "script/gc.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"
14 #include "ui/history.h"
15 #include "ui/log.h"
16
17 #define FONT_WIDTH_SCALE 3.0f
18 #define FONT_HEIGHT_SCALE 3.0f
19
20 #define LOG_CAPACITY 10
21 #define HISTORY_CAPACITY 20
22 #define PROMPT_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT)
23 #define LOG_HEIGHT (FONT_HEIGHT_SCALE * FONT_CHAR_HEIGHT * LOG_CAPACITY)
24
25 #define CONSOLE_HEIGHT (LOG_HEIGHT + PROMPT_HEIGHT)
26
27 #define SLIDE_DOWN_TIME 0.4f
28
29 #define CONSOLE_ALPHA (0.80f)
30 #define CONSOLE_BACKGROUND (color(0.20f, 0.20f, 0.20f, CONSOLE_ALPHA))
31 #define CONSOLE_FOREGROUND (color(0.80f, 0.80f, 0.80f, CONSOLE_ALPHA))
32 #define CONSOLE_ERROR (color(0.80f, 0.50f, 0.50f, CONSOLE_ALPHA))
33
34 #define CONSOLE_EVAL_RESULT_SIZE 256
35
36 struct Console
37 {
38     Lt *lt;
39     Gc *gc;
40     struct Scope scope;
41     Edit_field *edit_field;
42     Log *log;
43     Level *level;
44     History *history;
45     float a;
46     char *eval_result;
47 };
48
49 /* TODO(#354): Console does not allow to travel the history by pressing up and down */
50 /* TODO(#355): Console does not support Emacs keybindings */
51 /* TODO(#356): Console does not support autocompletion */
52 /* TODO(#357): Console does not show the state of the GC of the script */
53 /* TODO(#358): Console does not support copy, cut, paste operations */
54
55 static struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
56 {
57     assert(gc);
58     assert(scope);
59     assert(param);
60
61     /* TODO(#401): rect_apply_force doesn't sanitize it's input */
62
63     Level *level = (Level*) param;
64     const char *rect_id = CAR(args).atom->str;
65     struct Expr vector_force_expr = CAR(CDR(args));
66     const float force_x = (float) CAR(vector_force_expr).atom->num;
67     const float force_y = (float) CDR(vector_force_expr).atom->num;
68
69     print_expr_as_sexpr(stdout, args); printf("\n");
70
71     Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
72     if (rigid_rect != NULL) {
73         printf("Found rect `%s`\n", rect_id);
74         printf("Applying force (%f, %f)\n", force_x, force_y);
75         rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
76     } else {
77         fprintf(stderr, "Couldn't find rigid_rect `%s`", rect_id);
78     }
79
80     return eval_success(NIL(gc));
81 }
82
83 Console *create_console(Level *level,
84                         const Sprite_font *font)
85 {
86     Lt *lt = create_lt();
87
88     if (lt == NULL) {
89         return NULL;
90     }
91
92     Console *console = PUSH_LT(lt, malloc(sizeof(Console)), free);
93     if (console == NULL) {
94         throw_error(ERROR_TYPE_LIBC);
95         RETURN_LT(lt, NULL);
96     }
97     console->lt = lt;
98
99     console->gc = PUSH_LT(lt, create_gc(), destroy_gc);
100     if (console->gc == NULL) {
101         RETURN_LT(lt, NULL);
102     }
103
104     console->scope.expr = CONS(console->gc,
105                                NIL(console->gc),
106                                NIL(console->gc));
107     set_scope_value(
108         console->gc,
109         &console->scope,
110         SYMBOL(console->gc, "rect-apply-force"),
111         NATIVE(console->gc, rect_apply_force, level));
112
113     console->edit_field = PUSH_LT(
114         lt,
115         create_edit_field(
116             font,
117             vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
118             CONSOLE_FOREGROUND),
119         destroy_edit_field);
120     if (console->edit_field == NULL) {
121         RETURN_LT(lt, NULL);
122     }
123
124     console->log = PUSH_LT(
125         lt,
126         create_log(
127             font,
128             vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
129             LOG_CAPACITY),
130         destroy_log);
131
132     console->level = level;
133     console->a = 0;
134
135     console->eval_result = PUSH_LT(
136         lt,
137         malloc(sizeof(char) * CONSOLE_EVAL_RESULT_SIZE),
138         free);
139     if (console->eval_result == NULL) {
140         RETURN_LT(lt, NULL);
141     }
142     memset(console->eval_result, 0, sizeof(char) * CONSOLE_EVAL_RESULT_SIZE);
143
144     console->history = PUSH_LT(
145         lt,
146         create_history(HISTORY_CAPACITY),
147         destroy_history);
148     if (console->history == NULL) {
149         RETURN_LT(lt, NULL);
150     }
151
152     return console;
153 }
154
155 void destroy_console(Console *console)
156 {
157     assert(console);
158     RETURN_LT0(console->lt);
159 }
160
161 int console_handle_event(Console *console,
162                          const SDL_Event *event)
163 {
164     switch(event->type) {
165     case SDL_KEYDOWN:
166         switch(event->key.keysym.sym) {
167         case SDLK_RETURN: {
168             const char *source_code = edit_field_as_text(console->edit_field);
169
170             /* TODO(#387): console pushes empty strings to the history */
171             if (history_push(console->history, source_code) < 0) {
172                 return -1;
173             }
174
175             if (log_push_line(console->log, source_code, CONSOLE_FOREGROUND) < 0) {
176                 return -1;
177             }
178
179             struct ParseResult parse_result = read_expr_from_string(console->gc,
180                                                                     source_code);
181
182             if (parse_result.is_error) {
183                 if (log_push_line(console->log, parse_result.error_message, CONSOLE_ERROR)) {
184                     return -1;
185                 }
186
187                 edit_field_clean(console->edit_field);
188
189                 return 0;
190             }
191
192             struct EvalResult eval_result = eval(
193                 console->gc,
194                 &console->scope,
195                 parse_result.expr);
196
197             if (expr_as_sexpr(
198                     eval_result.expr,
199                     console->eval_result,
200                     CONSOLE_EVAL_RESULT_SIZE) < 0) {
201                 return -1;
202             }
203
204             if (log_push_line(console->log,
205                               console->eval_result,
206                               eval_result.is_error ?
207                               CONSOLE_ERROR :
208                               CONSOLE_FOREGROUND)) {
209                 return -1;
210             }
211
212             gc_collect(console->gc, console->scope.expr);
213             edit_field_clean(console->edit_field);
214         } return 0;
215
216         case SDLK_UP: {
217             edit_field_replace(
218                 console->edit_field,
219                 history_current(console->history));
220             history_prev(console->history);
221         } return 0;
222
223         case SDLK_DOWN: {
224             edit_field_replace(
225                 console->edit_field,
226                 history_current(console->history));
227             history_next(console->history);
228         } return 0;
229         }
230         break;
231     }
232
233     return edit_field_handle_event(console->edit_field, event);
234 }
235
236 int console_render(const Console *console,
237                    SDL_Renderer *renderer)
238 {
239     /* TODO(#364): console doesn't have any padding around the edit fields */
240     SDL_Rect view_port;
241     SDL_RenderGetViewport(renderer, &view_port);
242
243     const float e = console->a * (2 - console->a);
244     const float y = -(1.0f - e) * CONSOLE_HEIGHT;
245
246     if (fill_rect(renderer,
247                   rect(0.0f, y,
248                        (float) view_port.w,
249                        CONSOLE_HEIGHT),
250                   CONSOLE_BACKGROUND) < 0) {
251         return -1;
252     }
253
254     if (log_render(console->log,
255                    renderer,
256                    vec(0.0f, y)) < 0) {
257         return -1;
258     }
259
260     if (edit_field_render(console->edit_field,
261                           renderer,
262                           vec(0.0f, y + LOG_HEIGHT)) < 0) {
263         return -1;
264     }
265
266     return 0;
267 }
268
269 int console_update(Console *console, float delta_time)
270 {
271     assert(console);
272
273     /* TODO(#366): console slide down animation doesn't have any easing */
274
275     if (console->a < 1.0f) {
276         console->a += 1.0f / SLIDE_DOWN_TIME * delta_time;
277
278         if (console->a > 1.0f) {
279             console->a = 1.0f;
280         }
281     }
282
283     return 0;
284 }
285
286 void console_slide_down(Console *console)
287 {
288     assert(console);
289     console->a = 0.0f;
290 }