forked from gophergala2016/gophette
-
Notifications
You must be signed in to change notification settings - Fork 2
/
window_camera.go
48 lines (41 loc) · 1.19 KB
/
window_camera.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
package main
type windowCamera struct {
position Rectangle
bounds Rectangle
}
func newWindowCamera(windowW, windowH int) *windowCamera {
cam := &windowCamera{
// initially set no bounds (big integers)
bounds: Rectangle{-999999, -999999, 2 * 999999, 2 * 999999},
}
cam.setWindowSize(windowW, windowH)
return cam
}
func (cam *windowCamera) setWindowSize(w, h int) {
cx, cy := cam.position.Center()
cam.position.W, cam.position.H = w, h
cam.CenterAround(cx, cy)
}
func (cam *windowCamera) CenterAround(x, y int) {
cam.position.X = x - cam.position.W/2
cam.position.Y = y - cam.position.H/2
// keep the camera in the bounds
if cam.position.X < cam.bounds.X {
cam.position.X = cam.bounds.X
}
if cam.position.Y < cam.bounds.Y {
cam.position.Y = cam.bounds.Y
}
if cam.position.X+cam.position.W > cam.bounds.X+cam.bounds.W {
cam.position.X = cam.bounds.X + cam.bounds.W - cam.position.W
}
if cam.position.Y+cam.position.H > cam.bounds.Y+cam.bounds.H {
cam.position.Y = cam.bounds.Y + cam.bounds.H - cam.position.H
}
}
func (cam *windowCamera) SetBounds(bounds Rectangle) {
cam.bounds = bounds
}
func (cam *windowCamera) offset() (dx, dy int) {
return -cam.position.X, -cam.position.Y
}