-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.lua
348 lines (310 loc) · 9.6 KB
/
main.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
_G.lg = love.graphics
_G.lm = love.mouse
local urutora = require('urutora')
local u
local styleManager = require('styleManager')
require 'images'
require 'fonts'
-- todos:
-- fix slider movement inside scrolled panels - done
-- fix padding for panels - done
-- add modals for messages and questions (yes/no)
-- add scroll indicators (not sliders) - done
-- make images and animations gray when disabled - done
-- link sliders to panel's scrolling
-- digital joystick (8-directional) - done
-- multitouch support
-- style hot swap - done
-- blinking cursor in text fields and cursor displacement
-- disable/enable elements recursively - done
-- custom 'fixed' images for bgs and fgs
-- NOTES:
-- When using setStyle, all the previous styles is deleted first (reset to default)
local bgColor = { 0.2, 0.1, 0.3 }
for _, bg in ipairs(bgs) do bg:setFilter('nearest', 'nearest') end
bgIndex = 1
bgRotation = 0
local function initCanvasStuff()
w, h = 320 * 1, 180 * 1
canvas = lg.newCanvas(w, h)
canvas:setFilter('nearest', 'nearest')
canvasX, canvasY = 0, 0
sx = lg.getWidth() / canvas:getWidth()
sy = lg.getHeight() / canvas:getHeight()
end
local function doResizeStuff(w, h)
sx = math.floor(w / canvas:getWidth())
sx = sx < 1 and 1 or sx
sy = sx
if (canvas:getHeight() * sy) > h then
sy = math.floor(h / canvas:getHeight())
sy = sy < 1 and 1 or sy
sx = sy
end
canvasX = w / 2 - (canvas:getWidth() / 2) * sx
canvasY = h / 2 - (canvas:getHeight() / 2) * sy
u.setDimensions(canvasX, canvasY, sx, sy)
end
local function initStuff()
u = urutora:new()
initCanvasStuff()
u.setDefaultFont(proggyTiny)
doResizeStuff(lg.getDimensions())
transparentCursorImg = love.image.newImageData(1, 1)
lm.setCursor(lm.newCursor(transparentCursorImg))
-- lm.setRelativeMode(true)
ship1 = {
img = ship1Img,
x = 100,
y = 100
}
ship2 = {
img = ship2Img,
x = 110,
y = 100
}
end
local function initPanelC()
local panelC = u.panel({
-- debug = true,
rows = 8, cols = 1,
verticalScale = 2,
tag = 'panelc',
-- bgColor = {0.2, 0.2, 0.7, 1},
-- move 1/16 of the vewport for every mousewheel event
scrollSpeed = 1/16 -- 16 wheel steps to fully scroll
})
local function toggleDebug(evt)
evt.target.parent.debug = not evt.target.parent.debug
end
for i = 1, panelC.rows do
panelC:addAt(i, 1, u.button({ text = 'Button ' .. i }):action(toggleDebug))
end
return panelC
end
local function initPanelB(anotherPanel)
return u.panel({
-- debug = true,
rows = 10, cols = 2,
verticalScale = 2,
tag = 'panelb',
-- bgColor = {0.6, 0.1, 0.3, 1},
scrollSpeed = 1/10 -- 10 wheel steps to fully scroll
})
:colspanAt(1, 1, 2) -- panel label
:colspanAt(6, 2, 0.2) -- vertical slider
:rowspanAt(6, 2, 4) -- vertical slider
:rowspanAt(6, 1, 4) -- panel C
:addAt(2, 1, u.multi({ index = 3, items = { 'One', 'Two', 'Three', 'Four' }, tag = 'multi1' }))
:addAt(2, 2, u.toggle()
:action(function (evt)
evt.target.parent.debug = evt.value
end))
:addAt(3, 1, u.toggle())
:addAt(3, 2, u.toggle():center())
:addAt(1, 1, u.label({ text = 'Panel B (scroll)' }))
:addAt(6, 2, u.slider({ axis = 'y' }))
:addAt(4, 2, u.label({ text = 'joyX: 0', tag = 'xLabel' }):left())
:addAt(5, 2, u.label({ text = 'joyY: 0', tag = 'yLabel' }):left())
:addAt(4, 1, u.label({ text = '', tag = 'directionLabel' }))
:addAt(5, 1, u.label({ text = '', tag = 'lastCoordLabel' }))
:addAt(6, 1, anotherPanel)
end
local function initPanelA(anotherPanel)
-- rowspanAt(row, col) 1-base indexed
-- addAt(row, col) 1-base indexed
return u.panel({
-- debug = true,
rows = 18, cols = 4,
w = w, h = h,
scrollSpeed = 1/18,
verticalScale = 2,
tag = 'panela'
})
:rowspanAt(8, 2, 2) -- 2 rows for the joystick
:rowspanAt(2, 3, 2) -- love2d logo
:rowspanAt(2, 4, 2) -- Animation
:colspanAt(4, 3, 2) -- Click label
:colspanAt(5, 3, 2) -- panel B
:rowspanAt(5, 3, 5) -- panel B
:addAt(1, 1, u.label({ text = 'Label:' }):right())
:addAt(1, 2, u.label({ text = 'Panel A' }))
:addAt(2, 1, u.label({ text = 'Button:' }):right())
:addAt(2, 2, u.button({ text = 'Exit' })
:action(function(evt)
love.event.quit()
end))
:addAt(3, 1, u.label({ text = 'Slider:' }):right())
:addAt(3, 2, u.slider({ value = 0.2, tag = 'slider1' }))
:addAt(4, 1, u.label({ text = 'Text field:' }):right())
:addAt(4, 2, u.text({ text = 'привт', tag = 'russian' })
:setStyle({ font = robotoBold }))
:addAt(5, 1, u.label({ text = 'Multi:' }):right())
:addAt(5, 2, u.multi({ items = { 'LÖVE', 'Olive', 'Neon', 'Metal' } })
:action(function(evt)
bgIndex = evt.index
styleManager.handleStyleChanges(u, evt)
end))
:addAt(6, 1, u.label({ text = 'Toggle:' }):right())
:addAt(6, 2, u.toggle():right()
:action(function (evt)
evt.target.parent.debug = not evt.target.parent.debug
end))
:addAt(7, 1, u.label({ text = 'Prog. bar:' }):right())
:addAt(7, 2, u.progressBar({ speed = 0.25, value = 0.4, direction = -1 }):action(function (evt)
if evt.type == 'full' then
evt.target.direction = evt.target.direction * -1
elseif evt.type == 'empty' then
evt.target.direction = evt.target.direction * -1
end
end))
:addAt(8, 1, u.label({ text = 'Joystick:' }):right())
:addAt(8, 2, superJoystick)
:addAt(1, 3, u.label({ text = 'Image:' }))
:addAt(1, 4, u.label({ text = 'Animation:' }))
:addAt(2, 3, u.image({ image = logo })
:action(function(evt)
evt.target.keepAspectRatio = not evt.target.keepAspectRatio
end))
:addAt(2, 4, u.animation({
image = enemy,
frames = 26,
frameWidth = 110,
frameHeight = 84,
frameDelay = 0.08
})
:action(function(evt)
evt.target.keepOriginalSize = not evt.target.keepOriginalSize
end))
:addAt(4, 3, u.label({ text = 'Click on the images^' }))
:addAt(5, 3, anotherPanel)
end
function love.load()
initStuff()
superJoystick = u.joy({
layer1 = joyLayer1,
layer2 = joyLayer2,
layer3 = joyLayer3,
activateOn = 0.7, -- percent
}):action(function(evt)
local directionLabel = u:getByTag('directionLabel')
local lastCoordLabel = u:getByTag('lastCoordLabel')
local xLabel = u:getByTag('xLabel')
local yLabel = u:getByTag('yLabel')
if evt.type == 'moved' then
local x, y = u.utils.toFixed(evt.value.x, 2), u.utils.toFixed(evt.value.y, 2)
xLabel.text = 'joyX: ' .. x
yLabel.text = 'joyY: ' .. y
local dir = evt.value.directions
directionLabel.text = (function()
return (
(dir.left and 'l' or '') ..
(dir.right and 'r' or '') ..
(dir.up and 'u' or '') ..
(dir.down and 'd' or '')
)
end)()
elseif evt.type == 'released' then
local lx = u.utils.toFixed(evt.value.lastX, 2)
local ly = u.utils.toFixed(evt.value.lastY, 2)
xLabel.text = 'joyX: 0'
yLabel.text = 'joyY: 0'
directionLabel.text = ''
lastCoordLabel.text = lx .. ', ' .. ly
end
end)
local panelC = initPanelC()
local panelB = initPanelB(panelC)
panelA = initPanelA(panelB)
u:add(panelA)
u:getByTag('russian'):setStyle({ font = proggySquare })
-- simulate event
styleManager.handleStyleChanges(u, {
index = 1
})
--activation and deactivation elements by tag
--u:deactivateByTag('panela')
end
local x = 0
local y = 0
function love.update(dt)
u:update(dt)
bgRotation = bgRotation + dt * 5
if bgRotation >= 360 then bgRotation = 0 end
ship1.x = ship1.x + superJoystick:getX()
ship1.y = ship1.y + superJoystick:getY()
local dir = superJoystick:getDirections()
if dir.left then
ship2.x = ship2.x - dt * 60
end
if dir.right then
ship2.x = ship2.x + dt * 60
end
if dir.up then
ship2.y = ship2.y - dt * 60
end
if dir.down then
ship2.y = ship2.y + dt * 60
end
end
local function drawBg()
local bg = bgs[bgIndex]
lg.draw(bg,
canvas:getWidth() / 2,
canvas:getHeight() / 2,
math.rad(bgRotation),
canvas:getWidth() / bg:getWidth() * 3,
canvas:getHeight() / bg:getHeight() * 3,
bg:getWidth() / 2,
bg:getHeight() / 2
)
end
function drawCursor()
if lm.isDown(1) then
lg.setColor(1, 0, 0)
end
local x, y = u.utils:getMouse()
lg.draw(arrow, math.floor(x), math.floor(y))
lg.setColor(1, 1, 1)
end
function drawShips()
u.utils.draw(ship1.img, ship1.x, ship1.y, {centered = true})
u.utils.draw(ship2.img, ship2.x, ship2.y, {centered = true})
end
function love.draw()
lg.setCanvas({ canvas, stencil = true })
lg.clear(bgColor)
lg.setColor(1, 1, 1)
drawBg() -- spinning squares
u:draw()
drawCursor()
drawShips()
lg.setColor(1, 1, 1)
lg.setCanvas()
lg.draw(canvas, math.floor(canvasX), math.floor(canvasY), 0, sx, sy)
-- lg.print('FPS: ' .. love.timer.getFPS())
end
function love.mousepressed(x, y, button) u:pressed(x, y, button) end
function love.mousemoved(x, y, dx, dy) u:moved(x, y, dx, dy) end
function love.mousereleased(x, y, button) u:released(x, y) end
function love.textinput(text) u:textinput(text) end
function love.wheelmoved(x, y) u:wheelmoved(x, y) end
function love.keypressed(k, scancode, isrepeat)
u:keypressed(k, scancode, isrepeat)
if k == 'escape' then
love.event.quit()
end
if k == 'm' then
lm.setRelativeMode(not lm.getRelativeMode())
end
if k == 'f9' then
if not panelA.enabled then
panelA:enable()
else
panelA:disable()
end
end
end
function love.resize(w, h)
doResizeStuff(w, h)
end