summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-04 15:16:56 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-04 15:16:56 -0700
commitdf307f86ad32fcc841c770e8f5af72e8c0b80cfb (patch)
tree1881655a210d183ef775c1427906c765c1b14240
parentba56ae56ebec73ca8ea966fde4bb385f911a6669 (diff)
downloaddartboat-df307f86ad32fcc841c770e8f5af72e8c0b80cfb.tar.gz
dartboat-df307f86ad32fcc841c770e8f5af72e8c0b80cfb.tar.xz
web: abstract away most EM_ASM calls to dom file
-rw-r--r--web/web_control.c4
-rw-r--r--web/web_dom.c46
-rw-r--r--web/web_dom.h14
-rw-r--r--web/web_match.c2
-rw-r--r--web/web_match.h2
-rw-r--r--web/web_opts.c5
-rw-r--r--web/web_prompt.c62
-rw-r--r--web/web_scoreboard.c20
-rw-r--r--web/web_svg.c2
9 files changed, 105 insertions, 52 deletions
diff --git a/web/web_control.c b/web/web_control.c
index 321809c..0c2d4bd 100644
--- a/web/web_control.c
+++ b/web/web_control.c
@@ -118,7 +118,7 @@ void handle_next()
{
if (!state) {
prompt_select_mode();
- } else if (is_match_over()) {
+ } else if (match_is_over()) {
if (state->num_darts ||
(state->mode == M_PVC && state->legs[1]->rem <= 0))
prompt_end_match();
@@ -194,7 +194,7 @@ void user_undo()
if (state->mode == M_PVC) {
set_active_player(1);
} else if (state->mode == M_PVP) {
- if (is_match_over())
+ if (match_is_over())
set_active_player(state->active_player);
else
toggle_active_player();
diff --git a/web/web_dom.c b/web/web_dom.c
index 3a862f7..9e7684a 100644
--- a/web/web_dom.c
+++ b/web/web_dom.c
@@ -1,5 +1,6 @@
#include "web_dom.h"
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -68,3 +69,48 @@ void append_elemv(char *sel, int elemc, struct elem **elemv)
offsetof(struct elem, attr_names),
offsetof(struct elem, attr_vals));
}
+
+inline char *dom_elem_get_content(char *sel)
+{
+ return (char *)EM_ASM_INT({return elemGetContent($0)}, sel);
+}
+
+inline void dom_elem_set_content(char *sel, char *str)
+{
+ EM_ASM({elemSetContent($0, $1)}, sel, str);
+}
+
+inline void dom_elem_set_value(char *sel, char *str)
+{
+ EM_ASM({elemSetValue($0, $1)}, sel, str);
+}
+
+inline bool dom_elem_has_class(char *sel, char *class)
+{
+ return EM_ASM_INT({return elemHasClass($0, $1)}, sel, class);
+}
+
+inline void dom_elem_add_class(char *sel, char *class)
+{
+ EM_ASM({elemAddClass($0, $1)}, sel, class);
+}
+
+inline void dom_elem_remove_class(char *sel, char *class)
+{
+ EM_ASM({elemRemoveClass($0, $1)}, sel, class);
+}
+
+inline void dom_elem_toggle_class(char *sel, char *class)
+{
+ EM_ASM({elemToggleClass($0, $1)}, sel, class);
+}
+
+inline void dom_elem_set_uniq_class(char *sel, char *class, char *sel_set)
+{
+ EM_ASM({elemSetUniqClass($0, $1, $2)}, sel, class, sel_set);
+}
+
+inline void dom_elem_scroll_to_bottom(char *sel)
+{
+ EM_ASM({elemScrollToBottom($0)}, sel);
+}
diff --git a/web/web_dom.h b/web/web_dom.h
index a86d432..12205c2 100644
--- a/web/web_dom.h
+++ b/web/web_dom.h
@@ -1,6 +1,8 @@
#ifndef WEB_DOM_H
#define WEB_DOM_H
+#include <stdbool.h>
+
struct elem {
char *ns, *name, *content;
int n_attrs, size_attrs;
@@ -13,4 +15,16 @@ void elem_add_attr(struct elem *e, char *name, char *val);
void elem_add_attr_double(struct elem *e, char *name, double val);
void append_elemv(char *sel, int elemc, struct elem **elemv);
+char *dom_elem_get_content(char *sel);
+void dom_elem_set_content(char *sel, char *str);
+void dom_elem_set_value(char *sel, char *str);
+
+bool dom_elem_has_class(char *sel, char *class);
+void dom_elem_add_class(char *sel, char *class);
+void dom_elem_remove_class(char *sel, char *class);
+void dom_elem_toggle_class(char *sel, char *class);
+void dom_elem_set_uniq_class(char *sel, char *class, char *sel_set);
+
+void dom_elem_scroll_to_bottom(char *sel);
+
#endif
diff --git a/web/web_match.c b/web/web_match.c
index 47bc6e6..5f87f28 100644
--- a/web/web_match.c
+++ b/web/web_match.c
@@ -14,7 +14,7 @@ void free_state()
state = NULL;
}
-bool is_match_over()
+bool match_is_over()
{
return state->legs[0]->rem <= 0 || state->legs[1]->rem <= 0;
}
diff --git a/web/web_match.h b/web/web_match.h
index 4b4c7bb..478ad88 100644
--- a/web/web_match.h
+++ b/web/web_match.h
@@ -24,6 +24,6 @@ extern struct match_state *state;
void free_state();
-bool is_match_over();
+bool match_is_over();
#endif
diff --git a/web/web_opts.c b/web/web_opts.c
index c156598..f255a83 100644
--- a/web/web_opts.c
+++ b/web/web_opts.c
@@ -1,3 +1,4 @@
+#include "web_dom.h"
#include "web_opts.h"
#include "comp.h"
@@ -68,7 +69,7 @@ void opts_init()
char buf[64];
sprintf(buf, "%d", delay_ms);
- EM_ASM({elemSetValue($0, $1)}, "#delay", buf);
+ dom_elem_set_value("#delay", buf);
int len = sprintf(buf, "%4.2f", horizontal_stdev);
char *dot = strchr(buf, '.');
@@ -79,7 +80,7 @@ void opts_init()
*ptr = 0;
}
}
- EM_ASM({elemSetValue($0, $1)}, "#stdev", buf);
+ dom_elem_set_value("#stdev", buf);
}
void store_opt(char *opt, char *val)
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");
}
diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c
index 1b3660d..1d28cbf 100644
--- a/web/web_scoreboard.c
+++ b/web/web_scoreboard.c
@@ -27,7 +27,7 @@ void scoreboard_set_player_active(int pn)
else
sprintf(sel, "#p%d-info", pn);
- EM_ASM({elemSetUniqClass($0, $1, $2)}, psel, "active", "[id$=-info]");
+ dom_elem_set_uniq_class(psel, "active", "[id$=-info]");
}
static inline void buffer_str(char *str, char **buffer, char *flushed)
@@ -83,29 +83,25 @@ void scoreboard_flush_player_info(int pn)
if (buffered_str_changed(buffered_info[pn-1].name,
flushed_info[pn-1].name)) {
strcpy(sel + len, "name");
- EM_ASM({elemSetContent($0, $1)},
- sel, buffered_info[pn-1].name);
+ dom_elem_set_content(sel, buffered_info[pn-1].name);
}
if (buffered_str_changed(buffered_info[pn-1].rem,
flushed_info[pn-1].rem)) {
strcpy(sel + len, "rem");
- EM_ASM({elemSetContent($0, $1)},
- sel, buffered_info[pn-1].rem);
+ dom_elem_set_content(sel, buffered_info[pn-1].rem);
}
if (buffered_str_changed(buffered_info[pn-1].sugg,
flushed_info[pn-1].sugg)) {
strcpy(sel + len, "sugg");
- EM_ASM({elemSetContent($0, $1)},
- sel, buffered_info[pn-1].sugg);
+ dom_elem_set_content(sel, buffered_info[pn-1].sugg);
}
if (buffered_str_changed(buffered_info[pn-1].avg,
flushed_info[pn-1].avg)) {
strcpy(sel + len, "avg");
- EM_ASM({elemSetContent($0, $1)},
- sel, buffered_info[pn-1].avg);
+ dom_elem_set_content(sel, buffered_info[pn-1].avg);
}
free_flushed_str(flushed_info[pn-1].name, buffered_info[pn-1].name);
@@ -176,7 +172,7 @@ void show_player_info(int pn)
{
char sel[64];
sprintf(sel, "#p%d-info-inner", pn);
- EM_ASM({elemAddClass($0, $1)}, sel, "visible");
+ dom_elem_add_class(sel, "visible");
}
void clear_player_info(int pn)
@@ -215,7 +211,7 @@ void add_colour_class(char *buf, int pts)
void draw_visits()
{
- EM_ASM({elemSetContent($0, $1)}, "#visits", NULL);
+ dom_elem_set_content("#visits", NULL);
int n_visits = state->legs[0]->n_visits > state->legs[1]->n_visits ?
state->legs[0]->n_visits : state->legs[1]->n_visits;
@@ -265,5 +261,5 @@ void draw_visits()
elem_free(elemv[i]);
free(elemv);
- EM_ASM({elemScrollToBottom($0)}, "#visits");
+ dom_elem_scroll_to_bottom("#visits");
}
diff --git a/web/web_svg.c b/web/web_svg.c
index ffdd104..ab712f8 100644
--- a/web/web_svg.c
+++ b/web/web_svg.c
@@ -197,5 +197,5 @@ void svg_draw_point(double x, double y)
void svg_clear_points()
{
- EM_ASM({elemSetContent($0, $1)}, "#dartboard .overlay", NULL);
+ dom_elem_set_content("#dartboard .overlay", NULL);
}