Skip to content

Instantly share code, notes, and snippets.

@RoyAwesome
Created July 3, 2012 06:25
Show Gist options
  • Save RoyAwesome/3038058 to your computer and use it in GitHub Desktop.
Save RoyAwesome/3038058 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Cubetex
{
class ClientInputProcessor
{
const bool SilentMoveCommands = true;
public static bool inputForward = false;
public static bool inputLeft = false;
public static bool inputRight = false;
public static bool inputBackward = false;
private static int MouseXMag = 0;
private static int MouseYMag = 0;
public static bool inputMouseWheelDown = false;
public static bool inputMouseWheelUp = false;
private static UserCommand ucmd;
#region ConsoleComamnds
[ConsoleCommand("+Forward", SilentMoveCommands)]
[ConsoleCommand("-Forward", SilentMoveCommands)]
public static void InputForward(ConCommandArgs args)
{
if (args.CommandName.StartsWith("+"))
{
inputForward = true;
}
if (args.CommandName.StartsWith("-"))
{
inputForward = false;
}
}
[ConsoleCommand("+Left", SilentMoveCommands)]
[ConsoleCommand("-Left", SilentMoveCommands)]
public static void InputLeft(ConCommandArgs args)
{
if (args.CommandName.StartsWith("+"))
{
inputLeft = true;
}
if (args.CommandName.StartsWith("-"))
{
inputLeft = false;
}
}
[ConsoleCommand("+Right", SilentMoveCommands)]
[ConsoleCommand("-Right", SilentMoveCommands)]
public static void InputRight(ConCommandArgs args)
{
if (args.CommandName.StartsWith("+"))
{
inputRight = true;
}
if (args.CommandName.StartsWith("-"))
{
inputRight = false;
}
}
[ConsoleCommand("+Backward", SilentMoveCommands)]
[ConsoleCommand("-Backward", SilentMoveCommands)]
public static void InputBackward(ConCommandArgs args)
{
if (args.CommandName.StartsWith("+"))
{
inputBackward = true;
}
if (args.CommandName.StartsWith("-"))
{
inputBackward = false;
}
}
[ConsoleCommand("MouseXAxis", SilentMoveCommands)]
public static void MouseXAxis(ConCommandArgs args)
{
MouseXMag = int.Parse(args.args[0]);
}
[ConsoleCommand("MouseYAxis", SilentMoveCommands)]
public static void MouseYAxis(ConCommandArgs args)
{
MouseYMag = int.Parse(args.args[0]);
}
[ConsoleCommand("MouseScrollUp", SilentMoveCommands)]
public static void MouseScrollUp(ConCommandArgs args)
{
inputMouseWheelUp = true;
}
[ConsoleCommand("MouseScrollDown", SilentMoveCommands)]
public static void MouseScrollDown(ConCommandArgs args)
{
inputMouseWheelDown = true;
}
#endregion
public static IGameEntity CommandReciever = null;
public static void RegisterCommandReciever(IGameEntity entity)
{
CommandReciever = entity;
}
public static void ClearCommandReciever()
{
CommandReciever = null;
}
public static void SendUserCommand()
{
if (CommandReciever == null) return;
BaseWorldBehavior behav = (BaseWorldBehavior)CommandReciever.GetBehavior(typeof(BaseController));
(behav as BaseController).DoUserCommand(GetCurrentUsercommand());
}
public static void Update(GameTime deltaTime)
{
ProcessUserCommand();
inputMouseWheelDown = false;
inputMouseWheelUp = false;
if(CommandReciever != null) SendUserCommand();
}
private static void ProcessUserCommand()
{
InputCommands cmd = InputCommands.NONE;
MouseWheelState MWS = MouseWheelState.None;
if (inputBackward) cmd |= InputCommands.Back;
if (inputForward) cmd |= InputCommands.Forward;
if (inputRight) cmd |= InputCommands.Right;
if (inputLeft) cmd |= InputCommands.Left;
if(inputMouseWheelUp) MWS = MouseWheelState.Up;
if(inputMouseWheelDown) MWS = MouseWheelState.Down;
ucmd = new UserCommand(cmd, MouseXMag, MouseYMag, MWS );
}
public static UserCommand GetCurrentUsercommand()
{
return ucmd;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Nuclex.UserInterface;
using Nuclex;
using Nuclex.Input;
using Microsoft.Xna.Framework.Input;
namespace Cubetex
{
class InputController
{
static Dictionary<Keys, string> KeyBinds = new Dictionary<Keys, string>();
static Dictionary<MouseButtons, string> MouseBinds = new Dictionary<MouseButtons, string>();
[ConsoleCommand("Bind")]
public static void Bind(ConCommandArgs args)
{
if (args.args.Length < 3)
{
GameConsole.ConsoleMessage("Usage: bind [Key] [Command] <Override Console Block>");
return;
}
Keys key;
if (Enum.TryParse<Keys>(args.args[0], true, out key))
{
BindKey(key, args.args[1]);
}
MouseButtons button;
if (Enum.TryParse<MouseButtons>(args.args[0], true, out button))
{
BindKey(button, args.args[1]);
}
}
public static void BindKey(Keys key, string Command)
{
KeyBinds[key] = Command;
GameConsole.ConsoleMessage("Bound Key: " + key + " To " + Command);
}
public static void BindKey(MouseButtons key, string Command)
{
MouseBinds[key] = Command;
GameConsole.ConsoleMessage("Bound Key: " + key + " To " + Command);
}
public static void ProcessKeyPress(Keys key)
{
if (key == Keys.F1) GameConsole.DoCommand("ActivateConsole");
if (GameGuiManager.Gui.HasKeyboardFocus())
{
return;
}
if (!KeyBinds.ContainsKey(key)) return;
if (GameConsole.Active) return;
GameConsole.DoCommand(KeyBinds[key]);
}
public static void PrecessKeyRelease(Keys key)
{
if (GameGuiManager.Gui.HasKeyboardFocus())
{
return;
}
if (!KeyBinds.ContainsKey(key)) return;
if (GameConsole.Active) return;
if (!KeyBinds[key].StartsWith("+")) return;
string cmd = KeyBinds[key].Replace('+', '-');
GameConsole.DoCommand(cmd);
}
public static void ProcessButtonPress(MouseButtons button)
{
MouseState state = Mouse.GetState();
if(GameGuiManager.Gui.InjectInput(state.X, state.Y, button)) return;
if (!MouseBinds.ContainsKey(button)) return;
if (GameConsole.Active) return;
GameConsole.DoCommand(MouseBinds[button]);
}
public static void ProcessButtonRelease(MouseButtons button)
{
MouseState state = Mouse.GetState();
if (GameGuiManager.Gui.InjectInput(state.X, state.Y, button, false)) return;
if (!MouseBinds.ContainsKey(button)) return;
if (GameConsole.Active) return;
string cmd = MouseBinds[button].Replace('+', '-');
GameConsole.DoCommand(cmd);
}
public static void KeyPressed(Keys key)
{
ProcessKeyPress(key);
}
public static void KeyReleased(Keys Key)
{
PrecessKeyRelease(Key);
}
public static void ProcessCharacterPress(char c)
{
if (!GameGuiManager.Gui.HasKeyboardFocus()) return;
GameGuiManager.Gui.InjectKeypress(c);
}
public static void ReadDefaultBindFile()
{
using (StreamReader file = new StreamReader("Settings/KeyBinds.txt"))
{
while (!file.EndOfStream)
{
GameConsole.DoCommand(file.ReadLine());
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cubetex
{
[Flags]
enum InputCommands
{
NONE = 0x0,
Forward = 0x1,
Back = 0x2,
Left = 0x4,
Right = 0x8,
}
enum MouseWheelState : byte
{
Up,
Down,
None,
}
struct UserCommand
{
public InputCommands commands;
public int MouseDX;
public int MouseDY;
public MouseWheelState MouseWheel;
public UserCommand(InputCommands cmds = InputCommands.NONE, int mdx = 0, int mdy = 0, MouseWheelState mws = MouseWheelState.None)
{
commands = cmds;
MouseDX = mdx;
MouseDY = mdy;
MouseWheel = mws;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment