-
Notifications
You must be signed in to change notification settings - Fork 17
/
gui.lua
840 lines (738 loc) · 25.4 KB
/
gui.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
guielement = class("guielement")
function guielement:init(...)
local arg = {...}
self.active = true
self.priority = false
if arg[1] == "checkbox" then --checkbox(x, y, func, start)
self.type = arg[1]
self.x = arg[2]
self.y = arg[3]
self.func = arg[4]
if not arg[5] or arg[5] == false then
self.var = false
else
self.var = true
end
self.text = arg[6]
elseif arg[1] == "dropdown" then --dropdown(x, y, width (in chars), func, start, {entries})
self.type = arg[1]
self.x = arg[2]
self.y = arg[3]
self.starty = self.y
self.width = arg[4]
self.func = arg[5]
self.var = arg[6] or 1
self.entries = {}
for i = 7, #arg do
table.insert(self.entries, arg[i])
end
self.extended = false
self.cutoff = true
elseif arg[1] == "rightclick" then --rightclick(x, y, width (in chars), func, start, {entries})
self.type = arg[1]
self.x = arg[2]
self.y = arg[3]
self.width = arg[4]
self.func = arg[5]
self.var = arg[6] or nil
self.entries = {}
for i = 7, #arg do
table.insert(self.entries, arg[i])
end
self.priority = true
if self.y > 112 then
self.direction = "up"
else
self.direction = "down"
end
elseif arg[1] == "button" then --button(x, y, text, func, space, arguments (in a table), height, width, autorepeat)
self.type = arg[1]
self.x = arg[2]
self.y = arg[3]
self.text = arg[4]
self.func = arg[5]
self.space = arg[6] or 0
if type(arg[7]) ~= "table" then
self.arguments = {}
else
self.arguments = arg[7]
end
if type(self.text) == "string" then
self.height = (arg[8] or 1)*10
self.width = arg[9] or string.len(self.text)*8
elseif type(self.text) == "userdata" then
self.width = self.text:getWidth()-1
self.height = self.text:getHeight()+1
end
self.autorepeat = arg[10] or false
self.repeatwait = 0.3
self.repeatdelay = arg[10] or 0.05
if arg[10] == true then
self.repeatdelay = 0.05
end
self.repeatwaittimer = self.repeatwait
self.repeatdelaytimer = self.repeatdelay
self.bordercolorhigh = {1, 1, 1}
self.bordercolor = {0.5, 0.5, 0.5}
self.fillcolor = {0, 0, 0}
self.textcolor = {1, 1, 1}
elseif arg[1] == "scrollbar" then --scrollbar(x, y, range, width, height, default, dir, display, min, max, step, mousewheel)
self.type = arg[1]
self.x = arg[2]
self.y = arg[3]
self.range = arg[4]
self.width = arg[5]
self.height = arg[6]
self.value = tonumber(arg[7]) or 0
self.dir = arg[8] or "ver"
self.displaynumber = arg[9] or false
self.min = tonumber(arg[10]) or 0
self.max = tonumber(arg[11]) or 1
self.step = tonumber(arg[12]) or 0.01
self.usemousewheel = arg[13] or false
self.internvalue = (self.value-self.min)/(self.max-self.min)
if self.dir == "ver" then
self.yrange = self.range - self.height
else
self.xrange = self.range - self.width
end
self.backgroundcolor = {0.5, 0.5, 0.5}
self.bordercolorhigh = {1, 1, 1}
self.bordercolor = {0.5, 0.5, 0.5}
self.fillcolor = {0, 0, 0}
elseif arg[1] == "input" then --input(x, y, width, enterfunc, start, maxlength, height, numerical, spacing)
self.type = arg[1]
self.x = arg[2]
self.y = arg[3]
self.width = arg[4]
self.func = arg[5] or (function() self.inputting = false end)
self.value = arg[6] or ""
self.maxlength = arg[7] or 0
self.height = arg[8] or 1
self.numerical = arg[9] or false
self.spacing = arg[10] or 1
self.timer = 0
self.cursorblink = true
self.inputting = false
self.cursorpos = string.len(self.value)+1
self.offset = 0
if self.height == 1 then
if self.cursorpos > math.ceil(self.width) then
if self.cursorpos == math.ceil(self.width)+1 or #self.value ~= self.width then
self.offset = self.cursorpos-math.ceil(self.width)-1
else
self.offset = self.cursorpos-math.ceil(self.width)
end
end
end
self.bordercolorhigh = {1, 1, 1}
self.bordercolor = {0.5, 0.5, 0.5}
self.fillcolor = {0, 0, 0}
self.textcolor = {1, 1, 1}
elseif arg[1] == "text" then --(x, y, s, color)
self.type = arg[1]
self.x = arg[2]
self.y = arg[3]
self.value = arg[4] or ""
self.color = arg[5] or {1, 1, 1}
elseif arg[1] == "submenu" then
self.type = arg[1]
self.x = arg[2]
self.y = arg[3]
self.width = arg[4]
self.width2 = arg[5]
self.entries = arg[6]
self.value = arg[7] or 1
self.suby = self.y
self.subx = self.x
if self.y+(#self.entries*10) > height*16 then
if (#self.entries)*10 > height*16 then
self.missingy = (#self.entries)*10 - height*16
self.suby = 0
else
self.suby = height*16-(#self.entries)*10
end
end
if self.missingy then
self.subx = self.subx + 11
end
if self.subx+12+self.width*8+self.width2*8 > width*16 then
self.subx = self.x - (self.width+1)*8 - self.width2*8 - 3
end
self.extended = false
end
end
function guielement:update(dt)
if self.active then
if self.type == "scrollbar" then
if self.dragging then
if self.dir == "ver" then
local y = (mouse.getY()-self.draggingy) - self.y*scale
local actualyrange = self.yrange*scale
self.internvalue = y / actualyrange
self.internvalue = math.min(math.max(self.internvalue, 0), 1) --clamp
else
local x = (mouse.getX()-self.draggingx) - self.x*scale
local actualxrange = self.xrange*scale
self.internvalue = x / actualxrange
self.internvalue = math.min(math.max(self.internvalue, 0), 1) --clamp
end
--Apply the step
self.value = round((self.min + (self.max-self.min)*self.internvalue)/self.step)*self.step
end
elseif self.type == "input" then
self.timer = self.timer + dt
while self.timer > blinktime do
self.cursorblink = not self.cursorblink
self.timer = self.timer - blinktime
end
elseif self.type == "button" then
if self.autorepeat then
if self.holding then
if self.repeatwaittimer <= 0 then
self.repeatdelaytimer = self.repeatdelaytimer - dt
while self.repeatdelaytimer <= 0 do
if self.func then
self.func(unpack(self.arguments))
end
self.repeatdelaytimer = self.repeatdelaytimer + self.repeatdelay
end
else
self.repeatwaittimer = self.repeatwaittimer - dt
end
else
self.repeatwaittimer = self.repeatwait
self.repeatdelaytimer = self.repeatdelay
end
end
elseif self.type == "dropdown" then
if self.scrollbar then
self.scrollbar:update(dt)
self.y = -self.missingy*self.scrollbar.value
end
elseif self.type == "submenu" then
if self.scrollbar then
self.scrollbar:update(dt)
self.suby = -self.missingy*self.scrollbar.value
end
end
end
end
function guielement:draw(a, offx, offy)
local drawx = self.x + (offx or 0)
local drawy = self.y + (offy or 0)
love.graphics.setColor(1, 1, 1, a)
if self.type == "checkbox" then
local quad = 1
if self.var == true then
quad = 2
end
local high = 1
if self:inhighlight(mouse.getPosition()) then
high = 2
end
love.graphics.draw(checkboximg, checkboxquad[high][quad], drawx*scale, drawy*scale, 0, scale, scale)
if self.text then
properprint(self.text, (drawx+10)*scale, (drawy+1)*scale)
end
elseif self.type == "dropdown" then
local high = self:inhighlight(mouse.getPosition()) or (self.scrollbar and self.scrollbar.dragging)
if self.extended then
love.graphics.setColor(0.5, 0.5, 0.5, a)
love.graphics.rectangle("fill", (drawx+2)*scale, (drawy+2)*scale, (13+self.width*8)*scale, (10*(#self.entries+1)+1)*scale)
end
love.graphics.setColor(0.5, 0.5, 0.5, a)
if high then
love.graphics.setColor(1, 1, 1, a)
end
love.graphics.rectangle("fill", drawx*scale, drawy*scale, (3+self.width*8)*scale, 11*scale)
love.graphics.draw(dropdownarrowimg, (drawx+2+self.width*8)*scale, drawy*scale, 0, scale, scale)
love.graphics.setColor(0, 0, 0, a)
love.graphics.rectangle("fill", (drawx+1)*scale, (drawy+1)*scale, (1+self.width*8)*scale, 9*scale)
love.graphics.setColor(1, 1, 1, a)
if self.extended then
love.graphics.setColor(0.5, 0.5, 0.5, a)
end
local s = self.entries[self.var]
if self.cutoff then
s = string.sub(s, 1, self.width)
end
properprint(s, (drawx+1)*scale, (drawy+2)*scale)
if self.extended then
love.graphics.setColor(0.5, 0.5, 0.5, a)
if high then
love.graphics.setColor(1, 1, 1, a)
end
love.graphics.rectangle("fill", drawx*scale, (drawy+11)*scale, (13+self.width*8)*scale, (10*#self.entries)*scale)
for i = 1, #self.entries do
local s = self.entries[i]
if self.cutoff then
s = string.sub(s, 1, self.width+1)
end
if high ~= i then
love.graphics.setColor(0, 0, 0, a)
love.graphics.rectangle("fill", (drawx+1)*scale, (drawy+1+i*10)*scale, (11+self.width*8)*scale, 9*scale)
love.graphics.setColor(1, 1, 1, a)
properprint(s, (drawx+1)*scale, (drawy+2+10*i)*scale)
else
love.graphics.setColor(0, 0, 0, a)
properprint(s, (drawx+1)*scale, (drawy+2+10*i)*scale)
end
end
end
if self.scrollbar then
self.scrollbar:draw(a)
end
elseif self.type == "button" then
local high = self:inhighlight(mouse.getPosition())
local r, g, b = unpack(self.bordercolor)
love.graphics.setColor(r, g, b, a)
if high then
local r, g, b = unpack(self.bordercolorhigh)
love.graphics.setColor(r, g, b, a)
end
love.graphics.rectangle("fill", drawx*scale, drawy*scale, (3+self.width+self.space*2)*scale, (1+self.height+self.space*2)*scale)
local r, g, b = unpack(self.fillcolor)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("fill", (drawx+1)*scale, (drawy+1)*scale, (1+self.width+self.space*2)*scale, (-1+self.height+self.space*2)*scale)
local r, g, b = unpack(self.textcolor)
love.graphics.setColor(r, g, b, a)
if type(self.text) == "string" then
properprint(self.text, (drawx+1+self.space)*scale, (drawy+2+self.space)*scale)
elseif type(self.text) == "userdata" then
love.graphics.draw(self.text, (drawx+1+self.space)*scale, (drawy+1+self.space)*scale, 0, scale, scale)
end
elseif self.type == "scrollbar" then
if self.dir == "ver" then
local high = self:inhighlight(mouse.getPosition())
local r, g, b = unpack(self.backgroundcolor)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("fill", drawx*scale, drawy*scale, self.width*scale, (self.yrange+self.height)*scale)
local r, g, b = unpack(self.bordercolor)
love.graphics.setColor(r, g, b, a)
if self.dragging or high then
local r, g, b = unpack(self.bordercolorhigh)
love.graphics.setColor(r, g, b, a)
end
love.graphics.rectangle("fill", drawx*scale, (drawy+self.yrange*self.internvalue)*scale, (self.width)*scale, (self.height)*scale)
local r, g, b = unpack(self.fillcolor)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("fill", (drawx+1)*scale, (drawy+1+self.yrange*self.internvalue)*scale, (self.width-2)*scale, (self.height-2)*scale)
else
local high = self:inhighlight(mouse.getPosition())
local r, g, b = unpack(self.backgroundcolor)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("fill", drawx*scale, drawy*scale, (self.xrange+self.width)*scale, self.height*scale)
local r, g, b = unpack(self.bordercolor)
love.graphics.setColor(r, g, b, a)
if self.dragging or high then
local r, g, b = unpack(self.bordercolorhigh)
love.graphics.setColor(r, g, b, a)
end
love.graphics.rectangle("fill", (drawx+self.xrange*self.internvalue)*scale, drawy*scale, (self.width)*scale, (self.height)*scale)
local r, g, b = unpack(self.fillcolor)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("fill", (drawx+1+self.xrange*self.internvalue)*scale, (drawy+1)*scale, (self.width-2)*scale, (self.height-2)*scale)
if self.displaynumber then
love.graphics.setColor(1, 1, 1, a)
properprint(formatscrollnumber(self.value), math.floor((drawx+self.xrange*self.internvalue)*scale), (drawy+1)*scale)
end
end
elseif self.type == "input" then
local high = self:inhighlight(mouse.getPosition())
local r, g, b = unpack(self.bordercolor)
love.graphics.setColor(r, g, b, a)
if self.inputting or high then
local r, g, b = unpack(self.bordercolorhigh)
love.graphics.setColor(r, g, b, a)
end
love.graphics.rectangle("fill", drawx*scale, drawy*scale, (3+self.width*8+2*self.spacing)*scale, (1+self.height*10+2*self.spacing)*scale)
local r, g, b = unpack(self.fillcolor)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("fill", (drawx+1)*scale, (drawy+1)*scale, (1+self.width*8+2*self.spacing)*scale, (-1+self.height*10+2*self.spacing)*scale)
local r, g, b = unpack(self.textcolor)
love.graphics.setColor(r, g, b, a)
--format string
local oldstring = self.value
if self.height == 1 then
oldstring = string.sub(self.value, self.offset+1, self.offset+self.width)
end
local newstring = {}
for i = 1, string.len(oldstring), self.width do
if math.ceil(i/self.width) > self.height then
break
end
table.insert(newstring, string.sub(oldstring, i, i+self.width-1))
end
for i = 1, #newstring do
properprint(newstring[i], (drawx+1+self.spacing)*scale, (drawy+2+self.spacing+(i-1)*10)*scale)
end
--cursor
if self.inputting and self.cursorblink then
local x, y
if #newstring == 0 then
x, y = 1, 1
else
x, y = #newstring[#newstring]+1, #newstring
end
if x > self.width then
if y < self.height and self.height > 1 then
x = 1
y = y + 1
else
x = x - 1
end
end
love.graphics.rectangle("fill", (drawx+1+self.spacing+(x-1)*8)*scale, (drawy+9+self.spacing+(y-1)*10)*scale, 8*scale, 1*scale)
end
elseif self.type == "text" then
local r, g, b = unpack(self.color)
love.graphics.setColor(r, g, b, a)
properprint(self.value, drawx*scale, drawy*scale)
elseif self.type == "submenu" then
local high = self:inhighlight(mouse.getPosition()) or (self.scrollbar and self.scrollbar.dragging)
love.graphics.setColor(0.5, 0.5, 0.5, a)
if high then
love.graphics.setColor(1, 1, 1, a)
end
love.graphics.rectangle("fill", drawx*scale, drawy*scale, (3+self.width*8)*scale, 11*scale)
love.graphics.setColor(0, 0, 0, a)
love.graphics.rectangle("fill", (drawx+1)*scale, (drawy+1)*scale, (1+self.width*8)*scale, 9*scale)
love.graphics.setColor(1, 1, 1, a)
if self.extended then
love.graphics.setColor(0.5, 0.5, 0.5, a)
end
properprint(string.sub(self.entries[self.value], 1, self.width), (drawx+1)*scale, (drawy+2)*scale)
if self.extended then
love.graphics.setColor(0.5, 0.5, 0.5, a)
if high then
love.graphics.setColor(1, 1, 1, a)
end
love.graphics.rectangle("fill", (self.subx+9+self.width*8)*scale, (self.suby)*scale, (3+self.width2*8)*scale, (10*#self.entries+1)*scale)
for i = 1, #self.entries do
if high ~= i then
love.graphics.setColor(0, 0, 0, a)
love.graphics.rectangle("fill", (self.subx+10+self.width*8)*scale, (self.suby+1+(i-1)*10)*scale, (1+self.width2*8)*scale, 9*scale)
love.graphics.setColor(1, 1, 1, a)
properprint(string.sub(self.entries[i], 1, self.width2), (self.subx+10+self.width*8)*scale, (self.suby+2+10*(i-1))*scale)
else
love.graphics.setColor(0, 0, 0, a)
properprint(string.sub(self.entries[i], 1, self.width2), (self.subx+10+self.width*8)*scale, (self.suby+2+10*(i-1))*scale)
end
end
if self.scrollbar then
self.scrollbar:draw(a)
end
end
love.graphics.setColor(0.5, 0.5, 0.5, a)
if high then
love.graphics.setColor(1, 1, 1, a)
end
love.graphics.draw(dropdownarrowimg, (drawx+5+self.width*8)*scale, (drawy+5)*scale, -math.pi/2, scale, scale, 6, 6)
end
love.graphics.setColor(1, 1, 1, a)
end
function guielement:getheight()
if self.type == "dropdown" then
return 11+10*#self.entries
elseif self.type == "submenu" then
end
end
function guielement:click(x, y, button)
if self.active then
if self.type == "checkbox" then
if button == "l" then
if self:inhighlight(x, y) then
if not self.func then
self.var = not self.var
else
self.func()
end
end
end
elseif self.type == "dropdown" then
if self.scrollbar then
self.scrollbar:click(x, y, button)
end
if button == "l" then
if self.extended == false then
if self:inhighlight(x, y) then
self.extended = true
self.priority = true
if self.y+self:getheight() > height*16 then
self.y = height*16-self:getheight()
end
if self:getheight() > height*16 then
self.missingy = -self.y
self.y = 0
self.scrollbar = guielement:new("scrollbar", self.x-9, 0, height*16, 10, 30, 0, "ver", false, 0, 1, nil, true)
end
end
else
local high = self:inhighlight(x, y)
if high == -1 then --scrollbar
return
end
self.extended = false
self.priority = false
self.y = self.starty
self.scrollbar = nil
if high then
if high ~= 0 then
self.func(high)
end
return true
end
end
end
elseif self.type == "submenu" then
if self.scrollbar then
self.scrollbar:click(x, y, button)
end
if button == "l" then
if self.extended == false then
if self:inhighlight(x, y) then
self.extended = true
self.priority = true
if self.missingy then
self.scrollbar = guielement:new("scrollbar", self.subx-9+8+self.width*8, 0, height*16, 10, 30, 0, "ver", false, 0, 1, nil, true)
end
end
else
local high = self:inhighlight(x, y)
if high == -1 then --scrollbar
return true
end
self.extended = false
self.priority = false
if high and high ~= 0 then
self.value = high
return true
end
self.scrollbar = nil
end
elseif button == "wd" or button == "wu" then
return true
end
elseif self.type == "rightclick" then --Not used anymore I think?
if button == "l" then
local high = self:inhighlight(x, y)
if high then
if high ~= 0 then
self.func(high)
end
return true
end
end
elseif self.type == "button" then
if button == "l" then
if self:inhighlight(x, y) then
self.holding = true
if self.func then
self.func(unpack(self.arguments))
end
return true
end
end
elseif self.type == "scrollbar" then
if button == "l" then
if self.dir == "ver" then
if self:inhighlight(x, y) then
self.dragging = true
self.draggingy = y-(self.y+self.yrange*self.internvalue)*scale
end
else
if self:inhighlight(x, y) then
self.dragging = true
self.draggingx = x-(self.x+self.xrange*self.internvalue)*scale
end
end
elseif button == "wd" then
if self.usemousewheel then
self.internvalue = math.min(1, self.internvalue+0.2)
self.value = round((self.min + (self.max-self.min)*self.internvalue)/self.step)*self.step
end
elseif button == "wu" then
if self.usemousewheel then
self.internvalue = math.max(0, self.internvalue-0.2)
self.value = round((self.min + (self.max-self.min)*self.internvalue)/self.step)*self.step
end
end
elseif self.type == "input" then
if button == "l" then
if self:inhighlight(x, y) then
self.inputting = true
self.timer = 0
self.cursorblink = true
else
self.inputting = false
end
end
end
end
end
function guielement:textinput(text)
if self.type == "input" and self.inputting then
local ok = true
for i = 1, string.len(text) do
if not fontquads[text:sub(i, i)] then
ok = false
end
end
if not ok then return end -- Can you believe this is taken from Minecraft?
self.value = self.value .. text
self.cursorpos = self.cursorpos + string.len(text)
end
end
function guielement:keypress(key)
if self.active then
if self.type == "input" then
if self.inputting then
--[[if key == "-" or key == "," or key == ":" or key == ";" then
return
end]]
if key == "escape" then
self.inputting = false
elseif (key == "return" or key == "enter" or key == "kpenter") then
if self.func then
self:func()
else
self.inputting = false
end
elseif key == "backspace" then
self.value = string.sub(self.value, 1, string.len(self.value)-1)
if self.cursorpos > 1 and self.cursorpos > string.len(self.value)+1 then
self.cursorpos = self.cursorpos - 1
end
if self.offset > 0 and self.offset > string.len(self.value)-self.width+1 then
self.offset = self.offset - 1
end
--[[else
if string.len(self.value) < self.maxlength or self.maxlength == 0 then
local found = false
for i = 1, string.len(fontglyphs) do
if key == string.sub(fontglyphs, i, i) then
found = true
break
end
end
if found and (not self.numerical or tonumber(key) or key == "." or key == ",") then
if key == "," then
key = "."
end
self.value = self.value .. key
if self.cursorpos ~= self.maxlength then
self.cursorpos = self.cursorpos + 1
end
if self.cursorpos > self.offset+self.width then
self.offset = self.offset + 1
end
end
end]]
end
return true
end
end
end
end
function guielement:unclick(x, y, button)
if self.active then
if self.type == "scrollbar" then
self.dragging = false
elseif self.type == "button" then
self.holding = false
elseif self.type == "dropdown" then
if self.scrollbar then
self.scrollbar:unclick(x, y, button)
end
elseif self.type == "submenu" then
if self.scrollbar then
self.scrollbar:unclick(x, y, button)
end
end
end
end
function guielement:inhighlight(x, y)
if self.type == "checkbox" then
local xadd = 0
if self.text then
xadd = string.len(self.text)*8+1
end
if x >= self.x*scale and x < (self.x+9+xadd)*scale and y >= self.y*scale and y < (self.y+9)*scale then
return true
end
elseif self.type == "dropdown" then
if self.extended == false then
if x >= self.x*scale and x < (self.x+13+self.width*8)*scale and y >= self.y*scale and y < (self.y+11)*scale then
return true
end
else
local myx, widthadd = self.x, 0
if self.scrollbar then
myx = self.x - self.scrollbar.width+1
widthadd = self.scrollbar.width-1
end
if x >= myx*scale and x < (myx+13+self.width*8+widthadd)*scale and y >= self.y*scale and y < (self.y+10*#self.entries+11)*scale then
if x < self.x*scale then
return -1
end
--get which entry
return math.max(0, math.floor((y-(self.y+1)*scale) / (10*scale)))
end
end
elseif self.type == "submenu" then
local r = false
if x >= self.x*scale and x < (self.x+10+self.width*8)*scale and y >= self.y*scale and y < (self.y+11)*scale then
r = 0
end
if self.extended then
local subx = self.subx
local widthadd = 0
if self.scrollbar then
subx = subx - self.scrollbar.width
widthadd = self.scrollbar.width
end
if x >= (subx+9+self.width*8)*scale and x < (subx+12+self.width*8+self.width2*8+widthadd)*scale and y >= self.suby*scale and y < (self.suby+10*#self.entries+1)*scale then
if x < (subx+9+self.width*8+widthadd)*scale then
return -1
end
--get which entry
if math.max(0, math.floor((y-(self.suby+1)*scale) / (10*scale)))+1 then
r = math.max(0, math.floor((y-(self.suby+1)*scale) / (10*scale)))+1
end
end
end
return r
elseif self.type == "rightclick" then
if self.direction == "down" then
if x >= self.x*scale and x < (self.x+3+self.width*8)*scale and y >= self.y*scale and y < (self.y+10*#self.entries+1)*scale then
--get which entry
return math.max(0, math.floor((y-(self.y+1)*scale) / (10*scale))+1)
end
else
if x >= self.x*scale and x < (self.x+3+self.width*8)*scale and y >= (self.y-10*(#self.entries-1))*scale and y < (self.y+11)*scale then
--get which entry
return math.max(0, math.floor((self.y*scale-y) / (10*scale))+2)
end
end
elseif self.type == "button" then
if x >= self.x*scale and x < (self.x+3+self.width+self.space*2)*scale and y >= self.y*scale and y < (self.y+1+self.height+self.space*2)*scale then
return true
end
elseif self.type == "scrollbar" then
if self.dir == "ver" then
if x >= self.x*scale and x < (self.x+self.width)*scale and y >= (self.y+self.yrange*self.internvalue)*scale and y < (self.height+self.y+self.yrange*self.internvalue)*scale then
return true
end
else
if x >= (self.x+self.xrange*self.internvalue)*scale and x < (self.x+self.width+self.xrange*self.internvalue)*scale and y >= self.y*scale and y < (self.height+self.y)*scale then
return true
end
end
elseif self.type == "input" then
if x >= self.x*scale and x < (self.x+3+self.width*8+2*self.spacing)*scale and y >= self.y*scale and y < (self.y+1+self.height*10+2*self.spacing)*scale then
return true
end
end
return false
end