Skip to content

Commit

Permalink
Merge branch 'lua_ui_select_renderer_invalid' into 'master'
Browse files Browse the repository at this point in the history
Render invalid 'select' setting renderer values instead of silent failure

See merge request OpenMW/openmw!3584
  • Loading branch information
Zackhasacat committed Nov 21, 2023
2 parents a38d17a + 435e973 commit f230e3c
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions files/data/scripts/omw/settings/renderers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,26 @@ return function(registerRenderer)
if not argument.l10n then
error('"select" renderer requires a "l10n" argument')
end
if not pcall(function()
local _ = ipairs(argument.items)
assert(#argument.items > 0)
end)
then
error('"select" renderer requires an "items" array as an argument')
end
local l10n = core.l10n(argument.l10n)
local index = nil
local itemCount = 0
local itemCount = #argument.items
for i, item in ipairs(argument.items) do
itemCount = itemCount + 1
if item == value then
index = i
end
end
if not index then return {} end
local label = l10n(value)
local label = l10n(tostring(value))
local labelColor = nil
if index == nil then
labelColor = util.color.rgb(1, 0, 0)
end
local body = {
type = ui.TYPE.Flex,
props = {
Expand All @@ -177,6 +186,10 @@ return function(registerRenderer)
},
events = {
mouseClick = async:callback(function()
if not index then
set(argument.items[#argument.items])
return
end
index = (index - 2) % itemCount + 1
set(argument.items[index])
end),
Expand All @@ -187,6 +200,7 @@ return function(registerRenderer)
template = I.MWUI.templates.textNormal,
props = {
text = label,
textColor = labelColor,
},
external = {
grow = 1,
Expand All @@ -201,6 +215,10 @@ return function(registerRenderer)
},
events = {
mouseClick = async:callback(function()
if not index then
set(argument.items[1])
return
end
index = (index) % itemCount + 1
set(argument.items[index])
end),
Expand Down Expand Up @@ -246,7 +264,9 @@ return function(registerRenderer)
focusLoss = async:callback(function()
if not lastInput then return end
if not pcall(function() set(util.color.hex(lastInput)) end)
then set(value) end
then
set(value)
end
end),
},
}
Expand Down

0 comments on commit f230e3c

Please sign in to comment.