-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcamera.lua
177 lines (159 loc) · 6.8 KB
/
camera.lua
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
-- https://github.com/davisdude/Brady
--[[
Copyright (c) 2016 Davis Claiborne
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local function mixin( to, from ) for i, v in pairs( from or {} ) do to[i] = v end end
-- Rotate (px, py) about (ox, oy)
local function rotateAboutPoint( px, py, ox, oy, theta )
px, py = px - ox, py - oy
local cos, sin = math.cos( theta ), math.sin( theta )
return px * cos - py * sin + ox, px * sin + py * cos + oy
end
local function makePush( self, layer )
return function()
love.graphics.push( layer.mode )
love.graphics.origin()
love.graphics.translate( self.x + self.offsetX, self.y + self.offsetY )
love.graphics.rotate( self.rotation )
love.graphics.scale( self.scale * self.aspectRatioScale * layer.scale )
love.graphics.translate( -self.translationX * layer.relativeScale, -self.translationY * layer.relativeScale )
end
end
local function addLayer( self, name, scale, flags )
local new = {
name = name,
scale = scale,
relativeScale = 1, -- Controls the translation speed
mode = self.mode,
}
new.push = makePush( self, new )
new.pop = love.graphics.pop
mixin( new, flags )
self.layers[name] = new
return new
end
local function newCamera( w, h, flags )
local scale, scaleW, scaleH
local new = {
-- Attributes
x = 0, y = 0,
w = w, h = h,
translationX = 0, translationY = 0,
offsetX = w / 2, offsetY = h / 2,
scale = 1,
rotation = 0,
resizable = false,
maintainAspectRatio = false,
center = false,
aspectRatioScale = 1, -- Controls scale due to resizing
mode = 'transform',
layers = {},
-- General Functions
update = function( self, containerW, containerH )
if self.resizable then self:resizingFunction( self:getContainerDimensions() ) end
end,
resizingFunction = function( self, containerW, containerH )
if self.maintainAspectRatio then
containerW, containerH = containerW - 2 * self.x, containerH - 2 * self.y
scaleW, scaleH = containerW / self.w, containerH / self.h
scale = math.min( scaleW, scaleH )
self.w, self.h = scale * self.w, scale * self.h
else
self.w, self.h = containerW - 2 * self.x, containerH - 2 * self.y
end
self.aspectRatioScale = self.w / w
self.offsetX, self.offsetY = self.w / 2, self.h / 2
if self.center then
self.offsetX = self.offsetX + (containerW - self.w) / 2
self.offsetY = self.offsetY + (containerH - self.h) / 2
end
end,
getContainerDimensions = function( self ) return love.graphics.getDimensions() end,
addLayer = addLayer,
getLayer = function( self, name )
return ( type( name ) == 'table' and name or self.layers[name] )
end,
push = function( self, layer ) self:getLayer( layer or 'main' ):push() end,
pop = function( self, layer ) self:getLayer( layer or 'main' ):pop() end,
getWorldCoordinates = function( self, x, y, layer )
layer = self:getLayer( layer or 'main' )
local scaleFactor = self.scale * self.aspectRatioScale * layer.scale
x, y = x - self.x - self.offsetX, y - self.y - self.offsetY
x, y = rotateAboutPoint( x, y, 0, 0, -self.rotation )
x, y = x / scaleFactor, y / scaleFactor
return x + self.translationX * layer.relativeScale, y + self.translationY * layer.relativeScale
end,
getScreenCoordinates = function( self, x, y, layer )
layer = self:getLayer( layer or 'main' )
local scaleFactor = self.scale * self.aspectRatioScale * layer.scale
x, y = x - self.translationX / layer.relativeScale, y - self.translationY * layer.relativeScale
x, y = x * scaleFactor, y * scaleFactor
x, y = rotateAboutPoint( x, y, 0, 0, self.rotation )
x, y = x + self.x + self.offsetX, y + self.y + self.offsetY
return x, y
end,
getMouseWorldCoordinates = function( self, layer )
layer = self:getLayer( layer or 'main' )
local x, y = love.mouse.getPosition()
return self:getWorldCoordinates( x, y, layer )
end,
increaseScaleToPoint = function( self, ds, wx, wy )
if not wx then
wx, wy = self:getMouseWorldCoordinates()
end
local tx, ty = self:getTranslation()
self:increaseScale( ds )
self:increaseTranslation( ( wx - tx ) * ds / self.scale, ( wy - ty ) * ds / self.scale )
end,
scaleToPoint = function( self, s, wx, wy )
if not wx then
wx, wy = self:getMouseWorldCoordinates()
end
local tx, ty = self:getTranslation()
self:scaleBy( s )
self:increaseTranslation( ( wx - tx ) * ( 1 - 1 / s ), ( wy - ty ) * ( 1 - 1 / s ) )
end,
-- Getters/setters
setViewportPosition = function( self, x, y ) self.x, self.y = x, y end,
getViewportPosition = function( self ) return self.x, self.y end,
setOffset = function( self, x, y ) self.offsetX, self.offsetY = x, y end,
getOffset = function( self ) return self.offsetX, self.offsetY end,
setTranslation = function( self, x, y ) self.translationX, self.translationY = x or 0, y or 0 end,
getTranslation = function( self ) return self.translationX, self.translationY end,
increaseTranslation = function( self, dx, dy ) self.translationX, self.translationY = self.translationX + dx, self.translationY + dy end,
setRotation = function( self, theta ) self.rotation = theta end,
getRotation = function( self ) return self.rotation end,
increaseRotation = function( self, dr ) self.rotation = self.rotation + dr end,
setScale = function( self, s ) self.scale = s end,
getScale = function( self ) return self.scale end,
increaseScale = function( self, ds ) self.scale = self.scale + ds end,
scaleBy = function( self, ds ) self.scale = self.scale * ds end,
}
new.translate = new.increaseTranslation
new.rotate = new.increaseRotation
mixin( new, flags )
addLayer( new, 'main', 1 )
if new.center then
local containerW, containerH = new:getContainerDimensions()
new.offsetX = new.offsetX + (containerW - new.w) / 2
new.offsetY = new.offsetY + (containerH - new.h) / 2
end
return new
end
return setmetatable( { new = newCamera, }, { __call = function( _, ... ) return newCamera( ... ) end } )