forked from EngoEngine/engo
/
mouse.go
181 lines (160 loc) · 5.32 KB
/
mouse.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package engi
import (
"github.com/paked/engi/ecs"
)
// MouseComponent is the location for the MouseSystem to store its results;
// to be used / viewed by other Systems
type MouseComponent struct {
// Clicked is true whenever the Mouse was clicked over
// the entity space in this frame
Clicked bool
// Released is true whenever the left mouse button is released over the
// entity space in this frame
Released bool
// Hovered is true whenever the Mouse is hovering
// the entity space in this frame. This does not necessarily imply that
// the mouse button was pressed down in your entity space.
Hovered bool
// Dragged is true whenever the entity space was clicked,
// and then the mouse started moving (while holding)
Dragged bool
// RightClicked is true whenever the entity space was right-clicked
// in this frame
RightClicked bool
// RightReleased is true whenever the right mouse button is released over
// the entity space in this frame. This does not necessarily imply that
// the mouse button was pressed down in your entity space.
RightReleased bool
// Enter is true whenever the Mouse entered the entity space in that frame,
// but wasn't in that space during the previous frame
Enter bool
// Leave is true whenever the Mouse was in the space on the previous frame,
// but now isn't
Leave bool
// Position of the mouse at any moment this is generally used
// in conjunction with Track = true
MouseX float32
MouseY float32
// Set manually this to true and your mouse component will track the mouse
// and your entity will always be able to receive an updated mouse
// component even if its space is not under the mouse cursor
// WARNING: you MUST know why you want to use this because it will
// have serious performance impacts if you have many entities with
// a MouseComponent in tracking mode.
// This is ideally used for a really small number of entities
// that must really be aware of the mouse details event when the
// mouse is not hovering them
Track bool
}
// Type returns the string representation of the MouseComponent type
func (*MouseComponent) Type() string {
return "MouseComponent"
}
// MouseSystem listens for mouse events, and changes value for MouseComponent accordingly
type MouseSystem struct {
*ecs.System
mouseX float32
mouseY float32
mouseDown bool
}
// Type returns the string representation of the MouseSystem type
func (*MouseSystem) Type() string {
return "MouseSystem"
}
// New initializes the MouseSystem
func (m *MouseSystem) New(*ecs.World) {
m.System = ecs.NewSystem()
}
// Priority returns a priority of 10 (higher than most) to ensure that this System runs before all others
func (m *MouseSystem) Priority() int {
return 10
}
// Pre is called before all Update calls, and is used to compute internal values
func (m *MouseSystem) Pre() {
// Translate Mouse.X and Mouse.Y into "game coordinates"
m.mouseX = Mouse.X*cam.z*(gameWidth/windowWidth) + cam.x - (gameWidth/2)*cam.z
m.mouseY = Mouse.Y*cam.z*(gameHeight/windowHeight) + cam.y - (gameHeight/2)*cam.z
}
// Update sets the MouseComponent values for each Entity
func (m *MouseSystem) Update(entity *ecs.Entity, dt float32) {
var (
mc *MouseComponent
space *SpaceComponent
render *RenderComponent
ok bool
)
// We need MouseComponent to save our findings
if mc, ok = entity.ComponentFast(mc).(*MouseComponent); !ok {
return
}
// We need SpaceComponent for the location
if space, ok = entity.ComponentFast(space).(*SpaceComponent); !ok {
return
}
// We need RenderComponent for the Priority
if render, ok = entity.ComponentFast(render).(*RenderComponent); !ok {
return
}
// Reset some values
mc.Leave = false
mx := m.mouseX
my := m.mouseY
// Special case: HUD
if render.priority >= HUDGround {
mx = Mouse.X
my = Mouse.Y
}
// if the Mouse component is a tracker we always update it
// Check if the X-value is within range
// and if the Y-value is within range
if mc.Track || mx > space.Position.X && mx < (space.Position.X+space.Width) &&
my > space.Position.Y && my < (space.Position.Y+space.Height) {
mc.Enter = !mc.Hovered
mc.Hovered = true
mc.Released = false
// track mouse position so that systems that need to stay on the mouse
// position can do it (think an RTS when placing a new building and
// you get a ghost building following your mouse until you click to
// place it somewhere in your world.
mc.MouseX = mx
mc.MouseY = my
switch Mouse.Action {
case PRESS:
switch Mouse.Button {
case MouseButtonLeft:
mc.Clicked = true
case MouseButtonRight:
mc.RightClicked = true
}
m.mouseDown = true
case RELEASE:
switch Mouse.Button {
case MouseButtonLeft:
mc.Released = true
case MouseButtonRight:
mc.RightReleased = true
}
// dragging stops as soon as one of the currently pressed buttons
// is released
mc.Dragged = false
// mouseDown goes false as soon as one of the pressed buttons is
// released. Effectively ending any dragging
m.mouseDown = false
case MOVE:
if m.mouseDown {
mc.Dragged = true
}
}
} else {
if mc.Hovered {
mc.Leave = true
}
mc.Hovered = false
}
}
// Post is called after all Update calls, and is used to compute internal values
func (m *MouseSystem) Post() {
// reset mouse.Action value to something meaningless to avoid
// catching the same "signal" twice
Mouse.Action = NEUTRAL
}