-
Notifications
You must be signed in to change notification settings - Fork 10
/
mouse.go
67 lines (56 loc) · 1.58 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
package main
import (
"image"
"github.com/deluan/bring"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
type mouseInfo struct {
pos image.Point
pressedButtons []bring.MouseButton
}
func collectNewMouseInfo(win *pixelgl.Window, imgWidth, imgHeight int) *mouseInfo {
newMousePos := win.MousePosition()
newMouseBtns := pressedMouseButtons(win)
// If mouse is inside window boundaries and anything has changed
if win.MouseInsideWindow() &&
(win.MousePreviousPosition() != newMousePos || changeInMouseButtons(win)) {
winWidth := win.Bounds().Max.X
winHeight := win.Bounds().Max.Y
// Scale mouse position
scale := pixel.V(float64(imgWidth)/winWidth, float64(imgHeight)/winHeight)
newMousePos = newMousePos.ScaledXY(scale)
// OpenGL uses inverted Y
y := float64(imgHeight) - newMousePos.Y
pos := image.Pt(int(newMousePos.X), int(y))
return &mouseInfo{pos, newMouseBtns}
}
return nil
}
func changeInMouseButtons(win *pixelgl.Window) bool {
btns := []pixelgl.Button{
pixelgl.MouseButtonLeft,
pixelgl.MouseButtonRight,
pixelgl.MouseButtonMiddle,
}
for _, p := range btns {
if win.JustPressed(p) || win.JustReleased(p) {
return true
}
}
return false
}
func pressedMouseButtons(win *pixelgl.Window) []bring.MouseButton {
btnMap := map[pixelgl.Button]bring.MouseButton{
pixelgl.MouseButtonLeft: bring.MouseLeft,
pixelgl.MouseButtonRight: bring.MouseRight,
pixelgl.MouseButtonMiddle: bring.MouseMiddle,
}
var btns []bring.MouseButton
for p, b := range btnMap {
if win.Pressed(p) {
btns = append(btns, b)
}
}
return btns
}