forked from oakmound/oak
/
mouse.go
81 lines (74 loc) · 1.77 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
package mouse
import (
"github.com/oakmound/oak/collision"
"golang.org/x/mobile/event/mouse"
)
var (
//TrackMouseClicks enables the propagation of MouseClickOn during MouseRelease events
TrackMouseClicks = true
)
// Propagate triggers direct mouse events on entities which are clicked
func Propagate(eventName string, me Event) {
LastMouseEvent = me
hits := DefTree.SearchIntersect(me.ToSpace().Bounds())
for _, v := range hits {
sp := v.(*collision.Space)
sp.CID.Trigger(eventName, me)
}
if TrackMouseClicks {
if eventName == PressOn {
LastMousePress = me
} else if eventName == ReleaseOn {
if me.Button == LastMousePress.Button {
pressHits := DefTree.SearchIntersect(LastMousePress.ToSpace().Bounds())
for _, v1 := range pressHits {
sp1 := v1.(*collision.Space)
for _, v2 := range hits {
sp2 := v2.(*collision.Space)
if sp1.CID == sp2.CID {
sp1.CID.Trigger(ClickOn, me)
}
}
}
}
}
}
}
// GetMouseButton is a utitilty function which translates
// integer values of mouse keys from golang's event/mouse library
// into strings.
// Intended for internal use.
func GetMouseButton(b mouse.Button) (s string) {
switch b {
case mouse.ButtonLeft:
s = "LeftMouse"
case mouse.ButtonMiddle:
s = "MiddleMouse"
case mouse.ButtonRight:
s = "RightMouse"
case mouse.ButtonWheelUp:
s = "ScrollUpMouse"
case mouse.ButtonWheelDown:
s = "ScrollDownMouse"
default:
s = ""
}
return
}
// GetEventName returns a string event name given some mobile/mouse information
func GetEventName(d mouse.Direction, b mouse.Button) string {
switch d {
case mouse.DirPress:
return Press
case mouse.DirRelease:
return Release
default:
switch b {
case -2:
return ScrollDown
case -1:
return ScrollUp
}
}
return Drag
}