From ca3a7891d075cf7bffdba199b59bb512200b55f6 Mon Sep 17 00:00:00 2001 From: Octal Date: Mon, 9 Oct 2023 16:18:36 -0500 Subject: [PATCH] Move mouse buttons to veldrid too --- Core/Input/InputManager.cs | 8 ++++-- Voxel/Client/Keybinding/Button.cs | 39 +++++--------------------- Voxel/Client/Keybinding/Keybinds.cs | 6 ++-- Voxel/Client/Rendering/GameRenderer.cs | 24 ++++++++-------- Voxel/Content/shaders/blit.frag.glsl | 3 +- Voxel/Content/shaders/blit.vert.glsl | 3 +- Voxel/Content/shaders/simple.frag.glsl | 3 +- Voxel/Content/shaders/simple.vert.glsl | 5 ++-- 8 files changed, 33 insertions(+), 58 deletions(-) diff --git a/Core/Input/InputManager.cs b/Core/Input/InputManager.cs index 977f9c2..dbefa2c 100644 --- a/Core/Input/InputManager.cs +++ b/Core/Input/InputManager.cs @@ -14,6 +14,7 @@ public sealed class InputManager : IDisposable { private readonly HashSet PressedKeys = new(); private readonly Dictionary Actions = new(); private readonly HashSet Gamepads = new(); + private readonly HashSet PressedMouseButtons = new(); public InputManager(Game game) { Game = game; @@ -32,6 +33,9 @@ public InputManager(Game game) { public bool IsKeyPressed(Key key) => PressedKeys.Contains(key); + public bool IsMouseButtonPressed(MouseButton button) + => PressedMouseButtons.Contains(button); + public bool IsButtonPressed(GamepadButton button) { foreach (var gamepad in Gamepads) if (gamepad[button]) @@ -83,11 +87,11 @@ private void NativeWindowOnKeyUp(KeyEvent obj) { } private void NativeWindowOnMouseDown(MouseEvent mouseEvent) { - + PressedMouseButtons.Add(mouseEvent.MouseButton); } private void NativeWindowOnMouseUp(MouseEvent mouseEvent) { - + PressedMouseButtons.Remove(mouseEvent.MouseButton); } private void NativeWindowOnMouseMove(MouseMoveEventArgs mouseMoveEventArgs) { diff --git a/Voxel/Client/Keybinding/Button.cs b/Voxel/Client/Keybinding/Button.cs index 8a76605..34b6627 100644 --- a/Voxel/Client/Keybinding/Button.cs +++ b/Voxel/Client/Keybinding/Button.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; -using Microsoft.Xna.Framework.Input; using RenderSurface.Input.Gamepad; using Veldrid; +using VMouseButton = Veldrid.MouseButton; namespace Voxel.Client.Keybinding; @@ -54,53 +54,28 @@ public override string ToString() } public class MouseButton : Button { - private static readonly Dictionary Cache = new(); + private static readonly Dictionary Cache = new(); public new static MouseButton? FromString(string value) - => Enum.TryParse(value, out Type type) ? Get(type) : null; + => Enum.TryParse(value, out VMouseButton type) ? Get(type) : null; - public static MouseButton Get(Type type) { + public static MouseButton Get(VMouseButton type) { if (!Cache.ContainsKey(type)) Cache[type] = new(type); return Cache[type]; } - public readonly Type Button; + public readonly VMouseButton Button; - public override bool isPressed => GetState() == ButtonState.Pressed; + public override bool isPressed => VoxelClient.Instance.InputManager.IsMouseButtonPressed(Button); - private MouseButton(Type button) { + private MouseButton(VMouseButton button) { Button = button; } public override string ToString() => "Mouse."+Button; - - private ButtonState GetState() { - var state = Mouse.GetState(); - switch (Button) { - case Type.Left: - return state.LeftButton; - case Type.Middle: - return state.MiddleButton; - case Type.Right: - return state.RightButton; - case Type.Side1: - return state.XButton1; - case Type.Side2: - return state.XButton2; - } - return ButtonState.Released; - } - - public enum Type { - Left, - Middle, - Right, - Side1, - Side2 - } } public class ControllerNewButton : Button { diff --git a/Voxel/Client/Keybinding/Keybinds.cs b/Voxel/Client/Keybinding/Keybinds.cs index a2fa6e7..a33db84 100644 --- a/Voxel/Client/Keybinding/Keybinds.cs +++ b/Voxel/Client/Keybinding/Keybinds.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; -using Microsoft.Xna.Framework.Input; using RenderSurface.Input.Gamepad; using Veldrid; +using VMouseButton = Veldrid.MouseButton; namespace Voxel.Client.Keybinding; @@ -76,13 +76,13 @@ public static class Keybinds { public static readonly Keybind Use = new( "action.use", - MouseButton.Get(MouseButton.Type.Right), + MouseButton.Get(VMouseButton.Right), ControllerAxisButton.Get(GamepadAxis.LeftTrigger) ); public static readonly Keybind Attack = new( "action.attack", - MouseButton.Get(MouseButton.Type.Left), + MouseButton.Get(VMouseButton.Left), ControllerAxisButton.Get(GamepadAxis.RightTrigger) ); diff --git a/Voxel/Client/Rendering/GameRenderer.cs b/Voxel/Client/Rendering/GameRenderer.cs index e09c33b..1ed10bd 100644 --- a/Voxel/Client/Rendering/GameRenderer.cs +++ b/Voxel/Client/Rendering/GameRenderer.cs @@ -1,5 +1,5 @@ using GlmSharp; -using Veldrid; +using Voxel.Client.Keybinding; using Voxel.Client.Rendering.World; namespace Voxel.Client.Rendering; @@ -28,24 +28,24 @@ public override void Render(double delta) { dvec3 inputDir = dvec3.Zero; - if (Client.InputManager.IsKeyPressed(Key.A)) + if (Keybinds.StrafeLeft.isPressed) inputDir.x -= 1; - if (Client.InputManager.IsKeyPressed(Key.D)) + if (Keybinds.StrafeRight.isPressed) inputDir.x += 1; - if (Client.InputManager.IsKeyPressed(Key.W)) + if (Keybinds.Forward.isPressed) inputDir.z -= 1; - if (Client.InputManager.IsKeyPressed(Key.S)) + if (Keybinds.Backward.isPressed) inputDir.z += 1; - if (Client.InputManager.IsKeyPressed(Key.E)) + if (Keybinds.Crouch.isPressed) inputDir.y -= 1; - if (Client.InputManager.IsKeyPressed(Key.Q)) + if (Keybinds.Jump.isPressed) inputDir.y += 1; - if (Client.InputManager.IsKeyPressed(Key.Z)) - MainCamera.rotation *= quat.FromAxisAngle((float)delta, new vec3(0, 1, 0)); - if (Client.InputManager.IsKeyPressed(Key.X)) - MainCamera.rotation *= quat.FromAxisAngle((float)-delta, new vec3(0, 1, 0)); - + if (Keybinds.LookLeft.isPressed) + MainCamera.rotation *= quat.FromAxisAngle((float)delta, new(0, 1, 0)); + if (Keybinds.LookRight.isPressed) + MainCamera.rotation *= quat.FromAxisAngle((float)-delta, new(0, 1, 0)); + inputDir = MainCamera.rotation * (vec3)inputDir; MainCamera.position += inputDir * delta; diff --git a/Voxel/Content/shaders/blit.frag.glsl b/Voxel/Content/shaders/blit.frag.glsl index f44b493..9df3212 100644 --- a/Voxel/Content/shaders/blit.frag.glsl +++ b/Voxel/Content/shaders/blit.frag.glsl @@ -6,7 +6,6 @@ layout(location = 0) out vec4 fsout_Color; layout (set = 0, binding = 0) uniform sampler TextureSampler; layout (set = 0, binding = 1) uniform texture2D Texture; -void main() -{ +void main() { fsout_Color = texture(sampler2D(Texture, TextureSampler), fsin_texCoords); } diff --git a/Voxel/Content/shaders/blit.vert.glsl b/Voxel/Content/shaders/blit.vert.glsl index 6037296..6a0c1ce 100644 --- a/Voxel/Content/shaders/blit.vert.glsl +++ b/Voxel/Content/shaders/blit.vert.glsl @@ -12,8 +12,7 @@ layout (set = 1, binding = 0) uniform TextureDrawParams { vec2 DstSize; }; -void main() -{ +void main() { vec2 scaledDstMin = DstMin / DstSize; vec2 scaledDstMax = DstMax / DstSize; diff --git a/Voxel/Content/shaders/simple.frag.glsl b/Voxel/Content/shaders/simple.frag.glsl index 202af17..b4b47c2 100644 --- a/Voxel/Content/shaders/simple.frag.glsl +++ b/Voxel/Content/shaders/simple.frag.glsl @@ -8,7 +8,6 @@ layout(location = 0) out vec4 fsout_Color; layout (set = 1, binding = 0) uniform sampler TextureSampler; layout (set = 1, binding = 1) uniform texture2D Texture; -void main() -{ +void main() { fsout_Color = texture(sampler2D(Texture, TextureSampler), fsin_texCoords) * fsin_Color; } diff --git a/Voxel/Content/shaders/simple.vert.glsl b/Voxel/Content/shaders/simple.vert.glsl index ef74964..96bc049 100644 --- a/Voxel/Content/shaders/simple.vert.glsl +++ b/Voxel/Content/shaders/simple.vert.glsl @@ -20,7 +20,7 @@ layout (set = 2, binding = 0) uniform ModelData { mat4 ModelMatrix; }; -UnpackedVertex unpack(int packedColor, int packedUV){ +UnpackedVertex unpack(int packedColor, int packedUV) { UnpackedVertex ret; ret.color = vec4( @@ -38,8 +38,7 @@ UnpackedVertex unpack(int packedColor, int packedUV){ return ret; } -void main() -{ +void main() { mat4 mvp = ModelMatrix * VPMatrix; gl_Position = vec4(Position, 1) * mvp;