-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP8win.moon
More file actions
316 lines (264 loc) · 8.11 KB
/
P8win.moon
File metadata and controls
316 lines (264 loc) · 8.11 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
-- module: P8window
love = love
graphics = love.graphics
window = love.window
mouse = love.mouse
print = print
-- generale conf for P8win module.
-- @table moduleConf
-- @field debug debugMode
-- @field moduleName moduleName
moduleConf = {
debug: true
moduleName: "P8win"
}
local dump
Uid = ->
f = (x) ->
r = random(16) - 1
r = (x == "x") and (r + 1) or (r % 4) + 9
return ("0123456789abcdef")\sub r, r
return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx")\gsub("[xy]", f))
if moduleConf.debug then
m = assert require("moon")
dump = m.p
class Singleton
__inherited: (By) =>
By.getInstance = (...) ->
if I = By.Instance then return I
with I = By ...
By.Instance = I
-- pixel focused screen scaling.
-- @classmod P8Win
class P8Win extends Singleton
-- window size defined in conf.lua/moon
-- @table moduleCwinSizeonf
-- @field width
-- @field height
@winSize = {
width: graphics.getWidth!
height: graphics.getHeight!
}
-- module configuration
-- @table conf
-- @field allowResize
-- @field globalScaling
-- @field pixelPerfectFullscreen
-- @field cursor
-- @field showSysCursor
@conf = {
allowResize: false
globalScaling: true
pixelPerfectFullscreen: false
cursor: 0 -- 0 OS cursor
showSysCursor: false -- to use the costom cursor
}
-- sets the default filter and line style globaly. (Pixel centred)
@setGlobalFilterlLineStyle: =>
if @@conf.globalScaling
@@pDebug "Setting DefaultFilter && LineStyle"
graphics.setDefaultFilter "nearest", "nearest", 1 -- https://love2d.org/wiki/love.graphics.setDefaultFilter
graphics.setLineStyle "rough" -- https://love2d.org/wiki/love.graphics.setLineStyle
-- debug function
@pDebug: (...) =>
if moduleConf.debug
t = {...}
if #t == 1 and type(t[1]) == 'string'
print moduleConf.moduleName .. ': ' .. t[1]
else
print moduleConf.moduleName
dump {...}
-- gets the desk dimensions
getMonitorSize: =>
w, h = window.getDesktopDimensions 1
@monitor = {
w: w,
h: h,
}
-- gets the max scale for the window
getMaxScale: =>
fWidth = @monitor.w / @@winSize.width
fheight = @monitor.h / @@winSize.height
math = math
if fheight < fWidth
@maxScale = @monitor.h / @@winSize.height
@maxWinScale = math.floor (@monitor.h - 125) / @@winSize.height
else
@maxScale = @monitor.w / @@winSize.width
@maxWinScale = math.floor (@monitor.w - 125) / @@winSize.width
-- calculates the fullscreen offset for the canvas in full screen mode
-- @tparam number height
-- @tparam number width
calcFullScreenOffset: (height = @@winSize.height, width = @@winSize.width) =>
math = math
fullScale = @maxScale
if @pixelPerfectFullscreen
fullScale = math.floor @maxScale
gameWidth = @@winSize.width * fullScale
blankWidth = width - gameWidth
gameHeight = @@winSize.height * fullScale
blankHeight = height - gameHeight
@offset.x = math.floor blankWidth/2
@offset.y = math.floor blankHeight/2
if window.getFullscreen! == false
@offset = {x: 0, y: 0}
-- sets the scale
-- @tparam number scale
setGameScale: (scale) =>
@scale = scale
window.setMode @@winSize.width * @scale, @@winSize.height * @scale, {fullscreen: false, resizable: @@conf.allowResize, highdpi: false}
-- sets cursor visibility
-- @tparam bool visible
setCursorVisibility: (visible) =>
mouse.setVisible visible
-- creates images for the custom cursors
createCustomMouse: =>
for cc, v in ipairs @cursorsPathsOffset
@cursors[cc] = graphics.newImage v[1]
-- updates the custom mouse
updateCustomMouse: =>
math = math
@mouse.x = math.floor (mouse.getX! - @offset.x) / @scale
@mouse.y = math.floor (mouse.getY! - @offset.y) / @scale
-- init the instance
-- @tparam number scale
new: (scale, cursorsPathsOffset = nil) =>
@@pDebug "Initializing."
@monitor = {}
@maxScale = 0
@scale = 0
@maxWinScale = 0
@offset = {
x: 0
y: 0
}
@mouse = {
x: 0
y: 0
}
@cursors = {}
@cursorsPathsOffset = cursorsPathsOffset
if @cursorsPathsOffset
@currentCursor = 1 -- default cursor will be the first.
else
@currentCursor = 0
@afterShader = ->
-- MB: put this code in a setUp methode so it does not run when instancing ?
@@setGlobalFilterlLineStyle!
if @cursorsPathsOffset then @createCustomMouse!
@mainCanvas = graphics.newCanvas @@winSize.width, @@winSize.height
@shaderCanvas = graphics.newCanvas @@winSize.width, @@winSize.height
@shaderPool = {}
@getMonitorSize!
@getMaxScale!
@calcFullScreenOffset!
dScale = scale or @maxWinScale
@setGameScale dScale
if @currentCursor ~= 0
@setCursorVisibility @@conf.showSysCursor
else
@setCursorVisibility true -- sys cursor always visible if no cursors provided.
-- updates the instance
-- @tparam number dt
update: (dt) =>
@updateCustomMouse!
@calcFullScreenOffset!
-- sets the canvas that we will be drawing on
start: =>
graphics.setCanvas {@mainCanvas, stencil: true}
graphics.clear 0, 0, 0, 1
graphics.setColor 1, 1, 1, 1
-- return to the default canvas & apply shaders and cursor
stop: (hx = 0, hy = 0, hr = 0, hsx = 0, hsy = 0) =>
for shader=1, #@shaderPool
graphics.setCanvas {@shaderCanvas, stencil: true}
graphics.setShader @shaderPool[shader][2]
graphics.draw @mainCanvas
graphics.setShader!
graphics.setCanvas {@mainCanvas, stencil: true}
graphics.draw @shaderCanvas
@drawAfterShader!
if @cursorsPathsOffset and @currentCursor > 0
graphics.draw @cursors[@currentCursor], @mouse.x - @cursorsPathsOffset[@currentCursor][2], @mouse.y - @cursorsPathsOffset[@currentCursor][3]
graphics.setCanvas!
graphics.draw @mainCanvas, hx + @offset.x, hy + @offset.y, hr, hsx + @scale, hsy + @scale
-- sets the game scale
-- @tparam number s
setGameScale: (s) =>
@scale = s
window.setMode @@winSize.width * @scale, @@winSize.height * @scale, {fullscreen: false, resizable: @@conf.allowResize, highdpi: false}
-- toggles fullscreen
toggleFullscreen: =>
math = math
if window.getFullscreen! == false
fScale = @maxScale
if @@conf.pixelPerfectFullscreen
fScale = math.floor @maxScale
@setGameScale fScale
window.setFullscreen true, "desktop"
else
@setGameScale math.floor(@maxWinScale)
window.setFullscreen false
-- defines the resize function to follow the scale inc
defineLoveResize: =>
if @@conf.allowResize
with love
.resize = (w, h) ->
if @scale ~= @maxScale
if @@winSize.width * @scale < w
if @scale + 1 < @maxScale
@scale += 1
@setGameScale @scale
else
@setGameScale @scale
elseif @@winSize.width * @scale > w
if @scale - 1 >= 1
@scale -= 1
@setGameScale @scale
else
@setGameScale @scale
else
@setGameScale @scale
-- toggles the cursor
toggleCursor: =>
mouse.setVisible not mouse.isVisible!
-- sets the cursor
-- @tparam number cursorNumber
setCursor: (cursorNumber) =>
if cursorNumber <= #@cursors and cursorNumber >=0
@currentCursor = cursorNumber
else
return
-- gets cursors count
getCursorsCount: =>
#@cursors
-- SHADERS
-- pushes love shader
-- @tparam shader shader
pushShader: (shader) =>
shaderId = Uid!
@shaderPool[#@shaderPool + 1] = {shaderId, shader}
shaderId
-- clear shaders pool
clearShaders: =>
@shaderPool = {}
-- pops the last shader
popShader: =>
@shaderPool[#@shaderPool] = nil
-- gets shaders count
countShaders: =>
#@shaderPool
-- sets after shader draw func
-- @tparam function f
setDrawAfterShader: (f) =>
if type(f) == "function"
@afterShader = f
else
return nil
-- clear draw after shader func
clearDrawAfterShader: =>
@afterShader = ->
-- @local
drawAfterShader: =>
@afterShader!
P8Win