-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
1976 lines (1437 loc) · 44 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
9th is back with a tool woo i sure do hope floating text decos dont come within 3 updates of releasing this
this tool exports all the words from a text file using a specified font into multiple pngs, to be used for decorations
no i dont get any of the math either
CREDITS
https://github.com/EmmanuelOga/easing/blob/master/lib/easing.lua
https://love2d.org/wiki/HSV_color
https://github.com/EmmanuelOga/columns/blob/master/utils/color.lua
https://gist.github.com/jasonbradley/4357406
https://stackoverflow.com/questions/1426954/split-string-in-lua
https://github.com/rxi/json.lua
]]
_EXPORTERVERSION = '1.3'
-- variables
min = math.min
json = require 'json'
easing = require 'easing'
rdfont = love.graphics.newFont("rdfont.otf", 16)
rdfont:setFilter("nearest", "nearest")
previewrdfont = love.graphics.newFont("rdfont.otf", 16)
previewrdfont:setFilter("nearest", "nearest")
-- @customizable in-program
outlineColor = {0, 0, 0, 1}
textColor = {1, 1, 1, 1}
makeSpritesheet = false
ignoreDupes = true
fontSize = 24
wordAlign = 0.5
dropShadowAngle = 315
dropShadowDist = 3
dropShadowColor = {1, 0, 0, 0}
words = {} -- stores the words from the lyrics
uniquewords = {} -- stores unique words only
escaped_space_str = '_originallyescapedspace_terriblecode_woo_noedgecasesright__fuck_' -- i sure do hope this doesnt cause issues
camera = {
x = 0, -- pos
y = 0, -- pos
startTime = 0, -- anim
duration = 0, -- anim
ease = easing.outQuad, -- anim
startX = 0, -- anim
startY = 0, -- anim
endX = 0, -- anim
endY = 0, -- anim
animating = false,
update = function()
if camera.animating then
local progress = camera.ease(love.timer.getTime() - camera.startTime, 0, 1, camera.duration)
if progress < 1 then
camera.x = math.floor(lerp(camera.startX, camera.endX, progress) + 0.5)
camera.y = math.floor(lerp(camera.startY, camera.endY, progress) + 0.5)
else
camera.animating = false
camera.x = camera.endX
camera.y = camera.endY
end
end
end,
moveTo = function(x, y, dur, ease)
camera.startX = camera.x
camera.startY = camera.y
camera.endX = x
camera.endY = y
camera.duration = dur or 1
camera.startTime = love.timer.getTime()
camera.ease = ease or easing.outQuad
camera.animating = true
end
}
default = {
touchTest = function(mousex, mousey, obj)
return touching(mousex, mousey, obj.x, obj.y, obj.x + obj.width, obj.y + obj.height)
end,
onClickButton = function(b) end,
onClickPicker = function(p) end,
onClickTextInput = function(i) end,
onUnselectTick = function(t) end,
onClickTick = function(t)
t.clicked = not t.clicked
end,
onDrawButton = function(b)
if mousetouching(b) then
if b.activeFillColor then
love.graphics.setColor(b.activeFillColor[1], b.activeFillColor[2], b.activeFillColor[3], 1)
else
love.graphics.setColor(1, 1, 1, 1)
end
else
if b.inactiveFillColor then
love.graphics.setColor(b.inactiveFillColor[1], b.inactiveFillColor[2], b.inactiveFillColor[3], 1)
else
love.graphics.setColor(0.8, 0.8, 0.8, 1)
end
end
love.graphics.rectangle("fill", b.x, b.y, b.width, b.height, min(10,b.width/4), min(10,b.height/4), 20)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.push()
local width, lines = rdfont:getWrap(b.text, b.width)
local theight = rdfont:getHeight(b.text) * #lines
love.graphics.translate(b.x, b.y + (b.height - theight * b.textscale) / 2)
love.graphics.scale(b.textscale, b.textscale)
love.graphics.setColor(b.textcolor[1], b.textcolor[2], b.textcolor[3], 1)
love.graphics.printf(b.text, rdfont, 0, 0, b.width / b.textscale, 'center')
love.graphics.setColor(1, 1, 1, 1)
love.graphics.pop()
end,
onDrawText = function(t)
love.graphics.push()
local width, lines = t.font:getWrap(t.text, t.width)
local theight = t.font:getHeight() * #lines
local scaleFactor = t.fontSize/24
local outlineOffset = math.max(math.floor(scaleFactor)*2, 1)
love.graphics.translate(t.x, t.y + (t.height - theight * t.textscale) / 2)
love.graphics.scale(t.textscale, t.textscale)
-- main
love.graphics.setColor(t.textcolor[1], t.textcolor[2], t.textcolor[3], t.textcolor[4])
love.graphics.printf(t.text, t.font, 0, 0, t.width / t.textscale, 'center')
love.graphics.pop()
end,
onDrawPicker = function(p)
local slideradd = p.sliderHeight + p.sliderDistance
p:updateColors()
-- hue slider
do
love.graphics.rectangle('fill', p.x - p.outlineWidth, p.y - p.outlineWidth - slideradd/2, p.width + p.outlineWidth*2, p.sliderHeight + p.outlineWidth*2)
love.graphics.draw(p.huesliderimg, p.x, p.y - slideradd/2)
end
-- hue slider selector
do
love.graphics.setLineWidth(4)
love.graphics.setColor(0, 0, 0, 1)
love.graphics.rectangle('line', p.x - p.outlineWidth + p.hue * p.width, p.y - p.outlineWidth - slideradd/2, 8, p.sliderHeight + p.outlineWidth*2)
love.graphics.setLineWidth(2)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle('line', p.x - p.outlineWidth + p.hue * p.width, p.y - p.outlineWidth - slideradd/2, 8, p.sliderHeight + p.outlineWidth*2)
love.graphics.setLineWidth(1)
end
-- alpha slider
do
love.graphics.rectangle('fill', p.x - p.outlineWidth, p.y + p.height + p.outlineWidth*2 + p.sliderDistance + p.sliderHeight/2, p.width + p.outlineWidth*2, p.sliderHeight + p.outlineWidth*2)
love.graphics.draw(p.alphasliderimg, p.x, p.y + p.height + p.outlineWidth*3 + p.sliderDistance + p.sliderHeight/2)
end
-- alpha slider selector
do
love.graphics.setLineWidth(4)
love.graphics.setColor(0, 0, 0, 1)
love.graphics.rectangle('line', p.x - p.outlineWidth + p.alpha * p.width, p.y + p.height + p.outlineWidth*2 + p.sliderDistance + p.sliderHeight/2, 8, p.sliderHeight + p.outlineWidth*2)
love.graphics.setLineWidth(2)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle('line', p.x - p.outlineWidth + p.alpha * p.width, p.y + p.height + p.outlineWidth*2 + p.sliderDistance + p.sliderHeight/2, 8, p.sliderHeight + p.outlineWidth*2)
love.graphics.setLineWidth(1)
end
-- picker
do
love.graphics.rectangle('fill', p.x - p.outlineWidth, p.y - p.outlineWidth + slideradd/2, p.width + p.outlineWidth*2, p.height + p.outlineWidth*2)
love.graphics.draw(p.img, p.x, p.y + slideradd/2)
end
do
love.graphics.setLineWidth(4)
love.graphics.setColor(0, 0, 0, 1)
love.graphics.circle('line', p.x + p.selectedX * p.width, p.y + slideradd/2 + p.selectedY * p.height, 6)
love.graphics.setLineWidth(2)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.circle('line', p.x + p.selectedX * p.width, p.y + slideradd/2 + p.selectedY * p.height, 6)
love.graphics.setLineWidth(1)
end
end,
onDrawTextInput = function(i)
local used = i.text
love.graphics.setColor(1, 1, 1, 1)
if inputting == i then
love.graphics.setColor(1, 1, 0, 1)
used = used .. '|'
end
love.graphics.rectangle('fill', i.x - 4, i.y - 4, i.width + 8, i.height + 8)
love.graphics.setColor(0.5, 0.5, 0.5, 1)
love.graphics.rectangle('fill', i.x, i.y, i.width, i.height)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf(used, rdfont, i.x - i.width/2, i.y + i.height/2 - rdfont:getHeight(), i.width, 'center', 0, 2)
end,
onDrawTick = function(t)
local width = t.font:getWidth(t.text)
local height = t.font:getHeight() * math.ceil(width / t.width)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle('fill', t.x + t.side*0.5, t.y + t.side, t.side, t.side, 5, 5, 10)
if t.clicked then
love.graphics.setColor(0, 0, 0, 1)
love.graphics.printf('X', t.font, t.x, t.y + t.side, t.side*2, 'center')
end
local origh = t.height
t.x = t.x + 20
t.y = t.y + t.textoffsety
t.width = t.width - 20
t.height = 0
default.onDrawText(t)
t.x = t.x - 20
t.y = t.y - t.textoffsety
t.width = t.width + 20
t.height = origh
end
}
objects = {}
-- https://stackoverflow.com/questions/1426954/split-string-in-lua
function mysplit (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function clamp(v,minv,maxv) return math.max(math.min(v, maxv), minv) end
function lerp(a,b,t) return a * (1-t) + b * t end
-- https://love2d.org/wiki/HSV_color
function HSV(h, s, v)
if s <= 0 then return v,v,v end
h = h*6
local c = v*s
local x = (1-math.abs((h%2)-1))*c
local m,r,g,b = (v-c), 0, 0, 0
if h < 1 then
r, g, b = c, x, 0
elseif h < 2 then
r, g, b = x, c, 0
elseif h < 3 then
r, g, b = 0, c, x
elseif h < 4 then
r, g, b = 0, x, c
elseif h < 5 then
r, g, b = x, 0, c
else
r, g, b = c, 0, x
end
return r+m, g+m, b+m
end
-- https://github.com/EmmanuelOga/columns/blob/master/utils/color.lua
function rgbToHsv(r, g, b, a)
r, g, b, a = r / 255, g / 255, b / 255, a / 255
local max, min = math.max(r, g, b), math.min(r, g, b)
local h, s, v
v = max
local d = max - min
if max == 0 then s = 0 else s = d / max end
if max == min then
h = 0 -- achromatic
else
if max == r then
h = (g - b) / d
if g < b then h = h + 6 end
elseif max == g then h = (b - r) / d + 2
elseif max == b then h = (r - g) / d + 4
end
h = h / 6
end
return h, s, v, a
end
-- x, y are a point that's checked if it's inside of the area defined by x1, x2, y1, y2
function touching(x, y, x1, y1, x2, y2)
return (x > x1) and (x < x2) and (y > y1) and (y < y2)
end
function mousetouching(o)
return (o.touchTest or default.touchTest)(love.mouse.getX() + camera.x, love.mouse.getY() + camera.y, o)
end
-- functions
function newobject(args)
local o = {}
o.allowHold = false
o.touchTest = args.touchTest
o.font = args.font or rdfont
o.text = args.text or ''
o.textscale = args.textscale or 1
o.textcolor = args.textcolor or {1,1,1,1}
o.outlinecolor = args.outlinecolor or {0,0,0,0}
o.fontSize = 24
o.x = args.x or 0
o.y = args.y or 0
o.width = args.width or 0
o.height = args.height or 0
if args.centered then
o.x = o.x - o.width / 2
o.y = o.y - o.height / 2
end
objects[#objects+1] = o
for k,v in pairs(args) do
o[k] = o[k] or args[k]
end
return o
end
function newbutton(args)
args.textcolor = args.textcolor or {0,0,0}
local b = newobject(args)
b.onClick = args.onClick or default.onClickButton
b.onDraw = args.onDraw or default.onDrawButton
b.activeFillColor = args.activeFillColor or nil
b.inactiveFillColor = args.inactiveFillColor or nil
return b
end
function newtext(args)
local t = newobject(args)
t.onDraw = args.onDraw or default.onDrawText
return t
end
function newtextinput(args)
local i = newobject(args)
i.text = args.text or '#000000'
i.onDraw = args.onDraw or default.onDrawTextInput
i.onUnselect = args.onUnselect or default.onUnselectTick
i.onInput = args.onInput or function(i, text)
if text == '_backspace' then
if #i.text > 1 then
i.text = i.text:sub(1,-2)
end
elseif #i.text < 7 then
text = text:lower()
local allowed = {
['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true, ['5'] = true, ['6'] = true, ['7'] = true, ['8'] = true, ['9'] = true,
['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true, ['f'] = true
}
if not allowed[text] then return end
i.text = i.text .. text
if #i.text == 7 then
local hex = i.text:gsub('#', '')
local r, g, b = tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6)) -- https://gist.github.com/jasonbradley/4357406
local h, s, v = rgbToHsv(r,g,b,1)
if i == textcolorinput then
textcolorpicker.hue = h
textcolorpicker.selectedX = s
textcolorpicker.selectedY = 1-v
textcolorpicker:updateColors()
updateTextColor()
else
outlinecolorpicker.hue = h
outlinecolorpicker.selectedX = s
outlinecolorpicker.selectedY = 1-v
outlinecolorpicker:updateColors()
updateTextColor()
end
end
end
end
return i
end
function newcolorpicker(args)
local p = newobject(args)
p.onDraw = args.onDraw or default.onDrawPicker
p.onClick = args.onClick or default.onClickPicker
p.hue = args.hue or 0
p.alpha = args.alpha or 1
p.selectedX = args.selectedX or 0
p.selectedY = args.selectedY or 0
p.imgdata = love.image.newImageData(p.width, p.height)
p.img = love.graphics.newImage(p.imgdata)
p.sliderHeight = 25
p.sliderDistance = 25
p.huesliderimgdata = love.image.newImageData(p.width, p.sliderHeight)
p.huesliderimg = love.graphics.newImage(p.huesliderimgdata)
p.alphasliderimgdata = love.image.newImageData(p.width, p.sliderHeight)
p.alphasliderimg = love.graphics.newImage(p.alphasliderimgdata)
p.outlineWidth = 4
p.updateColors = function(p)
-- hue slider
p.huesliderimgdata:mapPixel(function(x, y)
x = x / p.width
y = y / p.sliderHeight
local r, g, b = HSV(x, 1, 1)
return r, g, b, 1
end)
p.huesliderimg:replacePixels(p.huesliderimgdata)
-- alpha slider
p.alphasliderimgdata:mapPixel(function(x, y)
local half = p.sliderHeight*0.5
local transp = 0.75
if x%(half*2) < half then if y > half then transp = 1 end
elseif y < half then transp = 1
end
local progress = x / p.width
local r, g, b = HSV(p.hue, p.selectedX, 1-p.selectedY)
return lerp(transp, r, progress), lerp(transp, g, progress), lerp(transp, b, progress), 1
end)
p.alphasliderimg:replacePixels(p.alphasliderimgdata)
-- picker
p.imgdata:mapPixel(function(x, y)
x = x / p.width
y = y / p.height
local r, g, b = HSV(p.hue, x, 1-y)
return r, g, b, 1
end)
p.img:replacePixels(p.imgdata)
end
p.touchTest = function(mousex, mousey, p)
local slideradd = p.sliderHeight + p.sliderDistance
if touching(mousex, mousey, p.x, p.y - slideradd/2, p.x + p.width, p.y - slideradd/2 + p.sliderHeight) then -- hue slider
p.holdingpart = 'hue'
return true
elseif touching(mousex, mousey, p.x, p.y + slideradd/2, p.x + p.width, p.y + slideradd/2 + p.height) then -- picker
p.holdingpart = 'picker'
return true
elseif touching(mousex, mousey, p.x, p.y + p.height + p.outlineWidth*2 + p.sliderDistance + p.sliderHeight*0.5, p.x + p.width, p.y + p.height + p.outlineWidth*3 + p.sliderDistance + p.sliderHeight*1.5) then -- alpha slider
p.holdingpart = 'alpha'
return true
end
end
return p
end
function newtick(args)
local t = newobject(args)
t.onDraw = args.onDraw or default.onDrawTick
t.onClick = args.onClick or default.onClickTick
t.side = args.side or 20
t.clicked = args.clicked or false
t.textoffsety = args.textoffsety or 0
return t
end
function updateFontSize(size)
fontSize = size
if preview.font ~= previewrdfont then -- custom font
local res, font = pcall(love.graphics.newFont, 'font.ttf', size)
if not res then
res, font = pcall(love.graphics.newFont, 'font.otf', size)
end
if not res then
fontnotifier.text = 'Couldn\'t find font!'
fontnotifier.textcolor = {1,0,0}
previewrdfont = love.graphics.newFont('rdfont.otf', math.floor(size/24*16+0.5))
previewrdfont:setFilter('nearest', 'nearest')
preview.font = previewrdfont
else
fontnotifier.text = ''
preview.font = font
end
else
previewrdfont = love.graphics.newFont('rdfont.otf', math.floor(size/24*16+0.5))
previewrdfont:setFilter('nearest', 'nearest')
preview.font = previewrdfont
end
preview.fontSize = fontSize
end
function updateTextColor()
textColor[1], textColor[2], textColor[3] = HSV(textcolorpicker.hue, textcolorpicker.selectedX, 1-textcolorpicker.selectedY)
textColor[4] = textcolorpicker.alpha
outlineColor[1], outlineColor[2], outlineColor[3] = HSV(outlinecolorpicker.hue, outlinecolorpicker.selectedX, 1-outlinecolorpicker.selectedY)
outlineColor[4] = outlinecolorpicker.alpha
dropShadowColor[1], dropShadowColor[2], dropShadowColor[3] = HSV(shadowcolorpicker.hue, shadowcolorpicker.selectedX, 1-shadowcolorpicker.selectedY)
dropShadowColor[4] = shadowcolorpicker.alpha
end
function love.load(args)
-- window stuff
local data = love.image.newImageData("icon.png")
local success = love.window.setIcon(data)
love.window.setTitle('Text Exporter v' .. _EXPORTERVERSION)
-- making sure the save dir exists
love.filesystem.write('README.txt', 'This is where you put your lyrics!\n\nSimply plop down a \'lyrics.txt\' file in this folder and the program will automatically extract each word (characters separated by a space).\n\nWhen it\'s done, the output will go into the \'output\' folder.')
love.filesystem.createDirectory('output')
-- main screen
do
newbutton{
x = 400,
y = 300,
width = 300,
height = 50,
centered = true,
text = 'Import \'lyrics.txt\'',
textscale = 2,
onDraw = nil,
onClick = function(o)
words = {}
uniquewords = {}
local contents, size = love.filesystem.read('lyrics.txt')
if contents then
local contents_minus_escaped_spaces = contents:gsub('\\ ', escaped_space_str)
for w in contents_minus_escaped_spaces:gmatch("%S+") do
w = w:gsub(escaped_space_str, ' ')
if w:find('\\n') then
local t = {}
local lasti = 1
for i = 1, #w do
if w:sub(i, i+1) == '\\n' then
t[#t+1] = w:sub(lasti, i-1)
lasti = i + 2
end
end
t[#t+1] = w:sub(lasti, -1)
final = table.concat(t, '\n')
words[#words+1] = final
local found = false
for _,w in ipairs(uniquewords) do
if w == final then
found = true
break
end
end
if not found then
uniquewords[#uniquewords+1] = final
end
elseif w:find('/') then
local t = mysplit(w, '/')
for i=1,#t do
local final = t[i]
for j=i-1,1,-1 do
final = t[j] .. final
end
words[#words+1] = final
local found = false
for _,w in ipairs(uniquewords) do
if w == final then
found = true
break
end
end
if not found then
uniquewords[#uniquewords+1] = final
end
end
else
words[#words+1] = w
local found = false
for _,word in ipairs(uniquewords) do
if word == w then
found = true
break
end
end
if not found then
uniquewords[#uniquewords+1] = w
end
end
end
if #words < 1 then
print('Found no words!')
lyricsnotifier.textcolor = {1,0,0}
lyricsnotifier.text = 'Couldn\'t find any words!'
lyricsnotifier.y = 300
startbutton:newState(false)
else
print('Found ' .. #words .. ' words.')
lyricsnotifier.textcolor = {0,1,0}
lyricsnotifier.text = 'Successfully loaded!'
lyricsnotifier.y = 300
startbutton:newState(true)
end
else
lyricsnotifier.textcolor = {1,0,0}
lyricsnotifier.text = 'Couldn\'t load!\nAre you sure \'lyrics.txt\' is present in the working directory?'
lyricsnotifier.y = 325
startbutton:newState(false)
end
end
}
lyricsnotifier = newtext{
x = 400,
y = 350,
width = 800,
height = 100,
centered = true,
text = '',
textcolor = {0,1,0},
textscale = 2
}
newbutton{
x = 400,
y = 120,
width = 150,
height = 50,
centered = true,
texscale = 1,
text = 'Open working\ndirectory',
onDraw = nil,
onClick = function(o)
love.system.openURL("file://" .. love.filesystem.getSaveDirectory())
end
}
startbutton = newbutton{
x = 400,
y = 500,
width = 300,
height = 50,
textscale = 2,
centered = true,
text = 'Start!',
onClick = function(b)
if startbutton.accessible then
print('Starting with ' .. #words .. ' words...')
print('Removing old words...')
love.filesystem.createDirectory('output')
local files = love.filesystem.getDirectoryItems('output')
for _,f in ipairs(files) do
love.filesystem.remove('output/'..f)
end
local scaleFactor = fontSize/24
local outlineOffset = math.max(math.floor(scaleFactor)*2, 1)
local r = math.rad(dropShadowAngle)
local shadowX, shadowY = math.floor(math.cos(r) * dropShadowDist + 0.5), -math.floor(math.sin(r) * dropShadowDist + 0.5)
local offsetX, offsetY = math.max(0, -shadowX), math.max(0, -shadowY)
local xPad = math.floor(8 * scaleFactor + 0.5)
love.graphics.setColor(1, 1, 1, 1)
local function drawText(text, font, x, y)
love.graphics.printf(text, font, x + offsetX, y + offsetY, 9999)
end
local function drawTextWithOutline(text, font, x, y)
-- drop shadow
love.graphics.setColor(dropShadowColor[1], dropShadowColor[2], dropShadowColor[3], dropShadowColor[4])
drawText(text, font, x + outlineOffset + xPad*wordAlign*2 + shadowX, y + outlineOffset + shadowY)
-- outline
love.graphics.setColor(outlineColor[1], outlineColor[2], outlineColor[3], outlineColor[4])
drawText(text, font, x + outlineOffset + xPad*wordAlign*2, y)
drawText(text, font, x + xPad*wordAlign*2, y + outlineOffset)
drawText(text, font, x + outlineOffset*2 + xPad*wordAlign*2, y + outlineOffset)
drawText(text, font, x + outlineOffset + xPad*wordAlign*2, y + outlineOffset*2)
-- main
love.graphics.setColor(textColor[1], textColor[2], textColor[3], textColor[4])
drawText(text, font, x + outlineOffset + xPad*wordAlign*2, y + outlineOffset)
end
local usedt = words
if not makeSpritesheet then
for i=1,#words do
-- draw word
local word = words[i]
local _, newlinecount = word:gsub('\n', '\n') -- magic
newlinecount = newlinecount + 1
local w = preview.font:getWidth(word) + outlineOffset + xPad*2 + math.abs(shadowX)
local h = preview.font:getHeight() * newlinecount - preview.font:getDescent() + 1 + math.abs(shadowY)
local canvas = love.graphics.newCanvas(w, h)
love.graphics.setCanvas(canvas)
love.graphics.clear()
local allWords = mysplit('_' .. word, '\n')
allWords[1] = allWords[1]:sub(2, -1)
for i,thisword in ipairs(allWords) do
drawTextWithOutline(thisword, preview.font, (w - preview.font:getWidth(thisword) - xPad*2 - 2) * wordAlign, (i-1) * preview.font:getHeight())
end
love.graphics.setCanvas()
-- save word
canvas:newImageData():encode('png', 'output/lyric-' .. i .. '.png')
end
else
local wordusagecount = {}
local wordfirstframe = {}
local sheetWidth = 0
local sheetHeight = 0
-- calculate max width
local wordcount = #uniquewords
local wordwidth = 0
local wordheight = 0
for _,w in ipairs(words) do
wordwidth = math.max(wordwidth, preview.font:getWidth(w))
local _, newlinecount = w:gsub('\n', '\n') -- magic
newlinecount = newlinecount + 1
wordheight = math.max(wordheight, preview.font:getHeight() * newlinecount - preview.font:getDescent() + 1 + math.abs(shadowY))
end
wordwidth = wordwidth + outlineOffset + xPad*2 + math.abs(shadowX)
-- try to make the spritesheet as square as possible to avoid any potential issues
local sqrt = math.sqrt(wordcount)
local columns = math.ceil(sqrt)
local lines = math.ceil(wordcount / columns)
-- adding a bit so that we have space for the outline and glow; xPad was just to make sure no fonts get cut off
local wordwidthpadded = wordwidth + 16
local wordheightpadded = wordheight + 16
sheetWidth = wordwidthpadded * columns
sheetHeight = wordheightpadded * lines
-- prepare json at the same time as drawing the words
local jsonT = {
size = {wordwidthpadded, wordheightpadded},
rowPreviewFrame = 0,
rowPreviewOffset = {0, 0},
clips = {
{
name = 'neutral',
frames = {0},
loop = 'onBeat',
fps = 0,
loopStart = 0,
portraitOffset = {0, 0},
portraitSize = {25, 25},
portraitScale = 2
}
}
}
-- canvas to draw onto so we can export as a png
local canvas = love.graphics.newCanvas(sheetWidth, sheetHeight)
love.graphics.setCanvas(canvas)
love.graphics.clear()
usedt = uniquewords
if not ignoreDupes then
usedt = words
end
local x, y, frame = 1, 1, 0
for _, wordWithNewlines in ipairs(usedt) do
local firstusedframe = false
local jsonname = wordWithNewlines:gsub('%s+', '_')
wordusagecount[jsonname] = (wordusagecount[jsonname] or 0) + 1
if wordusagecount[jsonname] > 1 then
firstusedframe = wordfirstframe[jsonname]
jsonname = jsonname .. ':' .. tostring(wordusagecount[jsonname])
else
wordfirstframe[jsonname] = frame
end
if not firstusedframe then