Skip to content

Commit efc440b

Browse files
Implement SMODS.GUI.scrollbar and SMODS.GUI.dropdown_select (#1253)
* add scrollbar * add dropdown * errors for proper usage * polish * docs pt 1 * comment patches * scroll progress now saves on a per-instance basis * docs (final) * Update ui.lua * Update ui.lua * add stuff * fix scrolling and convert stuff to absolute positioning * reimplement scroll mult * pass args into dropdown_element_def
1 parent c81183b commit efc440b

File tree

6 files changed

+555
-1
lines changed

6 files changed

+555
-1
lines changed

assets/1x/dropdown_arrow.png

754 Bytes
Loading

assets/2x/dropdown_arrow.png

1.54 KB
Loading

lovely/ui_elements.toml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,60 @@ position = 'before'
2222
payload = '''love.graphics.setColor(self.config.colour)'''
2323
match_indent = true
2424

25+
# Game:draw()
26+
# Draw dropdowns right before G.OVERLAY_MENU if the dropdown was created while G.OVERLAY_MENU didn't exist
27+
[[patches]]
28+
[patches.pattern]
29+
target = 'game.lua'
30+
pattern = '''
31+
if (self.OVERLAY_MENU) or (not self.F_HIDE_BG) then
32+
'''
33+
position = 'before'
34+
payload = '''
35+
for k, v in pairs(self.I.DROPDOWN) do
36+
if v.parent == G.ROOM_ATTACH then
37+
love.graphics.push()
38+
v:translate_container()
39+
v:draw()
40+
love.graphics.pop()
41+
end
42+
end
43+
'''
44+
match_indent = true
45+
46+
# Draw dropdowns right before the drag target if the dropdown was created while G.OVERLAY_MENU exists
47+
[[patches]]
48+
[patches.pattern]
49+
target = 'game.lua'
50+
pattern = '''
51+
if self.CONTROLLER.dragging.target and self.CONTROLLER.dragging.target ~= self.CONTROLLER.focused.target then
52+
'''
53+
position = 'before'
54+
payload = '''
55+
for k, v in pairs(self.I.DROPDOWN) do
56+
if v.parent == G.OVERLAY_MENU then
57+
love.graphics.push()
58+
v:translate_container()
59+
v:draw()
60+
love.graphics.pop()
61+
end
62+
end
63+
'''
64+
match_indent = true
65+
66+
# Game:set_globals()
67+
# Add G.I.DROPDOWN
68+
[[patches]]
69+
[patches.pattern]
70+
target = 'globals.lua'
71+
pattern = '''
72+
UIBOX = {},
73+
'''
74+
position = 'after'
75+
payload = '''
76+
DROPDOWN = {},
77+
'''
78+
match_indent = true
2579

2680
## multiple text input fix
2781
# create_text_input()

lsp_def/ui.lua

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,64 @@ function create_UIBox_your_collection_stickers() end
328328

329329
---@return UINode
330330
---@param args ScoreContainerArgs
331-
function SMODS.GUI.score_container(args) end
331+
function SMODS.GUI.score_container(args) end
332+
333+
---@class ScrollbarArgs
334+
---@field w number? The width of the scrollbar. Is optional if scrollbar is horizontal or knob_w is specified.
335+
---@field h number? The height of the scrollbar. Is optional if scrollbar is vertical or knob_h is specified.
336+
---@field bg_colour? table The background colour of the scrollbar.
337+
---@field colour? table The colour of the scrollbar's progress.
338+
---@field knob_colour? table The colour of the scrollbar's knob.
339+
---@field knob_h? number The height of the scrollbar's knob. Takes precedence over `h` if scrollbar is horizontal.
340+
---@field knob_w? number The width of the scrollbar's knob. Takes precedence over `w` if scrollbar is vertical.
341+
---@field ref_table? table The table whose `ref_value` should be updated based on this scrollbar's progress.
342+
---@field ref_value? string `ref_table[ref_value]` is set to a value between `min` and `max`.
343+
---@field scroll_collision_obj? SMODS.UIScrollBox The object to scroll based on this scrollbar's progress. This scrollbox will only update automatically if `ref_table` or `ref_value` are omitted.
344+
---@field ui_type? G.UIT Which type of UI node this should be.
345+
---@field horizontal? boolean Whether or not this scrollbar is horizontal. Default orientation is vertical.
346+
---@field min? number Minimum value of `ref_value`. Defaults to 0.
347+
---@field max? number Maximum value of `ref_value` Defaults to 1.
348+
---@field scroll_mult? number Multiplies the scrolling speed of this scrollbar when scrolled with mouse wheel or something similar.
349+
350+
---Returns an UI node that has the functionality of a scrollbar.
351+
---@param args ScrollbarArgs
352+
---@return table
353+
function SMODS.GUI.scrollbar(args) end
354+
355+
--- Handles SMODS.GUI.scrollbar functionality
356+
---@param e table
357+
function G.FUNCS.scrollbar(e) end
358+
359+
---@class DropdownSelectArgs
360+
---@field options string[] A list of all possible options that this dropdown button can have.
361+
---@field ref_table table The table whose `ref_value` should be updated to this dropdown's current value.
362+
---@field ref_value string `ref_table[ref_value]` is set to this dropdown's current value.
363+
---@field default? string The default value of this dropdown selection if no value is selected. If not specified, defaults to the first item of `options`.
364+
---@field scale? number The scale of the dropdown button's text. Defaults to 0.4.
365+
---@field dropdown_scale? number The scale of the text of the dropdown options. Defaults to 0.4.
366+
---@field minw? number Specifies the minimum width of the box that contains the text displayed as the current option.
367+
---@field dropdown_bg_colour? table The colour of the background of the dropdown menu. Defaults to lighten(G.C.BLACK, 0.2)
368+
---@field border_colour? table The colour of the border of the dropdown menu. Defaults to lighten(G.C.JOKER_GREY, 0.5).
369+
---@field dropdown_text_colour? table The colour of the text of the dropdown options. Defaults to G.C.UI.TEXT_LIGHT.
370+
---@field selected_colour? table The colour of the background of the currently selected option. Defaults to G.C.BLACK.
371+
---@field colour? table The colour of the dropdown button. Defaults to G.C.RED.
372+
---@field text_colour? table The colour of the dropdown button's text. Defaults to G.C.UI.TEXT_LIGHT.
373+
---@field dropdown_element_def? fun(option: string, args: DropdownSelectArgs): table If defined, each option will be displayed according to the UI nodes returned by this function. Note that the result of this function is passed into a row node.
374+
---@field max_menu_h? number The maximum height that the dropdown options should take up. If set, the dropdown's contents will be scrollable and a scrollbar will automatically appear.
375+
---@field disabled_colour? table The colour of the background disabled options. Defaults to G.C.CLEAR.
376+
---@field callback? string If set, dropdown options when clicked will call G.FUNCS[callback](e), with `e` being the specific option button pressed. `e.config.value` gets the value of the clicked option. Will not be called if the option is clicked on while disabled.
377+
---@field is_option_disabled? fun(option: string): boolean? If defined, an option will be disabled if this function returns a truthy value. This is called for every option.
378+
---@field no_unselect? boolean If set to `true`, prevents an option from being unselected.
379+
---@field align? string Aligns the text of the dropdown button. Works just like aligning regular UI nodes. Default is "cm".
380+
---@field id? string Assigns the given id to the config table of the root of the button UIBox if set.
381+
---@field option_align? string Aligns the text of the dropdown options. Works just like aligning regular UI nodes. Default is "cl".
382+
---@field close_on_select? boolean If true, the dropdown menu will automatically close when an option is selected or unselected.
383+
384+
---Returns a button that creates a dropdown selection menu when clicked on.
385+
---@param args DropdownSelectArgs
386+
function SMODS.GUI.dropdown_select(args) end
387+
388+
---Handles creating the dropdown menu. Don't call this manually.
389+
---@param args DropdownSelectArgs
390+
---@param parent_width number
391+
function SMODS.GUI.create_UIBox_dropdown_menu(args, parent_width) end

src/game_object.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ Set `prefix_config.key = false` on your object instead.]]):format(obj.key), obj.
480480
px = 66,
481481
py = 66,
482482
}
483+
SMODS.Atlas {
484+
key = 'dropdown_arrow',
485+
path = 'dropdown_arrow.png',
486+
px = 66,
487+
py = 66,
488+
}
483489

484490
-------------------------------------------------------------------------------------------------
485491
----- API CODE GameObject.Sound

0 commit comments

Comments
 (0)