-
Notifications
You must be signed in to change notification settings - Fork 2
/
mupen-lua-ugui.lua
1760 lines (1568 loc) · 79 KB
/
mupen-lua-ugui.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
-- mupen-lua-ugui 1.7.0
-- https://github.com/Aurumaker72/mupen-lua-ugui
local function folder(file)
local s = debug.getinfo(2, 'S').source:sub(2)
local p = file:gsub('[%(%)%%%.%+%-%*%?%^%$]', '%%%0'):gsub('[\\/]', '[\\/]') .. '$'
return s:gsub(p, '')
end
dofile(folder('mupen-lua-ugui.lua') .. 'breitbandgraphics.lua')
ugui = {
internal = {
-- per-uid library-side data, such as scroll position
control_data = {},
-- the current input state
environment = nil,
-- the last frame's input state
previous_environment = nil,
-- the position of the mouse at the last click
mouse_down_position = {x = 0, y = 0},
-- uid of the currently active control
active_control = nil,
-- whether the active control will be cleared after the mouse is released
clear_active_control_after_mouse_up = true,
-- rectangles which are excluded from hittesting (e.g.: the popped up list of a combobox)
hittest_free_rects = {},
-- array of functions which will be called at the end of the frame
late_callbacks = {},
-- Map of uids used in an active section (between begin_frame and end_frame)
used_uids = {},
---Validates the structure of a control. Must be called in every control function.
---@param control table A control which may or may not abide by the mupen-lua-ugui control contract
validate_control = function(control)
if not control.uid
or not control.rectangle
or not control.rectangle.x
or not control.rectangle.y
or not control.rectangle.width
or not control.rectangle.height
then
error('Attempted to show a malformed control.\r\n' .. debug.traceback())
end
end,
---Validates the structure of a control and registers its uid. Must be called in every control function.
---@param control table A control which may or may not abide by the mupen-lua-ugui control contract
validate_and_register_control = function(control)
ugui.internal.validate_control(control)
if ugui.internal.used_uids[control.uid] then
error(string.format('Attempted to show a control with uid %d, which is already in use! Note that some controls reserve more than one uid slot after them.', control.uid))
end
ugui.internal.used_uids[control.uid] = control.uid
end,
deep_clone = function(obj, seen)
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do
res[ugui.internal.deep_clone(k, s)] = ugui.internal.deep_clone(
v, s)
end
return res
end,
remove_range = function(string, start_index, end_index)
if start_index > end_index then
start_index, end_index = end_index, start_index
end
return string.sub(string, 1, start_index - 1) .. string.sub(string, end_index)
end,
is_mouse_just_down = function()
return ugui.internal.environment.is_primary_down and
not ugui.internal.previous_environment.is_primary_down
end,
is_mouse_just_up = function()
return not ugui.internal.environment.is_primary_down and
ugui.internal.previous_environment.is_primary_down
end,
is_mouse_wheel_up = function()
return ugui.internal.environment.wheel == 1
end,
is_mouse_wheel_down = function()
return ugui.internal.environment.wheel == -1
end,
remove_at = function(string, index)
if index == 0 then
return string
end
return string:sub(1, index - 1) .. string:sub(index + 1, string:len())
end,
insert_at = function(string, string2, index)
return string:sub(1, index) .. string2 .. string:sub(index + string2:len(), string:len())
end,
remap = function(value, from1, to1, from2, to2)
return (value - from1) / (to1 - from1) * (to2 - from2) + from2
end,
clamp = function(value, min, max)
if value == nil then
return value
end
return math.max(math.min(value, max), min)
end,
get_just_pressed_keys = function()
local keys = {}
for key, _ in pairs(ugui.internal.environment.held_keys) do
if not ugui.internal.previous_environment.held_keys[key] then
keys[key] = 1
end
end
return keys
end,
process_push = function(control)
if control.is_enabled == false then
return false
end
if ugui.internal.environment.is_primary_down and not ugui.internal.previous_environment.is_primary_down then
if BreitbandGraphics.is_point_inside_rectangle(ugui.internal.mouse_down_position,
control.rectangle) then
if not control.topmost and BreitbandGraphics.is_point_inside_any_rectangle(ugui.internal.environment.mouse_position, ugui.internal.hittest_free_rects) then
return false
end
ugui.internal.active_control = control.uid
ugui.internal.clear_active_control_after_mouse_up = true
return true
end
end
return false
end,
get_caret_index = function(text, relative_x)
local positions = {}
for i = 1, #text, 1 do
local width = BreitbandGraphics.get_text_size(text:sub(1, i),
ugui.standard_styler.font_size,
ugui.standard_styler.font_name).width
positions[#positions + 1] = width
end
for i = #positions, 1, -1 do
if relative_x > positions[i] then
return ugui.internal.clamp(i + 1, 1, #positions + 1)
end
end
return 1
end,
handle_special_key = function(key, has_selection, text, selection_start, selection_end, caret_index)
local sel_lo = math.min(selection_start, selection_end)
local sel_hi = math.max(selection_start, selection_end)
if key == 'left' then
if has_selection then
-- nuke the selection and set caret index to lower (left)
local lower_selection = sel_lo
selection_start = lower_selection
selection_end = lower_selection
caret_index = lower_selection
else
caret_index = caret_index - 1
end
elseif key == 'right' then
if has_selection then
-- nuke the selection and set caret index to higher (right)
local higher_selection = sel_hi
selection_start = higher_selection
selection_end = higher_selection
caret_index = higher_selection
else
caret_index = caret_index + 1
end
elseif key == 'space' then
if has_selection then
-- replace selection contents by one space
local lower_selection = sel_lo
text = ugui.internal.remove_range(text, sel_lo, sel_hi)
caret_index = lower_selection
selection_start = lower_selection
selection_end = lower_selection
text = ugui.internal.insert_at(text, ' ', caret_index - 1)
caret_index = caret_index + 1
else
text = ugui.internal.insert_at(text, ' ', caret_index - 1)
caret_index = caret_index + 1
end
elseif key == 'backspace' then
if has_selection then
local lower_selection = sel_lo
text = ugui.internal.remove_range(text, lower_selection, sel_hi)
caret_index = lower_selection
selection_start = lower_selection
selection_end = lower_selection
else
text = ugui.internal.remove_at(text,
caret_index - 1)
caret_index = caret_index - 1
end
else
return {
handled = false,
}
end
return {
handled = true,
text = text,
selection_start = selection_start,
selection_end = selection_end,
caret_index = caret_index,
}
end,
},
-- The possible states of a control, which are used by the styler
visual_states = {
--- The control doesn't accept user interactions
disabled = 0,
--- The control isn't being interacted with
normal = 1,
--- The mouse is over the control
hovered = 2,
--- The primary mouse button is pushed on the control or the control is currently capturing inputs
active = 3,
},
---Gets the basic visual state of a control
---@param control table The control
---@return _ integer The visual state
get_visual_state = function(control)
if control.is_enabled == false then
return ugui.visual_states.disabled
end
if ugui.internal.active_control ~= nil and ugui.internal.active_control == control.uid then
return ugui.visual_states.active
end
local now_inside = BreitbandGraphics.is_point_inside_rectangle(
ugui.internal.environment.mouse_position,
control.rectangle)
and
not BreitbandGraphics.is_point_inside_any_rectangle(ugui.internal.environment.mouse_position,
ugui.internal.hittest_free_rects)
local down_inside = BreitbandGraphics.is_point_inside_rectangle(
ugui.internal.mouse_down_position, control.rectangle)
and
not BreitbandGraphics.is_point_inside_any_rectangle(ugui.internal.mouse_down_position,
ugui.internal.hittest_free_rects)
if now_inside and not ugui.internal.environment.is_primary_down then
return ugui.visual_states.hovered
end
if down_inside and ugui.internal.environment.is_primary_down and not now_inside then
return ugui.visual_states.hovered
end
if now_inside and down_inside and ugui.internal.environment.is_primary_down then
return ugui.visual_states.active
end
return ugui.visual_states.normal
end,
--- A collection of stylers, which are responsible for drawing the UI
standard_styler = {
textbox_padding = 2,
track_thickness = 2,
bar_width = 6,
bar_height = 16,
item_height = 15,
menu_item_height = 22,
menu_overlap_size = 3,
menu_item_left_padding = 32,
menu_item_right_padding = 32,
font_size = 12,
cleartype = true,
scrollbar_thickness = 17,
joystick_tip_size = 8,
icon_size = 12,
font_name = 'MS Shell Dlg 2',
raised_frame_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#E1E1E1'),
[2] = BreitbandGraphics.hex_to_color('#E5F1FB'),
[3] = BreitbandGraphics.hex_to_color('#CCE4F7'),
[0] = BreitbandGraphics.hex_to_color('#CCCCCC'),
},
raised_frame_border_colors = {
[1] = BreitbandGraphics.hex_to_color('#ADADAD'),
[2] = BreitbandGraphics.hex_to_color('#0078D7'),
[3] = BreitbandGraphics.hex_to_color('#005499'),
[0] = BreitbandGraphics.hex_to_color('#BFBFBF'),
},
edit_frame_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[2] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[3] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[0] = BreitbandGraphics.hex_to_color('#FFFFFF'),
},
edit_frame_border_colors = {
[1] = BreitbandGraphics.hex_to_color('#7A7A7A'),
[2] = BreitbandGraphics.hex_to_color('#171717'),
[3] = BreitbandGraphics.hex_to_color('#0078D7'),
[0] = BreitbandGraphics.hex_to_color('#CCCCCC'),
},
list_frame_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[2] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[3] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[0] = BreitbandGraphics.hex_to_color('#FFFFFF'),
},
list_frame_border_colors = {
[1] = BreitbandGraphics.hex_to_color('#7A7A7A'),
[2] = BreitbandGraphics.hex_to_color('#7A7A7A'),
[3] = BreitbandGraphics.hex_to_color('#7A7A7A'),
[0] = BreitbandGraphics.hex_to_color('#7A7A7A'),
},
menu_frame_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#F2F2F2'),
[2] = BreitbandGraphics.hex_to_color('#F2F2F2'),
[3] = BreitbandGraphics.hex_to_color('#F2F2F2'),
[0] = BreitbandGraphics.hex_to_color('#F2F2F2'),
},
menu_frame_border_colors = {
[1] = BreitbandGraphics.hex_to_color('#CCCCCC'),
[2] = BreitbandGraphics.hex_to_color('#CCCCCC'),
[3] = BreitbandGraphics.hex_to_color('#CCCCCC'),
[0] = BreitbandGraphics.hex_to_color('#CCCCCC'),
},
menu_item_text_colors = {
[1] = BreitbandGraphics.hex_to_color('#000000'),
[2] = BreitbandGraphics.hex_to_color('#000000'),
[3] = BreitbandGraphics.hex_to_color('#000000'),
[0] = BreitbandGraphics.hex_to_color('#6D6D6D'),
},
menu_item_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#00000000'),
[2] = BreitbandGraphics.hex_to_color('#91C9F7'),
[3] = BreitbandGraphics.hex_to_color('#91C9F7'),
[0] = BreitbandGraphics.hex_to_color('#00000000'),
},
raised_frame_text_colors = {
[1] = BreitbandGraphics.colors.black,
[2] = BreitbandGraphics.colors.black,
[3] = BreitbandGraphics.colors.black,
[0] = BreitbandGraphics.repeated_to_color(160),
},
edit_frame_text_colors = {
[1] = BreitbandGraphics.colors.black,
[2] = BreitbandGraphics.colors.black,
[3] = BreitbandGraphics.colors.black,
[0] = BreitbandGraphics.repeated_to_color(160),
},
list_text_colors = {
[1] = BreitbandGraphics.colors.black,
[2] = BreitbandGraphics.colors.black,
[3] = BreitbandGraphics.colors.white,
[0] = BreitbandGraphics.repeated_to_color(160),
},
list_item_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[2] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[3] = BreitbandGraphics.hex_to_color('#0078D7'),
[0] = BreitbandGraphics.hex_to_color('#FFFFFF'),
},
joystick_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[2] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[3] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[0] = BreitbandGraphics.hex_to_color('#FFFFFF'),
},
joystick_outline_colors = {
[1] = BreitbandGraphics.hex_to_color('#000000'),
[2] = BreitbandGraphics.hex_to_color('#000000'),
[3] = BreitbandGraphics.hex_to_color('#000000'),
[0] = BreitbandGraphics.hex_to_color('#000000'),
},
joystick_tip_colors = {
[1] = BreitbandGraphics.hex_to_color('#FF0000'),
[2] = BreitbandGraphics.hex_to_color('#FF0000'),
[3] = BreitbandGraphics.hex_to_color('#FF0000'),
[0] = BreitbandGraphics.hex_to_color('#FF8080'),
},
joystick_line_colors = {
[1] = BreitbandGraphics.hex_to_color('#0000FF'),
[2] = BreitbandGraphics.hex_to_color('#0000FF'),
[3] = BreitbandGraphics.hex_to_color('#0000FF'),
[0] = BreitbandGraphics.hex_to_color('#8080FF'),
},
joystick_inner_mag_colors = {
[1] = BreitbandGraphics.hex_to_color('#FF000022'),
[2] = BreitbandGraphics.hex_to_color('#FF000022'),
[3] = BreitbandGraphics.hex_to_color('#FF000022'),
[0] = BreitbandGraphics.hex_to_color('#00000000'),
},
joystick_outer_mag_colors = {
[1] = BreitbandGraphics.hex_to_color('#FF0000'),
[2] = BreitbandGraphics.hex_to_color('#FF0000'),
[3] = BreitbandGraphics.hex_to_color('#FF0000'),
[0] = BreitbandGraphics.hex_to_color('#FF8080'),
},
joystick_mag_thicknesses = {
[1] = 2,
[2] = 2,
[3] = 2,
[0] = 2,
},
scrollbar_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#F0F0F0'),
[2] = BreitbandGraphics.hex_to_color('#F0F0F0'),
[3] = BreitbandGraphics.hex_to_color('#F0F0F0'),
[0] = BreitbandGraphics.hex_to_color('#F0F0F0'),
},
scrollbar_thumb_colors = {
[1] = BreitbandGraphics.hex_to_color('#CDCDCD'),
[2] = BreitbandGraphics.hex_to_color('#A6A6A6'),
[3] = BreitbandGraphics.hex_to_color('#606060'),
[0] = BreitbandGraphics.hex_to_color('#C0C0C0'),
},
trackbar_back_colors = {
[1] = BreitbandGraphics.hex_to_color('#E7EAEA'),
[2] = BreitbandGraphics.hex_to_color('#E7EAEA'),
[3] = BreitbandGraphics.hex_to_color('#E7EAEA'),
[0] = BreitbandGraphics.hex_to_color('#E7EAEA'),
},
trackbar_border_colors = {
[1] = BreitbandGraphics.hex_to_color('#D6D6D6'),
[2] = BreitbandGraphics.hex_to_color('#D6D6D6'),
[3] = BreitbandGraphics.hex_to_color('#D6D6D6'),
[0] = BreitbandGraphics.hex_to_color('#D6D6D6'),
},
trackbar_thumb_colors = {
[1] = BreitbandGraphics.hex_to_color('#007AD9'),
[2] = BreitbandGraphics.hex_to_color('#171717'),
[3] = BreitbandGraphics.hex_to_color('#CCCCCC'),
[0] = BreitbandGraphics.hex_to_color('#CCCCCC'),
},
---Draws an icon with the specified parameters
---The draw_icon implementation may choose to use either the color or visual_state parameter to determine the icon's appearance.
---Therefore, the caller must provide either a color or a visual state, or both.
draw_icon = function(rectangle, color, visual_state, key)
if not color and visual_state then
BreitbandGraphics.fill_rectangle(rectangle, BreitbandGraphics.colors.red)
return
end
if key == 'arrow_left' then
BreitbandGraphics.draw_text(rectangle,
'center',
'center',
{aliased = not ugui.standard_styler.cleartype},
color,
ugui.standard_styler.font_size,
'Segoe UI Mono',
'<')
elseif key == 'arrow_right' then
BreitbandGraphics.draw_text(rectangle,
'center',
'center',
{aliased = not ugui.standard_styler.cleartype},
color,
ugui.standard_styler.font_size,
'Segoe UI Mono',
'>')
elseif key == 'arrow_up' then
BreitbandGraphics.draw_text(rectangle,
'center',
'center',
{aliased = not ugui.standard_styler.cleartype},
color,
ugui.standard_styler.font_size,
'Segoe UI Mono',
'^')
elseif key == 'arrow_down' then
BreitbandGraphics.draw_text(rectangle,
'center',
'center',
{aliased = not ugui.standard_styler.cleartype},
color,
ugui.standard_styler.font_size,
'Segoe UI Mono',
'v')
elseif key == 'checkmark' then
local connection_point = {x = rectangle.x + rectangle.width * 0.3, y = rectangle.y + rectangle.height}
BreitbandGraphics.draw_line({x = rectangle.x, y = rectangle.y + rectangle.height / 2}, connection_point, color, 1)
BreitbandGraphics.draw_line(connection_point, {x = rectangle.x + rectangle.width, y = rectangle.y}, color, 1)
else
-- Unknown icon, probably a good idea to nag the user
BreitbandGraphics.fill_rectangle(rectangle, BreitbandGraphics.colors.red)
end
end,
draw_raised_frame = function(control, visual_state)
BreitbandGraphics.fill_rectangle(control.rectangle,
ugui.standard_styler.raised_frame_border_colors[visual_state])
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(control.rectangle, -1),
ugui.standard_styler.raised_frame_back_colors[visual_state])
end,
draw_edit_frame = function(control, rectangle, visual_state)
BreitbandGraphics.fill_rectangle(control.rectangle,
ugui.standard_styler.edit_frame_border_colors[visual_state])
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(control.rectangle, -1),
ugui.standard_styler.edit_frame_back_colors[visual_state])
end,
draw_list_frame = function(rectangle, visual_state)
BreitbandGraphics.fill_rectangle(rectangle,
ugui.standard_styler.list_frame_border_colors[visual_state])
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(rectangle, -1),
ugui.standard_styler.list_frame_back_colors[visual_state])
end,
draw_joystick_inner = function(rectangle, visual_state, position)
local back_color = ugui.standard_styler.joystick_back_colors[visual_state]
local outline_color = ugui.standard_styler.joystick_outline_colors[visual_state]
local tip_color = ugui.standard_styler.joystick_tip_colors[visual_state]
local line_color = ugui.standard_styler.joystick_line_colors[visual_state]
local inner_mag_color = ugui.standard_styler.joystick_inner_mag_colors[visual_state]
local outer_mag_color = ugui.standard_styler.joystick_outer_mag_colors[visual_state]
local mag_thickness = ugui.standard_styler.joystick_mag_thicknesses[visual_state]
BreitbandGraphics.fill_ellipse(BreitbandGraphics.inflate_rectangle(rectangle, -1),
back_color)
BreitbandGraphics.draw_ellipse(BreitbandGraphics.inflate_rectangle(rectangle, -1),
outline_color, 1)
BreitbandGraphics.draw_line({
x = rectangle.x + rectangle.width / 2,
y = rectangle.y,
}, {
x = rectangle.x + rectangle.width / 2,
y = rectangle.y + rectangle.height,
}, outline_color, 1)
BreitbandGraphics.draw_line({
x = rectangle.x,
y = rectangle.y + rectangle.height / 2,
}, {
x = rectangle.x + rectangle.width,
y = rectangle.y + rectangle.height / 2,
}, outline_color, 1)
local r = position.r - mag_thickness
if r > 0 then
BreitbandGraphics.fill_ellipse({
x = rectangle.x + rectangle.width / 2 - r / 2,
y = rectangle.y + rectangle.height / 2 - r / 2,
width = r,
height = r,
}, inner_mag_color)
r = position.r
BreitbandGraphics.draw_ellipse({
x = rectangle.x + rectangle.width / 2 - r / 2,
y = rectangle.y + rectangle.height / 2 - r / 2,
width = r,
height = r,
}, outer_mag_color, mag_thickness)
end
BreitbandGraphics.draw_line({
x = rectangle.x + rectangle.width / 2,
y = rectangle.y + rectangle.height / 2,
}, {
x = position.x,
y = position.y,
}, line_color, 3)
BreitbandGraphics.fill_ellipse({
x = position.x - ugui.standard_styler.joystick_tip_size / 2,
y = position.y - ugui.standard_styler.joystick_tip_size / 2,
width = ugui.standard_styler.joystick_tip_size,
height = ugui.standard_styler.joystick_tip_size,
}, tip_color)
end,
draw_scrollbar = function(container_rectangle, thumb_rectangle, visual_state)
BreitbandGraphics.fill_rectangle(container_rectangle,
ugui.standard_styler.scrollbar_back_colors[visual_state])
BreitbandGraphics.fill_rectangle(thumb_rectangle,
ugui.standard_styler.scrollbar_thumb_colors[visual_state])
end,
draw_list_item = function(item, rectangle, visual_state)
if not item then
return
end
BreitbandGraphics.fill_rectangle(rectangle,
ugui.standard_styler.list_item_back_colors[visual_state])
local size = BreitbandGraphics.get_text_size(item, ugui.standard_styler.font_size,
ugui.standard_styler.font_name)
BreitbandGraphics.draw_text({
x = rectangle.x + 2,
y = rectangle.y,
width = size.width * 2,
height = rectangle.height,
}, 'start', 'center', {aliased = not ugui.standard_styler.cleartype},
ugui.standard_styler.list_text_colors[visual_state],
ugui.standard_styler.font_size,
ugui.standard_styler.font_name,
item)
end,
draw_list = function(control, rectangle)
local visual_state = ugui.get_visual_state(control)
ugui.standard_styler.draw_list_frame(rectangle, visual_state)
local content_bounds = ugui.standard_styler.get_desired_listbox_content_bounds(control)
-- item y position:
-- y = (20 * (i - 1)) - (scroll_y * ((20 * #control.items) - control.rectangle.height))
local scroll_x = ugui.internal.control_data[control.uid].scroll_x and
ugui.internal.control_data[control.uid].scroll_x or 0
local scroll_y = ugui.internal.control_data[control.uid].scroll_y and
ugui.internal.control_data[control.uid].scroll_y or 0
local index_begin = (scroll_y *
(content_bounds.height - rectangle.height)) /
ugui.standard_styler.item_height
local index_end = (rectangle.height + (scroll_y *
(content_bounds.height - rectangle.height))) /
ugui.standard_styler.item_height
index_begin = ugui.internal.clamp(math.floor(index_begin), 1, #control.items)
index_end = ugui.internal.clamp(math.ceil(index_end), 1, #control.items)
local x_offset = math.max((content_bounds.width - control.rectangle.width) * scroll_x, 0)
BreitbandGraphics.push_clip(BreitbandGraphics.inflate_rectangle(rectangle, -1))
for i = index_begin, index_end, 1 do
local y_offset = (ugui.standard_styler.item_height * (i - 1)) -
(scroll_y * (content_bounds.height - rectangle.height))
local item_visual_state = ugui.visual_states.normal
if control.is_enabled == false then
item_visual_state = ugui.visual_states.disabled
end
if control.selected_index == i then
item_visual_state = ugui.visual_states.active
end
ugui.standard_styler.draw_list_item(control.items[i], {
x = rectangle.x - x_offset,
y = rectangle.y + y_offset,
width = math.max(content_bounds.width, control.rectangle.width),
height = ugui.standard_styler.item_height,
}, item_visual_state)
end
BreitbandGraphics.pop_clip()
end,
draw_menu_frame = function(rectangle, visual_state)
BreitbandGraphics.fill_rectangle(rectangle,
ugui.standard_styler.menu_frame_border_colors[visual_state])
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(rectangle, -1),
ugui.standard_styler.menu_frame_back_colors[visual_state])
end,
draw_menu_item = function(item, rectangle, visual_state)
BreitbandGraphics.fill_rectangle(rectangle,
ugui.standard_styler.menu_item_back_colors[visual_state])
BreitbandGraphics.push_clip({
x = rectangle.x,
y = rectangle.y,
width = rectangle.width,
height = rectangle.height,
})
if item.checked then
local icon_rect = BreitbandGraphics.inflate_rectangle({
x = rectangle.x + (ugui.standard_styler.menu_item_left_padding - rectangle.height) * 0.5,
y = rectangle.y,
width = rectangle.height,
height = rectangle.height,
}, -7)
ugui.standard_styler.draw_icon(icon_rect, ugui.standard_styler.menu_item_text_colors[visual_state], nil, 'checkmark')
end
if item.items then
local icon_rect = BreitbandGraphics.inflate_rectangle({
x = rectangle.x + rectangle.width - (ugui.standard_styler.menu_item_right_padding),
y = rectangle.y,
width = ugui.standard_styler.menu_item_right_padding,
height = rectangle.height,
}, -7)
ugui.standard_styler.draw_icon(icon_rect, ugui.standard_styler.menu_item_text_colors[visual_state], nil, 'arrow_right')
end
BreitbandGraphics.draw_text({
x = rectangle.x + ugui.standard_styler.menu_item_left_padding,
y = rectangle.y,
width = 9999999,
height = rectangle.height,
}, 'start', 'center', {aliased = not ugui.standard_styler.cleartype},
ugui.standard_styler.menu_item_text_colors[visual_state],
ugui.standard_styler.font_size,
ugui.standard_styler.font_name,
item.text)
BreitbandGraphics.pop_clip()
end,
draw_menu = function(control, rectangle)
local visual_state = ugui.get_visual_state(control)
ugui.standard_styler.draw_menu_frame(rectangle, visual_state)
local y = rectangle.y
for i, item in pairs(control.items) do
local rectangle = BreitbandGraphics.inflate_rectangle({
x = rectangle.x,
y = y,
width = rectangle.width,
height = ugui.standard_styler.menu_item_height,
}, -1)
local visual_state = ugui.visual_states.normal
if ugui.internal.control_data[control.uid].hovered_index and ugui.internal.control_data[control.uid].hovered_index == i then
visual_state = ugui.visual_states.hovered
end
if item.enabled == false then
visual_state = ugui.visual_states.disabled
end
ugui.standard_styler.draw_menu_item(item, rectangle, visual_state)
y = y + ugui.standard_styler.menu_item_height
end
end,
draw_button = function(control)
local visual_state = ugui.get_visual_state(control)
-- override for toggle_button
if control.is_checked and control.is_enabled ~= false then
visual_state = ugui.visual_states.active
end
ugui.standard_styler.draw_raised_frame(control, visual_state)
BreitbandGraphics.draw_text(control.rectangle, 'center', 'center',
{clip = true, aliased = not ugui.standard_styler.cleartype},
ugui.standard_styler.raised_frame_text_colors[visual_state],
ugui.standard_styler.font_size,
ugui.standard_styler.font_name, control.text)
end,
draw_togglebutton = function(control)
ugui.standard_styler.draw_button(control)
end,
draw_carrousel_button = function(control)
-- add a "fake" text field
local copy = ugui.internal.deep_clone(control)
copy.text = control.items and control.items[control.selected_index] or ''
ugui.standard_styler.draw_button(copy)
local visual_state = ugui.get_visual_state(control)
-- draw the arrows
ugui.standard_styler.draw_icon({
x = control.rectangle.x + ugui.standard_styler.textbox_padding,
y = control.rectangle.y,
width = ugui.standard_styler.icon_size,
height = control.rectangle.height,
}, ugui.standard_styler.raised_frame_text_colors[visual_state], visual_state, 'arrow_left')
ugui.standard_styler.draw_icon({
x = control.rectangle.x + control.rectangle.width - ugui.standard_styler.textbox_padding -
ugui.standard_styler.icon_size,
y = control.rectangle.y,
width = ugui.standard_styler.icon_size,
height = control.rectangle.height,
}, ugui.standard_styler.raised_frame_text_colors[visual_state], visual_state, 'arrow_right')
end,
draw_textbox = function(control)
local visual_state = ugui.get_visual_state(control)
local text = control.text or ''
if ugui.internal.active_control == control.uid and control.is_enabled ~= false then
visual_state = ugui.visual_states.active
end
ugui.standard_styler.draw_edit_frame(control, control.rectangle, visual_state)
local should_visualize_selection = not (ugui.internal.control_data[control.uid].selection_start == nil) and
not (ugui.internal.control_data[control.uid].selection_end == nil) and
control.is_enabled ~= false and
not (ugui.internal.control_data[control.uid].selection_start == ugui.internal.control_data[control.uid].selection_end)
if should_visualize_selection then
local string_to_selection_start = text:sub(1,
ugui.internal.control_data[control.uid].selection_start - 1)
local string_to_selection_end = text:sub(1,
ugui.internal.control_data[control.uid].selection_end - 1)
BreitbandGraphics.fill_rectangle({
x = control.rectangle.x +
BreitbandGraphics.get_text_size(string_to_selection_start,
ugui.standard_styler.font_size,
ugui.standard_styler.font_name)
.width + ugui.standard_styler.textbox_padding,
y = control.rectangle.y,
width = BreitbandGraphics.get_text_size(string_to_selection_end,
ugui.standard_styler.font_size,
ugui.standard_styler.font_name)
.width -
BreitbandGraphics.get_text_size(string_to_selection_start,
ugui.standard_styler.font_size,
ugui.standard_styler.font_name)
.width,
height = control.rectangle.height,
},
BreitbandGraphics.hex_to_color('#0078D7'))
end
BreitbandGraphics.draw_text({
x = control.rectangle.x + ugui.standard_styler.textbox_padding,
y = control.rectangle.y,
width = control.rectangle.width - ugui.standard_styler.textbox_padding * 2,
height = control.rectangle.height,
}, 'start', 'start', {clip = true, aliased = not ugui.standard_styler.cleartype},
ugui.standard_styler.edit_frame_text_colors[visual_state],
ugui.standard_styler.font_size,
ugui.standard_styler.font_name, text)
if should_visualize_selection then
local lower = ugui.internal.control_data[control.uid].selection_start
local higher = ugui.internal.control_data[control.uid].selection_end
if ugui.internal.control_data[control.uid].selection_start > ugui.internal.control_data[control.uid].selection_end then
lower = ugui.internal.control_data[control.uid].selection_end
higher = ugui.internal.control_data[control.uid].selection_start
end
local string_to_selection_start = text:sub(1,
lower - 1)
local string_to_selection_end = text:sub(1,
higher - 1)
local selection_start_x = control.rectangle.x +
BreitbandGraphics.get_text_size(string_to_selection_start,
ugui.standard_styler.font_size,
ugui.standard_styler.font_name).width +
ugui.standard_styler.textbox_padding
local selection_end_x = control.rectangle.x +
BreitbandGraphics.get_text_size(string_to_selection_end,
ugui.standard_styler.font_size,
ugui.standard_styler.font_name).width +
ugui.standard_styler.textbox_padding
BreitbandGraphics.push_clip({
x = selection_start_x,
y = control.rectangle.y,
width = selection_end_x - selection_start_x,
height = control.rectangle.height,
})
BreitbandGraphics.draw_text({
x = control.rectangle.x + ugui.standard_styler.textbox_padding,
y = control.rectangle.y,
width = control.rectangle.width - ugui.standard_styler.textbox_padding * 2,
height = control.rectangle.height,
}, 'start', 'start', {clip = true, aliased = not ugui.standard_styler.cleartype},
BreitbandGraphics.invert_color(ugui.standard_styler.edit_frame_text_colors
[visual_state]),
ugui.standard_styler.font_size,
ugui.standard_styler.font_name, text)
BreitbandGraphics.pop_clip()
end
local string_to_caret = text:sub(1, ugui.internal.control_data[control.uid].caret_index - 1)
local caret_x = BreitbandGraphics.get_text_size(string_to_caret,
ugui.standard_styler.font_size,
ugui.standard_styler.font_name).width +
ugui.standard_styler.textbox_padding
if visual_state == ugui.visual_states.active and math.floor(os.clock() * 2) % 2 == 0 and not should_visualize_selection then
BreitbandGraphics.draw_line({
x = control.rectangle.x + caret_x,
y = control.rectangle.y + 2,
}, {
x = control.rectangle.x + caret_x,
y = control.rectangle.y +
math.max(15,
BreitbandGraphics.get_text_size(string_to_caret, 12,
ugui.standard_styler.font_name)
.height), -- TODO: move text measurement into BreitbandGraphics
}, {
r = 0,
g = 0,
b = 0,
}, 1)
end
end,
draw_joystick = function(control)
local visual_state = ugui.get_visual_state(control)
local x = control.position and control.position.x or 0
local y = control.position and control.position.y or 0
local mag = control.mag or 0
-- joystick has no hover or active states
if not (visual_state == ugui.visual_states.disabled) then
visual_state = ugui.visual_states.normal
end
ugui.standard_styler.draw_raised_frame(control, visual_state)
ugui.standard_styler.draw_joystick_inner(control.rectangle, visual_state, {
x = ugui.internal.remap(ugui.internal.clamp(x, -128, 128), -128, 128,
control.rectangle.x, control.rectangle.x + control.rectangle.width),
y = ugui.internal.remap(ugui.internal.clamp(y, -128, 128), -128, 128,
control.rectangle.y, control.rectangle.y + control.rectangle.height),
r = ugui.internal.remap(ugui.internal.clamp(mag, 0, 128), 0, 128, 0,
math.min(control.rectangle.width, control.rectangle.height)),
})
end,
draw_track = function(control, visual_state, is_horizontal)
local track_rectangle = {}
if not is_horizontal then
track_rectangle = {
x = control.rectangle.x + control.rectangle.width / 2 -
ugui.standard_styler.track_thickness / 2,
y = control.rectangle.y,
width = ugui.standard_styler.track_thickness,
height = control.rectangle.height,
}
else
track_rectangle = {
x = control.rectangle.x,
y = control.rectangle.y + control.rectangle.height / 2 -
ugui.standard_styler.track_thickness / 2,
width = control.rectangle.width,
height = ugui.standard_styler.track_thickness,
}
end
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(track_rectangle, 1),
ugui.standard_styler.trackbar_border_colors[visual_state])
BreitbandGraphics.fill_rectangle(track_rectangle,
ugui.standard_styler.trackbar_back_colors[visual_state])
end,
draw_thumb = function(control, visual_state, is_horizontal, value)
local head_rectangle = {}
local effective_bar_height = math.min(
(is_horizontal and control.rectangle.height or control.rectangle.width) * 2,
ugui.standard_styler.bar_height)
if not is_horizontal then
head_rectangle = {
x = control.rectangle.x + control.rectangle.width / 2 -
effective_bar_height / 2,
y = control.rectangle.y + (value * control.rectangle.height) -
ugui.standard_styler.bar_width / 2,
width = effective_bar_height,
height = ugui.standard_styler.bar_width,
}
else
head_rectangle = {
x = control.rectangle.x + (value * control.rectangle.width) -
ugui.standard_styler.bar_width / 2,
y = control.rectangle.y + control.rectangle.height / 2 -
effective_bar_height / 2,
width = ugui.standard_styler.bar_width,
height = effective_bar_height,
}
end
BreitbandGraphics.fill_rectangle(head_rectangle,
ugui.standard_styler.trackbar_thumb_colors[visual_state])
end,
draw_trackbar = function(control)
local visual_state = ugui.get_visual_state(control)
if ugui.internal.active_control == control.uid and control.is_enabled ~= false then
visual_state = ugui.visual_states.active
end
local is_horizontal = control.rectangle.width > control.rectangle.height
ugui.standard_styler.draw_track(control, visual_state, is_horizontal)
ugui.standard_styler.draw_thumb(control, visual_state, is_horizontal, control
.value)
end,
draw_combobox = function(control)
local visual_state = ugui.get_visual_state(control)
local selected_item = control.items and (control.selected_index and control.items[control.selected_index] or '') or ''
if ugui.internal.control_data[control.uid].is_open and control.is_enabled ~= false then
visual_state = ugui.visual_states.active
end
ugui.standard_styler.draw_raised_frame(control, visual_state)
local text_color = ugui.standard_styler.raised_frame_text_colors[visual_state]
BreitbandGraphics.draw_text({
x = control.rectangle.x + ugui.standard_styler.textbox_padding * 2,
y = control.rectangle.y,
width = control.rectangle.width,