Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Move mouse buttons to veldrid too
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Oct 9, 2023
1 parent ace4fd2 commit ca3a789
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 58 deletions.
8 changes: 6 additions & 2 deletions Core/Input/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public sealed class InputManager : IDisposable {
private readonly HashSet<Key> PressedKeys = new();
private readonly Dictionary<Key, InputAction> Actions = new();
private readonly HashSet<SdlGamepad> Gamepads = new();
private readonly HashSet<MouseButton> PressedMouseButtons = new();

public InputManager(Game game) {
Game = game;
Expand All @@ -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])
Expand Down Expand Up @@ -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) {
Expand Down
39 changes: 7 additions & 32 deletions Voxel/Client/Keybinding/Button.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -54,53 +54,28 @@ public override string ToString()
}

public class MouseButton : Button {
private static readonly Dictionary<Type, MouseButton> Cache = new();
private static readonly Dictionary<VMouseButton, MouseButton> 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 {
Expand Down
6 changes: 3 additions & 3 deletions Voxel/Client/Keybinding/Keybinds.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
);

Expand Down
24 changes: 12 additions & 12 deletions Voxel/Client/Rendering/GameRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using GlmSharp;
using Veldrid;
using Voxel.Client.Keybinding;
using Voxel.Client.Rendering.World;

namespace Voxel.Client.Rendering;
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions Voxel/Content/shaders/blit.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 1 addition & 2 deletions Voxel/Content/shaders/blit.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions Voxel/Content/shaders/simple.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 2 additions & 3 deletions Voxel/Content/shaders/simple.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;

Expand Down

0 comments on commit ca3a789

Please sign in to comment.