2022-03-07 20:28:05 +00:00
|
|
|
local ui = require('openmw.ui')
|
|
|
|
local util = require('openmw.util')
|
2022-04-13 18:08:33 +00:00
|
|
|
local async = require('openmw.async')
|
2022-03-07 20:28:05 +00:00
|
|
|
|
|
|
|
local common = require('scripts.omw.settings.common')
|
|
|
|
|
|
|
|
local renderers = {}
|
|
|
|
local function registerRenderer(name, renderFunction)
|
|
|
|
renderers[name] = renderFunction
|
|
|
|
end
|
|
|
|
|
|
|
|
local groupOptions = {}
|
2022-04-09 17:58:32 +00:00
|
|
|
local localization = {}
|
2022-03-07 20:28:05 +00:00
|
|
|
|
2022-04-13 18:08:33 +00:00
|
|
|
local function renderSetting(groupKey, setting, value)
|
2022-03-07 20:28:05 +00:00
|
|
|
local renderFunction = renderers[setting.renderer]
|
|
|
|
if not renderFunction then
|
2022-04-09 17:58:32 +00:00
|
|
|
error(('Setting %s of %s has unknown renderer %s'):format(setting.key, groupKey, setting.renderer))
|
2022-03-07 20:28:05 +00:00
|
|
|
end
|
2022-04-13 18:08:33 +00:00
|
|
|
local settingName = localization[groupKey]
|
|
|
|
and localization[groupKey].settings[setting.key].name
|
|
|
|
or setting.key
|
|
|
|
local set = function(value)
|
2022-04-09 17:58:32 +00:00
|
|
|
local group = common.getGroup(groupKey)
|
|
|
|
group:set(setting.key, value)
|
2022-04-13 18:08:33 +00:00
|
|
|
renderSetting(groupKey, setting, value)
|
|
|
|
end
|
|
|
|
local element = groupOptions[groupKey].element
|
|
|
|
local settingsLayout = element.layout.content.settings
|
|
|
|
settingsLayout.content[setting.key] = {
|
|
|
|
name = setting.key,
|
|
|
|
type = ui.TYPE.Flex,
|
|
|
|
props = {
|
|
|
|
horizontal = true,
|
|
|
|
align = ui.ALIGNMENT.Start,
|
|
|
|
arrange = ui.ALIGNMENT.Center,
|
|
|
|
},
|
|
|
|
content = ui.content {
|
|
|
|
{
|
|
|
|
type = ui.TYPE.Text,
|
|
|
|
props = {
|
|
|
|
text = settingName .. ':',
|
|
|
|
textColor = util.color.rgb(1, 1, 1),
|
|
|
|
textSize = 30,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
renderFunction(value or setting.default, set, setting.argument),
|
|
|
|
{
|
|
|
|
type = ui.TYPE.Text,
|
|
|
|
props = {
|
|
|
|
text = 'Reset',
|
|
|
|
textColor = util.color.rgb(1, 1, 1),
|
|
|
|
textSize = 30,
|
|
|
|
},
|
|
|
|
events = {
|
|
|
|
mouseClick = async:callback(function()
|
|
|
|
set(setting.default)
|
|
|
|
end),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
element:update()
|
2022-03-07 20:28:05 +00:00
|
|
|
end
|
|
|
|
|
2022-04-09 17:58:32 +00:00
|
|
|
local function updateLocalization(groupKey)
|
|
|
|
local loc = localization[groupKey]
|
|
|
|
local options = groupOptions[groupKey]
|
|
|
|
if not options or not loc then return end
|
|
|
|
local searchHints = { loc.name, loc.description }
|
|
|
|
options.name = loc.name
|
|
|
|
options.searchHints = table.concat(searchHints, ' ')
|
|
|
|
local layout = options.element.layout
|
|
|
|
layout.content.header.props.text = loc.description
|
|
|
|
end
|
|
|
|
|
|
|
|
local function onGroupRegistered(groupKey)
|
|
|
|
local group = common.groups:get(groupKey)
|
|
|
|
local layout = {
|
|
|
|
type = ui.TYPE.Flex,
|
|
|
|
content = ui.content {
|
|
|
|
{
|
|
|
|
name = 'header',
|
|
|
|
type = ui.TYPE.Text,
|
|
|
|
props = {
|
|
|
|
text = '',
|
|
|
|
textSize = 30,
|
|
|
|
textColor = util.color.rgb(1, 1, 1),
|
|
|
|
},
|
|
|
|
},
|
2022-04-13 18:08:33 +00:00
|
|
|
{
|
|
|
|
name = 'settings',
|
|
|
|
type = ui.TYPE.Flex,
|
|
|
|
content = ui.content{},
|
|
|
|
},
|
2022-04-09 17:58:32 +00:00
|
|
|
},
|
|
|
|
}
|
2022-03-07 20:28:05 +00:00
|
|
|
local options = {
|
2022-04-09 17:58:32 +00:00
|
|
|
name = groupKey,
|
2022-03-07 20:28:05 +00:00
|
|
|
element = ui.create(layout),
|
2022-04-09 17:58:32 +00:00
|
|
|
searchHints = '',
|
2022-03-07 20:28:05 +00:00
|
|
|
}
|
2022-04-09 17:58:32 +00:00
|
|
|
groupOptions[groupKey] = options
|
2022-04-13 18:08:33 +00:00
|
|
|
for _, setting in pairs(group) do
|
|
|
|
layout.content.settings.content:add({ name = setting.key })
|
|
|
|
renderSetting(groupKey, setting, setting.default)
|
|
|
|
end
|
2022-04-09 17:58:32 +00:00
|
|
|
updateLocalization(groupKey)
|
|
|
|
print(('registering group %s'):format(groupKey))
|
2022-03-07 20:28:05 +00:00
|
|
|
ui.registerSettingsPage(options)
|
|
|
|
end
|
|
|
|
|
2022-04-09 17:58:32 +00:00
|
|
|
local function localizeGroup(groupKey, loc)
|
|
|
|
localization[groupKey] = loc
|
|
|
|
updateLocalization(groupKey)
|
|
|
|
end
|
|
|
|
|
2022-03-07 20:28:05 +00:00
|
|
|
return {
|
|
|
|
onGroupRegistered = onGroupRegistered,
|
|
|
|
registerRenderer = registerRenderer,
|
2022-04-09 17:58:32 +00:00
|
|
|
localizeGroup = localizeGroup,
|
2022-03-07 20:28:05 +00:00
|
|
|
}
|