]> git.lizzy.rs Git - nothing.git/blob - src/ui/console.c
Merge pull request #1 from tsoding/master
[nothing.git] / src / ui / console.c
1 #include "system/stacktrace.h"
2
3 #include "ebisp/gc.h"
4 #include "ebisp/interpreter.h"
5 #include "ebisp/parser.h"
6 #include "ebisp/scope.h"
7 #include "ebisp/std.h"
8 #include "game/level.h"
9 #include "sdl/renderer.h"
10 #include "system/log.h"
11 #include "system/log_script.h"
12 #include "system/lt.h"
13 #include "system/nth_alloc.h"
14 #include "ui/console.h"
15 #include "ui/console_log.h"
16 #include "ui/edit_field.h"
17 #include "ui/history.h"
18 #include "broadcast.h"
19
20 #define FONT_WIDTH_SCALE 3.0f
21 #define FONT_HEIGHT_SCALE 3.0f
22
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)
27
28 #define CONSOLE_HEIGHT (CONSOLE_LOG_HEIGHT + PROMPT_HEIGHT)
29
30 #define SLIDE_DOWN_TIME 0.4f
31
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))
36
37 #define CONSOLE_EVAL_RESULT_SIZE 256
38
39 struct Console
40 {
41     Lt *lt;
42     Gc *gc;
43     struct Scope scope;
44     Edit_field *edit_field;
45     Console_Log *console_log;
46     History *history;
47     float a;
48     char *eval_result;
49 };
50
51 /* TODO(#355): Console does not support Emacs keybindings */
52 /* TODO(#356): Console does not support autocompletion */
53 /* TODO(#357): Console does not show the state of the GC of the script */
54 /* TODO(#358): Console does not support copy, cut, paste operations */
55
56 Console *create_console(Broadcast *broadcast,
57                         const Sprite_font *font)
58 {
59     Lt *lt = create_lt();
60
61     if (lt == NULL) {
62         return NULL;
63     }
64
65     Console *console = PUSH_LT(lt, nth_calloc(1, sizeof(Console)), free);
66     if (console == NULL) {
67         RETURN_LT(lt, NULL);
68     }
69     console->lt = lt;
70
71     console->gc = PUSH_LT(lt, create_gc(), destroy_gc);
72     if (console->gc == NULL) {
73         RETURN_LT(lt, NULL);
74     }
75
76     console->scope.expr = CONS(console->gc,
77                                NIL(console->gc),
78                                NIL(console->gc));
79
80     load_std_library(console->gc, &console->scope);
81     load_log_library(console->gc, &console->scope);
82     /* TODO(#669): how to report EvalResult error from create_console? */
83     broadcast_load_library(broadcast, console->gc, &console->scope);
84
85     console->edit_field = PUSH_LT(
86         lt,
87         create_edit_field(
88             font,
89             vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
90             CONSOLE_FOREGROUND),
91         destroy_edit_field);
92     if (console->edit_field == NULL) {
93         RETURN_LT(lt, NULL);
94     }
95
96     console->console_log = PUSH_LT(
97         lt,
98         create_console_log(
99             font,
100             vec(FONT_WIDTH_SCALE, FONT_HEIGHT_SCALE),
101             CONSOLE_LOG_CAPACITY),
102         destroy_console_log);
103
104     console->a = 0;
105
106     console->eval_result = PUSH_LT(
107         lt,
108         nth_calloc(1, sizeof(char) * CONSOLE_EVAL_RESULT_SIZE),
109         free);
110     if (console->eval_result == NULL) {
111         RETURN_LT(lt, NULL);
112     }
113     memset(console->eval_result, 0, sizeof(char) * CONSOLE_EVAL_RESULT_SIZE);
114
115     console->history = PUSH_LT(
116         lt,
117         create_history(HISTORY_CAPACITY),
118         destroy_history);
119     if (console->history == NULL) {
120         RETURN_LT(lt, NULL);
121     }
122
123     return console;
124 }
125
126 void destroy_console(Console *console)
127 {
128     trace_assert(console);
129     RETURN_LT0(console->lt);
130 }
131
132 static int console_eval_input(Console *console)
133 {
134     const char *source_code = edit_field_as_text(console->edit_field);
135
136     /* TODO(#387): console pushes empty strings to the history */
137     if (history_push(console->history, source_code) < 0) {
138         return -1;
139     }
140
141     if (console_log_push_line(console->console_log, source_code, CONSOLE_FOREGROUND) < 0) {
142         return -1;
143     }
144
145     while (*source_code != 0) {
146         struct ParseResult parse_result = read_expr_from_string(console->gc,
147                                                                 source_code);
148
149         if (parse_result.is_error) {
150             if (console_log_push_line(console->console_log, parse_result.error_message, CONSOLE_ERROR)) {
151                 return -1;
152             }
153
154             edit_field_clean(console->edit_field);
155
156             return 0;
157         }
158
159         struct EvalResult eval_result = eval(
160             console->gc,
161             &console->scope,
162             parse_result.expr);
163
164         if (expr_as_sexpr(
165                 eval_result.expr,
166                 console->eval_result,
167                 CONSOLE_EVAL_RESULT_SIZE) < 0) {
168             return -1;
169         }
170
171         if (console_log_push_line(console->console_log,
172                           console->eval_result,
173                           eval_result.is_error ?
174                           CONSOLE_ERROR :
175                           CONSOLE_FOREGROUND)) {
176             return -1;
177         }
178
179         source_code = next_token(parse_result.end).begin;
180     }
181
182     gc_collect(console->gc, console->scope.expr);
183     edit_field_clean(console->edit_field);
184
185     return 0;
186 }
187
188 int console_handle_event(Console *console,
189                          const SDL_Event *event)
190 {
191     switch(event->type) {
192     case SDL_KEYDOWN:
193         switch(event->key.keysym.sym) {
194         case SDLK_RETURN:
195             return console_eval_input(console);
196
197         case SDLK_UP:
198             edit_field_replace(
199                 console->edit_field,
200                 history_current(console->history));
201             history_prev(console->history);
202             return 0;
203
204         case SDLK_DOWN:
205             edit_field_replace(
206                 console->edit_field,
207                 history_current(console->history));
208             history_next(console->history);
209             return 0;
210         }
211         break;
212     }
213
214     return edit_field_handle_event(console->edit_field, event);
215 }
216
217 int console_render(const Console *console,
218                    SDL_Renderer *renderer)
219 {
220     /* TODO(#364): console doesn't have any padding around the edit fields */
221     SDL_Rect view_port;
222     SDL_RenderGetViewport(renderer, &view_port);
223
224     const float e = console->a * (2 - console->a);
225     const float y = -(1.0f - e) * CONSOLE_HEIGHT;
226
227     if (fill_rect(renderer,
228                   rect(0.0f, y,
229                        (float) view_port.w,
230                        CONSOLE_HEIGHT),
231                   CONSOLE_BACKGROUND) < 0) {
232         return -1;
233     }
234
235     if (console_log_render(console->console_log,
236                    renderer,
237                    vec(0.0f, y)) < 0) {
238         return -1;
239     }
240
241     if (edit_field_render(console->edit_field,
242                           renderer,
243                           vec(0.0f, y + CONSOLE_LOG_HEIGHT)) < 0) {
244         return -1;
245     }
246
247     return 0;
248 }
249
250 int console_update(Console *console, float delta_time)
251 {
252     trace_assert(console);
253
254     if (console->a < 1.0f) {
255         console->a += 1.0f / SLIDE_DOWN_TIME * delta_time;
256
257         if (console->a > 1.0f) {
258             console->a = 1.0f;
259         }
260     }
261
262     return 0;
263 }
264
265 void console_slide_down(Console *console)
266 {
267     trace_assert(console);
268     console->a = 0.0f;
269 }