/// <summary> /// Get the action mapped to an input. /// </summary> /// <param name="input">The input.</param> /// <returns>The action.</returns> public string GetAction(InputMapping input) { return this.inputMappings[input]; }
/// <summary> /// Parse an input string to an <see cref="InputMapping"/>. /// </summary> /// <param name="str">The input string.</param> /// <returns>The <see cref="InputMapping"/> parsed from the string.</returns> public static InputMapping Parse(string str) { if (str.Length < 2) { throw new InputMappingException("Invalid mapping \"" + str + "\" (too short)"); } InputMapping im = new InputMapping(); switch (str[0]) { case '+': im.KeyDown = true; break; case '-': im.KeyDown = false; break; default: throw new InputMappingException("Invalid mapping \"" + str + "\" (invalid key state specifier)"); } string keyName = str.Substring(1); Keyboard.Key key; if (Enum.TryParse<Keyboard.Key>(keyName, out key)) { im.Key = key; im.Type = InputType.Keyboard; return im; } else { switch (keyName) { case "mouse1": im.Button = Mouse.Button.Left; im.Type = InputType.Mouse; break; case "mouse2": im.Button = Mouse.Button.Right; im.Type = InputType.Mouse; break; case "mouse3": im.Button = Mouse.Button.Middle; im.Type = InputType.Mouse; break; case "mouse4": im.Button = Mouse.Button.XButton1; im.Type = InputType.Mouse; break; case "mouse5": im.Button = Mouse.Button.XButton2; im.Type = InputType.Mouse; break; case "wheel": im.Type = InputType.MouseWheel; break; default: throw new InputMappingException("Invalid input name"); } return im; } }
/// <summary> /// Register an input to action mapping. /// </summary> /// <param name="input">The input.</param> /// <param name="action">The action.</param> public void AddMapping(InputMapping input, string action) { this.inputMappings.Add(input, action); this.actionMappings.Add(action, input); }
/// <summary> /// Called when the mouse wheel is moved. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> protected void OnMouseWheelMoved(object sender, MouseWheelEventArgs e) { GameEngine.Instance.ConsoleViewer.OnInput(e); if (GameEngine.Instance.ConsoleViewer.Enabled) { return; } InputMapping input = new InputMapping(e.Delta < 0); string action; if (!this.inputMappings.TryGetValue(input, out action)) { return; } if (this.ActionEvent != null) { this.ActionEvent(this, action); } }
/// <summary> /// Called when a mouse button is released. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> protected void OnMouseButtonReleased(object sender, MouseButtonEventArgs e) { if (GameEngine.Instance.ConsoleViewer.Enabled) { return; } InputMapping input = new InputMapping(e.Button, false); string action; if (!this.inputMappings.TryGetValue(input, out action)) { return; } if (this.ActionEvent != null) { this.ActionEvent(this, action); } }
/// <summary> /// Called when a key is pressed. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> protected void OnKeyPressed(object sender, KeyEventArgs e) { GameEngine.Instance.ConsoleViewer.OnInput(e); if (GameEngine.Instance.ConsoleViewer.Enabled) { return; } InputMapping input = new InputMapping(e.Code, true); string action; if (!this.inputMappings.TryGetValue(input, out action)) { return; } if (this.ActionEvent != null) { this.ActionEvent(this, action); } }
/// <summary> /// Remove an input mapping. /// </summary> /// <param name="input">The input to remove.</param> public void RemoveMapping(InputMapping input) { string action = this.inputMappings[input]; this.inputMappings.Remove(input); this.actionMappings.Remove(action); }