From: Christian Heller Date: Sat, 7 Nov 2020 23:59:37 +0000 (+0100) Subject: Refactor web client keybindings code. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/balance?a=commitdiff_plain;h=4892dca2b3072b5a3c92d5eec3442ef37db4d9d1;p=plomrogue2-experiments Refactor web client keybindings code. --- diff --git a/new2/rogue_chat_nocanvas_monochrome.html b/new2/rogue_chat_nocanvas_monochrome.html index 0f502ed..664409d 100644 --- a/new2/rogue_chat_nocanvas_monochrome.html +++ b/new2/rogue_chat_nocanvas_monochrome.html @@ -35,23 +35,7 @@ let websocket_location = "ws://localhost:8000"; let rows_selector = document.getElementById("n_rows"); let cols_selector = document.getElementById("n_cols"); -let key_switch_to_chat_selector = document.getElementById("key_switch_to_chat"); -let key_switch_to_play_selector = document.getElementById("key_switch_to_play"); -let key_switch_to_annotate_selector = document.getElementById("key_switch_to_annotate"); -let key_switch_to_portal_selector = document.getElementById("key_switch_to_portal"); -let key_switch_to_study_selector = document.getElementById("key_switch_to_study"); -let key_switch_to_edit_selector = document.getElementById("key_switch_to_edit"); -let key_flatten_selector = document.getElementById("key_flatten"); -let key_hex_move_upleft_selector = document.getElementById("key_hex_move_upleft"); -let key_hex_move_upright_selector = document.getElementById("key_hex_move_upright"); -let key_hex_move_right_selector = document.getElementById("key_hex_move_right"); -let key_hex_move_downright_selector = document.getElementById("key_hex_move_downright"); -let key_hex_move_downleft_selector = document.getElementById("key_hex_move_downleft"); -let key_hex_move_left_selector = document.getElementById("key_hex_move_left"); -let key_square_move_up_selector = document.getElementById("key_square_move_up"); -let key_square_move_left_selector = document.getElementById("key_square_move_left"); -let key_square_move_down_selector = document.getElementById("key_square_move_down"); -let key_square_move_right_selector = document.getElementById("key_square_move_right"); +let key_selectors = document.querySelectorAll('[id^="key_"]'); let terminal = { foreground: 'white', @@ -319,24 +303,9 @@ let tui = { this.init_keys(); }, init_keys: function() { - this.keys = { - switch_to_chat: key_switch_to_chat_selector.value, - switch_to_play: key_switch_to_play_selector.value, - switch_to_annotate: key_switch_to_annotate_selector.value, - switch_to_portal: key_switch_to_portal_selector.value, - switch_to_study: key_switch_to_study_selector.value, - switch_to_edit: key_switch_to_edit_selector.value, - flatten: key_flatten_selector.value, - hex_move_upleft: key_hex_move_upleft_selector.value, - hex_move_upright: key_hex_move_upright_selector.value, - hex_move_right: key_hex_move_right_selector.value, - hex_move_downright: key_hex_move_downright_selector.value, - hex_move_downleft: key_hex_move_downleft_selector.value, - hex_move_left: key_hex_move_left_selector.value, - square_move_up: key_square_move_up_selector.value, - square_move_left: key_square_move_left_selector.value, - square_move_down: key_square_move_down_selector.value, - square_move_right: key_square_move_right_selector.value, + this.keys = {}; + for (let key_selector of key_selectors) { + this.keys[key_selector.id.slice(4)] = key_selector.value; } if (game.map_geometry == 'Square') { this.movement_keys = { @@ -810,42 +779,14 @@ cols_selector.addEventListener('input', function() { tui.window_width = terminal.cols / 2, tui.full_refresh(); }, false); -key_switch_to_chat_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_switch_to_play_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_switch_to_annotate_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_switch_to_portal_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_switch_to_study_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_switch_to_edit_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_flatten_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_hex_move_upleft_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_hex_move_upright_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_hex_move_right_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_hex_move_downright_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_hex_move_downleft_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_hex_move_left_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_square_move_up_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_square_move_left_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_square_move_down_selector.addEventListener('input', function() { tui.init_keys(); }, false); -key_square_move_right_selector.addEventListener('input', function() { tui.init_keys(); }, false); +for (let key_selector of key_selectors) { + key_selector.addEventListener('input', function() { + tui.init_keys(); + }, false); +} window.setInterval(function() { - if (!(['input', 'n_cols', 'n_rows', - 'key_switch_to_chat', - 'key_switch_to_play', - 'key_switch_to_annotate', - 'key_switch_to_portal', - 'key_switch_to_study', - 'key_switch_to_edit', - 'key_flatten', - 'key_hex_move_upleft', - 'key_hex_move_upright', - 'key_hex_move_right', - 'key_hex_move_downright', - 'key_hex_move_downleft', - 'key_hex_move_left', - 'key_square_move_up', - 'key_square_move_left', - 'key_square_move_down', - 'key_square_move_right',].includes(document.activeElement.id))) { + if (!(['input', 'n_cols', 'n_rows'].includes(document.activeElement.id) + || document.activeElement.id.startsWith('key_'))) { tui.inputEl.focus(); } }, 100);