1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 17:53:53 +00:00
openmw/files/builtin_scripts/scripts/omw/settings/render.lua

122 lines
3.7 KiB
Lua
Raw Normal View History

2022-03-07 20:28:05 +00:00
local ui = require('openmw.ui')
local util = require('openmw.util')
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 = {}
local localization = {}
2022-03-07 20:28:05 +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
error(('Setting %s of %s has unknown renderer %s'):format(setting.key, groupKey, setting.renderer))
2022-03-07 20:28:05 +00:00
end
local settingName = localization[groupKey]
and localization[groupKey].settings[setting.key].name
or setting.key
local set = function(value)
local group = common.getGroup(groupKey)
group:set(setting.key, value)
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
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),
},
},
{
name = 'settings',
type = ui.TYPE.Flex,
content = ui.content{},
},
},
}
2022-03-07 20:28:05 +00:00
local options = {
name = groupKey,
2022-03-07 20:28:05 +00:00
element = ui.create(layout),
searchHints = '',
2022-03-07 20:28:05 +00:00
}
groupOptions[groupKey] = options
for _, setting in pairs(group) do
layout.content.settings.content:add({ name = setting.key })
renderSetting(groupKey, setting, setting.default)
end
updateLocalization(groupKey)
print(('registering group %s'):format(groupKey))
2022-03-07 20:28:05 +00:00
ui.registerSettingsPage(options)
end
local function localizeGroup(groupKey, loc)
localization[groupKey] = loc
updateLocalization(groupKey)
end
2022-03-07 20:28:05 +00:00
return {
onGroupRegistered = onGroupRegistered,
registerRenderer = registerRenderer,
localizeGroup = localizeGroup,
2022-03-07 20:28:05 +00:00
}