summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-03 21:55:21 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-03 21:55:21 -0700
commit2709b070c5fde766c0e9fe98ade169a865e6c9ad (patch)
tree3e8c19cccfbd9fa88b21c8e44e9cd7f7e6d30b9f
parent1ab3c4995e44f44a845dce7b241554c0621527b9 (diff)
downloaddartboat-2709b070c5fde766c0e9fe98ade169a865e6c9ad.tar.gz
dartboat-2709b070c5fde766c0e9fe98ade169a865e6c9ad.tar.xz
web: oi from c
-rw-r--r--web/static/dartboat.js18
-rw-r--r--web/static/style.css4
-rw-r--r--web/web_control.c8
-rw-r--r--web/web_prompt.c20
-rw-r--r--web/web_prompt.h2
5 files changed, 30 insertions, 22 deletions
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index 4edba8a..eaf76da 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -1,25 +1,9 @@
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
-let oi_timeout;
-function oi() {
- $('#oi').style.visibility = 'visible';
-
- oi_timeout = setTimeout(() => { oi_timeout = null; clearOi(); }, 3000);
-}
-
-function clearOi() {
- $('#oi').style.visibility = 'hidden';
-
- if (oi_timeout) {
- clearTimeout(oi_timeout);
- oi_timeout = null;
- }
-}
-
function scheduleCCall(f, ms, ...args) {
const func = UTF8ToString(f);
- setTimeout(() => Module[`_${func}`](...args), ms);
+ return setTimeout(() => Module[`_${func}`](...args), ms);
}
function toCString(str) { // caller must free
diff --git a/web/static/style.css b/web/static/style.css
index 43c5b8b..e770c85 100644
--- a/web/static/style.css
+++ b/web/static/style.css
@@ -95,6 +95,10 @@ div#oi {
justify-content: center;
}
+div#oi.visible {
+ visibility: visible;
+}
+
div#info {
position: relative;
diff --git a/web/web_control.c b/web/web_control.c
index 94e58b7..e1e322d 100644
--- a/web/web_control.c
+++ b/web/web_control.c
@@ -145,7 +145,7 @@ EMSCRIPTEN_KEEPALIVE
void user_visit(int points)
{
if (!is_points_valid(points, state->active_leg->rem)) {
- EM_ASM(oi());
+ oi();
return;
}
@@ -175,7 +175,7 @@ EMSCRIPTEN_KEEPALIVE
void user_undo()
{
if (!state->legs[0]->n_visits) {
- EM_ASM(oi());
+ oi();
return;
}
@@ -229,7 +229,7 @@ EMSCRIPTEN_KEEPALIVE
void user_num_darts(int n)
{
if (n < 1 || n > 3) {
- EM_ASM(oi());
+ oi();
return;
}
@@ -243,7 +243,7 @@ EMSCRIPTEN_KEEPALIVE
void start_match(int mode)
{
if (mode < M_FIRST || mode > M_LAST) {
- EM_ASM(oi());
+ oi();
return;
}
diff --git a/web/web_prompt.c b/web/web_prompt.c
index e96e567..814b08e 100644
--- a/web/web_prompt.c
+++ b/web/web_prompt.c
@@ -9,6 +9,8 @@
#include <emscripten/emscripten.h>
+int oi_timeout;
+
struct prompt_state {
char *msgl, *input, *msgr;
};
@@ -17,6 +19,22 @@ struct prompt_state prompt_buffered, prompt_flushed;
enum prompt_mode pm;
+void oi()
+{
+ EM_ASM({elemAddClass($0, $1)}, "#oi", "visible");
+ oi_timeout = EM_ASM_INT({return scheduleCCall($0, $1)}, "clear_oi", 3000);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void clear_oi()
+{
+ if (oi_timeout) {
+ EM_ASM({elemRemoveClass($0, $1)}, "#oi", "visible");
+ EM_ASM({clearTimeout($0)}, oi_timeout);
+ oi_timeout = 0;
+ }
+}
+
static inline void buffer_str(char *str, char **buffer, char *flushed)
{
if (str == *buffer || (str && *buffer && !strcmp(str, *buffer)))
@@ -170,7 +188,7 @@ void prompt_handle_pre(char *command)
if (pm == PM_DARTBOARD)
return;
- EM_ASM(clearOi());
+ clear_oi();
if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) &&
strcmp(command, "undo"))
diff --git a/web/web_prompt.h b/web/web_prompt.h
index 8758000..7d503f8 100644
--- a/web/web_prompt.h
+++ b/web/web_prompt.h
@@ -11,6 +11,8 @@ enum prompt_mode {
extern enum prompt_mode pm;
+void oi();
+
void prompt_set_msgl(char *str);
void prompt_set_msgr(char *str);
void prompt_set_input(char *str);