Merge pull request #137 from phath0m/monogame

Added MonoGame.Extended.Input nuget package
This commit is contained in:
Michael VanOverbeek 2017-08-05 12:12:42 -04:00 committed by GitHub
commit cc42d10200
4 changed files with 51 additions and 195 deletions

View file

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended.Input.InputListeners;
using ShiftOS.Engine;
using ShiftOS.Frontend.Desktop;
using ShiftOS.Frontend.GUI;
@ -279,15 +280,18 @@ namespace ShiftOS.Frontend.GraphicsSubsystem
public class KeyEvent
{
public KeyEvent(bool control, bool alt, bool shift, Keys key)
public KeyEvent(KeyboardEventArgs e)
{
ControlDown = control;
AltDown = alt;
ShiftDown = shift;
Key = key;
KeyChar = key.ToCharacter(shift);
ControlDown = false;
ShiftDown = e.Modifiers.HasFlag(KeyboardModifiers.Shift);
ControlDown = e.Modifiers.HasFlag(KeyboardModifiers.Control);
AltDown = e.Modifiers.HasFlag(KeyboardModifiers.Alt);
Key = e.Key;
KeyChar = e.Character ?? '\0' ;
}
public bool ControlDown { get; private set; }
public bool AltDown { get; private set; }
public bool ShiftDown { get; set; }
@ -295,142 +299,4 @@ namespace ShiftOS.Frontend.GraphicsSubsystem
public char KeyChar { get; private set; }
}
public static class KeysExtensions
{
/*
* Notice: The following keymapping does <i>not</i> take into account the user's keyboard
* layout. This is written under the assumption the keyboard is en_US.
*
* @MichaelTheShifter I'm leaving you to figure out how to make this work with other layouts.
*
* My suggestion would be to simply do what you are doing with strings, define a JSON file
* mapping each character to its associated character.
*/
private static Dictionary<Keys, char> keymapDefault = new Dictionary<Keys, char>() {
{ Keys.Space, ' ' },
{ Keys.Tab, '\t' },
{ Keys.Enter, '\n' },
{ Keys.Back, '\b' },
{ Keys.A, 'a'},
{ Keys.B, 'b'},
{ Keys.C, 'c' },
{ Keys.D, 'd' },
{ Keys.E, 'e' },
{ Keys.F, 'f' },
{ Keys.G, 'g' },
{ Keys.H, 'h' },
{ Keys.I, 'i' },
{ Keys.J, 'j' },
{ Keys.K, 'k' },
{ Keys.L, 'l' },
{ Keys.M, 'm' },
{ Keys.N, 'n' },
{ Keys.O, 'o' },
{ Keys.P, 'p' },
{ Keys.Q, 'q' },
{ Keys.R, 'r' },
{ Keys.S, 's' },
{ Keys.T, 't' },
{ Keys.U, 'u' },
{ Keys.V, 'v' },
{ Keys.W, 'w' },
{ Keys.X, 'x' },
{ Keys.Y, 'y' },
{ Keys.Z, 'z' },
{ Keys.D0, '0' },
{ Keys.D1, '1' },
{ Keys.D2, '2' },
{ Keys.D3, '3' },
{ Keys.D4, '4' },
{ Keys.D5, '5' },
{ Keys.D6, '6' },
{ Keys.D7, '7' },
{ Keys.D8, '8' },
{ Keys.D9, '9' },
{ Keys.OemTilde, '`' },
{ Keys.OemMinus, '-' },
{ Keys.OemPlus, '+' },
{ Keys.OemOpenBrackets, '[' },
{ Keys.OemCloseBrackets, ']'},
{ Keys.OemBackslash, '\\'},
{ Keys.OemPipe, '\\' },
{ Keys.OemSemicolon, ';' },
{ Keys.OemQuotes, '\'' },
{ Keys.OemComma, ',' },
{ Keys.OemPeriod, '.' },
{ Keys.OemQuestion, '/' },
};
private static Dictionary<Keys, char> keymapShift = new Dictionary<Keys, char> () {
{ Keys.Space, ' ' },
{ Keys.Tab, '\t' },
{ Keys.Enter, '\n' },
{ Keys.Back, '\b' },
{ Keys.A, 'A'},
{ Keys.B, 'B'},
{ Keys.C, 'C' },
{ Keys.D, 'D' },
{ Keys.E, 'E' },
{ Keys.F, 'F' },
{ Keys.G, 'G' },
{ Keys.H, 'H' },
{ Keys.I, 'I' },
{ Keys.J, 'J' },
{ Keys.K, 'K' },
{ Keys.L, 'L' },
{ Keys.M, 'M' },
{ Keys.N, 'N' },
{ Keys.O, 'O' },
{ Keys.P, 'P' },
{ Keys.Q, 'Q' },
{ Keys.R, 'R' },
{ Keys.S, 'S' },
{ Keys.T, 'T' },
{ Keys.U, 'U' },
{ Keys.V, 'V' },
{ Keys.W, 'W' },
{ Keys.X, 'X' },
{ Keys.Y, 'Y' },
{ Keys.Z, 'Z' },
{ Keys.D0, ')' },
{ Keys.D1, '!' },
{ Keys.D2, '@' },
{ Keys.D3, '#' },
{ Keys.D4, '$' },
{ Keys.D5, '%' },
{ Keys.D6, '^' },
{ Keys.D7, '&' },
{ Keys.D8, '*' },
{ Keys.D9, '(' },
{ Keys.OemTilde, '~' },
{ Keys.OemMinus, '_' },
{ Keys.OemPlus, '+' },
{ Keys.OemOpenBrackets, '{' },
{ Keys.OemCloseBrackets, '}'},
{ Keys.OemBackslash, '|'},
{ Keys.OemPipe, '|' },
{ Keys.OemSemicolon, ':' },
{ Keys.OemQuotes, '\'' },
{ Keys.OemComma, '<' },
{ Keys.OemPeriod, '>' },
{ Keys.OemQuestion, '?' },
};
public static char ToCharacter(this Keys key, bool shift)
{
if (shift && keymapShift.ContainsKey(key))
{
return keymapShift[key];
}
if (!shift && keymapDefault.ContainsKey(key))
{
return keymapDefault[key];
}
return '\0'; // Ideally all keys should be included in this map
}
}
}

