130 lines
3.9 KiB
C#
130 lines
3.9 KiB
C#
using System.Numerics;
|
|
using Raylib_cs;
|
|
|
|
namespace DaggerFramework
|
|
{
|
|
public class RaylibInputHandler : InputHandler
|
|
{
|
|
public override int GetCharPressed()
|
|
{
|
|
return Raylib.GetCharPressed();
|
|
}
|
|
|
|
public override Vector2 GetInputDirection(KeyboardKey leftKey, KeyboardKey rightKey, KeyboardKey upKey, KeyboardKey downKey)
|
|
{
|
|
Vector2 dir = Vector2.Zero;
|
|
|
|
if (IsKeyboardKeyDown(leftKey))
|
|
dir += new Vector2(-1, 0);
|
|
if (IsKeyboardKeyDown(rightKey))
|
|
dir += new Vector2(1, 0);
|
|
if (IsKeyboardKeyDown(upKey))
|
|
dir += new Vector2(0, -1);
|
|
if (IsKeyboardKeyDown(downKey))
|
|
dir += new Vector2(0, 1);
|
|
|
|
return dir == Vector2.Zero ? Vector2.Zero : Vector2.Normalize(dir);
|
|
}
|
|
|
|
public override Vector2 GetInputDirection(string leftAction, string rightAction, string upAction, string downAction)
|
|
{
|
|
Vector2 dir = Vector2.Zero;
|
|
|
|
if (IsActionDown(leftAction))
|
|
dir += new Vector2(-1, 0);
|
|
if (IsActionDown(rightAction))
|
|
dir += new Vector2(1, 0);
|
|
if (IsActionDown(upAction))
|
|
dir += new Vector2(0, -1);
|
|
if (IsActionDown(downAction))
|
|
dir += new Vector2(0, 1);
|
|
|
|
|
|
return dir == Vector2.Zero ? Vector2.Zero : Vector2.Normalize(dir);
|
|
}
|
|
|
|
public override Vector2 GetMousePosition()
|
|
{
|
|
return Raylib.GetMousePosition();
|
|
}
|
|
|
|
public override float GetMouseWheelMovement()
|
|
{
|
|
return Raylib.GetMouseWheelMove();
|
|
}
|
|
|
|
public override void HideCursor()
|
|
{
|
|
Raylib.HideCursor();
|
|
}
|
|
|
|
public override bool IsActionDown(string action)
|
|
{
|
|
IEnumerable<InputAction>? mappings;
|
|
|
|
if (TryGetInputMappings(action, out mappings))
|
|
{
|
|
foreach (InputAction inputAction in mappings)
|
|
if (inputAction.IsDown(this)) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override bool IsActionPressed(string action)
|
|
{
|
|
IEnumerable<InputAction>? mappings;
|
|
|
|
if (TryGetInputMappings(action, out mappings))
|
|
{
|
|
foreach (InputAction inputAction in mappings)
|
|
if (inputAction.IsPressed(this)) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override bool IsActionReleased(string action)
|
|
{
|
|
IEnumerable<InputAction>? mappings;
|
|
|
|
if (TryGetInputMappings(action, out mappings))
|
|
{
|
|
foreach (InputAction inputAction in mappings)
|
|
if (inputAction.IsReleased(this)) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override bool IsKeyboardKeyDown(DaggerFramework.KeyboardKey key)
|
|
{
|
|
Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key;
|
|
OnInput?.Invoke();
|
|
return Raylib.IsKeyDown(rayKey);
|
|
}
|
|
|
|
public override bool IsMouseButtonDown(MouseButton button)
|
|
{
|
|
return Raylib.IsMouseButtonDown((Raylib_cs.MouseButton)button);
|
|
}
|
|
|
|
public override bool KeyboardKeyJustPressed(KeyboardKey key)
|
|
{
|
|
Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key;
|
|
return Raylib.IsKeyPressed(rayKey);
|
|
}
|
|
|
|
public override bool KeyboardKeyJustReleased(KeyboardKey key)
|
|
{
|
|
return Raylib.IsKeyReleased((Raylib_cs.KeyboardKey)key);
|
|
}
|
|
|
|
public override void SetMousePosition(Vector2 position)
|
|
{
|
|
Raylib.SetMousePosition((int)position.X, (int)position.Y);
|
|
}
|
|
public override void ShowCursor() => Raylib.ShowCursor();
|
|
public override bool IsCursorHidden() => Raylib.IsCursorHidden();
|
|
}
|
|
} |