-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
371 lines (341 loc) · 15.7 KB
/
main.lua
File metadata and controls
371 lines (341 loc) · 15.7 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
math.randomseed(os.time())
-- CUSTOM FILES:
-- .psp - packstorm pool: words only
-- .pss - packstorm style: words and flows
love.filesystem.setIdentity("packstorm")
-- create the styles folder in case there isnt one and store the path
love.filesystem.createDirectory("styles/")
local stylesFolder = love.filesystem.getSaveDirectory() .. "/styles/"
love.filesystem.createDirectory("pools/")
local poolsFolder = love.filesystem.getSaveDirectory() .. "/pools/"
-- load the config variables from hconf.lua
hconf = require "hconf"
-- load the flow parser
flows = require "packparse"
-- styles are a table of words and flows that replicate the style of a certain packer
-- each style is stored in a .pss file in the styles folder
-- NOTE: .pss files are just lua files in disguise, so you can use lua syntax
-- when the styles are loaded in, store them in a table
styles = {}
-- when pools are loaded, store all their words in a table
-- this is so that the flow parser can use them
words = {
nouns = {},
adjectives = {},
verbs = {}
}
current_style = nil
joke = "select a style and press space!"
-- print a message to the console to tell the user that the console is open for debugging purposes
if hconf.verbose then
print("Hi there!")
print("Don't panic, I'm not hacking into your computer or anything. I'm just here to print out information that will be useful if you need to report a crash or something.")
print("If you still don't believe me, you can view the source code at github.com/haileylgbt/packstorm")
print("(You can turn this message off along with other reminders by setting verbose to false in hconf.lua)")
print("")
end
ui_ready = false
function love.load()
-- load the fonts
text = {
debug = love.graphics.newFont("fonts/UbuntuMono-Regular.ttf", 16),
regular_small = love.graphics.newFont("fonts/Ubuntu-Regular.ttf", 16),
regular = love.graphics.newFont("fonts/Ubuntu-Regular.ttf", 18),
regular_big = love.graphics.newFont("fonts/Ubuntu-Regular.ttf", 20),
title = love.graphics.newFont("fonts/Ubuntu-BoldItalic.ttf", 64),
loading = love.graphics.newFont("fonts/Ubuntu-Bold.ttf", 48),
joke = love.graphics.newFont("fonts/Ubuntu-Regular.ttf", 24),
header = love.graphics.newFont("fonts/Ubuntu-Bold.ttf", 32)
}
-- info about the loading progress will be printed to the console
-- this is useful for debugging
print("LOVE started successfully! Using version", love.getVersion())
print("Scanning styles folder...")
-- get all the files in the styles folder
local files = love.filesystem.getDirectoryItems("styles")
-- if there are no files, print an error and wait for the user to quit
if #files == 0 then
--[[
love.errhand("No files found! Be sure to place your styles in the styles folder!")
print("Press any key to quit...")
love.event.wait()
]]
end
print("Found", #files, "files! Sorting...")
-- loop through all the files
-- if the file is a .pss file, add to the pss file tally
-- if the file is template.pss, do nothing
-- if the file is not a .pss file, add to the other file tally
local pss_files = 0
local other_files = 0
for i, file in ipairs(files) do
if file:sub(-4) == ".pss" then
pss_files = pss_files + 1
elseif file == "template.pss" then
-- do nothing
else
other_files = other_files + 1
end
end
print("Files sorted!")
-- if there are no .pss files, print an error and wait for the user to press a key to quit
--[[
if pss_files == 0 then
love.errhand("No .pss files found! Be sure to place your styles in the styles folder!")
print("Press any key to quit...")
end
]]
-- print a breakdown of the files found
print("Found", pss_files, "styles and", other_files, "other files!")
-- if verbose is true and there's at least 1 other file, print a message to the console
if other_files > 0 and hconf.verbose then
print("Be sure to put images in the images folder!")
end
-- loop through all the files
-- if the file is a .pss file, load using love.filesystem.load
-- if the file is template.pss, do nothing
-- print a message for each file that is loaded
-- index the styles table by the name of the file
for i, file in ipairs(files) do
if file == "template.pss" then
-- do nothing
elseif file:sub(-4) == ".pss" then
local style = love.filesystem.load("styles/" .. file)()
print("Loaded style", style.name, "by", style.author)
styles[style.name] = style
end
end
print("Styles loaded!")
print("Creating style buttons...")
for name, style in pairs(styles) do
style.ui_buttonx = 0
style.ui_buttony = 0
style.ui_buttonw = 200
style.ui_buttonh = 50
style.ui_listindex = nil
style.ui_hovering = false
style.is_current = false
end
-- sort the styles alphabetically and set each style's ui_listindex to its index
-- in the sorted list
print("Indexing styles alphabetically...")
local sorted_styles = {}
for name, style in pairs(styles) do
table.insert(sorted_styles, style)
end
table.sort(sorted_styles, function(a, b) return a.name < b.name end)
for i, style in ipairs(sorted_styles) do
style.ui_listindex = i
end
-- set the current style to the first style in the list
print("Styles ready!")
-- begin pool loading process
print("Scanning pools folder...")
-- get all the files in the pools folder
local files = love.filesystem.getDirectoryItems("pools")
-- if there are no files, print an error and wait for the user to quit
if #files == 0 then
love.errhand("No files found! Be sure to place your pools in the pools folder!")
print("Press any key to quit...")
love.event.wait()
end
print("Found", #files, "files! Sorting...")
-- loop through all the files
-- if the file is a .psp file, add to the psp file tally
-- if the file is template.psp, do nothing
-- if the file is not a .psp file, add to the other file tally
local psp_files = 0
local other_files = 0
for i, file in ipairs(files) do
if file:sub(-4) == ".psp" then
psp_files = psp_files + 1
elseif file == "template.psp" then
-- do nothing
else
other_files = other_files + 1
end
end
print("Files sorted!")
-- if there are no .psp files, print an error and wait for the user to press a key to quit
--[[
if psp_files == 0 then
love.errhand("No .psp files found! Be sure to place your pools in the pools folder!")
print("Press any key to quit...")
end
]]
-- print a breakdown of the files found
print("Found", psp_files, "pools and", other_files, "other files!")
-- if verbose is true and there's at least 1 other file, print a message to the console
if other_files > 0 and hconf.verbose then
print("Be sure to put images in the images folder!")
end
-- loop through all the files
-- if the file is a .psp file, load using love.filesystem.load
-- if the file is template.psp, do nothing
-- print a message for each file that is loaded
-- when a pool is loaded, add its nouns, verbs, and adjectives in the words table
-- dont add the pool itself to the pools table
for i, file in ipairs(files) do
if file == "template.psp" then
-- do nothing
elseif file:sub(-4) == ".psp" then
local pool = love.filesystem.load("pools/" .. file)()
print("Loading pool", pool.name, "by", pool.curator)
-- add the pool's words.nouns into words.nouns
for i, noun in ipairs(pool.words.nouns) do
table.insert(words.nouns, noun)
end
print("Loaded", #pool.words.nouns, "nouns")
-- add the pool's words.verbs into words.verbs
for i, verb in ipairs(pool.words.verbs) do
table.insert(words.verbs, verb)
end
print("Loaded", #pool.words.verbs, "verbs")
-- add the pool's words.adjectives into words.adjectives
for i, adjective in ipairs(pool.words.adjectives) do
table.insert(words.adjectives, adjective)
end
print("Loaded", #pool.words.adjectives, "adjectives")
print("Loaded a total of", #pool.words.nouns + #pool.words.verbs + #pool.words.adjectives, "words!")
end
end
print("UI is ready!")
ui_ready = true
end
function love.keypressed(key, scancode, isrepeat)
if key == "escape" then
love.event.quit()
end
if key == "f3" then
hconf.debug = not hconf.debug
end
-- when space is pressed, get a random flow from the current style, parse it and set the joke variable to the result
if key == "space" and current_style then
local flow = current_style.flows[math.random(#current_style.flows)]
local parsed_flow = flows.parse(flow, words)
joke = parsed_flow
print(joke)
end
end
function love.update(dt)
mouse_x, mouse_y = love.mouse.getPosition()
-- calculate the position of each button and set each style's ui_buttonx, ui_buttony and ui_listindex variables
-- the beginning of the list is under the styles header (8, 128)
-- each button should be spaced out by 8 pixels vertically
for name, style in pairs(styles) do
style.ui_buttonx = 8
style.ui_buttony = 150 + (style.ui_listindex - 1) * style.ui_buttonh * 1.25
end
-- if the mouse is hovering over a style's button, set that style's ui_hovering variable to true
-- if the mouse is not hovering over a style's button, set that style's ui_hovering variable to false
for name, style in pairs(styles) do
if mouse_x >= style.ui_buttonx and mouse_x <= style.ui_buttonx + style.ui_buttonw and mouse_y >= style.ui_buttony and mouse_y <= style.ui_buttony + style.ui_buttonh then
style.ui_hovering = true
else
style.ui_hovering = false
end
end
end
function love.mousepressed(x, y, button, istouch)
if button == 1 then
for name, style in pairs(styles) do
if x >= style.ui_buttonx and x <= style.ui_buttonx + style.ui_buttonw and y >= style.ui_buttony and y <= style.ui_buttony + style.ui_buttonh then
current_style = style
style.is_current = true
else
style.is_current = false
end
end
end
end
function love.draw()
if not ui_ready then
love.graphics.setColor(255, 255, 255)
-- ubuntu bold, 48, centered, middle of the screen, "loading..."
love.graphics.setFont(text.loading)
love.graphics.printf("loading!", 0, love.graphics.getHeight() / 2 - 50, love.graphics.getWidth(), "center")
-- ubuntu regular, 16, centered, below the loading message
love.graphics.setFont(text.regular)
love.graphics.printf("we're loading the styles and organizing the menu. if this is still hear after one second then your computer is reeeeally slow or a hidden error occured.", 0, love.graphics.getHeight() / 2 + 16, love.graphics.getWidth(), "center")
else
-- set color to white
love.graphics.setColor(255, 255, 255)
-- ubuntu bold italic, 48px, top left of window with reasonable margin, "packstorm"
love.graphics.setFont(text.title)
love.graphics.print("packstorm", 8, 1)
-- ubuntu mono, small but readable, just under the title, "by hailey#0048"
love.graphics.setFont(text.debug)
love.graphics.print("by hailey#0048", 8, 72)
-- draw the number of styles, total words, nouns, verbs, and adjectives in the top right corner
-- example: "styles: 1, words: 1, nouns: 1, verbs: 1, adjectives: 1"
love.graphics.setFont(text.regular_small)
love.graphics.print("words: " .. #words.nouns + #words.verbs + #words.adjectives, love.graphics.getWidth() - 128, 24)
love.graphics.print("nouns: " .. #words.nouns, love.graphics.getWidth() - 128, 40)
love.graphics.print("verbs: " .. #words.verbs, love.graphics.getWidth() - 128, 56)
love.graphics.print("adjectives: " .. #words.adjectives, love.graphics.getWidth() - 128, 72)
love.graphics.setColor(128, 128, 128, 0.25)
love.graphics.rectangle("fill", 8, love.graphics.getHeight() - 0 - 128, love.graphics.getWidth() - 16, 100)
love.graphics.setColor(255, 255, 255)
-- draw the generated pack
love.graphics.setFont(text.joke)
love.graphics.printf(joke, 16, love.graphics.getHeight() - 0 - 128 + 8, love.graphics.getWidth() - 32, "left")
-- styles header
-- ubuntu bold, 32px, above the styles list, "styles"
love.graphics.setFont(text.header)
love.graphics.print("styles", 8, 104)
love.graphics.setFont(text.regular_big)
-- draw the styles list
-- every button has a grey filled background and white text
-- when the mouse is over a button, add a white border
for name, style in pairs(styles) do
if style.ui_hovering then
if style.is_current then
love.graphics.setColor(255, 255, 255, 0.85)
else
love.graphics.setColor(128, 128, 128, 0.30)
end
love.graphics.rectangle("fill", style.ui_buttonx, style.ui_buttony, style.ui_buttonw, style.ui_buttonh)
love.graphics.setColor(0, 255, 255, 255)
love.graphics.rectangle("line", style.ui_buttonx, style.ui_buttony, style.ui_buttonw, style.ui_buttonh)
else
if style.is_current then
love.graphics.setColor(255, 255, 255, 255)
else
love.graphics.setColor(128, 128, 128, 0.25)
end
love.graphics.rectangle("fill", style.ui_buttonx, style.ui_buttony, style.ui_buttonw, style.ui_buttonh)
end
--white
if style.is_current then
love.graphics.setColor(0, 0, 0, 255)
else
love.graphics.setColor(255, 255, 255, 255)
end
love.graphics.print(style.name, style.ui_buttonx + 8, style.ui_buttony + 8)
if style.ui_hovering then
love.graphics.setColor(0, 0, 0, 255)
love.graphics.rectangle("fill", mouse_x + 8, mouse_y + 8, 300 + 16, 150 + 16)
love.graphics.setColor(0, 255, 255, 255)
love.graphics.rectangle("line", mouse_x + 8, mouse_y + 8, 300 + 16, 150 + 16)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print(style.author, mouse_x + 12, mouse_y + 12)
love.graphics.printf(style.description, mouse_x + 12, mouse_y + 16 + love.graphics.getFont():getHeight(style.author), 300, "left")
end
end
-- draw a long rounded grey rectangle at the bottom middle of the screen with a big margin
end
-- if the user presses f3, toggle the debug overlay in the bottom left
if hconf.debug then
-- ubuntu mono, 16px
love.graphics.setFont(text.debug)
love.graphics.setColor(0, 255, 255)
love.graphics.print("debug! :D", 400, 0)
love.graphics.print("fps: " .. love.timer.getFPS(), 400, 16)
-- print the current position of the mouse
love.graphics.print("mouse: " .. mouse_x .. ", " .. mouse_y, 400, 32)
-- print the current style
if current_style then
love.graphics.print("current style: " .. current_style.name, 400, 48)
end
end
end