diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-09 15:56:22 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-09 15:56:22 -0700 |
commit | 9ba644fd849ef7664be009e00206bf5eb1b85762 (patch) | |
tree | 2a154ab0891b4c4d070a547341893f47072f7b4e /web/web_prompt.c | |
parent | 22750c5853698817278cef512d253175ccc08c56 (diff) | |
download | dartboat-9ba644fd849ef7664be009e00206bf5eb1b85762.tar.gz dartboat-9ba644fd849ef7664be009e00206bf5eb1b85762.tar.xz |
web: dynamically generate (list-mode) keypad keys
This is currently only for match mode selection, but we'll reuse it to
implement more menus.
Diffstat (limited to 'web/web_prompt.c')
-rw-r--r-- | web/web_prompt.c | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/web/web_prompt.c b/web/web_prompt.c index 2ceb2d4..3b5bc68 100644 --- a/web/web_prompt.c +++ b/web/web_prompt.c @@ -13,8 +13,18 @@ int oi_timeout; +enum prompt_mode { + PM_DARTBOARD, + PM_VISIT, + PM_NUM_DARTS, + PM_END_MATCH, + PM_SELECT_MODE +}; enum prompt_mode pm; +static char *list_optv[10]; +static int list_optc; + static struct { bufstr msgl, msgr, input; } bufs; @@ -61,6 +71,42 @@ void prompt_flush() dom_set_content("#prompt-input", bufstr_flush(&bufs.input)); } +static void clear_list_opts() +{ + for (int i = 0; i < list_optc; ++i) + free(list_optv[i]); + list_optc = 0; +} + +static void add_list_opt(char *str) +{ + list_optv[list_optc++] = strdup(str); +} + +static void flush_list_opts() +{ + struct dom_elem **elemv = malloc(list_optc * sizeof(*elemv)); + + for (int i = 0; i < list_optc; ++i) { + struct dom_elem *e = dom_elem_init(NULL, "div", 2); + asprintf(&e->content, "[%d] %s", i + 1, list_optv[i]); + dom_elem_add_attr(e, "class", "key"); + + char buf[64]; + sprintf(buf, "append:%d;submit", i + 1); + dom_elem_add_attr(e, "data-command", buf); + + elemv[i] = e; + } + + dom_set_content("#keypad-list", NULL); + dom_append_elemv("#keypad-list", list_optc, elemv); + + for (int i = 0; i < list_optc; ++i) + dom_elem_free(elemv[i]); + free(elemv); +} + void set_prompt_mode(enum prompt_mode mode) { pm = mode; @@ -72,7 +118,7 @@ void set_prompt_mode(enum prompt_mode mode) dom_set_uniq_class( pm == PM_DARTBOARD ? "#keypad-dartboard" : - pm == PM_SELECT_MODE ? "#keypad-select_mode" : + pm == PM_SELECT_MODE ? "#keypad-list" : "#keypad-default", "visible", ".keypad"); } @@ -121,6 +167,11 @@ void prompt_select_mode() clear_player_info(pn); dom_set_content("#visits", NULL); + clear_list_opts(); + add_list_opt("Play against bot"); + add_list_opt("One-player scoreboard"); + add_list_opt("Two-player scoreboard"); + flush_list_opts(); set_prompt_mode(PM_SELECT_MODE); prompt_set_msgl("Select match mode:"); prompt_set_msgr(NULL); |