-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmouse.go
283 lines (246 loc) · 9.27 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package engoBox2dSystem
import (
"log"
"github.com/EngoEngine/ecs"
"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/common"
"github.com/EngoEngine/engo/math"
"github.com/ByteArena/box2d"
)
// MouseSystemPriority ensures the mouse system is updated before any other systems
const MouseSystemPriority = 100
// 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 left-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
// RightDragged is true whenever the entity space was right-clicked,
// and then the mouse started moving (while holding)
RightDragged 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
// Modifier is used to store the eventual modifiers that were pressed during
// the same time the different click events occurred
Modifier engo.Modifier
// startedDragging is used internally to see if *this* is the object that is being dragged
startedDragging bool
// startedRightDragging is used internally to see if *this* is the object that is being right-dragged
rightStartedDragging bool
// IsHUDShader is used to update the mouse component properly for the common.HUDShader
IsHUDShader bool
}
type mouseEntity struct {
*ecs.BasicEntity
*MouseComponent
*common.SpaceComponent
*common.RenderComponent
*Box2dComponent
}
// MouseSystem listens for mouse events and changes value for MouseComponent accordingly
type MouseSystem struct {
entities []mouseEntity
world *ecs.World
camera *common.CameraSystem
mouseX float32
mouseY float32
mouseDown bool
rightMouseDown bool
}
// Priority implements prioritizer interface
func (m *MouseSystem) Priority() int { return MouseSystemPriority }
// New adds world and camera to the MouseSystem
func (m *MouseSystem) New(w *ecs.World) {
m.world = w
// First check to see if the CameraSystem is available
for _, system := range m.world.Systems() {
switch sys := system.(type) {
case *common.CameraSystem:
m.camera = sys
}
}
if m.camera == nil {
log.Println("ERROR: CameraSystem not found - have you added the `RenderSystem` before the `MouseSystem`?")
return
}
}
// Add adds a new entity to the MouseSystem
func (m *MouseSystem) Add(basic *ecs.BasicEntity, mouse *MouseComponent, space *common.SpaceComponent, render *common.RenderComponent, box *Box2dComponent) {
m.entities = append(m.entities, mouseEntity{basic, mouse, space, render, box})
}
// AddByInterface adds the entity that implements the Mouseable interface to the MouseSystem
func (m *MouseSystem) AddByInterface(o Mouseable) {
m.Add(o.GetBasicEntity(), o.GetMouseComponent(), o.GetSpaceComponent(), o.GetRenderComponent(), o.GetBox2dComponent())
}
// Remove removes an entity from the MouseSystem
func (m *MouseSystem) Remove(basic ecs.BasicEntity) {
delete := -1
for index, entity := range m.entities {
if entity.ID() == basic.ID() {
delete = index
break
}
}
if delete >= 0 {
m.entities = append(m.entities[:delete], m.entities[delete+1:]...)
}
}
// Update updates the MouseComponent based on location of cursor and state of the mouse buttons
func (m *MouseSystem) Update(dt float32) {
// Translate Mouse.X and Mouse.Y into "game coordinates"
switch engo.CurrentBackEnd {
case engo.BackEndGLFW:
m.mouseX = engo.Input.Mouse.X*m.camera.Z() + (m.camera.X()-(engo.GameWidth()/2)*m.camera.Z())/engo.GetGlobalScale().X
m.mouseY = engo.Input.Mouse.Y*m.camera.Z() + (m.camera.Y()-(engo.GameHeight()/2)*m.camera.Z())/engo.GetGlobalScale().Y
case engo.BackEndMobile:
m.mouseX = engo.Input.Mouse.X*m.camera.Z() + (m.camera.X()-(engo.GameWidth()/2)*m.camera.Z()+(engo.ResizeXOffset/2))/engo.GetGlobalScale().X
m.mouseY = engo.Input.Mouse.Y*m.camera.Z() + (m.camera.Y()-(engo.GameHeight()/2)*m.camera.Z()+(engo.ResizeYOffset/2))/engo.GetGlobalScale().Y
case engo.BackEndWeb:
m.mouseX = engo.Input.Mouse.X*m.camera.Z() + (m.camera.X()-(engo.GameWidth()/2)*m.camera.Z()+(engo.ResizeXOffset/2))/engo.GetGlobalScale().X
m.mouseY = engo.Input.Mouse.Y*m.camera.Z() + (m.camera.Y()-(engo.GameHeight()/2)*m.camera.Z()+(engo.ResizeYOffset/2))/engo.GetGlobalScale().Y
}
// Rotate if needed
if m.camera.Angle() != 0 {
sin, cos := math.Sincos(m.camera.Angle() * math.Pi / 180)
m.mouseX, m.mouseY = m.mouseX*cos+m.mouseY*sin, m.mouseY*cos-m.mouseX*sin
}
for _, e := range m.entities {
// Reset all values except these
*e.MouseComponent = MouseComponent{
Track: e.MouseComponent.Track,
Hovered: e.MouseComponent.Hovered,
startedDragging: e.MouseComponent.startedDragging,
rightStartedDragging: e.MouseComponent.rightStartedDragging,
IsHUDShader: e.MouseComponent.IsHUDShader,
}
if e.MouseComponent.Track {
e.MouseComponent.MouseX = m.mouseX
e.MouseComponent.MouseY = m.mouseY
}
mx := m.mouseX
my := m.mouseY
if e.SpaceComponent == nil || e.Box2dComponent == nil {
continue
}
//set box2d body to SpaceComponent's position and rotation
e.Body.SetTransform(Conv.ToBox2d2Vec(e.Center()), Conv.DegToRad(e.Rotation))
if e.RenderComponent != nil {
// Hardcoded special case for the HUD | TODO: make generic instead of hardcoding
if e.MouseComponent.IsHUDShader {
mx = engo.Input.Mouse.X
my = engo.Input.Mouse.Y
}
if e.RenderComponent.Hidden {
continue // skip hidden components
}
}
mousePoint := box2d.B2Vec2{
X: Conv.PxToMeters(mx),
Y: Conv.PxToMeters(my),
}
var containsMouse bool
for f := e.Body.GetFixtureList(); f != nil; f = f.GetNext() {
if f.TestPoint(mousePoint) {
containsMouse = true
break
}
}
// 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 e.MouseComponent.Track || e.MouseComponent.startedDragging || containsMouse {
e.MouseComponent.Enter = !e.MouseComponent.Hovered
e.MouseComponent.Hovered = true
e.MouseComponent.Released = false
if !e.MouseComponent.Track {
// If we're tracking, we've already set these
e.MouseComponent.MouseX = mx
e.MouseComponent.MouseY = my
}
switch engo.Input.Mouse.Action {
case engo.Press:
switch engo.Input.Mouse.Button {
case engo.MouseButtonLeft:
e.MouseComponent.Clicked = true
e.MouseComponent.startedDragging = true
m.mouseDown = true
case engo.MouseButtonRight:
e.MouseComponent.RightClicked = true
e.MouseComponent.rightStartedDragging = true
m.rightMouseDown = true
}
case engo.Release:
switch engo.Input.Mouse.Button {
case engo.MouseButtonLeft:
e.MouseComponent.Released = true
case engo.MouseButtonRight:
e.MouseComponent.RightReleased = true
}
case engo.Move:
if m.mouseDown && e.MouseComponent.startedDragging {
e.MouseComponent.Dragged = true
}
if m.rightMouseDown && e.MouseComponent.rightStartedDragging {
e.MouseComponent.RightDragged = true
}
}
} else {
if e.MouseComponent.Hovered {
e.MouseComponent.Leave = true
}
e.MouseComponent.Hovered = false
}
if engo.Input.Mouse.Action == engo.Release {
switch engo.Input.Mouse.Button {
case engo.MouseButtonLeft:
e.MouseComponent.Dragged = false
e.MouseComponent.startedDragging = false
m.mouseDown = false
case engo.MouseButtonRight:
e.MouseComponent.RightDragged = false
e.MouseComponent.rightStartedDragging = false
m.rightMouseDown = false
}
}
// propagate the modifiers to the mouse component so that game
// implementers can take different decisions based on those
e.MouseComponent.Modifier = engo.Input.Mouse.Modifer
}
//Remove all bodies on list for removal
removeBodies()
}