Skip to content

Commit 757a3c7

Browse files
authored
🐭 Add mouse buttons to mouse event args (#85)
1 parent cf044f1 commit 757a3c7

File tree

7 files changed

+70
-17
lines changed

7 files changed

+70
-17
lines changed

Bearded.UI.Tests/Bearded.UI.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@
2929
<!-- Needed to make indexes work -->
3030
<PackageReference Include="IndexRange" Version="1.0.2" />
3131
<!-- Needed to make C#9 features work -->
32-
<Compile Include="IsExternalInit.cs" />
3332
</ItemGroup>
3433
</Project>

Bearded.UI/EventArgs/MouseButtonEventArgs.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ public sealed class MouseButtonEventArgs : MouseEventArgs
77
{
88
public MouseButton MouseButton { get; }
99

10-
public MouseButtonEventArgs(Vector2d mousePosition, ModifierKeys modifierKeys, MouseButton mouseButton)
11-
: base(mousePosition, modifierKeys)
10+
public MouseButtonEventArgs(
11+
Vector2d mousePosition, MouseButtons mouseButtons, ModifierKeys modifierKeys, MouseButton mouseButton)
12+
: base(mousePosition, mouseButtons, modifierKeys)
1213
{
1314
MouseButton = mouseButton;
1415
}

Bearded.UI/EventArgs/MouseButtons.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using Bearded.Utilities.Input;
3+
4+
namespace Bearded.UI.EventArgs
5+
{
6+
public readonly struct MouseButtons : IEquatable<MouseButtons>
7+
{
8+
public bool Left { get; }
9+
public bool Right { get; }
10+
public bool Middle { get; }
11+
12+
public MouseButtons(bool left, bool right, bool middle)
13+
{
14+
Left = left;
15+
Right = right;
16+
Middle = middle;
17+
}
18+
19+
public static MouseButtons None => new();
20+
21+
public static MouseButtons FromInputManager(InputManager inputManager)
22+
{
23+
return new MouseButtons(
24+
inputManager.LeftMousePressed, inputManager.RightMousePressed, inputManager.MiddleMousePressed);
25+
}
26+
27+
public MouseButtons WithLeft() => new(true, Right, Middle);
28+
public MouseButtons WithRight() => new(Left, true, Middle);
29+
public MouseButtons WithMiddle() => new(Left, Right, true);
30+
31+
public bool IsSupersetOf(MouseButtons other) =>
32+
(Left && other.Left) == other.Left &&
33+
(Right && other.Right) == other.Right &&
34+
(Middle && other.Middle) == other.Middle;
35+
36+
public bool Equals(MouseButtons other) =>
37+
Left == other.Left && Right == other.Right && Middle == other.Middle;
38+
39+
public override bool Equals(object? obj) => obj is MouseButtons other && Equals(other);
40+
41+
public override int GetHashCode() => HashCode.Combine(Left, Right, Middle);
42+
43+
public static bool operator ==(MouseButtons left, MouseButtons right) => Equals(left, right);
44+
45+
public static bool operator !=(MouseButtons left, MouseButtons right) => !Equals(left, right);
46+
}
47+
}

Bearded.UI/EventArgs/MouseEventArgs.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ namespace Bearded.UI.EventArgs
55
public class MouseEventArgs : RoutedEventArgs
66
{
77
public Vector2d MousePosition { get; }
8+
public MouseButtons MouseButtons { get; }
89
public ModifierKeys ModifierKeys { get; }
910

10-
public MouseEventArgs(Vector2d mousePosition, ModifierKeys modifierKeys)
11+
public MouseEventArgs(Vector2d mousePosition, MouseButtons mouseButtons, ModifierKeys modifierKeys)
1112
{
1213
MousePosition = mousePosition;
14+
MouseButtons = mouseButtons;
1315
ModifierKeys = modifierKeys;
1416
}
1517
}

Bearded.UI/EventArgs/MouseScrollEventArgs.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ public sealed class MouseScrollEventArgs : MouseEventArgs
88
public float DeltaScrollF { get; }
99

1010
public MouseScrollEventArgs(
11-
Vector2d mousePosition, ModifierKeys modifierKeys, int deltaScroll, float deltaScrollF)
12-
: base(mousePosition, modifierKeys)
11+
Vector2d mousePosition,
12+
MouseButtons mouseButtons,
13+
ModifierKeys modifierKeys,
14+
int deltaScroll,
15+
float deltaScrollF)
16+
: base(mousePosition, mouseButtons, modifierKeys)
1317
{
1418
DeltaScroll = deltaScroll;
1519
DeltaScrollF = deltaScrollF;

Bearded.UI/Events/MouseEventManager.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Bearded.UI.Controls;
1+
using System.Collections.Immutable;
2+
using Bearded.UI.Controls;
23
using Bearded.UI.EventArgs;
34
using Bearded.Utilities.Input;
45
using OpenTK.Mathematics;
@@ -10,10 +11,8 @@ namespace Bearded.UI.Events
1011
{
1112
sealed class MouseEventManager
1213
{
13-
private static readonly MouseButton[] mouseButtons =
14-
{
15-
MouseButton.Left, MouseButton.Middle, MouseButton.Right
16-
};
14+
private static readonly ImmutableArray<MouseButton> mouseButtonsWithEvents =
15+
ImmutableArray.Create(MouseButton.Left, MouseButton.Middle, MouseButton.Right);
1716

1817
private readonly RootControl root;
1918
private readonly InputManager inputManager;
@@ -28,7 +27,8 @@ internal MouseEventManager(RootControl root, InputManager inputManager)
2827

2928
internal void Update()
3029
{
31-
var mousePosition = root.TransformViewportPosToFramePos((Vector2d) inputManager.MousePosition);
30+
var mousePosition = root.TransformViewportPosToFramePos(inputManager.MousePosition);
31+
var mouseButtons = MouseButtons.FromInputManager(inputManager);
3232
var modifierKeys = ModifierKeys.FromInputManager(inputManager);
3333

3434
var path = EventRouter.FindPropagationPath(
@@ -47,7 +47,7 @@ internal void Update()
4747
var (removedFromPath, addedToPath) = previousPropagationPath != null
4848
? EventPropagationPath.CalculateDeviation(previousPropagationPath, path)
4949
: (EventPropagationPath.Empty, path);
50-
var eventArgs = new MouseEventArgs(mousePosition, modifierKeys);
50+
var eventArgs = new MouseEventArgs(mousePosition, mouseButtons, modifierKeys);
5151

5252
// Mouse exit
5353
removedFromPath.PropagateEvent(
@@ -68,20 +68,20 @@ internal void Update()
6868
(c, e) => c.MouseMoved(e));
6969

7070
// Mouse clicks
71-
foreach (var btn in mouseButtons)
71+
foreach (var btn in mouseButtonsWithEvents)
7272
{
7373
var action = inputManager.Actions.Mouse.FromButton(btn);
7474
if (action.Hit)
7575
{
7676
path.PropagateEvent(
77-
new MouseButtonEventArgs(mousePosition, modifierKeys, btn),
77+
new MouseButtonEventArgs(mousePosition, mouseButtons, modifierKeys, btn),
7878
(c, e) => c.PreviewMouseButtonHit(e),
7979
(c, e) => c.MouseButtonHit(e));
8080
}
8181
if (action.Released)
8282
{
8383
path.PropagateEvent(
84-
new MouseButtonEventArgs(mousePosition, modifierKeys, btn),
84+
new MouseButtonEventArgs(mousePosition, mouseButtons, modifierKeys, btn),
8585
(c, e) => c.PreviewMouseButtonReleased(e),
8686
(c, e) => c.MouseButtonReleased(e));
8787
}
@@ -93,7 +93,7 @@ internal void Update()
9393
{
9494
path.PropagateEvent(
9595
new MouseScrollEventArgs(
96-
mousePosition, modifierKeys, inputManager.DeltaScroll, inputManager.DeltaScrollF),
96+
mousePosition, mouseButtons, modifierKeys, inputManager.DeltaScroll, inputManager.DeltaScrollF),
9797
(c, e) => c.PreviewMouseScrolled(e),
9898
(c, e) => c.MouseScrolled(e));
9999
}
File renamed without changes.

0 commit comments

Comments
 (0)