View file

@ -86,6 +86,14 @@
<Compile Include="Window.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="MonoGame.Extended, Version=0.6.568.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MonoGame.Extended.0.6.568\lib\portable-net45+win8+wpa81\MonoGame.Extended.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MonoGame.Extended.Input, Version=0.6.568.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MonoGame.Extended.Input.0.6.568\lib\portable-net45+win8+wpa81\MonoGame.Extended.Input.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MonoGame.Framework">
<HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\DesktopGL\MonoGame.Framework.dll</HintPath>
</Reference>

View file

@ -5,6 +5,7 @@ using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended.Input.InputListeners;
using Newtonsoft.Json;
using ShiftOS.Engine;
using ShiftOS.Frontend.GraphicsSubsystem;
@ -30,6 +31,8 @@ namespace ShiftOS.Frontend
private bool DisplayDebugInfo = false;
private KeyboardListener keyboardListener = new KeyboardListener ();
public ShiftOS()
{
Story.FailureRequested += (message) =>
@ -68,11 +71,36 @@ namespace ShiftOS.Frontend
//Fullscreen
graphicsDevice.IsFullScreen = uconf.Fullscreen;
// keyboard events
keyboardListener.KeyPressed += KeyboardListener_KeyPressed;
UIManager.Init(this);
}
private Keys lastKey = Keys.None;
private void KeyboardListener_KeyPressed(object sender, KeyboardEventArgs e)
{
if (e.Key == Keys.F11)
{
UIManager.Fullscreen = !UIManager.Fullscreen;
}
else if (e.Modifiers.HasFlag(KeyboardModifiers.Control) && e.Key == Keys.D)
{
DisplayDebugInfo = !DisplayDebugInfo;
}
else if (e.Modifiers.HasFlag(KeyboardModifiers.Control) && e.Key == Keys.E)
{
UIManager.ExperimentalEffects = !UIManager.ExperimentalEffects;
}
else
{
// Notice: I would personally recommend just using KeyboardEventArgs instead of KeyEvent
// from now on, but what ever. -phath0m
UIManager.ProcessKeyEvent(new KeyEvent(e));
}
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
@ -160,8 +188,7 @@ namespace ShiftOS.Frontend
MouseTexture = null;
// TODO: Unload any non ContentManager content here
}
private double kb_elapsedms = 0;
private double mouseMS = 0;
private MouseState LastMouseState;
@ -244,55 +271,8 @@ namespace ShiftOS.Frontend
//Let's see how keyboard input works.
//Hmmm... just like the mouse...
var keystate = Keyboard.GetState();
keyboardListener.Update(gameTime);
//Simple... just iterate through this list and generate some key events?
var keys = keystate.GetPressedKeys();
if (keys.Length > 0)
{
var key = keys.FirstOrDefault(x => x != Keys.LeftControl && x != Keys.RightControl && x != Keys.LeftShift && x != Keys.RightShift && x != Keys.LeftAlt && x != Keys.RightAlt);
if (lastKey != key)
{
kb_elapsedms = 0;
lastKey = key;
}
}
if (keystate.IsKeyDown(lastKey))
{
if (kb_elapsedms == 0 || kb_elapsedms >= 500)
{
if (lastKey == Keys.F11)
{
UIManager.Fullscreen = !UIManager.Fullscreen;
}
else
{
var shift = keystate.IsKeyDown(Keys.LeftShift) || keystate.IsKeyDown(Keys.RightShift);
var alt = keystate.IsKeyDown(Keys.LeftAlt) || keystate.IsKeyDown(Keys.RightAlt);
var control = keystate.IsKeyDown(Keys.LeftControl) || keystate.IsKeyDown(Keys.RightControl);
if (control && lastKey == Keys.D)
{
DisplayDebugInfo = !DisplayDebugInfo;
}
else if (control && lastKey == Keys.E)
{
UIManager.ExperimentalEffects = !UIManager.ExperimentalEffects;
}
else
{
var e = new KeyEvent(control, alt, shift, lastKey);
UIManager.ProcessKeyEvent(e);
}
}
}
kb_elapsedms += gameTime.ElapsedGameTime.TotalMilliseconds;
}
else
{
kb_elapsedms = 0;
}
//Cause layout update on all elements
UIManager.LayoutUpdate(gameTime);

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MonoGame.Extended" version="0.6.568" targetFramework="net45" />
<package id="MonoGame.Extended.Input" version="0.6.568" targetFramework="net45" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
<package id="TrueTypeSharp" version="1.0.5" targetFramework="net45" />
</packages>