home · contact · privacy
Remove redundant button texts.
[plomrogue2] / rogue_chat.html
index 381825429fb4240e7aa521c881e9e4157073df4d..ded3658df8d8cafe2d466cc334c3f8d45733fb79 100644 (file)
@@ -20,60 +20,60 @@ terminal rows: <input id="n_rows" type="number" step=4 min=24 value=24 />
 keyboard input/control: <span id="keyboard_control"></span>
 </div>
 <h3>button controls for mouse players</h3>
-<table style="float: left">
+<table id="move_table" style="float: left">
   <tr>
-    <td style="text-align: right"><button id="move_upleft">up-left</button></td>
-    <td style="text-align: center"><button id="move_up">up</button></td>
-    <td><button id="move_upright">up-right</button></td>
+    <td style="text-align: right"><button id="hex_move_upleft"></button></td>
+    <td style="text-align: center"><button id="square_move_up"></button></td>
+    <td><button id="hex_move_upright"></button></td>
   </tr>
   <tr>
-    <td style="text-align: right;"><button id="move_left">left</button></td>
+    <td style="text-align: right;"><button id="square_move_left"></button><button id="hex_move_left">left</button></td>
     <td stlye="text-align: center;">move</td>
-    <td><button id="move_right">right</button></td>
+    <td><button id="square_move_right"></button><button id="hex_move_right"></button></td>
   </tr>
   <tr>
-    <td><button id="move_downleft">down-left</button></td>
-    <td style="text-align: center"><button id="move_down">down</button></td>
-    <td><button id="move_downright">down-right</button></td>
+    <td><button id="hex_move_downleft"></button></td>
+    <td style="text-align: center"><button id="square_move_down"></button></td>
+    <td><button id="hex_move_downright"></button></td>
   </tr>
 </table>
 <table>
   <tr>
-    <td><button id="help">help</button></td>
+    <td><button id="help"></button></td>
   </tr>
   <tr>
-    <td><button id="switch_to_chat">chat mode</button><br /></td>
+    <td><button id="switch_to_chat"></button><br /></td>
   </tr>
   <tr>
-    <td><button id="switch_to_study">study mode</button></td>
-    <td><button id="toggle_map_mode">toggle map view</button>
+    <td><button id="switch_to_study"></button></td>
+    <td><button id="toggle_map_mode"></button>
   </tr>
   <tr>
-    <td><button id="switch_to_play">play mode</button></td>
+    <td><button id="switch_to_play"></button></td>
     <td>
-      <button id="take_thing">pick up thing</button>
-      <button id="drop_thing">drop thing</button>
-      <button id="teleport">teleport</button>
+      <button id="take_thing"></button>
+      <button id="drop_thing"></button>
+      <button id="teleport"></button>
     </td>
   </tr>
   <tr>
-    <td><button id="switch_to_edit">map edit mode</button></td>
+    <td><button id="switch_to_edit"></button></td>
     <td>
-      <button id="switch_to_write">change terrain</button>
-      <button id="flatten">flatten surroundings</button>
-      <button id="switch_to_annotate">annotate tile</button>
-      <button id="switch_to_portal">edit portal</button>
-      <button id="switch_to_name_thing">name thing</button>
-      <button id="switch_to_password">enter map edit password</button>
+      <button id="switch_to_write"></button>
+      <button id="flatten"></button>
+      <button id="switch_to_annotate"></button>
+      <button id="switch_to_portal"></button>
+      <button id="switch_to_name_thing"></button>
+      <button id="switch_to_password"></button>
     </td>
   </tr>
   <tr>
-    <td><button id="switch_to_admin_enter">admin mode</button></td>
+    <td><button id="switch_to_admin_enter"></button></td>
     <td>
-      <button id="switch_to_control_pw_type">change protection character password</button>
-      <button id="switch_to_control_tile_type">change protection areas</button>
-      <button id="switch_to_admin_thing_protect">change thing protection</button>
-      <button id="toggle_tile_draw">toggle protection character drawing</button>
+      <button id="switch_to_control_pw_type"></button>
+      <button id="switch_to_control_tile_type"></button>
+      <button id="switch_to_admin_thing_protect"></button>
+      <button id="toggle_tile_draw"></button>
     </td>
   <tr>
   </tr>
