forked from 26F-Studio/Zenitha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.lua
2406 lines (2180 loc) · 68.2 KB
/
widget.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
---@class Zenitha.WidgetArg: table
---
---@field type 'text'|'image'|'button'|'button_fill'|'button_invis'|'checkBox'|'switch'|'slider'|'slider_fill'|'slider_progress'|'selector'|'inputBox'|'textBox'|'listBox'|string
---@field name? string
---@field pos? table
---
---@field x? number
---@field y? number
---@field w? number
---@field h? number
---@field widthLimit? number
---
---@field color? Zenitha.ColorStr|Zenitha.Color
---@field text? string|function
---@field fontSize? number
---@field fontType? string
---@field image? string|love.Drawable Can use slash-path to read from IMG lib
---@field alignX? 'left'|'right'|'center'
---@field alignY? 'top'|'bottom'|'center'
---@field labelPos? 'left'|'right'|'top'|'bottom' Some widget (like Slider) didn't use 'top'
---@field labelDistance? number
---@field disp? function Must return the value that widget should show
---@field code? function Called 'When triggered'
---@field visibleFunc? function Used to determine if widget is visible when scene changed
---@field visibleTick? function Used to change widget's visibility every frame
---
---@field lineWidth? number
---@field cornerR? number Round corner ratio
---
---@field textColor? Zenitha.ColorStr|Zenitha.Color
---@field fillColor? Zenitha.ColorStr|Zenitha.Color
---@field frameColor? Zenitha.ColorStr|Zenitha.Color
---@field activeColor? Zenitha.ColorStr|Zenitha.Color
---@field idleColor? Zenitha.ColorStr|Zenitha.Color
---
---@field sound_press? string
---@field sound_hover? string
---
---@field ang? number [image]
---@field k? number [image]
---
---@field sound_on? string [checkBox]
---@field sound_off? string [checkBox]
---
---@field axis? {minVal:number, maxVal:number, step?:number} [slider]
---@field smooth? boolean Unit point visibility [slider]
---@field valueShow? false|'int'|'float'|'percent'|function Value showing mode or function [called with widgetObj) [slider]
---@field lineDist? number Outline dist from the bat [slider_fill]
---
---@field selFontSize? number [selector]
---@field selFontType? string [selector]
---@field list? table [selector]
---@field show? function [selector]
---
---@field secret? boolean [inputBox]
---@field regex? string [inputBox]
---@field maxInputLength? number [inputBox]
---@field sound_input? string [inputBox]
---@field sound_bksp? string [inputBox]
---@field sound_clear? string [inputBox]
---@field sound_fail? string [inputBox]
---
---@field scrollBarPos? number [textBox & listBox]
---@field scrollBarWidth? number [textBox & listBox]
---@field scrollBarDist? number [textBox & listBox]
---@field scrollBarColor? Zenitha.ColorStr|Zenitha.Color [textBox & listBox]
---@field lineHeight? number [textBox & listBox]
---
---@field yOffset? number [textBox]
---@field fixContent? boolean [textBox]
---
---@field drawFunc? function [listBox]
---@field releaseDist? number [listBox]
---@field stencilMode? 'total'|'single'|false [listBox]
---@field sound_click? string [listBox]
---@field sound_select? string [listBox]
local gc_translate,gc_scale=GC.translate,GC.scale
local gc_push,gc_pop=GC.push,GC.pop
local gc_setColor,gc_setLineWidth=GC.setColor,GC.setLineWidth
local gc_draw,gc_line=GC.draw,GC.line
local gc_rectangle,gc_circle=GC.rectangle,GC.circle
local gc_print,gc_printf=GC.print,GC.printf
local gc_mStr=GC.mStr
local gc_stc_reset,gc_stc_stop=GC.stc_reset,GC.stc_stop
local gc_stc_circ,gc_stc_rect=GC.stc_circ,GC.stc_rect
local gc_mRect=GC.mRect
local kb=love.keyboard
local timer=love.timer.getTime
local assert,next=assert,next
local floor,ceil=math.floor,math.ceil
local max,min=math.max,math.min
local abs,clamp=math.abs,MATH.clamp
local sub,ins,rem=string.sub,table.insert,table.remove
local SCN,SCR,xOy=SCN,SCR,SCR.xOy
local setFont,getFont=FONT.set,FONT.get
local utf8=require('utf8')
local indexMeta={
__index=function(L,k)
for i=1,#L do
if L[i].name==k then
return L[i]
end
end
end,
}
local function updateWheel(self,d)
self._floatWheel=self._floatWheel+(d or 0)
if abs(self._floatWheel)>=1 then
local n=MATH.sign(self._floatWheel)*floor(abs(self._floatWheel))
self._floatWheel=self._floatWheel-n
return n
end
end
local function alignDraw(self,drawable,x,y,ang,kx,ky)
local w=drawable:getWidth()
local h=drawable:getHeight()
if not kx then kx=min(self.widthLimit/w,1) end
if not ky then ky=kx or 1 end
local ox=self.alignX=='left' and 0 or self.alignX=='right' and w or w*.5
local oy=self.alignY=='top' and 0 or self.alignY=='bottom' and h or h*.5
gc_draw(drawable,x,y,ang,kx,ky,ox,oy)
end
local leftAngle=GC.load{20,20,
{'setLW',5},
{'line', 18,2,1,10,18,18},
}
local rightAngle=GC.load{20,20,
{'setLW',5},
{'line', 2,2,19,10,2,18},
}
local Widgets={}
--------------------------------------------------------------
---@class Zenitha.Widget.base not used by user
---@field _widget true
---@field type string
---@field name string|false
---
---@field color Zenitha.ColorStr|Zenitha.Color
---@field textColor Zenitha.ColorStr|Zenitha.Color
---@field fillColor Zenitha.ColorStr|Zenitha.Color
---@field frameColor Zenitha.ColorStr|Zenitha.Color
---@field activeColor Zenitha.ColorStr|Zenitha.Color
---@field idleColor Zenitha.ColorStr|Zenitha.Color
---
---@field lineWidth number
---@field cornerR number
---
---@field sound_press string|false
---@field sound_hover string|false
---
---@field _text love.Drawable|false
---@field _image love.Drawable|false
---@field _hoverTime number
---@field _hoverTimeMax number
---@field _pressed boolean
---@field _pressTime number
---@field _pressTimeMax number
---@field _visible boolean|nil
---
---@field reset function
---@field press function
---@field release function
---@field scroll function
---@field drag function
---@field update function
---@field draw function
---@field isAbove function
---@field arrowKey function
---@field keypress function
---@field code function
Widgets.base={
_widget=true,
type='null',
name=false,
text=false,
image=false,
keepFocus=false,
x=0,y=0,
color='L',
textColor='L',
fillColor='L',
frameColor='L',
activeColor='LY',
idleColor='L',
pos=false,
lineWidth=4,cornerR=3,
fontSize=30,fontType=false,
widthLimit=1e99,
labelPos='left',
alignX='center',alignY='center',
sound_press=false,sound_hover=false,
isAbove=NULL,
draw=NULL,
visibleFunc=false, -- function return a boolean
visibleTick=false, -- function return a boolean
_text=false,
_image=false,
_hoverTime=0,
_hoverTimeMax=.1,
_pressed=false,
_pressTime=0,
_pressTimeMax=.05,
_visible=nil,
buildArgs={},
}
function Widgets.base:getInfo()
local str=''
for _,v in next,self.buildArgs do
str=str..v..'='..tostring(self[v])..'\n'
end
return str
end
function Widgets.base:reset()
assert(not self.name or type(self.name)=='string',"[widget].name need string")
assert(type(self.x)=='number',"[widget].x need number")
assert(type(self.y)=='number',"[widget].y need number")
if type(self.color)=='string' then self.color=COLOR[self.color] end
assert(type(self.color)=='table',"[widget].color need table")
if type(self.textColor)=='string' then self.textColor=COLOR[self.textColor] end
assert(type(self.textColor)=='table',"[widget].textColor need table")
if type(self.fillColor)=='string' then self.fillColor=COLOR[self.fillColor] end
assert(type(self.fillColor)=='table',"[widget].fillColor need table")
if type(self.frameColor)=='string' then self.frameColor=COLOR[self.frameColor] end
assert(type(self.frameColor)=='table',"[widget].frameColor need table")
if type(self.activeColor)=='string' then self.activeColor=COLOR[self.activeColor] end
assert(type(self.activeColor)=='table',"[widget].activeColor need table")
if type(self.idleColor)=='string' then self.idleColor=COLOR[self.idleColor] end
assert(type(self.idleColor)=='table',"[widget].idleColor need table")
assert(type(self.lineWidth)=='number',"[widget].lineWidth need number")
assert(type(self.cornerR)=='number',"[widget].cornerR need number")
assert(self.alignX=='left' or self.alignX=='right' or self.alignX=='center',"[widget].alignX need 'left', 'right' or 'center'")
assert(self.alignY=='top' or self.alignY=='bottom' or self.alignY=='center',"[widget].alignY need 'top', 'bottom' or 'center'")
assert(self.labelPos=='left' or self.labelPos=='right' or self.labelPos=='top' or self.labelPos=='bottom',"[widget].labelPos need 'left', 'right', 'top', or 'bottom'")
if self.pos then
assert(
type(self.pos)=='table' and
(type(self.pos[1])=='number' or self.pos[1]==false) and
(type(self.pos[2])=='number' or self.pos[2]==false),
"[widget].pos[1] and [2] need number|false}"
)
self._x=self.x+(self.pos[1] and self.pos[1]*(SCR.w0+2*SCR.x/SCR.k)-SCR.x/SCR.k or 0)
self._y=self.y+(self.pos[2] and self.pos[2]*(SCR.h0+2*SCR.y/SCR.k)-SCR.y/SCR.k or 0)
else
self._x=self.x
self._y=self.y
end
assert(type(self.fontSize)=='number',"[widget].fontSize need number")
assert(type(self.fontType)=='string' or self.fontType==false,"[widget].fontType need string")
assert(type(self.widthLimit)=='number',"[widget].widthLimit need number")
assert(not self.visibleFunc or type(self.visibleFunc)=='function',"[widget].visibleFunc need function")
assert(not self.visibleTick or type(self.visibleTick)=='function',"[widget].visibleTick need function")
assert(not self.sound_press or type(self.sound_press)=='string',"[widget].sound_press need string")
assert(not self.sound_hover or type(self.sound_hover)=='string',"[widget].sound_hover need string")
self._text=self.text or self.name and ("["..self.name.."]")
if self._text then
if type(self._text)=='function' then
self._text=self._text()
end
assert(type(self._text)=='string',"[widget].text need string|fun():string")
self._text=GC.newText(getFont(self.fontSize,self.fontType),self._text)
else
self._text=PAPER
end
self._image=false
if self.image then
if type(self.image)=='string' then
local path=STRING.split(self.image,'/')
local _img=IMG
repeat
_img=_img[rem(path,1)]
until not (path[1] and _img)
self._image=_img or PAPER
else
self._image=self.image
end
end
self._hoverTime=0
if self._visible==nil then
self._visible=true
end
if self.visibleFunc then
self._visible=self.visibleFunc()
elseif self.visibleTick then
self._visible=self.visibleTick()
end
end
function Widgets.base:setVisible(bool)
if bool==nil then
if self.visibleFunc then
self._visible=self.visibleFunc()
elseif self.visibleTick then
self._visible=self.visibleTick()
end
else
self._visible=bool and true or false
end
end
function Widgets.base:update(dt)
if self._pressed then
self._pressTime=min(self._pressTime+dt,self._pressTimeMax)
else
self._pressTime=max(self._pressTime-dt,0)
end
if WIDGET.sel==self then
self._hoverTime=min(self._hoverTime+dt,self._hoverTimeMax)
else
self._hoverTime=max(self._hoverTime-dt,0)
end
end
function Widgets.base.press() end
function Widgets.base.release() end
function Widgets.base.drag() end
function Widgets.base.scroll() end
---@class Zenitha.Widget.text: Zenitha.Widget.base
Widgets.text=setmetatable({
type='text',
text=false,
buildArgs={
'name',
'pos',
'x','y',
'color','text',
'fontSize','fontType',
'alignX','alignY',
'widthLimit',
'visibleFunc',
'visibleTick',
},
},{__index=Widgets.base,__metatable=true})
function Widgets.text:reset()
Widgets.base.reset(self)
end
function Widgets.text:draw()
if self._text then
gc_setColor(self.color)
alignDraw(self,self._text,self._x,self._y)
end
end
---@class Zenitha.Widget.image: Zenitha.Widget.base
---@field _kx number|false
---@field _ky number|false
Widgets.image=setmetatable({
type='image',
w=false,h=false,k=false,
ang=0,
image=false,
_kx=false,_ky=false,
buildArgs={
'name',
'pos',
'x','y',
'k','w','h', -- Not compatible
'ang',
'image',
'alignX','alignY',
'visibleFunc',
'visibleTick',
},
},{__index=Widgets.base,__metatable=true})
function Widgets.image:reset()
Widgets.base.reset(self)
assert(not self.k or not (self.w or self.h),"[image].w/h and .k cannot appear simultaneously")
if not self._image then return end
if self.k then
self._kx,self._ky=self.k,self.k
elseif self.w or self.h then
if self.w then self._kx=self.w/self._image:getWidth() end
if self.h then self._ky=self.h/self._image:getHeight() end
if not self.w then self._kx=self._ky end
if not self.h then self._ky=self._kx end
else
self._kx,self._ky=1,1
end
end
function Widgets.image:setImage(_img)
self.image=_img
self:reset()
end
function Widgets.image:draw()
if self._image then
gc_setColor(1,1,1)
alignDraw(self,self._image,self._x,self._y,self.ang,self._kx,self._ky)
end
end
---@class Zenitha.Widget.button: Zenitha.Widget.base
---@field w number
---@field h number
---@field sound_trigger string|false
Widgets.button=setmetatable({
type='button',
w=40,h=false,
text=false,
image=false,
cornerR=10,
sound_trigger=false,
textColor=false,
code=NULL,
buildArgs={
'name',
'pos',
'x','y','w','h',
'lineWidth','cornerR',
'alignX','alignY',
'text','image',
'color','textColor',
'fontSize','fontType',
'sound_trigger',
'sound_press','sound_hover',
'code',
'visibleFunc',
'visibleTick',
},
},{__index=Widgets.base,__metatable=true})
function Widgets.button:reset()
if self.textColor==false then self.textColor=self.color end
Widgets.base.reset(self)
if not self.h then self.h=self.w end
assert(self.w and type(self.w)=='number',"[button].w need number")
assert(self.h and type(self.h)=='number',"[button].h need number")
assert(not self.sound_trigger or type(self.sound_trigger)=='string',"[button].sound_trigger need string")
self.widthLimit=self.w
end
function Widgets.button:isAbove(x,y)
return
abs(x-self._x)<self.w*.5 and
abs(y-self._y)<self.h*.5
end
function Widgets.button:press()
self._pressed=true
end
function Widgets.button:release(_,_,k)
if self._pressed then
self._pressed=false
if self.sound_trigger then
SFX.play(self.sound_trigger)
end
self.code(k)
end
end
function Widgets.button:drag(x,y)
if not self:isAbove(x,y) and self==WIDGET.sel then
WIDGET.unFocus()
self._pressed=false
end
end
function Widgets.button:draw()
gc_push('transform')
gc_translate(self._x,self._y)
if self._pressTime>0 then
gc_scale(1-self._pressTime/self._pressTimeMax*.0626)
end
local w,h=self.w,self.h
local c=self.color
-- Background
gc_setColor(c[1],c[2],c[3],.1+.2*self._hoverTime/self._hoverTimeMax)
gc_mRect('fill',0,0,w,h,self.cornerR)
-- Frame
gc_setLineWidth(self.lineWidth)
gc_setColor(.2+c[1]*.8,.2+c[2]*.8,.2+c[3]*.8,.95)
gc_mRect('line',0,0,w,h,self.cornerR)
-- Drawable
if self._image then
gc_setColor(1,1,1)
alignDraw(self,self._image)
end
if self._text then
gc_setColor(self.textColor)
alignDraw(self,self._text)
end
gc_pop()
end
---@class Zenitha.Widget.button_fill: Zenitha.Widget.button
Widgets.button_fill=setmetatable({
type='button_fill',
textColor='D',
buildArgs=TABLE.combine(Widgets.button.buildArgs,{'textColor'}),
},{__index=Widgets.button,__metatable=true})
function Widgets.button_fill:draw()
gc_push('transform')
gc_translate(self._x,self._y)
if self._pressTime>0 then
gc_scale(1-self._pressTime/self._pressTimeMax*.0626)
end
local w,h=self.w,self.h
local HOV=self._hoverTime/self._hoverTimeMax
local c=self.color
local r,g,b=c[1],c[2],c[3]
-- Rectangle
gc_setColor(.15+r*.7*(1-HOV*.26),.15+g*.7*(1-HOV*.26),.15+b*.7*(1-HOV*.26),.9)
gc_mRect('fill',0,0,w,h,self.cornerR)
-- Drawable
if self._image then
gc_setColor(1,1,1)
alignDraw(self,self._image)
end
if self._text then
gc_setColor(self.textColor)
alignDraw(self,self._text)
end
gc_pop()
end
---@class Zenitha.Widget.button_invis: Zenitha.Widget.button
Widgets.button_invis=setmetatable({
type='button_invis',
sound_trigger=false,
},{__index=Widgets.button,__metatable=true})
function Widgets.button_invis:draw()
gc_push('transform')
gc_translate(self._x,self._y)
local w,h=self.w,self.h
local HOV=self._hoverTime/self._hoverTimeMax
local c=self.color
-- Rectangle
gc_setColor(c[1],c[2],c[3],HOV*.16)
gc_mRect('fill',0,0,w,h,self.cornerR)
-- Drawable
if self._image then
gc_setColor(1,1,1)
alignDraw(self,self._image)
end
if self._text then
gc_setColor(c)
alignDraw(self,self._text)
end
gc_pop()
end
---@class Zenitha.Widget.checkBox: Zenitha.Widget.base
---@field w number
---@field sound_on string|false
---@field sound_off string|false
Widgets.checkBox=setmetatable({
type='checkBox',
w=30,
text=false,
image=false,
labelDistance=20,
sound_on=false,sound_off=false,
disp=false, -- function return a boolean
code=NULL,
buildArgs={
'name',
'pos',
'x','y','w',
'lineWidth','cornerR',
'labelPos',
'labelDistance',
'color','text',
'fontSize','fontType',
'widthLimit',
'sound_on','sound_off',
'sound_press','sound_hover',
'disp','code',
'visibleFunc',
'visibleTick',
},
},{__index=Widgets.base,__metatable=true})
function Widgets.checkBox:reset()
Widgets.base.reset(self)
assert(type(self.disp)=='function',"[checkBox].disp need function")
assert(not self.sound_on or type(self.sound_on)=='string',"[checkBox].sound_on need string")
assert(not self.sound_off or type(self.sound_off)=='string',"[checkBox].sound_off need string")
self.alignX=self.labelPos=='left' and 'right' or self.labelPos=='right' and 'left' or 'center'
self.alignY=self.labelPos=='top' and 'bottom' or self.labelPos=='bottom' and 'top' or 'center'
end
function Widgets.checkBox:isAbove(x,y)
return
abs(x-self._x)<self.w*.5 and
abs(y-self._y)<self.w*.5
end
function Widgets.checkBox:press(_,_,k)
self.code(k)
if self.disp() then
if self.sound_on then
SFX.play(self.sound_on)
end
else
if self.sound_off then
SFX.play(self.sound_off)
end
end
end
function Widgets.checkBox:draw()
gc_push('transform')
gc_translate(self._x,self._y)
local w=self.w
local HOV=self._hoverTime/self._hoverTimeMax
local c=self.color
-- Background
gc_setColor(c[1],c[2],c[3],.3*HOV)
gc_mRect('fill',0,0,w,w,self.cornerR)
-- Frame
gc_setLineWidth(self.lineWidth)
gc_setColor(.2+c[1]*.8,.2+c[2]*.8,.2+c[3]*.8)
gc_mRect('line',0,0,w,w,self.cornerR)
if self.disp() then
gc_scale(.5*w)
gc_setLineWidth(self.lineWidth*2/w)
gc_line(-.7,.05,-.2,.5,.7,-.55)
gc_scale(2/w)
end
-- Drawable
local x2,y2=0,0
if self.labelPos=='left' then
x2=-w*.5-self.labelDistance
elseif self.labelPos=='right' then
x2=w*.5+self.labelDistance
elseif self.labelPos=='top' then
y2=-w*.5-self.labelDistance
elseif self.labelPos=='bottom' then
y2=w*.5+self.labelDistance
end
if self._image then
gc_setColor(1,1,1)
alignDraw(self,self._image,x2,y2)
end
if self._text then
gc_setColor(c)
alignDraw(self,self._text,x2,y2)
end
gc_pop()
end
---@class Zenitha.Widget.switch: Zenitha.Widget.checkBox
---@field _slideTime number
Widgets.switch=setmetatable({
type='switch',
h=30,
fillColor='lS',
text=false,
image=false,
labelDistance=20,
disp=false, -- function return a boolean
code=NULL,
_slideTime=false,
buildArgs={
'name',
'pos',
'x','y','h',
'labelPos',
'labelDistance',
'color','fillColor',
'text','fontSize','fontType',
'lineWidth','widthLimit',
'sound_on','sound_off',
'sound_press','sound_hover',
'disp','code',
'visibleFunc',
'visibleTick',
},
},{__index=Widgets.checkBox,__metatable=true})
function Widgets.switch:reset()
Widgets.base.reset(self)
assert(type(self.disp)=='function',"[switch].disp need function")
self._slideTime=0
self.alignX=self.labelPos=='left' and 'right' or self.labelPos=='right' and 'left' or 'center'
self.alignY=self.labelPos=='top' and 'bottom' or self.labelPos=='bottom' and 'top' or 'center'
end
function Widgets.switch:isAbove(x,y)
return
self.disp and
abs(x-self._x)<self.h and
abs(y-self._y)<self.h*.5
end
function Widgets.switch:update(dt)
Widgets.base.update(self,dt)
if self.disp() then
self._slideTime=min(self._slideTime+dt,self._hoverTimeMax/2)
else
self._slideTime=max(self._slideTime-dt,-self._hoverTimeMax/2)
end
end
function Widgets.switch:draw()
gc_push('transform')
gc_translate(self._x,self._y)
local h=self.h
local HOV=self._hoverTime/self._hoverTimeMax
local c=self.color
-- Background
gc_setColor(self.fillColor[1],self.fillColor[2],self.fillColor[3],self._slideTime/self._hoverTimeMax+.5)
gc_mRect('fill',0,0,h*2,h,h*.5)
-- Frame
gc_setLineWidth(self.lineWidth)
gc_setColor(.2+c[1]*.8,.2+c[2]*.8,.2+c[3]*.8,.8+.2*HOV)
gc_mRect('line',0,0,h*2,h,h*.5)
-- Axis
gc_setColor(1,1,1,.8+.2*HOV)
gc_circle('fill',h*(self._slideTime/self._hoverTimeMax),0,h*(.35+HOV*.05))
-- Drawable
local x2,y2=0,0
if self.labelPos=='left' then
x2=-h-self.labelDistance
elseif self.labelPos=='right' then
x2=h+self.labelDistance
elseif self.labelPos=='top' then
y2=-h*.5-self.labelDistance
elseif self.labelPos=='bottom' then
y2=h*.5+self.labelDistance
end
if self._image then
gc_setColor(1,1,1)
alignDraw(self,self._image,x2,y2)
end
if self._text then
gc_setColor(c)
alignDraw(self,self._text,x2,y2)
end
gc_pop()
end
---@class Zenitha.Widget.slider: Zenitha.Widget.base
---@field w number
---@field valueShow false|'int'|'float'|'percent'|function
---@field numFontSize number
---@field numFontType false|string
---@field sound_drag string|false
---@field soundInterval number
---@field soundPitchRange number
---@field _showFunc function
---@field _pos number
---@field _pos0 number
---@field _rangeL number|false
---@field _rangeR number|false
---@field _rangeWidth number
---@field _unit number
---@field _smooth boolean
---@field _textShowTime number
---@field _lastSoundTime number
Widgets.slider=setmetatable({
type='slider',
w=100,
axis={0,1},
smooth=false,
text=false,
image=false,
labelDistance=20,
numFontSize=25,numFontType=false,
valueShow=nil,
textAlwaysShow=false,
sound_drag=false,
soundInterval=.0626,
soundPitchRange=0,
disp=false, -- function return the displaying _value
code=NULL,
_floatWheel=0,
_showFunc=false,
_pos=false,
_pos0=false,
_rangeL=false,
_rangeR=false,
_rangeWidth=false, -- just _rangeR-_rangeL, for convenience
_unit=false,
_smooth=false,
_textShowTime=false,
_approachSpeed=26,
_lastSoundTime=0,
buildArgs={
'name',
'pos',
'x','y','w',
'lineWidth','cornerR',
'axis','smooth',
'labelPos',
'labelDistance',
'color','textColor','fillColor',
'text',
'fontSize','fontType',
'numFontSize','numFontType',
'widthLimit',
'textAlwaysShow',
'sound_drag',
'soundInterval',
'soundPitchRange',
'valueShow',
'disp','code',
'visibleFunc',
'visibleTick',
},
},{__index=Widgets.base,__metatable=true})
local sliderShowFunc={
null=function()
return ''
end,
int=function(S)
return S._pos0
end,
float=function(S)
return floor(S._pos0*100+.5)*.01
end,
percent=function(S)
return floor(S._pos0*100+.5)..'%'
end,
}
function Widgets.slider:reset()
Widgets.base.reset(self)
assert(self.w and type(self.w)=='number',"[slider].w need number")
assert(type(self.numFontSize)=='number',"[widget].numFontSize need number")
assert(type(self.numFontType)=='string' or self.numFontType==false,"[widget].numFontType need string")
assert(type(self.disp)=='function',"[slider].disp need function")
assert(
type(self.axis)=='table' and (#self.axis==2 or #self.axis==3) and
type(self.axis[1])=='number' and
type(self.axis[2])=='number' and
(not self.axis[3] or type(self.axis[3])=='number'),
"[slider].axis need {low,high} or {low,high,unit}"
)
assert(self.smooth==nil or type(self.smooth)=='boolean',"[slider].smooth need boolean")
assert(not self.sound_drag or type(self.sound_drag)=='string',"[slider].sound_drag need string")
assert(type(self.soundInterval)=='number',"[slider].soundInterval need number")
assert(type(self.soundPitchRange)=='number',"[slider].soundPitchRange need number")
self._rangeL=self.axis[1]
self._rangeR=self.axis[2]
self._rangeWidth=self._rangeR-self._rangeL
self._unit=self.axis[3]
if self.smooth==nil then
self._smooth=not self.axis[3]
else
self._smooth=self.smooth
end
self._pos=self._rangeL
self._pos0=self._rangeL
self._textShowTime=3
if self.valueShow then
if type(self.valueShow)=='function' then
self._showFunc=self.valueShow
elseif type(self.valueShow)=='string' then
self._showFunc=assert(sliderShowFunc[self.valueShow],"[slider].valueShow need function, or 'int', 'float', or 'percent'")
end
elseif self.valueShow==false then -- Show nothing if false
self._showFunc=sliderShowFunc.null
else -- Use default if nil
if self._unit and self._unit%1==0 then
self._showFunc=sliderShowFunc.int
else
self._showFunc=sliderShowFunc.percent
end
end
assert(self.labelPos~='top',"[slider].labelPos cannot be 'top'")
self.alignX=self.labelPos=='left' and 'right' or self.labelPos=='right' and 'left' or 'center'
if self.labelPos=='bottom' then
self.alignY='top'
self.labelDistance=max(self.labelDistance,20)
end
end
function Widgets.slider:isAbove(x,y)
return
x>self._x-10 and
x<self._x+self.w+10 and
abs(y-self._y)<25
end
function Widgets.slider:update(dt)
Widgets.base.update(self,dt)
if self._visible then
self._pos0=self.disp()
self._pos=MATH.expApproach(self._pos,self._pos0,dt*self._approachSpeed)
end
if WIDGET.sel==self then
self._textShowTime=2
end
if not self.textAlwaysShow then
self._textShowTime=max(self._textShowTime-dt,0)
end
end
function Widgets.slider:draw()
local x,y=self._x,self._y
local HOV=self._hoverTime/self._hoverTimeMax
local x2=x+self.w
local rangeL,rangeR=self._rangeL,self._rangeR
local c=self.color
local fc=self.fillColor
local r,g,b=c[1],c[2],c[3]
local fr,fg,fb=fc[1],fc[2],fc[3]
gc_setColor(r,g,b,.5+HOV*.36)
-- Units
if not self._smooth and self._unit then
gc_setLineWidth(self.lineWidth)
for p=rangeL,rangeR,self._unit do
local X=x+(x2-x)*(p-rangeL)/self._rangeWidth
gc_line(X,y+7,X,y-7)
end
end
-- Axis
gc_setLineWidth(self.lineWidth*2)
gc_line(x,y,x2,y)
-- Block
local pos=clamp(self._pos,rangeL,rangeR)
local cx=x+(x2-x)*(pos-rangeL)/self._rangeWidth
local bx,by=cx-10-HOV*2,y-16-HOV*5
local bw,bh=20+HOV*4,32+HOV*10
gc_setColor((self._pos0<rangeL or self._pos0>rangeR) and COLOR.lR or self.fillColor)
gc_rectangle('fill',bx,by,bw,bh,self.cornerR)
-- Glow
if HOV>0 then
gc_setLineWidth(self.lineWidth*.5)
gc_setColor(r,g,b,HOV*.8)