From df307f86ad32fcc841c770e8f5af72e8c0b80cfb Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Wed, 4 May 2022 15:16:56 -0700 Subject: web: abstract away most EM_ASM calls to dom file --- web/web_prompt.c | 62 ++++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 33 deletions(-) (limited to 'web/web_prompt.c') diff --git a/web/web_prompt.c b/web/web_prompt.c index 22ec4c1..06aa089 100644 --- a/web/web_prompt.c +++ b/web/web_prompt.c @@ -1,4 +1,5 @@ #include "web_control.h" +#include "web_dom.h" #include "web_prompt.h" #include "web_match.h" #include "web_scoreboard.h" @@ -21,7 +22,7 @@ enum prompt_mode pm; void oi() { - EM_ASM({elemAddClass($0, $1)}, "#oi", "visible"); + dom_elem_add_class("#oi", "visible"); oi_timeout = EM_ASM_INT({return scheduleCCall($0, $1)}, "clear_oi", 3000); } @@ -29,11 +30,11 @@ void oi() EMSCRIPTEN_KEEPALIVE void clear_oi() { - if (oi_timeout) { - EM_ASM({elemRemoveClass($0, $1)}, "#oi", "visible"); - EM_ASM({clearTimeout($0)}, oi_timeout); - oi_timeout = 0; - } + if (!oi_timeout) return; + + dom_elem_remove_class("#oi", "visible"); + EM_ASM({clearTimeout($0)}, oi_timeout); + oi_timeout = 0; } static inline void buffer_str(char *str, char **buffer, char *flushed) @@ -77,14 +78,11 @@ static inline void free_flushed_str(char *flushed, char *buffered) void prompt_flush() { if (buffered_str_changed(prompt_buffered.msgl, prompt_flushed.msgl)) - EM_ASM({elemSetContent($0, $1)}, - "#prompt-msg-l", prompt_buffered.msgl); + dom_elem_set_content("#prompt-msg-l", prompt_buffered.msgl); if (buffered_str_changed(prompt_buffered.msgr, prompt_flushed.msgr)) - EM_ASM({elemSetContent($0, $1)}, - "#prompt-msg-r", prompt_buffered.msgr); + dom_elem_set_content("#prompt-msg-r", prompt_buffered.msgr); if (buffered_str_changed(prompt_buffered.input, prompt_flushed.input)) - EM_ASM({elemSetContent($0, $1)}, - "#prompt-input", prompt_buffered.input); + dom_elem_set_content("#prompt-input", prompt_buffered.input); free_flushed_str(prompt_flushed.msgl, prompt_buffered.msgl); free_flushed_str(prompt_flushed.msgr, prompt_buffered.msgr); @@ -98,11 +96,11 @@ void set_prompt_mode(enum prompt_mode mode) pm = mode; if (pm != PM_DARTBOARD) - EM_ASM({elemAddClass($0, $1)}, "#prompt", "active"); + dom_elem_add_class("#prompt", "active"); else - EM_ASM({elemRemoveClass($0, $1)}, "#prompt", "active"); + dom_elem_remove_class("#prompt", "active"); - EM_ASM({elemSetUniqClass($0, $1, $2)}, + dom_elem_set_uniq_class( pm == PM_DARTBOARD ? "#keypad-dartboard" : pm == PM_SELECT_MODE ? "#keypad-select_mode" : "#keypad-default", @@ -114,8 +112,8 @@ void prompt_visit() set_prompt_mode(PM_VISIT); prompt_set_msgl("Enter points:"); prompt_set_msgr(NULL); - EM_ASM({elemSetContent($0, $1)}, "#key-submit", "OK"); - EM_ASM({elemSetContent($0, $1)}, "#key-rem", "REMAINING"); + dom_elem_set_content("#key-submit", "OK"); + dom_elem_set_content("#key-rem", "REMAINING"); } void prompt_bot_visit() @@ -130,8 +128,8 @@ void prompt_num_darts() set_prompt_mode(PM_NUM_DARTS); prompt_set_msgl("Darts needed?"); prompt_set_msgr(NULL); - EM_ASM({elemSetContent($0, $1)}, "#key-submit", "OK"); - EM_ASM({elemSetContent($0, $1)}, "#key-rem", "REMAINING"); + dom_elem_set_content("#key-submit", "OK"); + dom_elem_set_content("#key-rem", "REMAINING"); } void prompt_end_match() @@ -142,48 +140,46 @@ void prompt_end_match() state->mode == M_PVC && state->legs[1]->rem <= 0 ? "Bot wins. :(" : "You win! :)"); prompt_set_msgr(NULL); - EM_ASM({elemSetContent($0, $1)}, "#key-submit", "END MATCH"); - EM_ASM({elemSetContent($0, $1)}, "#key-rem", "REMATCH"); + dom_elem_set_content("#key-submit", "END MATCH"); + dom_elem_set_content("#key-rem", "REMATCH"); } void prompt_select_mode() { - EM_ASM({elemSetUniqClass($0, $1, $2)}, - NULL, "visible", "[id$=-info-inner]"); + dom_elem_set_uniq_class(NULL, "visible", "[id$=-info-inner]"); for (int pn = 1; pn < 3; ++pn) clear_player_info(pn); - EM_ASM({elemSetContent($0, $1)}, "#visits", NULL); + dom_elem_set_content("#visits", NULL); set_prompt_mode(PM_SELECT_MODE); prompt_set_msgl("Select match mode:"); prompt_set_msgr(NULL); } -bool is_key_active(char *k) +bool key_is_active(char *k) { char sel[64]; sprintf(sel, "#key-%s", k); - return EM_ASM_INT({return elemHasClass($0, $1)}, sel, "active"); + return dom_elem_has_class(sel, "active"); } void toggle_key(char *k) { char sel[64]; sprintf(sel, "#key-%s", k); - EM_ASM({elemToggleClass($0, $1)}, sel, "active"); + dom_elem_toggle_class(sel, "active"); } void deactivate_key(char *k) { char sel[64]; sprintf(sel, "#key-%s", k); - EM_ASM({elemRemoveClass($0, $1)}, sel, "active"); + dom_elem_remove_class(sel, "active"); } char *prompt_get() { - return (char *)EM_ASM_INT({return elemGetContent($0)}, - "#prompt-input"); + return dom_elem_get_content("#prompt-input"); } void prompt_handle_pre(char *command) @@ -260,7 +256,7 @@ void prompt_handle_clear() void prompt_handle_submit() { if (pm == PM_END_MATCH) { - if (is_key_active("submit")) + if (key_is_active("submit")) end_match(); toggle_key("submit"); return; @@ -286,7 +282,7 @@ void prompt_handle_submit() void prompt_handle_rem() { if (pm == PM_END_MATCH) { - if (is_key_active("rem")) + if (key_is_active("rem")) start_match(state->mode); toggle_key("rem"); return; @@ -308,7 +304,7 @@ void prompt_handle_undo() return; prompt_handle_clear(); - if (is_key_active("undo")) + if (key_is_active("undo")) user_undo(); toggle_key("undo"); } -- cgit v1.2.3-70-g09d2