Loads and loads of small improvements I added while developing on my game

This commit is contained in:
Michel Fedde 2021-03-02 19:54:19 +01:00
parent 41421b1df9
commit a7c71e7ea1
107 changed files with 2278 additions and 1023 deletions

View file

@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows.Documents;
using System.Windows.Forms;
using OpenTK.Input;
using SharpDX.Win32;
namespace SM.Base.Controls
{
public class Keyboard
{
internal static KeyboardState? _keyboardState;
internal static List<Key> _lastPressedKeys = new List<Key>();
public static bool IsAnyKeyPressed => _keyboardState?.IsAnyKeyDown == true;
internal static void SetStage()
{
if (_keyboardState.HasValue)
{
_lastPressedKeys = new List<Key>();
foreach (object o in Enum.GetValues(typeof(Key)))
{
if (_keyboardState.Value[(Key)o]) _lastPressedKeys.Add((Key)o);
}
}
_keyboardState = OpenTK.Input.Keyboard.GetState();
}
public static bool IsDown(Key key, bool once = false) => _keyboardState?[key] == true && !(once && _lastPressedKeys.Contains(key));
public static bool WasDown(Key key) => _keyboardState?[key] == false && _lastPressedKeys.Contains(key);
public static bool IsUp(Key key, bool once = false) => _keyboardState?[key] == false && !(once && !_lastPressedKeys.Contains(key));
public static bool AreSpecificKeysPressed(int startIndex, int endIndex, bool once = false)
{
if (startIndex > endIndex)
throw new ArgumentException("The startIndex is greater than the endIndex.", nameof(startIndex));
int length = endIndex - startIndex;
for (int i = 0; i < length; i++)
{
int actualIndex = i + startIndex;
Key key = (Key) actualIndex;
if (IsDown(key, once)) return true;
}
return false;
}
public static bool AreSpecificKeysPressed(params Key[] keys) => AreSpecificKeysPressed(false, keys);
public static bool AreSpecificKeysPressed(bool once, params Key[] keys)
{
foreach (Key key in keys)
{
if (IsDown(key, once)) return true;
}
return false;
}
public static bool AreSpecificKeysPressed(int startIndex, int endIndex, out Key[] pressedKeys, bool once = false)
{
if (startIndex > endIndex)
throw new ArgumentException("The startIndex is greater than the endIndex.", nameof(startIndex));
int length = endIndex - startIndex;
bool success = false;
List<Key> keys = new List<Key>();
for (int i = 0; i < length; i++)
{
int actualIndex = i + startIndex;
Key key = (Key)actualIndex;
if (IsDown(key, once))
{
keys.Add(key);
success = true;
}
}
pressedKeys = keys.ToArray();
return success;
}
public static bool AreSpecificKeysPressed(out Key[] pressedKey, params Key[] keys) => AreSpecificKeysPressed(false, out pressedKey, keys);
public static bool AreSpecificKeysPressed(bool once, out Key[] pressedKeys, params Key[] keys)
{
List<Key> pressedKey = new List<Key>();
bool success = false;
foreach (Key key in keys)
{
if (IsDown(key, once))
{
pressedKey.Add(key);
success = true;
}
}
pressedKeys = pressedKey.ToArray();
return success;
}
}
}

View file

@ -1,7 +1,11 @@
#region usings
using System;
using System.Collections.Generic;
using System.Windows.Documents;
using OpenTK;
using OpenTK.Input;
using SM.Base.Windows;
#endregion
@ -11,41 +15,51 @@ namespace SM.Base.Controls
/// Mouse controller
/// </summary>
/// <typeparam name="TWindow">The type of window this controller is connected to.</typeparam>
public class Mouse<TWindow>
where TWindow : IGenericWindow
public class Mouse
{
/// <summary>
/// The window it is connected to.
/// </summary>
protected TWindow _window;
/// <summary>
/// The constructor
/// </summary>
/// <param name="window">The window, its listen to.</param>
protected internal Mouse(TWindow window)
{
_window = window;
}
internal static MouseState? _mouseState;
internal static List<MouseButton> _lastButtonsPressed = new List<MouseButton>();
/// <summary>
/// The current position of the mouse in the screen.
/// </summary>
public Vector2 InScreen { get; private set; }
public static Vector2 InScreen { get; private set; }
/// <summary>
/// The current position of the mouse in the screen from 0..1.
/// </summary>
public Vector2 InScreenNormalized { get; private set; }
public static Vector2 InScreenNormalized { get; private set; }
/// <summary>
/// The event to update the values.
/// </summary>
/// <param name="mmea">The event args.</param>
protected void MouseMoveEvent(MouseMoveEventArgs mmea)
internal static void MouseMoveEvent(MouseMoveEventArgs mmea, IGenericWindow window)
{
InScreen = new Vector2(mmea.X, mmea.Y);
InScreenNormalized = new Vector2(mmea.X / (float) _window.Width, mmea.Y / (float) _window.Height);
InScreenNormalized = new Vector2(mmea.X / (float)window.Width, mmea.Y / (float)window.Height);
}
internal static void SetState()
{
if (_mouseState.HasValue)
{
_lastButtonsPressed = new List<MouseButton>();
foreach (object o in Enum.GetValues(typeof(MouseButton)))
{
if (_mouseState.Value[(MouseButton)o]) _lastButtonsPressed.Add((MouseButton)o);
}
}
_mouseState = OpenTK.Input.Mouse.GetState();
}
public static bool IsDown(MouseButton button, bool once = false) => _mouseState?[button] == true && !(once && _lastButtonsPressed.Contains(button));
public static bool IsUp(MouseButton button, bool once = false) =>
_mouseState?[button] == false && !(once && !_lastButtonsPressed.Contains(button));
}
}