-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
92 lines (79 loc) · 2.06 KB
/
input.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
// Handling of most user input,
// excluding only ones that don't need abstraction out of Layout in ui.go
package main
import (
"image"
"github.com/AllenDang/giu"
)
func (a *Anaxim) unpauseInto(s Speed) {
previousSpeed := a.speed
a.speed = s
if previousSpeed == Paused {
go a.runSim()
}
}
func (a *Anaxim) setSpeedToUnlimited() {
a.unpauseInto(Unlimited)
a.speedWidgets.pause = a.PauseButton("Pause")
a.speedWidgets.max = a.MaxButton("Disable max")
}
func (a *Anaxim) setSpeedToCustom() {
a.unpauseInto(Custom)
a.speedWidgets.pause = a.PauseButton("Pause")
a.speedWidgets.max = a.MaxButton("Enable max")
}
func (a *Anaxim) setSpeedToPaused() {
a.speed = Paused
a.speedWidgets.pause = a.PauseButton("Resume")
a.speedWidgets.max = a.MaxButton("Enable max")
}
func clickPause(a *Anaxim) {
switch a.speed {
case Paused:
a.setSpeedToUnlimited()
case Custom:
a.setSpeedToPaused()
case Unlimited:
a.setSpeedToPaused()
}
giu.Update()
}
func clickMax(a *Anaxim) {
switch a.speed {
case Paused:
a.setSpeedToUnlimited()
case Custom:
a.setSpeedToUnlimited()
case Unlimited:
a.setSpeedToCustom()
}
giu.Update()
}
func snapPointToGrid(pt image.Point, gridSize int) image.Point {
return image.Pt(
pt.X/mapResize*mapResize+gridSize,
pt.Y/mapResize*mapResize+gridSize,
)
}
func (a *Anaxim) mapInputEvents() giu.Widget {
return giu.Event().OnHover(func() {
drawCursorPos := giu.GetCursorPos()
mousePos := giu.GetMousePos()
a.howeringOverCellCanvasPoint = snapPointToGrid(mousePos, mapResize)
overImagePos := image.Point{
X: mousePos.X - (drawCursorPos.X - a.mapWidth*mapResize) + 8, //magic padding number
Y: mousePos.Y - drawCursorPos.Y,
}
pixelPos := image.Point{
X: overImagePos.X / mapResize,
Y: overImagePos.Y / mapResize,
}
a.howeringOverCellAt = pixelPos
}).OnClick(giu.MouseButtonLeft, func() {
a.inspectingCellAt = a.howeringOverCellAt
a.inspectingCanvasPoint = a.howeringOverCellCanvasPoint
a.inspectingCell = a.simulation.humanGrid.CellAt(
a.inspectingCellAt.X, a.inspectingCellAt.Y)
a.updateMapTexture()
})
}