diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2016-04-06 00:26:28 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2016-04-06 00:26:28 +0200 |
commit | 5659a1baba94d10f76e72c8bbb9fa7576ab4f19b (patch) | |
tree | 1f096951e2233dc897c5c083eac3a700fa27ecca /src/ui.c | |
parent | c7acaee6651a84ef9102b29350c37803c8f435a9 (diff) |
Proper console, run-time sim editing.
Diffstat (limited to 'src/ui.c')
-rw-r--r-- | src/ui.c | 152 |
1 files changed, 123 insertions, 29 deletions
@@ -8,7 +8,8 @@ struct { float color_main[4]; float color_light[4]; float color_info[4]; - float color_select[4]; + float color_select[4]; + float color_background_console[4]; } theme = { .font_size = 20, @@ -18,10 +19,12 @@ struct { .color_main = {0.75, 0.75, 0.75, 1}, .color_light = {0.85, 0.85, 0.85, 1}, .color_info = {1, 1, 1, 1}, - .color_select = {1, 0, 0, 1} + .color_select = {1, 0, 0, 1}, + .color_background_console = {1.0, 0.7, 0.0, 0.6} }; #define MAX_INFO 1024 +#define MAX_CONSOLE 1023 typedef struct { phy_sim *sim; @@ -32,9 +35,6 @@ typedef struct { bool select_valid; vec3_t select; - int64_t info_time; - char info[MAX_INFO]; - float margin_bottom, margin_top; mst2_t origin, scale; // in virtual space @@ -59,6 +59,14 @@ struct ui_window_s { ui_window_type type; ui_simview simview; + + itc_chan ctl; + char console[MAX_CONSOLE + 1]; + int console_len; + bool show_console; + + int64_t info_time; + char info[MAX_INFO]; }; static struct { @@ -87,6 +95,7 @@ static void ui_window_destroy(ui_window *uiw) uis.windows = uiw->next; r_xsection_destroy(&uiw->simview.xsection); + itc_chan_destroy(&uiw->ctl); free(uiw); } @@ -117,6 +126,7 @@ void ui_renderer_window_register(r_window *rw) ", uiw = %p\n", SDL_GetWindowID(rw->window), uiw); r_xsection_create(&uiw->simview.xsection); + itc_chan_create(&uiw->ctl); } static ui_window *ui_find_window(int id) @@ -135,10 +145,67 @@ void ui_infof(ui_window *uiw, const char *fmt, ...) va_list vl; va_start(vl, fmt); - vsnprintf(uiw->simview.info, MAX_INFO, fmt, vl); + vsnprintf(uiw->info, MAX_INFO, fmt, vl); va_end(vl); - uiw->simview.info_time = get_time(); + uiw->info_time = get_time(); +} + +void ui_console_add(ui_window *uiw, char *ch) +{ + int add_len; + + add_len = strlen(ch); + + if (!add_len || *ch == 1) // wtf is \x01 ? + return; + + if (uiw->console_len + add_len > MAX_CONSOLE) + return; + + memcpy(uiw->console + uiw->console_len, ch, add_len); + uiw->console_len += add_len; + uiw->console[uiw->console_len] = 0; +} + +void ui_console_erase(ui_window *uiw) +{ + char *p; + + if (!uiw->console_len) + return; + + p = uiw->console + uiw->console_len - 1; + + while (p > uiw->console && ((*p) & 0b11000000) == 0b10000000) + p--; + + uiw->console_len = p - uiw->console; + *p = 0; +} + +void ui_event_console(SDL_Event *event, ui_window *uiw) +{ + switch (event->type) { + case SDL_KEYDOWN: + switch (event->key.keysym.sym) { + case SDLK_BACKSPACE: + ui_console_erase(uiw); + break; + + case SDLK_RETURN: + con_exec(uiw->console, &uiw->ctl); + uiw->console[0] = 0; + uiw->console_len = 0; + uiw->show_console = false; + SDL_StopTextInput(); + break; + } + + case SDL_TEXTINPUT: + ui_console_add(uiw, event->text.text); + break; + } } void ui_event_window(SDL_Event *event, ui_window *uiw) @@ -148,6 +215,20 @@ void ui_event_window(SDL_Event *event, ui_window *uiw) if (!uiw->initialized) return; + if (uiw->show_console) { + ui_event_console(event, uiw); + return; + } + + if (event->type == SDL_KEYDOWN && + event->key.keysym.sym == SDLK_RETURN) { + uiw->show_console = true; + SDL_StartTextInput(); + } + + if (!uiw->simview.sim->valid) + return; + switch (event->type) { case SDL_MOUSEWHEEL: { @@ -255,11 +336,6 @@ void ui_event_window(SDL_Event *event, ui_window *uiw) itc_chan_push(&sv->sim->ctl, PHY_CMD_STEP, NULL); break; - case SDLK_r: - ui_infof(uiw, "Symulacja wyzerowana"); - itc_chan_push(&sv->sim->ctl, PHY_CMD_RESET, NULL); - break; - case SDLK_4: ui_infof(uiw, "Przekrój: pola elektrycznego"); sv->xsection_flags &= ~XSECTION_H; @@ -287,8 +363,8 @@ void ui_event_window(SDL_Event *event, ui_window *uiw) itc_chan_push(&sv->sim->ctl, PHY_CMD_DEBUG, coords); - break; } + break; } } } @@ -296,7 +372,7 @@ void ui_event_window(SDL_Event *event, ui_window *uiw) void ui_event(SDL_Event *event) { if (event->type == SDL_KEYDOWN && - event->key.keysym.sym == SDLK_RETURN) { + event->key.keysym.sym == SDLK_F3) { if (!uis.use_window_open_timer || uis.window_open_timer + 500000000 <= get_time()) { @@ -333,11 +409,6 @@ void ui_event(SDL_Event *event) } } -void ui_animate_exp(float *val, float targ, float lambda, float dt) -{ - *val = targ + (*val - targ) * exp(-lambda * dt); -} - void ui_draw_simview_xsection(ui_window *uiw) { ui_simview *sv = &uiw->simview; @@ -515,18 +586,16 @@ void ui_draw_simview_bars(ui_window *uiw, float dt) SDL_mutexV(sim->rotate_lock); } -void ui_draw_simview_info(ui_window *uiw, int64_t time) +void ui_draw_info(ui_window *uiw, int64_t time) { - ui_simview *sv = &uiw->simview; - - if (sv->info_time && sv->info_time + 2000000000ll > time) + if (uiw->info_time && uiw->info_time + 3000000000ll > time) { vec4_t color; - if (sv->info_time + 1000000000ll >= time) + if (uiw->info_time + 2000000000ll >= time) color[3] = 1.0f; else - color[3] = 1.0f - (time - sv->info_time - 1000000000ll) + color[3] = 1.0f - (time - uiw->info_time - 2000000000ll) / 1.0e9f; v3_copy(color, theme.color_info); @@ -537,16 +606,31 @@ void ui_draw_simview_info(ui_window *uiw, int64_t time) v3_copy(color, theme.color_text); r_draw_text(uiw->rw, uiw->w / 2.0f, theme.font_size, - theme.font_size, sv->info, color, TEXT_CENTERX); + theme.font_size, uiw->info, color, TEXT_CENTERX); } } void ui_draw_simview(ui_window *uiw, int64_t time, float dt) { + if (!uiw->simview.sim->valid) + return; + ui_draw_simview_xsection(uiw); ui_draw_simview_selection(uiw); ui_draw_simview_bars(uiw, dt); - ui_draw_simview_info(uiw, time); +} + +void ui_draw_console(ui_window *uiw) +{ + float y; + + y = uiw->h / 2 - theme.font_size / 2; + + r_draw_rect(uiw->rw, 0, y, uiw->w, theme.font_size, + theme.color_background_console); + + r_draw_text(uiw->rw, 0, y, theme.font_size, uiw->console, + theme.color_text, 0); } void ui_draw_window(ui_window *uiw) @@ -554,9 +638,18 @@ void ui_draw_window(ui_window *uiw) ui_simview *sv = &uiw->simview; int64_t time; float dt; + int cmd_num; + char *cmd_data; time = get_time(); + while (!itc_chan_pop(&uiw->ctl, &cmd_num, (void**)&cmd_data)) { + if (cmd_num == CMD_CON_FEEDBACK) { + ui_infof(uiw, "%s", cmd_data); + free(cmd_data); + } + } + if (!uiw->initialized) { uiw->initialized = true; uiw->last_frame = time; @@ -570,8 +663,6 @@ void ui_draw_window(ui_window *uiw) sv->margin_top = theme.font_size; sv->margin_bottom = 0; - sv->info_time = 0; - r_clear(uiw->rw, r_color_black); return; } @@ -580,6 +671,9 @@ void ui_draw_window(ui_window *uiw) r_clear(uiw->rw, r_color_black); ui_draw_simview(uiw, time, dt); + if (uiw->show_console) + ui_draw_console(uiw); + ui_draw_info(uiw, time); r_flip(uiw->rw); uiw->last_frame = time; |