@@ -127,7 +127,7 @@ let mode_helps = {
         'long': 'This mode allows you to study the map and its tiles in detail.  Move the question mark over a tile, and the right half of the screen will show detailed information on it.  Toggle the map view to show or hide different information layers.'},
     'edit': {
         'short': 'world edit',
-        'long': 'This mode allows you to change the game world in various ways.  Individual map tiles can be protected by "protection characters", which you can see by toggling into the protections map view.  You can edit a tile if you set the map edit password that matches its protection character.  The character "." marks the absence of protection:  Such tiles can always be edited.'
+        'long': 'This mode allows you to change the game world in various ways.  Individual map tiles can be protected by "protection characters", which you can see by toggling into the protections map view.  You can edit a tile if you set the world edit password that matches its protection character.  The character "." marks the absence of protection:  Such tiles can always be edited.'
     },
     'name_thing': {
         'short': 'name thing',
@@ -194,6 +194,28 @@ let mode_helps = {
         'long': 'This mode allows you access to actions limited to administrators.'
     }
 }
+let key_descriptions = {
+    'help': 'help',
+    'flatten': 'flatten surroundings',
+    'teleport': 'teleport',
+    'take_thing': 'pick up thing',
+    'drop_thing': 'drop thing',
+    'toggle_map_mode': 'toggle map view',
+    'toggle_tile_draw': 'toggle protection character drawing',
+    'hex_move_upleft': 'up-left',
+    'hex_move_upright': 'up-right',
+    'hex_move_right': 'right',
+    'hex_move_left': 'left',
+    'hex_move_downleft': 'down-left',
+    'hex_move_downright': 'down-right',
+    'square_move_up': 'up',
+    'square_move_left': 'left',
+    'square_move_down': 'down',
+    'square_move_right': 'right',
+};
+for (const mode_name of Object.keys(mode_helps)) {
+    key_descriptions['switch_to_' + mode_name] = mode_helps[mode_name].short;
+};
 
 let rows_selector = document.getElementById("n_rows");
 let cols_selector = document.getElementById("n_cols");
@@ -216,6 +238,15 @@ for (let key_selector of key_selectors) {
     restore_selector_value(key_selector);
 }
 
