-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmupen-lua-ugui.lua
2187 lines (1898 loc) · 99 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 2.0.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')
---@alias UID number
---Unique identifier for a control. Must be unique within a frame.
---@alias ControlRectangleComputationDelegate fun(layout: LayoutSection, index: integer, control: Control): Rectangle
---A function which computes the rectangle for a control using the specified layout section.
---@class Environment
---@field public mouse_position { x: number, y: number } The mouse position.
---@field public wheel number The mouse wheel delta.
---@field public is_primary_down boolean? Whether the primary mouse button is being pressed.
---@field public held_keys table<string, boolean> A map of held key identifiers to booleans. A key not being present or its value being 'false' means it is not held.
---@field public window_size { x: number, y: number }? The rendering bounds. If nil, no rendering bounds are considered and certain controls, such as menus, might overflow off-screen.
---@class LayoutSection
---@field public rectangle { x: number?, y: number?, width: number?, height: number? }? The rectangle region of the layout section. Depending on the section type, some components might be ignored. If nil or any contained field is nil, the field or the entire rectangle will be replaced with the corresponding value from the current state of the layout operation.
---@field package compute_control_rectangle ControlRectangleComputationDelegate? The layout section's bounds internal computation function. Doesn't need to be provided by external callers utilizing the layout section API via push_xyz().
---@field package data any? The layout section's internal data. Doesn't need to be provided by external callers utilizing the layout section API via push_xyz().
---The base class for all layout sections.
---@class StackPanel : LayoutSection
---@field public horizontal boolean? Whether the layout flow is horizontal. If nil, false is assumed.
---@field public gap number? The gap between elements. If nil, 0 is assumed.
---A StackPanel layout section, which orders its elements sequentially on the specified axis, adding gaps inbetween if specified.
---@class Control
---@field public uid UID The unique identifier of the control.
---@field public rectangle Rectangle The rectangle in which the control is drawn.
---@field public is_enabled boolean? Whether the control is enabled. If nil or true, the control is enabled.
---@field package topmost boolean? Whether the control is drawn at the end of the frame, after all other controls.
---The base class for all controls.
---@class Button : Control
---@field public text string The text displayed on the button.
---A button which can be clicked.
---@class ToggleButton : Button
---@field public is_checked boolean Whether the button is checked.
---A button which can be toggled on and off.
---@class CarrouselButton : Control
---@field public items string[] The items contained in the carrousel button.
---@field public selected_index integer The index of the currently selected item into the items array.
---A button which can be toggled on and off.
---TODO: Make wraparound optional
---@class TextBox : Control
---@field public text string The text contained in the textbox.
---A textbox which can be edited.
---@class Joystick : Control
---@field public position Vector2 The joystick's position with the range 0-128 on both axes.
---@field public mag number? The joystick's magnitude circle radius with the range `0-128`. If nil, no magnitude circle will be drawn.
---A joystick which can be interacted with.
---@class Trackbar : Control
---@field public value number The current value in the range 0-1.
---A trackbar which can have its value adjusted.
---@class ComboBox : Control
---@field public items string[] The items contained in the control.
---@field public selected_index integer The index of the currently selected item into the items array.
---A combobox which allows the user to choose from a list of items.
---@class ListBox : Control
---@field public items string[] The items contained in the control.
---@field public selected_index integer The index of the currently selected item into the items array.
---@field public horizontal_scroll boolean? Whether horizontal scrolling will be enabled when items go beyond the width of the control. Will impact performance greatly, use with care.
---A listbox which allows the user to choose from a list of items.
---If the items don't fit in the control's bounds vertically, vertical scrolling will be enabled.
---If the items don't fit in the control's bounds horizontally, horizontal scrolling will be enabled if horizontal_scroll is true.
---@class ScrollBar : Control
---@field public value number The scroll proportion in the range 0-1.
---@field public ratio number The overflow ratio, which is calculated by dividing the desired content dimensions by the relevant attached control's (e.g.: a listbox's) dimensions.
---A scrollbar which allows scrolling horizontally or vertically, depending on the control's dimensions.
---@class MenuItem
---@field public items MenuItem[]? The item's child items. If nil or empty, the item has no child items and is clickable.
---@field public enabled boolean? Whether the item is enabled. If nil or true, the item is enabled.
---@field public checked boolean? Whether the item is checked. If true, the item is checked.
---@field public text string The item's text.
---Represents an item inside of a Menu.
---@class MenuResult
---@field public item MenuItem? The item that was clicked, or nil if none was.
---@field public dismissed boolean Whether the menu was dismissed by clicking outside of it.
---@class Menu : Control
---@field public items MenuItem[] The items contained in the menu.
---A menu, which allows the user to choose from a list of items.
ugui = {
internal = {
---@type table<UID, any>
---Map of control UIDs to their data.
control_data = {},
---@type Environment
---The environment for the current frame.
environment = nil,
---@type Environment
---The environment for the previous frame.
previous_environment = nil,
---@type Vector2
-- The position of the mouse the last time the primary button was pressed.
mouse_down_position = {x = 0, y = 0},
---@type UID|nil
-- UID of the currently active control.
active_control = nil,
---@type boolean
-- Whether the active control will be reset to nil after the mouse is released.
clear_active_control_after_mouse_up = true,
---@type Rectangle[]
-- Rectangles which are excluded from hittesting (e.g.: the popped up list of a combobox)
hittest_free_rects = {},
---@type function[]
-- Functions which will be called at the end of the frame. This array is reset when a new frame begins.
late_callbacks = {},
---@type { [UID]: boolean }
-- Map of uids used in an active section (between begin_frame and end_frame). Used to prevent uid collisions.
used_uids = {},
---@type LayoutSection[]
---The current layout stack.
layout_stack = {},
---@type Rectangle
---The rectangle of the most recently shown control. Reset on frame end.
last_control_rectangle = nil,
---Whether a frame is currently in progress.
frame_in_progress = false,
---Validates the structure of a control. Must be called in every control function.
---@param control Control 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 Control 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] = true
ugui.internal.last_control_rectangle = control.rectangle
end,
---Deeply clones a table.
---@param obj table The table to clone.
---@param seen table? Internal. Pass nil as a caller.
---@return table A cloned instance of the table.
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,
---Removes a range of characters from a string.
---@param string string The string to remove characters from.
---@param start_index integer The index of the first character to remove.
---@param end_index integer The index of the last character to remove.
---@return string # A new string with the characters removed.
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,
---@return boolean # Whether LMB was just pressed.
is_mouse_just_down = function()
local value = ugui.internal.environment.is_primary_down and not ugui.internal.previous_environment.is_primary_down
return value and true or false
end,
---@return boolean # Whether LMB was just released.
is_mouse_just_up = function()
local value = not ugui.internal.environment.is_primary_down and ugui.internal.previous_environment.is_primary_down
return value and true or false
end,
---@return boolean # Whether the mouse wheel was just moved up.
is_mouse_wheel_up = function()
return ugui.internal.environment.wheel == 1
end,
---@return boolean # Whether the mouse wheel was just moved down.
is_mouse_wheel_down = function()
return ugui.internal.environment.wheel == -1
end,
---Removes the character at the specified index from a string.
---@param string string The string to remove the character from.
---@param index integer The index of the character to remove.
---@return string # A new string with the character removed.
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,
---Inserts a string into another string at the specified index.
---@param string string The original string to insert the other string into.
---@param string2 string The other string.
---@param index integer The index into the first string to begin inserting the second string at.
---@return string # A new string with the other string inserted.
insert_at = function(string, string2, index)
return string:sub(1, index) .. string2 .. string:sub(index + string2:len(), string:len())
end,
---Remaps a value from one range to another.
---@param value number The value.
---@param from1 number The lower bound of the first range.
---@param to1 number The upper bound of the first range.
---@param from2 number The lower bound of the second range.
---@param to2 number The upper bound of the second range.
---@return number # The new remapped value.
remap = function(value, from1, to1, from2, to2)
return (value - from1) / (to1 - from1) * (to2 - from2) + from2
end,
---Limits a value to a range.
---@param value number The value.
---@param min number The lower bound.
---@param max number The upper bound.
---@return number # The new limited value.
clamp = function(value, min, max)
-- FIXME: Remove this nil check, deal with the fallout.
if value == nil then
return value
end
return math.max(math.min(value, max), min)
end,
---Gets all the keys that are newly pressed since the last frame.
---@return table<string, boolean> # The newly pressed keys.
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,
---Processes clicking on a control.
---@param control Control A control.
---@return boolean # Whether the control was clicked.
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,
---Gets the character index for the specified relative x position in a textbox.
---Considers font_size and font_name, as provided by the styler.
---@param text string The textbox's text.
---@param relative_x number The relative x position.
---@return integer The character index.
---FIXME: This should be moved to BreitbandGraphics!!!
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.params.font_size,
ugui.standard_styler.params.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,
---@class TextBoxNavigationKeyProcessingResult
---@field public handled boolean Whether the key press was handled.
---@field public text string? The new textbox text.
---@field public selection_start integer? The new textbox selection start index.
---@field public selection_end integer? The new textbox selection end index.
---@field public caret_index integer? The new textbox caret index.
---Handles navigation key presses in a textbox.
---@param key string The pressed key identifier.
---@param has_selection boolean Whether the textbox has a selection.
---@param text string The textbox's text.
---@param selection_start integer The textbox selection start index.
---@param selection_end integer The textbox selection end index.
---@param caret_index integer The textbox caret index.
---@return TextBoxNavigationKeyProcessingResult # The result of the navigation key press processing.
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,
---Performs the required layout operations on the specified control.
---@param control Control The control table. Its contents may be mutated.
do_layout = function (control)
for i = 1, #ugui.internal.layout_stack, 1 do
local section = ugui.internal.layout_stack[i]
control.rectangle = section:compute_control_rectangle(i, control)
end
end,
},
---@type table<string, ControlRectangleComputationDelegate>
---Map of layout section names to their control rectangle computation delegates.
layout_computation_functions = {
['StackPanel'] = function(layout, index, control)
---@cast layout StackPanel
if index ~= #ugui.internal.layout_stack then
return control.rectangle
end
if not layout.data then
layout.data = {x = layout.rectangle.x, y = layout.rectangle.y, i = 1}
end
local rectangle = ugui.internal.deep_clone(control.rectangle)
local gap = layout.gap or 0
local function add_accumulators()
if layout.horizontal then
layout.data.x = layout.data.x + rectangle.width + gap
else
layout.data.y = layout.data.y + rectangle.height + gap
end
end
if (layout.horizontal and layout.rectangle.width ~= 0)
or (not layout.horizontal and layout.rectangle.height ~= 0) then
add_accumulators()
end
rectangle.x = layout.data.x
rectangle.y = layout.data.y
layout.data.i = layout.data.i + 1
if (layout.horizontal and layout.rectangle.width == 0)
or (not layout.horizontal and layout.rectangle.height == 0) then
add_accumulators()
end
return rectangle
end,
},
---@enum VisualState
-- The possible states of a control, which are used by the styler for drawing.
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 control is currently capturing inputs.
active = 3,
},
---Gets the basic visual state of a control.
---@param control Control The control.
---@return VisualState # The control's 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,
--- The standard style implementation, which is responsible for drawing controls.
standard_styler = {
--- The styler parameters, which determine how controls are drawn.
params = {
--- Whether font filtering is enabled.
cleartype = true,
--- The font name.
font_name = 'MS Shell Dlg 2',
--- The monospace variant font name.
monospace_font_name = 'Consolas',
--- The font size.
font_size = 12,
--- The icon size.
icon_size = 12,
button = {
back = {
[1] = BreitbandGraphics.hex_to_color('#E1E1E1'),
[2] = BreitbandGraphics.hex_to_color('#E5F1FB'),
[3] = BreitbandGraphics.hex_to_color('#CCE4F7'),
[0] = BreitbandGraphics.hex_to_color('#CCCCCC'),
},
border = {
[1] = BreitbandGraphics.hex_to_color('#ADADAD'),
[2] = BreitbandGraphics.hex_to_color('#0078D7'),
[3] = BreitbandGraphics.hex_to_color('#005499'),
[0] = BreitbandGraphics.hex_to_color('#BFBFBF'),
},
text = {
[1] = BreitbandGraphics.hex_to_color('#000000'),
[2] = BreitbandGraphics.hex_to_color('#000000'),
[3] = BreitbandGraphics.hex_to_color('#000000'),
[0] = BreitbandGraphics.hex_to_color('#A0A0A0'),
},
},
textbox = {
padding = {x = 2, y = 0},
back = {
[1] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[2] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[3] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[0] = BreitbandGraphics.hex_to_color('#FFFFFF'),
},
border = {
[1] = BreitbandGraphics.hex_to_color('#7A7A7A'),
[2] = BreitbandGraphics.hex_to_color('#171717'),
[3] = BreitbandGraphics.hex_to_color('#0078D7'),
[0] = BreitbandGraphics.hex_to_color('#CCCCCC'),
},
text = {
[1] = BreitbandGraphics.hex_to_color('#000000'),
[2] = BreitbandGraphics.hex_to_color('#000000'),
[3] = BreitbandGraphics.hex_to_color('#000000'),
[0] = BreitbandGraphics.hex_to_color('#A0A0A0'),
},
},
listbox = {
back = {
[1] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[2] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[3] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[0] = BreitbandGraphics.hex_to_color('#FFFFFF'),
},
border = {
[1] = BreitbandGraphics.hex_to_color('#7A7A7A'),
[2] = BreitbandGraphics.hex_to_color('#7A7A7A'),
[3] = BreitbandGraphics.hex_to_color('#7A7A7A'),
[0] = BreitbandGraphics.hex_to_color('#7A7A7A'),
},
},
listbox_item = {
height = 15,
back = {
[1] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[2] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[3] = BreitbandGraphics.hex_to_color('#0078D7'),
[0] = BreitbandGraphics.hex_to_color('#FFFFFF'),
},
text = {
[1] = BreitbandGraphics.hex_to_color('#000000'),
[2] = BreitbandGraphics.hex_to_color('#000000'),
[3] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[0] = BreitbandGraphics.hex_to_color('#A0A0A0'),
},
},
menu = {
overlap_size = 3,
back = {
[1] = BreitbandGraphics.hex_to_color('#F2F2F2'),
[2] = BreitbandGraphics.hex_to_color('#F2F2F2'),
[3] = BreitbandGraphics.hex_to_color('#F2F2F2'),
[0] = BreitbandGraphics.hex_to_color('#F2F2F2'),
},
border = {
[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 = {
height = 22,
left_padding = 32,
right_padding = 32,
back = {
[1] = BreitbandGraphics.hex_to_color('#00000000'),
[2] = BreitbandGraphics.hex_to_color('#91C9F7'),
[3] = BreitbandGraphics.hex_to_color('#91C9F7'),
[0] = BreitbandGraphics.hex_to_color('#00000000'),
},
border = {
[1] = BreitbandGraphics.hex_to_color('#CCCCCC'),
[2] = BreitbandGraphics.hex_to_color('#CCCCCC'),
[3] = BreitbandGraphics.hex_to_color('#CCCCCC'),
[0] = BreitbandGraphics.hex_to_color('#CCCCCC'),
},
text = {
[1] = BreitbandGraphics.hex_to_color('#000000'),
[2] = BreitbandGraphics.hex_to_color('#000000'),
[3] = BreitbandGraphics.hex_to_color('#000000'),
[0] = BreitbandGraphics.hex_to_color('#6D6D6D'),
},
},
joystick = {
tip_size = 8,
back = {
[1] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[2] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[3] = BreitbandGraphics.hex_to_color('#FFFFFF'),
[0] = BreitbandGraphics.hex_to_color('#FFFFFF'),
},
outline = {
[1] = BreitbandGraphics.hex_to_color('#000000'),
[2] = BreitbandGraphics.hex_to_color('#000000'),
[3] = BreitbandGraphics.hex_to_color('#000000'),
[0] = BreitbandGraphics.hex_to_color('#000000'),
},
tip = {
[1] = BreitbandGraphics.hex_to_color('#FF0000'),
[2] = BreitbandGraphics.hex_to_color('#FF0000'),
[3] = BreitbandGraphics.hex_to_color('#FF0000'),
[0] = BreitbandGraphics.hex_to_color('#FF8080'),
},
line = {
[1] = BreitbandGraphics.hex_to_color('#0000FF'),
[2] = BreitbandGraphics.hex_to_color('#0000FF'),
[3] = BreitbandGraphics.hex_to_color('#0000FF'),
[0] = BreitbandGraphics.hex_to_color('#8080FF'),
},
inner_mag = {
[1] = BreitbandGraphics.hex_to_color('#FF000022'),
[2] = BreitbandGraphics.hex_to_color('#FF000022'),
[3] = BreitbandGraphics.hex_to_color('#FF000022'),
[0] = BreitbandGraphics.hex_to_color('#00000000'),
},
outer_mag = {
[1] = BreitbandGraphics.hex_to_color('#FF0000'),
[2] = BreitbandGraphics.hex_to_color('#FF0000'),
[3] = BreitbandGraphics.hex_to_color('#FF0000'),
[0] = BreitbandGraphics.hex_to_color('#FF8080'),
},
mag_thicknesses = {
[1] = 2,
[2] = 2,
[3] = 2,
[0] = 2,
},
},
scrollbar = {
thickness = 17,
back = {
[1] = BreitbandGraphics.hex_to_color('#F0F0F0'),
[2] = BreitbandGraphics.hex_to_color('#F0F0F0'),
[3] = BreitbandGraphics.hex_to_color('#F0F0F0'),
[0] = BreitbandGraphics.hex_to_color('#F0F0F0'),
},
thumb = {
[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 = {
track_thickness = 2,
bar_width = 6,
bar_height = 16,
back = {
[1] = BreitbandGraphics.hex_to_color('#E7EAEA'),
[2] = BreitbandGraphics.hex_to_color('#E7EAEA'),
[3] = BreitbandGraphics.hex_to_color('#E7EAEA'),
[0] = BreitbandGraphics.hex_to_color('#E7EAEA'),
},
border = {
[1] = BreitbandGraphics.hex_to_color('#D6D6D6'),
[2] = BreitbandGraphics.hex_to_color('#D6D6D6'),
[3] = BreitbandGraphics.hex_to_color('#D6D6D6'),
[0] = BreitbandGraphics.hex_to_color('#D6D6D6'),
},
thumb = {
[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.
---@param rectangle Rectangle The icon's bounds.
---@param color Color? The icon's fill color.
---@param visual_state VisualState? The icon's visual state.
---@param key string The icon's identifier.
draw_icon = function(rectangle, color, visual_state, key)
-- NOTE: visual_state is not utilized by the standard implementation of draw_icon.
if not color then
BreitbandGraphics.fill_rectangle(rectangle, BreitbandGraphics.colors.red)
return
end
local font_name = 'Segoe UI Mono'
local font_size = ugui.standard_styler.params.font_size
if key == 'arrow_left' then
BreitbandGraphics.draw_text2({
text = '<',
rectangle = rectangle,
color = color,
font_name = font_name,
font_size = font_size,
aliased = not ugui.standard_styler.params.cleartype,
})
elseif key == 'arrow_right' then
BreitbandGraphics.draw_text2({
text = '>',
rectangle = rectangle,
color = color,
font_name = font_name,
font_size = font_size,
aliased = not ugui.standard_styler.params.cleartype,
})
elseif key == 'arrow_up' then
BreitbandGraphics.draw_text2({
text = '^',
rectangle = rectangle,
color = color,
font_name = font_name,
font_size = font_size,
aliased = not ugui.standard_styler.params.cleartype,
})
elseif key == 'arrow_down' then
BreitbandGraphics.draw_text2({
text = 'v',
rectangle = rectangle,
color = color,
font_name = font_name,
font_size = font_size,
aliased = not ugui.standard_styler.params.cleartype,
})
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,
---Draws a raised frame with the specified parameters.
---@param control Control The control table.
---@param visual_state VisualState The control's visual state.
draw_raised_frame = function(control, visual_state)
BreitbandGraphics.fill_rectangle(control.rectangle,
ugui.standard_styler.params.button.border[visual_state])
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(control.rectangle, -1),
ugui.standard_styler.params.button.back[visual_state])
end,
---Draws an edit frame with the specified parameters.
---@param control Control The control table.
---@param visual_state VisualState The control's visual state.
draw_edit_frame = function(control, rectangle, visual_state)
BreitbandGraphics.fill_rectangle(control.rectangle,
ugui.standard_styler.params.textbox.border[visual_state])
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(control.rectangle, -1),
ugui.standard_styler.params.textbox.back[visual_state])
end,
---Draws a list frame with the specified parameters.
---@param rectangle Rectangle The control bounds.
---@param visual_state VisualState The control's visual state.
draw_list_frame = function(rectangle, visual_state)
BreitbandGraphics.fill_rectangle(rectangle,
ugui.standard_styler.params.listbox.border[visual_state])
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(rectangle, -1),
ugui.standard_styler.params.listbox.back[visual_state])
end,
---Draws a joystick's inner part with the specified parameters.
---@param rectangle Rectangle The control bounds.
---@param visual_state VisualState The control's visual state.
---@param position Vector2 The joystick's position.
draw_joystick_inner = function(rectangle, visual_state, position)
local back_color = ugui.standard_styler.params.joystick.back[visual_state]
local outline_color = ugui.standard_styler.params.joystick.outline[visual_state]
local tip_color = ugui.standard_styler.params.joystick.tip[visual_state]
local line_color = ugui.standard_styler.params.joystick.line[visual_state]
local inner_mag_color = ugui.standard_styler.params.joystick.inner_mag[visual_state]
local outer_mag_color = ugui.standard_styler.params.joystick.outer_mag[visual_state]
local mag_thickness = ugui.standard_styler.params.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.params.joystick.tip_size / 2,
y = position.y - ugui.standard_styler.params.joystick.tip_size / 2,
width = ugui.standard_styler.params.joystick.tip_size,
height = ugui.standard_styler.params.joystick.tip_size,
}, tip_color)
end,
---Draws a scrollbar with the specified parameters.
---@param container_rectangle Rectangle The scrollbar container's bounds.
---@param thumb_rectangle Rectangle The scrollbar thumb's bounds.
---@param visual_state VisualState The control's visual state.
draw_scrollbar = function(container_rectangle, thumb_rectangle, visual_state)
BreitbandGraphics.fill_rectangle(container_rectangle,
ugui.standard_styler.params.scrollbar.back[visual_state])
BreitbandGraphics.fill_rectangle(thumb_rectangle,
ugui.standard_styler.params.scrollbar.thumb[visual_state])
end,
---Draws a list item with the specified parameters.
---@param item string The list item's text.
---@param rectangle Rectangle The list item's bounds.
---@param visual_state VisualState The control's visual state.
draw_list_item = function(item, rectangle, visual_state)
if not item then
return
end
BreitbandGraphics.fill_rectangle(rectangle,
ugui.standard_styler.params.listbox_item.back[visual_state])
local size = BreitbandGraphics.get_text_size(item, ugui.standard_styler.params.font_size, ugui.standard_styler.params.font_name)
local text_rect = {
x = rectangle.x + 2,
y = rectangle.y,
width = size.width * 2,
height = rectangle.height,
}
BreitbandGraphics.draw_text2({
text = item,
rectangle = text_rect,
color = ugui.standard_styler.params.listbox_item.text[visual_state],
align_x = BreitbandGraphics.alignment.start,
font_name = ugui.standard_styler.params.font_name,
font_size = ugui.standard_styler.params.font_size,
aliased = not ugui.standard_styler.params.cleartype,
})
end,
---Draws a list with the specified parameters.
---@param control ListBox The control table.
---@param rectangle Rectangle The list item's bounds.
draw_list = function(control, rectangle)
local visual_state = ugui.get_visual_state(control)
ugui.standard_styler.draw_list_frame(rectangle, visual_state)
if not control.items then
return
end
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.params.listbox_item.height
local index_end = (rectangle.height + (scroll_y *
(content_bounds.height - rectangle.height))) /
ugui.standard_styler.params.listbox_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.params.listbox_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.params.listbox_item.height,
}, item_visual_state)
end
BreitbandGraphics.pop_clip()
end,
---Draws a menu frame with the specified parameters.
---@param rectangle Rectangle The control's bounds.
---@param visual_state VisualState The control's visual state.
draw_menu_frame = function(rectangle, visual_state)
BreitbandGraphics.fill_rectangle(rectangle,
ugui.standard_styler.params.menu.border[visual_state])
BreitbandGraphics.fill_rectangle(BreitbandGraphics.inflate_rectangle(rectangle, -1),