-
Notifications
You must be signed in to change notification settings - Fork 4
/
listbox.lua
1291 lines (1037 loc) · 35.6 KB
/
listbox.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
--listbox by Luiz Renato @darkmetalic v1.0 for LÖVE2D 0.10+
-- https://github.com/darkmetalic/ListBox
--Default values
local listbox={
x=100,y=100, -- listbox position x,y (number)
w=200,h=400, -- listbox size Width x Height (number)
hor=true,ver=true, -- Show scroller Horizontal and Vertical (boolean)
sort=false, -- sort mode: string "asc" or "desc" or boolean true(asc) or false(disabled)
enabled=true, -- enable or disable the listbox, it remains visible, but does not interact (boolean)
visible=true, -- visible or invisible, the listbox may interact invisibly if it is not disabled
adjust=true, -- adjuste (boolean)
rounded=false, -- rounded rectangle (boolean)
expand=false, -- expand the listbox (boolean)
expandmode="size", -- expand mode: size true (boolean|string) or position "pos" (string)
expx=12, -- expand pixels (number)
radius=15, -- scroll ball size/radius (number)
sep=";;", -- separator that limits where the text ends and the data starts in the index (string)
ismouse=true, -- if you will use mouse (boolean)
istouch=false, -- if you will use touches (boolean)
gw = love.graphics.getWidth(), -- first Width captured to resize, do not works if you confugured(conf.lua) in fullscreen (or set gw)
gh = love.graphics.getHeight(), -- first Height captured to resize, do not works if you confugured(conf.lua) in fullscreen (or set gh)
incenter=false, -- in center of screen (boolean)
asize=false, -- auto size the listbox (boolean)
font = love.graphics.newFont(12), -- listbox font (object)
fcolor={0,190,0}, -- font color RGB (table)
bordercolor={50,50,50}, -- border color RGB (table)
selectedcolor={50,50,50}, -- selected color RGB (table)
fselectedcolor={200,200,200}, -- font selected color RGB (table)
selectedtip="fill", -- selected mode "fill" or "line" (string)
bgcolor={20,20,20}, -- background selected color RGB (table)
showindex=false, -- show index (boolean)
indexzeros=1, -- index left zeros (number)
selected=false, -- first index selected (number) or (false) none
gpadding=5 -- global padding (number)
}
function listbox:newprop(tlist)
for new, value in pairs(tlist) do self[new]=value end
listbox.ispress=false
listbox.over=false
listbox.rsize = self.rounded and 10 or 0
listbox.defw=self.w
listbox.defh=self.h
listbox.defx=self.x
listbox.defy=self.y
listbox.time=0
listbox.ptime=0
listbox.dtime=0
listbox.isdouble=false
listbox.count=0
listbox.tmpselected=0
listbox.paddingsc=10
listbox.scrypos=self.y
listbox.scrvw=self.radius+listbox.paddingsc
listbox.scrvx=self.x+self.w
listbox.scrvh=self.radius+listbox.paddingsc
listbox.scrvy=self.y+self.scrvh
listbox.scrhpos=self.w
listbox.scrhw=self.radius+listbox.paddingsc
listbox.scrhx=self.x
listbox.scrhh=self.radius+listbox.paddingsc
listbox.scrhy=self.y+self.h
listbox.min=1 listbox.max=0
listbox.fwmin=1 listbox.fwmax=0
listbox.fwmaxshow=0
listbox.maxsizew=0
listbox.paddingw=self.gpadding
listbox.paddingh=self.gpadding
listbox.tlist={}
listbox.str={}
listbox.fw=listbox.font:getWidth("o")
listbox.fh=listbox.font:getHeight("A")
listbox.imin=self.y+listbox.paddingh
listbox.imax=self.y+listbox.paddingh+listbox.fh
local i,aux=0,0
while aux<listbox.h-listbox.fh do
aux=aux+listbox.fh
i=i+1
end
listbox.max = i>2 and i or 2
listbox.paddingh=(listbox.h-aux)/i+listbox.paddingh
end
listbox.__index = listbox
function listbox:getcount()
return #self.str>0 and #self.str or false
end
listbox.maxn = listbox.getcount
function listbox:getselected()
return type(self.selected)=="number" and self.selected or false
end
function listbox:getdata(i)
if not self.str[i] then return "" end
return self.str[i]:sub(self.str[i]:find(listbox.sep)+listbox.sep:len(),self.str[i]:len())
end
function listbox:gettext(i)
if not self.str[i] then return "" end
for str in (self.str[i]..self.sep):gmatch("([^"..self.sep.."]*)"..self.sep) do
return str
end
end
function listbox:additem(str,data,update)
data = (type(data)=="string" or type(data)=="number") and listbox.sep..data or listbox.sep
str = (type(str)=="string" or type(str)=="number") and str or nil
if not str then return false end
table.insert(self.str,str..data)
i = #self.str
self.fwmax = self.fwmax<self.font:getWidth(str) and str:len() or self.fwmax
self.maxsizew = self.maxsizew<self.font:getWidth(str) and self.font:getWidth(str) or self.maxsizew
local tmpw = self.font:getWidth(str)
while tmpw>self.w-self.paddingw do
str = str:sub(1,str:len()-1)
tmpw = self.font:getWidth(str)
self.fwmaxshow = str:len()-1
end
listbox:resort(self.sort)
if update then listbox:uplist() end
return true
end
listbox.insert = listbox.additem
function listbox:calc(y)
self.scrypos=y
local pos=y-self.y
local c = #self.str>100 and math.ceil(pos*(#self.str)/self.h) or math.floor(pos*(#self.str)/self.h)
if c>(self.max-1)*#self.str/100 and pos<self.h-5 then
self.min = (c<#self.str-self.max+1 and pos+1<self.h) and c or (#self.str-self.max)+1
for i=1, self.max do local id=self.min+i-1 self.tlist[i]=listbox:gettext(id) end
elseif pos>=self.h-5 then
self.min = #self.str-self.max+1
for i=1, self.max do local id=self.min+i-1 self.tlist[i]=listbox:gettext(id) end
else
self.min=1
for i=1, self.max do local id=self.min+i-1 self.tlist[i]=listbox:gettext(id) end
end
self.fwmin=1
end
function listbox:setselected(i)
if i<1 or i>#self.str or not i then return false end
self.selected=i
listbox:calc2(self.selected)
return true
end
function listbox:unselect()
self.selected=nil
end
listbox.deselect = listbox.unselect
function listbox:setrounded(bool)
self.rounded=bool
listbox.rsize = self.rounded and 10 or 0
end
function listbox:findlist(str,mode,sensible)
str = (type(str)=="string" or type(str)=="number") and str or nil
if not str then return false end
local results={}
if mode=="text" then
for i, result in pairs(self.str) do
if listbox:gettext(i):find(listbox:insensible(str)) and not sensible then
table.insert(results,i)
end
if listbox:gettext(i):find(str) and sensible then
table.insert(results,i)
end
end
elseif mode=="data" then
for i, result in pairs(self.str) do
if listbox:getdata(i):find(listbox:insensible(str)) and not sensible then
table.insert(results,i)
end
if listbox:getdata(i):find(str) and sensible then
table.insert(results,i)
end
end
elseif mode=="textdata" then
for i, result in pairs(self.str) do
if result:find(listbox:insensible(str)) and not sensible then
table.insert(results,i)
end
if result:find(str) and sensible then
table.insert(results,i)
end
end
else
for i, result in pairs(self.str) do
if result:find(listbox:insensible(str)) then
table.insert(results,i)
end
end
end
return #results>0 and results or false
end
listbox.search = listbox.findlist
function listbox:resort(sort,update)
self.sort = sort and (type(sort)~="string" and "asc" or sort) or false
if self.sort and self.sort=="asc" then
table.sort(self.str, function(a,b) return a<b end)
elseif self.sort and self.sort=="desc" then
table.sort(self.str, function(a,b) return a>b end)
end
if update then listbox:uplist() end
end
function listbox:setexpand(mode)
mode = (mode or type(mode)=="string") and (type(mode)=="string" and mode or "size") or false
if mode then
self.expmode=mode
self.expand=true
return true
else
self.expand=false
return false
end
end
function listbox:addexpand(x,y)
if self.expmode=="pos" then
self.incenter=false
if self.x>1 and self.y>1 then
self.x,self.y=x-self.expx/2-self.w,y-self.expx/2-self.h
self.defx,self.defy=self.x,self.y
else
self.x,self.y=self.x+10,self.y+10
self.defx,self.defy=self.w,self.h
listbox.isexpanding=false
self.ispress=false
end
else
self.expmode="size"
local min
if self.showindex then
local tmpwi = self.font:getWidth(#self.str.." ")
min = tmpwi+self.font:getWidth("W")+self.paddingw*3
else
min = self.font:getWidth("W")+self.paddingw*3
end
self.asize=false
if self.w>min and self.h>min then
self.w,self.h=x-self.expx/2-self.x,y-self.expx/2-self.y
self.defw,self.defh=self.w,self.h
love.graphics.rectangle("fill", self.x+self.w, self.y+self.h,self.expx,self.expx,self.rsize,self.rsize)
else
self.w,self.h=self.w+10,self.h+10
self.defw,self.defh=self.w,self.h
listbox.isexpanding=false
self.ispress=false
end
end
self.scrypos=self.y
self.scrvx=self.x+self.w
self.scrvy=self.y+self.scrvh
self.scrhpos=self.x
self.scrhy=self.y+self.h
if self.expmode=="size" then
self.min=1 self.max=0
self.fwmin=1
self.tlist={}
local im,aux=0,0
while aux<self.h-self.fh do
aux=aux+self.fh
im=im+1
end
self.max = im>2 and im or 2
if (#self.tlist<self.max) then
for i=1, self.max do self.tlist[i]=listbox:gettext(i) end
end end
end
function listbox:setsize(w,h)
self.w = type(w)=="number" and w or self.w
self.h = type(h)=="number" and h or self.h
self.scrypos=self.y
self.scrvw=self.radius+self.paddingsc
self.scrvx=self.x+self.w
self.scrvh=self.radius+self.paddingsc
self.scrvy=self.y+self.scrvh
self.scrhpos=self.x
self.scrhw=self.radius+self.paddingsc
self.scrhx=self.x
self.scrhh=self.radius+self.paddingsc
self.scrhy=self.y+self.h
self.min=1 self.max=0
self.fwmin=1 self.fwmax=2
self.fwmaxshow = 0
self.maxsizew = 0
self.fw=self.font:getWidth("o")
self.fh=self.font:getHeight("A")
self.imin=self.y+self.paddingh
self.imax=self.y+self.paddingh+self.fh
for i, str in ipairs(self.str) do
self.fwmax = self.fwmax<self.font:getWidth(str) and str:len() or self.fwmax
self.maxsizew = self.maxsizew<self.font:getWidth(str) and self.font:getWidth(str) or self.maxsizew
local tmpw = self.font:getWidth(str)
while tmpw>self.w-self.paddingw do
str = str:sub(1,str:len()-1)
tmpw = self.font:getWidth(str)
self.fwmaxshow = str:len()-1
end
end
self.tlist={}
local im,aux=0,0
while aux<self.h-self.fh do
aux=aux+self.fh
im=im+1
end
self.max = im>2 and im or 2
if (#self.tlist<self.max) then
for i=1, self.max do self.tlist[i]=listbox:gettext(i) end
end
self.paddingh=(self.h-aux)/im+self.gpadding
end
function listbox:setpos(x,y)
self.x = type(x)=="number" and x or self.x
self.y = type(y)=="number" and y or self.y
self.scrypos=self.y
self.scrvx=self.x+self.w
self.scrhpos=self.x
self.scrhy=self.y+self.h
end
function listbox:setfont(font)
self.font = font
listbox:uplist()
end
function listbox:resize()
local min
if self.showindex then
local tmpwi = self.font:getWidth(#self.str.." ")
min = tmpwi+self.font:getWidth("W")+self.paddingw*3
else
min = self.font:getWidth("W")+self.paddingw*3
end
self.asize=false
local w,h = love.graphics.getDimensions()
local sizew,sizeh=w/self.gw,h/self.gh
if self.w>min and self.h>min then
self.w,self.h=self.defw*sizew,self.defh*sizeh
self.x,self.y=self.defx*sizew,self.defy*sizeh
else
self.w,self.h=self.w+10,self.h+10
self.x,self.y=self.x+10,self.y+10
end
self.scrypos=self.y
self.scrvw=self.radius+self.paddingsc
self.scrvx=self.x+self.w
self.scrvh=self.radius+self.paddingsc
self.scrvy=self.y+self.scrvh
self.scrhpos=self.x
self.scrhw=self.radius+self.paddingsc
self.scrhx=self.x
self.scrhh=self.radius+self.paddingsc
self.scrhy=self.y+self.h
self.min=1 self.max=0
self.fwmin=1 self.fwmax=2
self.fwmaxshow = 0
self.maxsizew = 0
self.fw=self.font:getWidth("o")
self.fh=self.font:getHeight("A")
self.imin=self.y+self.paddingh
self.imax=self.y+self.paddingh+self.fh
for i, str in ipairs(self.str) do
self.fwmax = self.fwmax<self.font:getWidth(str) and str:len() or self.fwmax
self.maxsizew = self.maxsizew<self.font:getWidth(str) and self.font:getWidth(str) or self.maxsizew
local tmpw = self.font:getWidth(str)
while tmpw>self.w-self.paddingw do
str = str:sub(1,str:len()-1)
tmpw = self.font:getWidth(str)
self.fwmaxshow = str:len()-1
end
end
self.tlist={}
local im,aux=0,0
while aux<self.h-self.fh do
aux=aux+self.fh
im=im+1
end
self.max = im>2 and im or 2
if (#self.tlist<self.max) then
for i=1, self.max do self.tlist[i]=listbox:gettext(i) end
end
end
function listbox:getsize()
return self.w,self.h
end
function listbox:getpos()
return self.x,self.y
end
function listbox:delitem(i)
if not i or (i and i<1 or i>#self.str) then return false end
if #self.str<=1 then listbox:delall() return true end
table.remove(self.str,i)
listbox:uplist()
if i>self.max*2 then
if i<#self.str then listbox:setselected(1) else listbox:setselected(1) listbox:setselected(#self.str) end
else
self.min= i>self.max and self.min+i-1 or 1
listbox:setselected(1)
end
return true
end
listbox.remove = listbox.delitem
function listbox:uplist()
listbox:resort(self.sort)
for i, str in ipairs(self.str) do
self.maxsizew = 0
self.fwmax = self.fwmax<self.font:getWidth(str) and str:len() or self.fwmax
self.maxsizew = self.maxsizew<self.font:getWidth(str) and self.font:getWidth(str) or self.maxsizew
local tmpw = self.font:getWidth(str)
while tmpw>self.w-self.paddingw do
str = str:sub(1,str:len()-1)
tmpw = self.font:getWidth(str)
self.fwmaxshow = str:len()-1
end
end
self.tlist={}
local im,aux=0,0
while aux<self.h-self.fh do
aux=aux+self.fh
im=im+1
end
self.max = im>2 and im or 2
if (#self.tlist<self.max) then
for i=1, self.max do self.tlist[i]=listbox:gettext(i) end
end
end
function listbox:settext(i,str)
if i<1 or i>#self.str or not i then return false end
str = (type(str)=="string" or type(str)=="number") and str or nil
if not str then return false end
local data = self.sep..listbox:getdata(i)
self.str[i]=str..data
listbox:uplist()
return true
end
function listbox:setdata(i,data)
if i<1 or i>#self.str or not i then return false end
data = (type(data)=="string" or type(data)=="number") and listbox.sep..data or false
if not data then return false end
self.str[i]=listbox:gettext(i)..data
return true
end
function listbox:setitem(i,str,data)
if i<1 or i>#self.str or not i then return false end
data = (type(data)=="string" or type(data)=="number") and listbox.sep..data or listbox.sep
str = (type(str)=="string" or type(str)=="number") and str or nil
if not str then return false end
self.str[i]=str..data
listbox:uplist()
return true
end
function listbox:getfusion(i)
return self.str[i]:gsub(self.sep,"")
end
listbox.concat = listbox.getfusion
function listbox:delall()
self.str={}
self.tlist={}
self.selected=nil
self.min=1 self.max=2
self.fwmin=1 self.fwmax=2
self.fwmaxshow=0
self.maxsizew = 0
end
listbox.empty = listbox.delall
function listbox:setvisible(bool)
self.visible=bool
end
function listbox:setenabled(bool)
self.enabled=bool
end
function listbox:isvisible()
return self.visible
end
function listbox:isenabled()
return self.enabled
end
function listbox:getfileext(fpath)
return fpath:match("^.+(%..+)$")
end
function listbox:getfilename(fpath)
fpath=fpath:sub(1,fpath:len()-4)
return fpath:match("^.+/(.+)$")
end
function listbox:getdirname(fpath)
return fpath:match("^.+/(.+)$")
end
function listbox:onlynumbers(str)
local tb={}
for n in str:gmatch("%d+") do
table.insert(tb,n)
end
return #tb>0 and table.concat(tb) or ""
end
function listbox:onlyletters(str)
local tb={}
for l in str:gmatch("%a+") do
table.insert(tb,l)
end
return #tb>0 and table.concat(tb) or ""
end
function listbox:enudir(fpath,exts)
local itens,ext,files={},{},{}
for fext in (exts.." "):gmatch("([^%s]*)%s") do table.insert(ext,fext) end
itens=love.filesystem.getDirectoryItems(fpath)
for i, f in ipairs(itens) do
if #ext>0 and love.filesystem.isFile(fpath.."/"..f) then
for j, e in pairs(ext) do
if listbox:getfileext(f)==e then table.insert(files,f) end
end
end
if not exts then table.insert(files,f) end
end
return #files>0 and files or false
end
function listbox:sectotime(sec,milliseconds)
local h,m,s=0,0,0
h=math.floor(sec/3600)%60
m=math.floor(sec/60)%60
s=sec%60
if milliseconds then
return sec and string.format("%02i:%02i:%.1f",h,m,s) or "00:00:00.0"
else
return sec and string.format("%02i:%02i:%02i",h,m,s) or "00:00:00"
end
end
function listbox:rgbtohex(tcolor)
local hex={}
for i, color in ipairs(tcolor) do
color=string.format("%X", color*256)
hex[i]=string.format("%02s",string.sub(color,1,2))
end
return table.concat(hex)
end
function listbox:autosize(bool)
self.asize = (bool or self.asize) and true or false
local tmpi = self.showindex and string.format("%0"..self.indexzeros.."i",#self.str).." " or ""
local tmpwi = self.font:getWidth(tmpi)
if self.asize then
while self.fh*#self.str+self.paddingh<self.h-(self.paddingh+1) do
self.h=self.h-1
end
if self.maxsizew+tmpwi+self.paddingw*2>self.w then
while (self.maxsizew)+tmpwi+self.paddingw*2>self.w-(self.paddingw+1) do
self.w=self.w+1
end
end
self.defw,self.defh=self.w,self.h
self.scrypos=self.y
self.scrvx=self.x+self.w
self.scrvy=self.y+self.scrvh
self.scrhpos=self.x
self.scrhy=self.y+self.h
self.min=1 self.max=0
self.fwmin=1 self.fwmax=2
self.fwmaxshow = 0
self.maxsizew = 0
for i, str in ipairs(self.str) do
self.fwmax = self.fwmax<self.font:getWidth(str) and str:len() or self.fwmax
self.maxsizew = self.maxsizew<self.font:getWidth(str) and self.font:getWidth(str) or self.maxsizew
local tmpw = self.font:getWidth(str)
while tmpw>self.w-self.paddingw do
str = str:sub(1,str:len()-1)
tmpw = self.font:getWidth(str)
self.fwmaxshow = str:len()-1
end
end
self.tlist={}
local im,aux=0,0
while aux<self.h-self.fh do
aux=aux+self.fh
im=im+1
end
self.max = im>2 and im or 2
if (#self.tlist<self.max) then
for i=1, self.max do self.tlist[i]=listbox:gettext(i) end
end
end
self.asize=false
end
function listbox:setindex(bool)
self.showindex = (bool or nil) and true or false
if self.showindex then listbox:uplist() end
end
function listbox:center(bool)
self.incenter = (bool or self.incenter) and true or false
if self.incenter then
local width, height = love.graphics.getDimensions()
local newx,newy = width/2-self.w/2,height/2-self.h/2
if self.x~=newx and self.y~=newy then
listbox:setpos(newx,newy)
end end end
function listbox:isover()
return self.over
end
function listbox:export(path,f,mode)
mode = (mode==true or (mode and mode=="text")) and true or false
f = (f==true or (f and f=="savedir")) and true or false
local index,fpath="",path
local name = love.filesystem.getIdentity()
if type(name)~="string" or name=="" then
name="save"
love.filesystem.setIdentity(name)
end
if not love.filesystem.exists(name) then
love.filesystem.createDirectory(name)
end
osString = love.system.getOS()
f = (osString=="Android" or osString=="iOS") and true or f
f = f==true and love.filesystem.newFile(path,"w") or io.open(path, "w")
for i=1,#self.str do
index = self.showindex and string.format("%0"..self.indexzeros.."i",(self.min+i-1)).." " or ""
if i<#self.str then
if mode then
f:write(index..listbox:gettext(i).."\n")
else
f:write(index..self.str[i].."\n")
end
else
if mode then
f:write(index..listbox:gettext(i))
else
f:write(index..self.str[i])
end
end
end
f:close()
if not love.filesystem.exists(path) then
path=listbox:mount(path).."/"..path
end
if not love.filesystem.exists(path) then
path=love.filesystem.getWorkingDirectory().."/"..fpath
end
if not love.filesystem.exists(path) then
path=love.filesystem.getSaveDirectory().."/"..fpath
end
if not love.filesystem.exists(path) then
path=love.filesystem.getSourceBaseDirectory().."/"..fpath
end
return love.filesystem.exists(path) and true or false
end
listbox.save = listbox.export
function listbox:mount(path)
path=listbox:getdirname(love.filesystem.getSourceBaseDirectory())
if love.filesystem.isFused() then
local dir = love.filesystem.getSourceBaseDirectory()
local success = love.filesystem.mount(dir, path)
end
return path
end
function listbox:import(path)
local fpath=path
if not love.filesystem.exists(path) then
path=listbox:mount(path).."/"..path
end
if not love.filesystem.exists(path) then
path=love.filesystem.getWorkingDirectory().."/"..fpath
end
if not love.filesystem.exists(path) then
path=love.filesystem.getSaveDirectory().."/"..fpath
end
if not love.filesystem.exists(path) then
path=love.filesystem.getSourceBaseDirectory().."/"..fpath
end
if not love.filesystem.exists(path) then return false end
if #self.str>0 then listbox:delall() end
local count=1
for line in love.filesystem.lines(path) do
if line~="" then
if line:find(self.sep) then
self.str[count]=line
str = listbox:gettext(count)
self.fwmax = self.fwmax<self.font:getWidth(str) and str:len() or self.fwmax
local tmpw = self.font:getWidth(str)
while tmpw>self.w-self.paddingw do
str = str:sub(1,str:len()-1)
tmpw = self.font:getWidth(str)
self.fwmaxshow = str:len()-1
end
listbox:resort(self.sort)
else
listbox:additem(line)
end
count=count+1
end
end
listbox:uplist()
return #self.str>0 and true or false
end
listbox.open = listbox.import
function listbox:insensible(pattern)
local p = pattern:gsub("(%%?)(.)", function(percent, letter)
if percent ~= "" or not letter:match("%a") then
return percent .. letter
else
return string.format("[%s%s]", letter:lower(), letter:upper())
end
end)
return p
end
function listbox:calc2(i)
self.fwmin=1
local c = i
if math.abs(self.min-c)==self.max then
if c<#self.str then
self.min=self.min+1
else
self.min=#self.str-self.max+1
self.selected=#self.str
end
for i=1, self.max do local id=self.min+i-1 self.tlist[i]=listbox:gettext(id) end
end
if (math.abs(self.min-c)>self.max+1) then
self.min = c<#self.str-self.max+1 and c or #self.str-self.max+1
for i=1, self.max do local id=self.min+i-1 self.tlist[i]=listbox:gettext(id) end
end
if math.abs(self.min-c-2)==1 then
self.min=self.min-1
for i=1, self.max do local id=self.min+i-1 self.tlist[i]=listbox:gettext(id) end
end
self.scrypos = self.y+math.floor(self.h*c/#self.str)
if c>=#self.str then
self.scrypos = self.h+self.y
end
if c<=1 then
self.scrypos = self.y
end
end
function listbox:isdoublec()
return self.isdouble
end
function listbox:key(key,shortcut)
if not self.selected or not self.enabled then return end
love.keyboard.setKeyRepeat(true)
if key=="down" then
self.selected = self.selected<#self.str and self.selected+1 or #self.str
elseif key=="up" then
self.selected = self.selected>1 and self.selected-1 or 1
end
if key:len()==1 and shortcut then
for i, first in ipairs(self.str) do
first=first:sub(1,1)
if key:lower()==first:lower() then self.selected=i break end
end
end
listbox:calc2(self.selected)
return key
end
function listbox:mousew(x,y)
if not self.selected or not self.enabled then return end
if y>0 then
self.selected = self.selected>1 and self.selected-1 or 1
elseif y<0 then
self.selected = self.selected<#self.str and self.selected+1 or #self.str
end
listbox:calc2(self.selected)
end
function listbox:drawtext(i,x,y)
if (i>#self.str) or (self.min+i-1<1) or self.tlist[i]=="" then return end
local deffont=love.graphics.getFont()
local tmpi = self.showindex and string.format("%0"..self.indexzeros.."i",(self.min+i-1)).." " or ""
local tmpf = self.tlist[i]
local tmpt = tmpf
local tmpw = self.font:getWidth(tmpf)
local tmpwi = self.font:getWidth(tmpi)
local index = self.y+self.fh*i-self.fh+self.paddingh
while tmpw+tmpwi+self.paddingw+self.fw>=self.w-(self.paddingw+1) do
tmpt = tmpt:sub(1,tmpt:len()-1)
tmpw = self.font:getWidth(tmpt)
self.fwmaxshow=tmpt:len()
end
if not listbox.isexpanding and self.adjust then
while self.fh*i+self.paddingh>self.h-self.paddingh do
self.h=self.h+1
self.scrhy=self.y+self.h
end
end
self.fwmaxshow = self.showindex and math.abs(self.fwmaxshow+self.fwmin-1) or math.abs(self.fwmaxshow+self.fwmin-1)
tmpf = self.font:getWidth(tmpf)>(self.w-tmpwi)-self.paddingw and (self.fwmin<2 and tmpf:sub(self.fwmin,self.fwmaxshow+1) or tmpf:sub(self.fwmin,self.fwmaxshow) ) or tmpf
if (x>self.x and x<self.x+(self.w-self.radius*1.1)) and (y>index and y<index+self.fh) and self.ispress and self.enabled and not listbox.ish and self.time>self.ptime then
self.ptime=self.time+0.2
self.dtime = self.time>self.dtime and self.time+0.5 or self.dtime
self.count = self.time<self.dtime and self.count+1 or 0
self.selected=self.min+i-1
self.ispress=false
end
if self.count>1 then
self.isdouble = true
end
if self.time>self.dtime then
self.isdouble=false
self.count = 0
self.dtime = 0
end
if self.selected==self.min+i-1 then
self.imin=index
love.graphics.setColor(self.selectedcolor)
love.graphics.rectangle(self.selectedtip, self.x, self.imin, self.w, self.fh)
love.graphics.setColor(self.fselectedcolor)
else
love.graphics.setColor(self.fcolor)
end
love.graphics.setFont(self.font)
love.graphics.print(tmpi..tmpf, self.x+self.paddingw, index)
love.graphics.setFont(deffont)
end
function listbox:update(dt)
self.time=self.time+dt
if self.ismouse then
if love.mouse.isDown(1) then
self.ispress=true
else
self.ispress=false
end
end
self.selectchange = self.tmpselected~=self.selected and true or false
self.tmpselected = self.tmpselected~=self.selected and self.selected or self.tmpselected
end
function listbox:isselectchange()
return self.selectchange
end
function listbox:press(x,y)
self.ispress=true
end
function listbox:released(x,y)
self.ispress=false
end
function listbox:moved(x,y)
self.ispress=true
end
function listbox:draw()
local x,y=0,0
if self.ismouse then
x,y=love.mouse.getPosition()
end
if self.istouch then
for i, id in ipairs(love.touch.getTouches()) do
x,y=love.touch.getPosition(id)
end
end
if (x>self.x and x<self.x+self.w) and (y>self.y and y<self.y+self.h) then
self.over=true
else
self.over=false
end
if not self.visible then return end
if (self.scrypos+self.radius<self.y+self.h) and not listbox.isexpanding and not self.isscrlling and self.expand and x>=self.x+self.w and x<=self.x+self.w+self.expx+1 and y>=self.y+self.h and y<=self.y+self.h+self.expx+1 and self.ispress then
print(self.scrypos)
self.asize=false
listbox.isexpanding = true
end