-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.go
82 lines (71 loc) · 2.05 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
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
func (g *Game) handleMouseClicks() {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
if !g.panel.LeftMouseButtonPress(ebiten.CursorPosition()) {
g.redrawPSLayer = true
cx, cy := ebiten.CursorPosition()
cx -= g.offsetX
cy -= g.offsetY
g.panel.Clear()
clickedObject := false
for _, p := range g.level.Planets() {
if p.MouseButton(cx, cy) {
clickedObject = true
p.Highlight()
showPlanetPanel(g.panel, p, g.resourceData, g.level.AllowedResources())
showBuildOptionsPanel(p, g)
} else {
p.Unhighlight()
}
}
for _, s := range g.player.Structures() {
if s.MouseButton(cx, cy) {
clickedObject = true
s.Highlight()
showStructurePanel(g, s)
} else {
s.Unhighlight()
}
}
if !clickedObject {
g.dragging = true
g.mouseDragX, g.mouseDragY = ebiten.CursorPosition()
}
}
} else if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
g.redrawPSLayer = true
g.dragging = false
g.panel.LeftMouseButtonRelease(ebiten.CursorPosition())
} else if g.dragging && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
g.redrawPSLayer = true
// FIXME continuing to drag mouse after you reach the screen edge moves
// the cursor position but not the viewport, so planet selection targets
// are wrong
newX, newY := ebiten.CursorPosition()
var xMovement, yMovement int
if newX < g.mouseDragX && -1*g.offsetX+g.windowW < g.level.W {
xMovement = newX - g.mouseDragX
g.offsetX += xMovement
g.mouseDragX = newX
}
if newX > g.mouseDragX && g.offsetX < 0 {
xMovement = newX - g.mouseDragX
g.offsetX += xMovement
g.mouseDragX = newX
}
if newY > g.mouseDragY && g.offsetY < 0 {
yMovement = newY - g.mouseDragY
g.offsetY += yMovement
g.mouseDragY = newY
}
if newY < g.mouseDragY && -1*g.offsetY+g.windowH < g.level.H {
yMovement = newY - g.mouseDragY
g.offsetY += yMovement
g.mouseDragY = newY
}
}
}