-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcamera.go
177 lines (124 loc) · 3.37 KB
/
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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"math"
"github.com/veandco/go-sdl2/sdl"
)
type Camera struct {
Position Point
TargetPosition Point
Zoom float32
TargetZoom float32
Softness float32
}
func NewCamera() *Camera {
return &Camera{
Zoom: 1,
TargetZoom: 1,
Softness: 0.2,
}
}
func (camera *Camera) Update() {
softness := camera.Softness
if !globals.Settings.Get(SettingsSmoothMovement).AsBool() {
softness = 1
}
camera.Zoom += (camera.TargetZoom - camera.Zoom) * softness
if math.Abs(float64(camera.TargetZoom-camera.Zoom)) < 0.01 {
camera.Zoom = camera.TargetZoom
}
globals.Renderer.SetScale(camera.Zoom, camera.Zoom)
camera.Position = camera.Position.Add(camera.TargetPosition.Sub(camera.Position).Mult(softness))
}
func (camera *Camera) JumpTo(pos Point, zoom float32) {
camera.TargetPosition = pos
camera.Position = pos
camera.TargetZoom = zoom
camera.Zoom = zoom
camera.Update()
}
func (camera *Camera) SetZoom(targetZoom float32) {
if targetZoom < 0.05 {
targetZoom = 0.05
} else if targetZoom >= 10 {
targetZoom = 10
}
camera.TargetZoom = targetZoom
}
func (camera *Camera) AddZoom(zoomInAmount float32) {
camera.SetZoom(camera.TargetZoom + zoomInAmount)
softness := camera.Softness
if !globals.Settings.Get(SettingsSmoothMovement).AsBool() {
softness = 1
}
if globals.Settings.Get(SettingsZoomToCursor).AsBool() && camera.TargetZoom > camera.Zoom {
zoomdiff := globals.Mouse.WorldPosition().Sub(camera.TargetPosition).Mult(softness * 0.25)
camera.TargetPosition = camera.TargetPosition.Add(zoomdiff)
}
}
func (camera *Camera) Offset() Point {
width := globals.ScreenSize.X / 2 / camera.Zoom
height := globals.ScreenSize.Y / 2 / camera.Zoom
point := Point{(camera.Position.X - width), (camera.Position.Y - height)}.Rounded()
return point
}
func (camera *Camera) TranslateRect(rect *sdl.FRect) *sdl.FRect {
pos := camera.TranslatePoint(Point{rect.X, rect.Y})
return &sdl.FRect{
X: pos.X,
Y: pos.Y,
W: rect.W,
H: rect.H,
}
}
func (camera *Camera) TranslatePoint(point Point) Point {
return point.Sub(camera.Offset())
}
func (camera *Camera) UntranslatePoint(point Point) Point {
point = point.Add(camera.Offset().Inverted())
return point
}
func (camera *Camera) ViewArea() *sdl.Rect {
width := int32(globals.ScreenSize.X / camera.Zoom)
height := int32(globals.ScreenSize.Y / camera.Zoom)
rect := &sdl.Rect{
X: int32(camera.Position.X - float32(width/2)),
Y: int32(camera.Position.Y - float32(height/2)),
W: width,
H: height,
}
return rect
}
func (camera *Camera) FocusOn(zoom bool, cards ...*Card) {
if len(cards) == 0 {
return
}
// Focus on the first page of the card
globals.Project.SetPage(cards[0].Page)
topLeft := Point{math.MaxFloat32, math.MaxFloat32}
bottomRight := Point{-math.MaxFloat32, -math.MaxFloat32}
for _, c := range cards {
if c.Rect.X < topLeft.X {
topLeft.X = c.Rect.X
}
if c.Rect.Y < topLeft.Y {
topLeft.Y = c.Rect.Y
}
if c.Rect.X+c.Rect.W > bottomRight.X {
bottomRight.X = c.Rect.X + c.Rect.W
}
if c.Rect.Y+c.Rect.H > bottomRight.Y {
bottomRight.Y = c.Rect.Y + c.Rect.H
}
}
diff := bottomRight.Sub(topLeft)
camera.TargetPosition = topLeft.Add(diff.Div(2))
if zoom {
zx := globals.ScreenSize.X / diff.X
zy := globals.ScreenSize.Y / diff.Y
if zy > zx {
camera.SetZoom(zx * 0.8)
} else {
camera.SetZoom(zy * 0.8)
}
}
}