-
Notifications
You must be signed in to change notification settings - Fork 4
/
mouse.go
64 lines (55 loc) · 1.26 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
package main
func main() {}
type mouseEvent struct {
event int
x float64
y float64
}
const (
MOUSE_UP = iota
MOUSE_DOWN
MOUSE_MOVE
)
var mouseEvents = make(chan mouseEvent, 100)
// The next three functions are all called by Haxe event handlers
func MouseUp(x, y float64) { mouseEvents <- mouseEvent{MOUSE_UP, x, y} }
func MouseDown(x, y float64) { mouseEvents <- mouseEvent{MOUSE_DOWN, x, y} }
func MouseMove(x, y float64) { mouseEvents <- mouseEvent{MOUSE_MOVE, x, y} }
// This function is called to set-up the mouse handling
func MouseGoroutine() {
go handleMouse()
}
var SpriteX, SpriteY, cacheOffsetX, cacheOffsetY float64
var Status string
func init() {
SpriteX = 100.0
SpriteY = 150.0
}
func handleMouse() {
for {
e := <-mouseEvents
switch e.event {
case MOUSE_DOWN:
Status = "MOUSE_DOWN"
cacheOffsetX = SpriteX - e.x
cacheOffsetY = SpriteY - e.y
awaitingMouseUp := true
for awaitingMouseUp {
e = <-mouseEvents
switch e.event {
case MOUSE_MOVE:
Status = "MOUSE_MOVE"
SpriteX = e.x + cacheOffsetX
SpriteY = e.y + cacheOffsetY
case MOUSE_UP:
Status = "MOUSE_UP"
awaitingMouseUp = false
default:
Status = "awaiting MOUSE_UP"
}
}
default:
Status = "awaiting MOUSE_DOWN"
}
}
}