1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-25 15:11:33 +00:00

Merge branch 'lua_ui_select_renderer_invalid' into 'master'

Render invalid 'select' setting renderer values instead of silent failure

See merge request OpenMW/openmw!3584
This commit is contained in:
Zackhasacat 2023-11-21 23:06:22 +00:00
commit f230e3cab0

View file

@ -151,17 +151,26 @@ return function(registerRenderer)
if not argument.l10n then if not argument.l10n then
error('"select" renderer requires a "l10n" argument') error('"select" renderer requires a "l10n" argument')
end 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 l10n = core.l10n(argument.l10n)
local index = nil local index = nil
local itemCount = 0 local itemCount = #argument.items
for i, item in ipairs(argument.items) do for i, item in ipairs(argument.items) do
itemCount = itemCount + 1
if item == value then if item == value then
index = i index = i
end end
end end
if not index then return {} end local label = l10n(tostring(value))
local label = l10n(value) local labelColor = nil
if index == nil then
labelColor = util.color.rgb(1, 0, 0)
end
local body = { local body = {
type = ui.TYPE.Flex, type = ui.TYPE.Flex,
props = { props = {
@ -177,6 +186,10 @@ return function(registerRenderer)
}, },
events = { events = {
mouseClick = async:callback(function() mouseClick = async:callback(function()
if not index then
set(argument.items[#argument.items])
return
end
index = (index - 2) % itemCount + 1 index = (index - 2) % itemCount + 1
set(argument.items[index]) set(argument.items[index])
end), end),
@ -187,6 +200,7 @@ return function(registerRenderer)
template = I.MWUI.templates.textNormal, template = I.MWUI.templates.textNormal,
props = { props = {
text = label, text = label,
textColor = labelColor,
}, },
external = { external = {
grow = 1, grow = 1,
@ -201,6 +215,10 @@ return function(registerRenderer)
}, },
events = { events = {
mouseClick = async:callback(function() mouseClick = async:callback(function()
if not index then
set(argument.items[1])
return
end
index = (index) % itemCount + 1 index = (index) % itemCount + 1
set(argument.items[index]) set(argument.items[index])
end), end),
@ -246,7 +264,9 @@ return function(registerRenderer)
focusLoss = async:callback(function() focusLoss = async:callback(function()
if not lastInput then return end if not lastInput then return end
if not pcall(function() set(util.color.hex(lastInput)) end) if not pcall(function() set(util.color.hex(lastInput)) end)
then set(value) end then
set(value)
end
end), end),
}, },
} }