-
Notifications
You must be signed in to change notification settings - Fork 84
/
viewport.go
117 lines (105 loc) · 2.6 KB
/
viewport.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package oak
import (
"image"
"github.com/oakmound/oak/dlog"
"github.com/oakmound/oak/event"
"github.com/oakmound/oak/physics"
)
var (
// ViewPos represents the point in the world which the viewport is anchored at.
ViewPos = image.Point{}
useViewBounds = false
viewBounds rect
)
type rect struct {
minX, minY, maxX, maxY int
}
// SetScreen sends a signal to the draw loop to set the viewport to be at x,y
func SetScreen(x, y int) {
dlog.Verb("Requesting ViewPoint ", x, y)
viewportCh <- [2]int{x, y}
}
var (
defaultUpdateScreen = func(x, y int) {
if useViewBounds {
if viewBounds.minX <= x && viewBounds.maxX >= x+ScreenWidth {
dlog.Verb("Set ViewX to ", x)
ViewPos.X = x
} else if viewBounds.minX > x {
ViewPos.X = viewBounds.minX
} else if viewBounds.maxX < x+ScreenWidth {
ViewPos.X = viewBounds.maxX - ScreenWidth
}
if viewBounds.minY <= y && viewBounds.maxY >= y+ScreenHeight {
dlog.Verb("Set ViewY to ", y)
ViewPos.Y = y
} else if viewBounds.minY > y {
ViewPos.Y = viewBounds.minY
} else if viewBounds.maxY < y+ScreenHeight {
ViewPos.Y = viewBounds.maxY - ScreenHeight
}
} else {
dlog.Verb("Set ViewXY to ", x, " ", y)
ViewPos = image.Point{x, y}
}
logicHandler.Trigger(event.ViewportUpdate, []float64{float64(ViewPos.X), float64(ViewPos.Y)})
dlog.Verb("ViewX, Y: ", ViewPos.X, " ", ViewPos.Y)
}
updateScreen = defaultUpdateScreen
)
// SetViewportBounds sets the minimum and maximum position of the viewport, including
// screen dimensions
func SetViewportBounds(x1, y1, x2, y2 int) {
if x2 < ScreenWidth {
x2 = ScreenWidth
}
if y2 < ScreenHeight {
y2 = ScreenHeight
}
useViewBounds = true
viewBounds = rect{x1, y1, x2, y2}
dlog.Info("Viewport bounds set to, ", x1, y1, x2, y2)
newViewX := ViewPos.X
newViewY := ViewPos.Y
if newViewX < x1 {
newViewX = x1
} else if newViewX > x2 {
newViewX = x2
}
if newViewY < y1 {
newViewY = y1
} else if newViewY > y2 {
newViewY = y2
}
if newViewX != ViewPos.X || newViewY != ViewPos.Y {
viewportCh <- [2]int{newViewX, newViewY}
}
}
func moveViewportBinding(speed int) func(int, interface{}) int {
return func(cID int, n interface{}) int {
dX := 0
dY := 0
if IsDown("UpArrow") {
dY--
}
if IsDown("DownArrow") {
dY++
}
if IsDown("LeftArrow") {
dX--
}
if IsDown("RightArrow") {
dX++
}
ViewPos.X += dX * speed
ViewPos.Y += dY * speed
if viewportLocked {
return event.UnbindSingle
}
return 0
}
}
// ViewVector returns ViewPos as a Vector
func ViewVector() physics.Vector {
return physics.NewVector(float64(ViewPos.X), float64(ViewPos.Y))
}