-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMouseHelper.cs
More file actions
136 lines (116 loc) · 3.77 KB
/
MouseHelper.cs
File metadata and controls
136 lines (116 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using DotNetBrowser.Geometry;
using DotNetBrowser.Input.Mouse;
using DotNetBrowser.Input.Mouse.Events;
using UnityEngine;
namespace Assets.Scripts
{
internal class MouseHelper
{
private readonly IMouse mouse;
private readonly bool[] pressStatus = {false, false, false, false, false};
public float ScrollSpeed = 1000.0f;
public MouseHelper(IMouse mouse)
{
this.mouse = mouse;
}
public Size ViewSize { get; set; }
public Point Point { get; set; }
private void OnMousePressed(Point location, MouseButton button)
{
if (pressStatus[(int) button]) return;
pressStatus[(int) button] = true;
MousePressedEventArgs args = new MousePressedEventArgs
{
Location = location,
Button = button,
ClickCount = 1
};
mouse.Pressed.Raise(args);
}
private void OnMouseDragged(Point location, MouseButton button)
{
MouseDraggedEventArgs args = new MouseDraggedEventArgs
{
Location = location,
Button = button
};
mouse.Dragged.Raise(args);
}
private void OnMouseReleased(Point location, MouseButton button)
{
if (!pressStatus[(int) button]) return;
pressStatus[(int) button] = false;
MouseReleasedEventArgs args = new MouseReleasedEventArgs
{
Location = location,
Button = button,
ClickCount = 1
};
mouse.Released.Raise(args);
}
public void MouseWheel()
{
Point p = Point ?? GetMousePoint();
if (p != null)
{
MouseWheelMovedEventArgs args = new MouseWheelMovedEventArgs
{
Location = p,
DeltaY = Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed
};
mouse.WheelMoved.Raise(args);
}
}
public void MouseDown()
{
MouseEvent(Input.GetMouseButtonDown, OnMousePressed);
}
public void MouseDrag()
{
MouseEvent(Input.GetMouseButton, OnMouseDragged);
}
public void MouseUp()
{
MouseEvent(Input.GetMouseButtonUp, OnMouseReleased);
}
public void MouseMoved()
{
Point point3D = GetMousePoint();
Point p = Point ?? point3D;
if (p != null)
{
MouseMovedEventArgs args = new MouseMovedEventArgs
{
Location = p
};
mouse.Moved.Raise(args);
}
}
private Point GetMousePoint()
{
if (Camera.main != null)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
int x = (int)(ViewSize.Width * hit.textureCoord.x);
int y = (int)(ViewSize.Height * hit.textureCoord.y);
return new Point(x, y);
}
}
return null;
}
private void MouseEvent(Func<int, bool> mouseButtonUsed, Action<Point, MouseButton> mouseAction)
{
Point p = Point ?? GetMousePoint();
Point = null;
if (p != null)
{
if (mouseButtonUsed(0)) mouseAction(p, MouseButton.Left);
if (mouseButtonUsed(1)) mouseAction(p, MouseButton.Right);
if (mouseButtonUsed(2)) mouseAction(p, MouseButton.Middle);
}
}
}
}