API: Add a Dialog:buttonset() component
Did other users suggest similar ideas?
- No
- Yes/Links to similar ideas
Is your feature request related to a problem? Please describe.
In an extension I am developing I wanted a dialog to have something in between radio buttons and a combobox, something exactly like the Color Mode or Background options in the New Sprite dialog:
Describe the solution you'd like
Similar to the Dialog:radio() and Dialog:combobx(), I want a buttonset component named something like Dialog:buttonset().
-- Option 1: similar to combobox
Dialog("Buttonset"):buttonset{
options = {
"Option 1",
"Option 2",
"Option 3"
}
}
-- Option 2: combination of radio buttons and tabs
-- :buttonset() starts a set of :button() components
Dialog("Buttonset"):buttonset{ label = "Choose" }
:button{ text = "Option 1" }
:button{ text = "Option 2" }
:button{ text = "Option 3" }
-- In similar fashion to :endtabs(), this will end
-- the buttonset
:endbuttonset()The above example usages don't have the icon property expected from the example from the New Sprite dialog. For that, the :button() component can get one or the options property would get a text and icon property for each entry.
icon property-- Option 1: similar to combobox
Dialog("Buttonset"):buttonset{
options = {
{ text = "Option 1", icon = "included_file.png" }
{ text = "Option 2", icon = "icon_name" }
{ text = "Option 3", icon = "icon_name" }
}
}
-- Option 2: combination of radio buttons and tabs
-- :buttonset() starts a set of :button() components
Dialog("Buttonset"):buttonset{ label = "Choose" }
:button{ text = "Option 1", icon = "included_file.png" }
:button{ text = "Option 2", icon = "icon_name" }
:button{ text = "Option 3", icon = "icon_name" }
-- In similar fashion to :endtabs(), this will end
-- the buttonset
:endbuttonset()Describe alternatives you've considered
Currently, the only alternative seems to be to use Dialog:canvas() and use the GraphicsContext:drawThemeRect() to draw the buttons manually. This is very manual work, you have to change the states of the buttons manually, keep track of where the mouse is moving and change the UI accordingly, and is a way longer implementation than comparable components.
buttonset implementation, not even including correct focus and tab behaviourlocal buttonsetChosenOption = "1"
local buttonsetMouse = { position = Point(-1, -1), leftClick = false }
local buttonsetStates = {
normal = {
part = "buttonset_item_normal",
color = "button_normal_text"
},
hot = {
part = "buttonset_item_hot",
color = "button_hot_text"
},
pushed = {
part = "buttonset_item_pushed",
color = "button_selected_text"
},
focused = {
part = "buttonset_item_focused",
color = "button_normal_text"
}
}
local buttonset = {
{
text = "Option 1",
onclick = function ()
buttonsetChosenOption = "1"
end
},
{
text = "Option 2",
onclick = function ()
buttonsetChosenOption = "2"
end
},
{
text = "Option 3",
onclick = function ()
buttonsetChosenOption = "3"
end
}
}
local buttonsetFocusedButton = buttonset[1]
local dlgButtonset = Dialog("Buttonset")
dlgButtonset:canvas{
width = 133,
height = 17,
label = "Buttonset",
onpaint = function (ev)
local ctx = ev.context
local nextX = 0
for _,button in ipairs(buttonset) do
local state = buttonsetStates.normal
if button == buttonsetFocusedButton then
state = buttonsetStates.focused
end
local size = ctx:measureText(button.text)
local padding = 5 * 2
local bounds = Rectangle(nextX, 0, size.width + padding, size.height + padding)
nextX = nextX + bounds.w
button.bounds = bounds
local isMouseOver = button.bounds:contains(buttonsetMouse.position)
if isMouseOver then
state = buttonsetStates.hot or state
if buttonsetMouse.leftClick then
state = buttonsetStates.pushed
end
end
ctx:drawThemeRect(state.part, bounds)
local center = Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2)
ctx.color = app.theme.color[state.color]
ctx:fillText(button.text, center.x - size.width / 2, center.y - size.height / 2)
end
end,
onmousemove = function (ev)
buttonsetMouse.position = Point(ev.x, ev.y)
dlgButtonset:repaint()
end,
onmousedown = function (ev)
buttonsetMouse.leftClick = ev.button == MouseButton.LEFT
dlgButtonset:repaint()
end,
onmouseup = function (_)
if not buttonsetMouse.leftClick then
return
end
for _,button in ipairs(buttonset) do
local isMouseOver = button.bounds:contains(buttonsetMouse.position)
if isMouseOver then
button.onclick()
buttonsetFocusedButton = button
end
end
buttonsetMouse.leftClick = false
dlgButtonset:repaint()
end
}
dlgButtonset:button{
text = "OK",
onclick = function ()
dlgButtonset:close()
chosenOption("Option " .. buttonsetChosenOption, "Buttonset")
end
}
:show{ wait = true }Additional context
Code I used as a single script to preview the examples below-- #region Extra stuff
if not app.isUIAvailable then
return
end
local function chosenOption(option, inputType)
print("You chose ", option, " with ", inputType)
end
-- #endregion
-- #region Radio Buttons
local dlgRadioButtons = Dialog("Radio Buttons")
:newrow{ always = true }
:radio{ text = "Option 1", id = "1", label = "Radio buttons", selected = true }
:radio{ text = "Option 2", id = "2" }
:radio{ text = "Option 3", id = "3" }
dlgRadioButtons:button{
text = "OK",
onclick = function()
dlgRadioButtons:close()
local option = "0"
for _,opt in ipairs({ "1", "2", "3" }) do
if dlgRadioButtons.data[opt] then
option = opt
end
end
chosenOption("Option " .. option, "Radio Buttons")
end
}
:show{ wait = true }
-- #endregion
-- #region Combobox
local dlgCombobox = Dialog("Combobox")
:combobox{
id = "option",
label = "Combobox",
options = {
"Option 1",
"Option 2",
"Option 3",
}
}
dlgCombobox:button{
text = "OK",
onclick = function ()
dlgCombobox:close()
chosenOption(dlgCombobox.data.option, "Combobox")
end
}
:show{ wait = true }
-- #endregion
-- #region Buttons
local buttonsChosenOption = "1"
local dlgButtons = Dialog("Buttons")
:button{
text = "Option 1",
id = "1",
onclick = function()
buttonsChosenOption = "1"
end,
label = "Buttons"
}:button{
text = "Option 2",
id = "2",
onclick = function()
buttonsChosenOption = "2"
end
}:button{
text = "Option 3",
id = "3",
onclick = function()
buttonsChosenOption = "3"
end
}
dlgButtons:newrow():button{
text = "OK",
onclick = function ()
dlgButtons:close()
chosenOption("Option " .. buttonsChosenOption, "Buttons")
end
}
:show{ wait = true }
-- #endregion
-- #region Buttonset
local buttonsetChosenOption = "1"
local buttonsetMouse = { position = Point(-1, -1), leftClick = false }
local buttonsetStates = {
normal = {
part = "buttonset_item_normal",
color = "button_normal_text"
},
hot = {
part = "buttonset_item_hot",
color = "button_hot_text"
},
pushed = {
part = "buttonset_item_pushed",
color = "button_selected_text"
},
focused = {
part = "buttonset_item_focused",
color = "button_normal_text"
}
}
local buttonset = {
{
text = "Option 1",
onclick = function ()
buttonsetChosenOption = "1"
end
},
{
text = "Option 2",
onclick = function ()
buttonsetChosenOption = "2"
end
},
{
text = "Option 3",
onclick = function ()
buttonsetChosenOption = "3"
end
}
}
local buttonsetFocusedButton = buttonset[1]
local dlgButtonset = Dialog("Buttonset")
dlgButtonset:canvas{
width = 133,
height = 17,
label = "Buttonset",
onpaint = function (ev)
local ctx = ev.context
local nextX = 0
for _,button in ipairs(buttonset) do
local state = buttonsetStates.normal
if button == buttonsetFocusedButton then
state = buttonsetStates.focused
end
local size = ctx:measureText(button.text)
local padding = 5 * 2
local bounds = Rectangle(nextX, 0, size.width + padding, size.height + padding)
nextX = nextX + bounds.w
button.bounds = bounds
local isMouseOver = button.bounds:contains(buttonsetMouse.position)
if isMouseOver then
state = buttonsetStates.hot or state
if buttonsetMouse.leftClick then
state = buttonsetStates.pushed
end
end
ctx:drawThemeRect(state.part, bounds)
local center = Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2)
ctx.color = app.theme.color[state.color]
ctx:fillText(button.text, center.x - size.width / 2, center.y - size.height / 2)
end
end,
onmousemove = function (ev)
buttonsetMouse.position = Point(ev.x, ev.y)
dlgButtonset:repaint()
end,
onmousedown = function (ev)
buttonsetMouse.leftClick = ev.button == MouseButton.LEFT
dlgButtonset:repaint()
end,
onmouseup = function (_)
if not buttonsetMouse.leftClick then
return
end
for _,button in ipairs(buttonset) do
local isMouseOver = button.bounds:contains(buttonsetMouse.position)
if isMouseOver then
button.onclick()
buttonsetFocusedButton = button
end
end
buttonsetMouse.leftClick = false
dlgButtonset:repaint()
end
}
dlgButtonset:button{
text = "OK",
onclick = function ()
dlgButtonset:close()
chosenOption("Option " .. buttonsetChosenOption, "Buttonset")
end
}
:show{ wait = true }
-- #endregion| Screenshot | Using | Lines of code | Notes |
|---|---|---|---|
Dialog:radio() |
±30 | Don't look great horizontal | |
Dialog:combobox() |
±51 | Requires additional clicks to open and view options | |
Dialog:button() |
±55 | Have to keep track of chosen option manually, selected option isn't visible in the UI | |
Dialog:canvas() |
±198 | Manual work, doesn't even include proper tab-flow, have to keep track of chosen option manually |
Source: aseprite/aseprite