-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewport.lua
More file actions
232 lines (192 loc) · 5.95 KB
/
viewport.lua
File metadata and controls
232 lines (192 loc) · 5.95 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
local Viewport = {}
Viewport.__index = Viewport
local roundDownToNearest = function(val, multiple)
return multiple * (math.floor(val/multiple))
end
function Viewport.new(opts)
local self = {}
setmetatable(self, Viewport)
opts = opts or {}
setmetatable(opts,{__index={
width = 640,
height = 360,
multiple = 1,
scale = 0,
resizable = true,
fs = false
}})
self:setWidth(opts.width)
self:setHeight(opts.height)
self:setScaleMultiple(opts.multiple)
self:setScale(opts.scale)
self:setResizable(opts.resizable)
self:setFullscreen(opts.fs)
self:setupScreen()
return self
end
function Viewport:setupScreen()
self:setScale(self.scale)
love.graphics.setDefaultFilter('nearest', 'nearest', 0)
if(self:setFullscreen(self.fs)) then
love.window.setMode(0, 0, {fullscreen = true, fullscreentype = "desktop"})
else
love.window.setMode(self.width * self.r_scale,
self.height * self.r_scale,
{resizable = self.resizable})
end
self.r_width = self.width * self.r_scale
self.r_height = self.height * self.r_scale
self.draw_ox = (love.graphics.getWidth() - (self.r_width)) / 2
self.draw_oy = (love.graphics.getHeight() - (self.r_height)) / 2
end
function Viewport:setScale(scale)
local scale = roundDownToNearest(scale, self.multiple)
self.scale = scale
local screen_w, screen_h = love.window.getDesktopDimensions()
local pixel_scale = love.window.getPixelScale()
screen_w = screen_w * pixel_scale
screen_h = screen_h * pixel_scale
if (not self.fs) then
-- subtract some height so that windowed mode doesn't scale
-- beyond titlebar + application bar height in windows
screen_w = screen_w - (64 * pixel_scale)
screen_h = screen_h - (96 * pixel_scale)
end
local max_scale = math.min(roundDownToNearest(screen_w / self.width, self.multiple),
roundDownToNearest(screen_h / self.height, self.multiple))
if (max_scale < 1) then
max_scale = 1
end
if (self.fs or (scale or 0) <= 0 or (scale or 0) > max_scale) then
self.r_scale = max_scale
else
self.r_scale = scale
end
return self.r_scale
end
function Viewport:fixSize(w, h)
local screen_w, screen_h = love.window.getDesktopDimensions()
local pixel_scale = love.window.getPixelScale()
screen_w = screen_w * pixel_scale
screen_h = screen_h * pixel_scale
if (not self.fs) then
-- subtract some height so that windowed mode doesn't scale
-- beyond titlebar + application bar height in windows
screen_w = screen_w - (64 * pixel_scale)
screen_h = screen_h - (96 * pixel_scale)
end
local cur_scale = math.max(roundDownToNearest(w / self.width, self.multiple),
roundDownToNearest(h / self.height, self.multiple))
print(cur_scale)
local max_scale = math.min(roundDownToNearest(screen_w / self.width, self.multiple),
roundDownToNearest(screen_h / self.height, self.multiple))
if (cur_scale < 1) then
self.scale = 1
elseif(cur_scale > max_scale) then
self.scale = max_scale
else
self.scale = cur_scale
end
self:setupScreen()
end
function Viewport:getWidth()
return self.width
end
function Viewport:setWidth(width)
local screen_w, screen_h = love.window.getDesktopDimensions()
local pixel_scale = love.window.getPixelScale()
screen_w = screen_w * pixel_scale
screen_h = screen_h * pixel_scale
self.width = math.floor(math.min(width, screen_w))
return self.width
end
function Viewport:getHeight()
return self.height
end
function Viewport:setHeight(height)
local screen_w, screen_h = love.window.getDesktopDimensions()
local pixel_scale = love.window.getPixelScale()
screen_w = screen_w * pixel_scale
screen_h = screen_h * pixel_scale
self.height = math.floor(math.min(height, screen_h))
return self.height
end
function Viewport:getScaleMultiple()
return self.multiple
end
function Viewport:setScaleMultiple(multiple)
self.multiple = multiple
return self.multiple
end
function Viewport:getResizable()
return self.resizable
end
function Viewport:setResizable(resizable)
self.resizable = resizable
return self.resizable
end
function Viewport:getParams()
return {
width = self.width,
height = self.height,
scale = self.scale,
fs = self.fs,
r_scale = self.r_scale,
r_width = self.r_width,
r_height = self.r_height,
draw_ox = self.draw_ox,
draw_oy = self.draw_oy
}
end
function Viewport:setFullscreen(mode)
if (mode == nil) then
self.fs = not self.fs
elseif (mode) then
self.fs = true
else
self.fs = false
end
return self.fs
end
function Viewport:left(x)
return x
end
function Viewport:top(y)
return y
end
function Viewport:lefttop(x, y)
return x, y
end
function Viewport:right(x, w)
w = tonumber(w) or 0
return self.width - x - w
end
function Viewport:righttop(x, y, w, h)
w = tonumber(w) or 0
return self.width - x - w, y
end
function Viewport:bottom(y, h)
h = tonumber(h) or 0
return self.height - y - h
end
function Viewport:leftbottom(x, y, w, h)
h = tonumber(h) or 0
return x, self.height - y - h
end
function Viewport:rightbottom(x, y, w, h)
w = tonumber(w) or 0
h = tonumber(h) or 0
return self.width - x - w, self.height - y - h
end
function Viewport:pushScale()
love.graphics.push()
love.graphics.translate(self.draw_ox, self.draw_oy)
love.graphics.scale(self.r_scale, self.r_scale)
love.graphics.setScissor(self.draw_ox, self.draw_oy, self.r_width, self.r_height)
end
function Viewport:popScale()
love.graphics.scale(1)
love.graphics.pop()
love.graphics.setScissor()
end
return Viewport