+function escapeHTML(str) {
+    return str.
+        replace(/&/g, '&amp;').
+        replace(/</g, '&lt;').
+        replace(/>/g, '&gt;').
+        replace(/'/g, '&apos;').
+        replace(/"/g, '&quot;');
+};
+
 let terminal = {
   foreground: 'white',
   background: 'black',
@@ -249,14 +280,6 @@ let terminal = {
       }, 100);
   },
   refresh: function() {
-      function escapeHTML(str) {
-          return str.
-              replace(/&/g, '&amp;').
-              replace(/</g, '&lt;').
-              replace(/>/g, '&gt;').
-              replace(/'/g, '&apos;').
-              replace(/"/g, '&quot;');
-      };
       let pre_content = '';
       for (let y = 0; y < this.rows; y++) {
           let line = this.content[y].join('');
@@ -499,6 +522,7 @@ class Mode {
         this.name = name;
         this.short_desc = mode_helps[name].short;
         this.available_modes = [];
+        this.available_actions = [];
         this.has_input_prompt = has_input_prompt;
         this.shows_info= shows_info;
         this.is_intro = is_intro;
@@ -570,16 +594,28 @@ let tui = {
   mode_control_pw_pw: new Mode('control_pw_pw', true),
   mode_control_tile_type: new Mode('control_tile_type', true),
   mode_control_tile_draw: new Mode('control_tile_draw'),
+  action_tasks: {
+      'flatten': 'FLATTEN_SURROUNDINGS',
+      'take_thing': 'PICK_UP',
+      'drop_thing': 'DROP',
+      'move': 'MOVE',
+  },
   init: function() {
+      this.mode_chat.available_modes = ["play", "study", "edit", "admin_enter"]
       this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter"]
+      this.mode_play.available_actions = ["move", "take_thing", "drop_thing", "teleport"];
       this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
+      this.mode_study.available_actions = ["toggle_map_mode", "move_explorer"];
       this.mode_admin.available_modes = ["admin_thing_protect", "control_pw_type",
                                          "control_tile_type", "chat",
                                          "study", "play", "edit"]
+      this.mode_admin.available_actions = ["move"];
       this.mode_control_tile_draw.available_modes = ["admin_enter"]
+      this.mode_control_tile_draw.available_actions = ["toggle_tile_draw"];
       this.mode_edit.available_modes = ["write", "annotate", "portal", "name_thing",
                                         "password", "chat", "study", "play",
                                         "admin_enter"]
+      this.mode_edit.available_actions = ["move", "flatten", "toggle_map_mode"]
       this.mode = this.mode_waiting_for_server;
       this.inputEl = document.getElementById("input");
       this.inputEl.focus();
@@ -589,39 +625,41 @@ let tui = {
       this.init_keys();
   },
   init_keys: function() {
+    document.getElementById("move_table").hidden = true;
     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 = {
-            [this.keys.square_move_up]: 'UP',
-            [this.keys.square_move_left]: 'LEFT',
-            [this.keys.square_move_down]: 'DOWN',
-            [this.keys.square_move_right]: 'RIGHT'
-        };
-        document.getElementById("move_upright").hidden = true;
-        document.getElementById("move_upleft").hidden = true;
-        document.getElementById("move_downright").hidden = true;
-        document.getElementById("move_downleft").hidden = true;
-        document.getElementById("move_up").hidden = false;
-        document.getElementById("move_down").hidden = false;
-    } else if (game.map_geometry == 'Hex') {
-        document.getElementById("move_upright").hidden = false;
-        document.getElementById("move_upleft").hidden = false;
-        document.getElementById("move_downright").hidden = false;
-        document.getElementById("move_downleft").hidden = false;
-        document.getElementById("move_up").hidden = true;
-        document.getElementById("move_down").hidden = true;
-        this.movement_keys = {
-            [this.keys.hex_move_upleft]: 'UPLEFT',
-            [this.keys.hex_move_upright]: 'UPRIGHT',
-            [this.keys.hex_move_right]: 'RIGHT',
-            [this.keys.hex_move_downright]: 'DOWNRIGHT',
-            [this.keys.hex_move_downleft]: 'DOWNLEFT',
-            [this.keys.hex_move_left]: 'LEFT'
-        };
+    this.movement_keys = {};
+    let geometry_prefix = 'undefinedMapGeometry_';
+    if (game.map_geometry) {
+        geometry_prefix = game.map_geometry.toLowerCase() + '_';
+    }
+    for (const key_name of Object.keys(key_descriptions)) {
+        if (key_name.startsWith(geometry_prefix)) {
+            let direction = key_name.split('_')[2].toUpperCase();
+            let key = this.keys[key_name];
+            this.movement_keys[key] = direction;
+        }
+    };
+    for (const move_button of document.querySelectorAll('[id*="_move_"]')) {
+        if (move_button.id.startsWith('key_')) {
+            continue;
+        }
+        move_button.hidden = true;
+    };
+    for (const move_button of document.querySelectorAll('[id^="' + geometry_prefix + 'move_"]')) {
+        document.getElementById("move_table").hidden = false;
+        move_button.hidden = false;
     };
+    for (let el of document.getElementsByTagName("button")) {
+      let action_desc = key_descriptions[el.id];
+      let action_key = '[' + this.keys[el.id] + ']';
+      el.innerHTML = escapeHTML(action_desc) + '<br /><span class="keyboard_controlled">' + escapeHTML(action_key) + '</span>';
+    }
+  },
+  task_action_on: function(action) {
+      return game.tasks.includes(this.action_tasks[action]);
   },
   switch_mode: function(mode_name) {
     if (this.mode.name == 'control_tile_draw') {
@@ -672,25 +710,21 @@ let tui = {
         el.disabled = true;
     }
     document.getElementById("help").disabled = false;
-    if (this.mode.name == 'play' || this.mode.name == 'study' || this.mode.name == 'control_tile_draw' || this.mode.name == 'edit' || this.mode.name == 'admin') {
-        for (const move_key of document.querySelectorAll('[id^="move_"]')) {
-            move_key.disabled = false;
-        }
-    }
-    if (!this.mode.is_intro && this.mode.name != 'play') {
-        document.getElementById("switch_to_play").disabled = false;
-    }
-    if (!this.mode.is_intro && this.mode.name != 'study') {
-        document.getElementById("switch_to_study").disabled = false;
-    }
-    if (!this.mode.is_intro && this.mode.name != 'chat') {
-        document.getElementById("switch_to_chat").disabled = false;
-    }
-    if (!this.mode.is_intro && this.mode.name != 'edit') {
-        document.getElementById("switch_to_edit").disabled = false;
+    for (const action of this.mode.available_actions) {
+        if (["move", "move_explorer"].includes(action)) {
+            for (const move_key of document.querySelectorAll('[id*="_move_"]')) {
+                move_key.disabled = false;
+            }
+        } else if (Object.keys(this.action_tasks).includes(action)) {
+            if (this.task_action_on(action)) {
+                document.getElementById(action).disabled = false;
+            }
+        } else {
+            document.getElementById(action).disabled = false;
+        };
     }
-    if (!this.mode.is_intro && this.mode.name != 'admin' && this.mode.name != 'admin_enter') {
-        document.getElementById("switch_to_admin_enter").disabled = false;
+    for (const mode_name of this.mode.available_modes) {
+            document.getElementById('switch_to_' + mode_name).disabled = false;
     }
     if (this.mode.name == 'login') {
         if (this.login_name) {
@@ -698,32 +732,6 @@ let tui = {
         } else {
             this.log_msg("? need login name");
         }
-    } else if (this.mode.name == 'play') {
-        if (game.tasks.includes('PICK_UP')) {
-            document.getElementById("take_thing").disabled = false;
-        }
-        if (game.tasks.includes('DROP')) {
-            document.getElementById("drop_thing").disabled = false;
-        }
-        if (game.tasks.includes('MOVE')) {
-        }
-        document.getElementById("teleport").disabled = false;
-    } else if (this.mode.name == 'edit') {
-        if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
-            document.getElementById("flatten").disabled = false;
-        }
-        document.getElementById("switch_to_annotate").disabled = false;
-        document.getElementById("switch_to_write").disabled = false;
-        document.getElementById("switch_to_portal").disabled = false;
-        document.getElementById("switch_to_password").disabled = false;
-        document.getElementById("switch_to_name_thing").disabled = false;
-        document.getElementById("toggle_map_mode").disabled = false;
-    } else if (this.mode.name == 'admin') {
-        document.getElementById("switch_to_control_pw_type").disabled = false;
-        document.getElementById("switch_to_control_tile_type").disabled = false;
-        document.getElementById("switch_to_admin_thing_protect").disabled = false;
-    } else if (this.mode.name == 'study') {
-        document.getElementById("toggle_map_mode").disabled = false;
     } else if (this.mode.is_single_char_entry) {
         this.show_help = true;
     } else if (this.mode.name == 'admin_enter') {
@@ -737,7 +745,6 @@ let tui = {
     } else if (this.mode.name == 'control_pw_pw') {
         this.log_msg('@ enter protection password for "' + this.tile_control_char + '":');
     } else if (this.mode.name == 'control_tile_draw') {
-        document.getElementById("toggle_tile_draw").disabled = false;
         this.log_msg('@ can draw protection character "' + this.tile_control_char + '", turn drawing on/off with [' + this.keys.toggle_tile_draw + '], finish with [' +  this.keys.switch_to_admin_enter + '].')
     }
     this.full_refresh();
@@ -1000,48 +1007,28 @@ let tui = {
           movement_keys_desc = Object.keys(this.movement_keys).join(',');
       }
       let content = this.mode.short_desc + " help\n\n" + this.mode.help_intro + "\n\n";
-      if (this.mode.name == 'play') {
-          content += "Available actions:\n";
-          if (game.tasks.includes('MOVE')) {
-              content += "[" + movement_keys_desc + "] – move player\n";
-          }
-          if (game.tasks.includes('PICK_UP')) {
-              content += "[" + this.keys.take_thing + "] – pick up thing\n";
-          }
-          if (game.tasks.includes('DROP')) {
-              content += "[" + this.keys.drop_thing + "] – drop thing\n";
-          }
-          content += "[" + tui.keys.teleport + "] – teleport\n";
-          content += '\n';
-      } else if (this.mode.name == 'study') {
-          content += "Available actions:\n";
-          content += '[' + movement_keys_desc + '] – move question mark\n';
-          content += '[' + this.keys.toggle_map_mode + '] – toggle map view\n';
-          content += '\n';
-      } else if (this.mode.name == 'edit') {
-          content += "Available actions:\n";
-          if (game.tasks.includes('MOVE')) {
-              content += "[" + movement_keys_desc + "] – move player\n";
-          }
-          if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
-              content += "[" + tui.keys.flatten + "] – flatten surroundings\n";
-          }
-          content += '[' + this.keys.toggle_map_mode + '] – toggle map view\n';
-          content += '\n';
-      } else if (this.mode.name == 'control_tile_draw') {
-          content += "Available actions:\n";
-          content += "[" + tui.keys.toggle_tile_draw + "] – toggle protection character drawing\n";
-          content += '\n';
-      } else if (this.mode.name == 'chat') {
+      if (this.mode.name == 'chat') {
           content += '/nick NAME – re-name yourself to NAME\n';
           content += '/' + this.keys.switch_to_play + ' or /play – switch to play mode\n';
           content += '/' + this.keys.switch_to_study + ' or /study – switch to study mode\n';
-          content += '/' + this.keys.switch_to_edit + ' or /edit – switch to map edit mode\n';
+          content += '/' + this.keys.switch_to_edit + ' or /edit – switch to world edit mode\n';
           content += '/' + this.keys.switch_to_admin_enter + ' or /admin – switch to admin mode\n';
-      } else if (this.mode.name == 'admin') {
+      } else if (this.mode.available_actions.length > 0) {
           content += "Available actions:\n";
-          if (game.tasks.includes('MOVE')) {
-              content += "[" + movement_keys_desc + "] – move player\n";
+          for (let action of this.mode.available_actions) {
+              if (Object.keys(this.action_tasks).includes(action)) {
+                  if (!this.task_action_on(action)) {
+                      continue;
+                  }
+              }
+              if (action == 'move_explorer') {
+                  action = 'move';
+              }
+              if (action == 'move') {
+                  content += "[" + movement_keys_desc + "] – move\n"
+              } else {
+                  content += "[" + this.keys[action] + "] – " + key_descriptions[action] + "\n";
+              }
           }
           content += '\n';
       }
@@ -1391,14 +1378,11 @@ tui.inputEl.addEventListener('keydown', (event) => {
     } else if (tui.mode.name == 'play') {
           if (tui.mode.mode_switch_on_key(event)) {
               null;
-          } else if (event.key === tui.keys.take_thing
-                     && game.tasks.includes('PICK_UP')) {
+          } else if (event.key === tui.keys.take_thing && tui.task_action_on('take_thing')) {
               server.send(["TASK:PICK_UP"]);
-          } else if (event.key === tui.keys.drop_thing
-                     && game.tasks.includes('DROP')) {
+          } else if (event.key === tui.keys.drop_thing && tui.task_action_on('drop_thing')) {
               server.send(["TASK:DROP"]);
-          } else if (event.key in tui.movement_keys
-                     && game.tasks.includes('MOVE')) {
+          } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
               server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
           } else if (event.key === tui.keys.teleport) {
               game.teleport();
@@ -1422,18 +1406,15 @@ tui.inputEl.addEventListener('keydown', (event) => {
     } else if (tui.mode.name == 'admin') {
         if (tui.mode.mode_switch_on_key(event)) {
               null;
-        } else if (event.key in tui.movement_keys
-                   && game.tasks.includes('MOVE')) {
+        } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
             server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
         };
     } else if (tui.mode.name == 'edit') {
         if (tui.mode.mode_switch_on_key(event)) {
               null;
-        } else if (event.key in tui.movement_keys
-                   && game.tasks.includes('MOVE')) {
+        } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
             server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
-        } else if (event.key === tui.keys.flatten
-                   && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
+        } else if (event.key === tui.keys.flatten && tui.task_action_on('flatten')) {
             server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
         } else if (event.key == tui.keys.toggle_map_mode) {
             tui.toggle_map_mode();
@@ -1475,12 +1456,17 @@ window.setInterval(function() {
 }, 5000);
 window.setInterval(function() {
     let val = "?";
+    let span_decoration = "none";
     if (document.activeElement == tui.inputEl) {
         val = "on (click outside terminal to change)";
     } else {
         val = "off (click into terminal to change)";
+        span_decoration = "line-through";
     };
     document.getElementById("keyboard_control").textContent = val;
+    for (const span of document.querySelectorAll('.keyboard_controlled')) {
+        span.style.textDecoration = span_decoration;
+    }
 }, 100);
 document.getElementById("terminal").onclick = function() {
     tui.inputEl.focus();
@@ -1515,60 +1501,15 @@ document.getElementById("flatten").onclick = function() {
 document.getElementById("teleport").onclick = function() {
     game.teleport();
 };
-document.getElementById("move_upleft").onclick = function() {
-    if (tui.mode.name == 'play' || tui.mode.name == 'edit') {
-        server.send(['TASK:MOVE', 'UPLEFT']);
-    } else {
-        explorer.move('UPLEFT');
-    };
-};
-document.getElementById("move_left").onclick = function() {
-    if (tui.mode.name == 'play' || tui.mode.name == 'edit') {
-        server.send(['TASK:MOVE', 'LEFT']);
-    } else {
-        explorer.move('LEFT');
-    };
-};
-document.getElementById("move_downleft").onclick = function() {
-    if (tui.mode.name == 'play' || tui.mode.name == 'edit') {
-        server.send(['TASK:MOVE', 'DOWNLEFT']);
-    } else {
-        explorer.move('DOWNLEFT');
-    };
-};
-document.getElementById("move_down").onclick = function() {
-    if (tui.mode.name == 'play' || tui.mode.name == 'edit') {
-        server.send(['TASK:MOVE', 'DOWN']);
-    } else {
-        explorer.move('DOWN');
-    };
-};
-document.getElementById("move_up").onclick = function() {
-    if (tui.mode.name == 'play' || tui.mode.name == 'edit') {
-        server.send(['TASK:MOVE', 'UP']);
-    } else {
-        explorer.move('UP');
-    };
-};
-document.getElementById("move_upright").onclick = function() {
-    if (tui.mode.name == 'play' || tui.mode.name == 'edit') {
-        server.send(['TASK:MOVE', 'UPRIGHT']);
-    } else {
-        explorer.move('UPRIGHT');
-    };
-};
-document.getElementById("move_right").onclick = function() {
-    if (tui.mode.name == 'play' || tui.mode.name == 'edit') {
-        server.send(['TASK:MOVE', 'RIGHT']);
-    } else {
-        explorer.move('RIGHT');
-    };
-};
-document.getElementById("move_downright").onclick = function() {
-    if (tui.mode.name == 'play' || tui.mode.name == 'edit') {
-        server.send(['TASK:MOVE', 'DOWNRIGHT']);
-    } else {
-        explorer.move('DOWNRIGHT');
+for (const move_button of document.querySelectorAll('[id*="_move_"]')) {
+    let direction = move_button.id.split('_')[2].toUpperCase();
+    move_button.onclick = function() {
+        if (tui.mode.available_actions.includes("move")
+            || tui.mode.available_actions.includes("move_explorer")) {
+            server.send(['TASK:MOVE', direction]);
+        } else {
+            explorer.move(direction);
+        };
     };
 };
 </script>