1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-17 19:20:49 +00:00

Separate setting keys and names, support localization

This commit is contained in:
uramer 2022-04-09 19:58:32 +02:00
parent 1f5e3b78d4
commit a0c0c39a8c
4 changed files with 97 additions and 66 deletions

View File

@ -52,28 +52,28 @@ local function isGlobalScope(scope)
return scope == SCOPE.Global or scope == SCOPE.SaveGlobal
end
local function getSetting(groupName, settingName)
local group = groups:get(groupName)
local function getSetting(groupKey, settingKey)
local group = groups:get(groupKey)
if not group then
error('Unknown group')
end
local setting = group[settingName]
local setting = group[settingKey]
if not setting then
error('Unknown setting')
end
return setting
end
local function getSettingValue(groupName, settingName)
local setting = getSetting(groupName, settingName)
local function getSettingValue(groupKey, settingKey)
local setting = getSetting(groupKey, settingKey)
local scopeSection = scopes[setting.scope]
if not scopeSection then
error(('Setting %s is not available in this context'):format(setting.name))
error(('Setting %s is not available in this context'):format(setting.key))
end
if not scopeSection:get(groupName) then
scopeSection:set(groupName, {})
if not scopeSection:get(groupKey) then
scopeSection:set(groupKey, {})
end
return scopeSection:get(groupName)[setting.name] or setting.default
return scopeSection:get(groupKey)[setting.key] or setting.default
end
local function notifySettingChange(scope, event)
@ -89,11 +89,11 @@ local function notifySettingChange(scope, event)
end
end
local function setSettingValue(groupName, settingName, value)
local setting = getSetting(groupName, settingName)
local function setSettingValue(groupKey, settingKey, value)
local setting = getSetting(groupKey, settingKey)
local event = {
groupName = groupName,
settingName = setting.name,
groupName = groupKey,
settingName = setting.key,
value = value,
}
if isPlayerScript and isGlobalScope(setting.scope) then
@ -102,43 +102,43 @@ local function setSettingValue(groupName, settingName, value)
end
local scopeSection = scopes[setting.scope]
if not scopeSection:get(groupName) then
scopeSection:set(groupName, {})
if not scopeSection:get(groupKey) then
scopeSection:set(groupKey, {})
end
local copy = scopeSection:getCopy(groupName)
copy[setting.name] = value
scopeSection:set(groupName, copy)
local copy = scopeSection:getCopy(groupKey)
copy[setting.key] = value
scopeSection:set(groupKey, copy)
notifySettingChange(setting.scope, event)
end
local groupMeta = {
__index = {
get = function(self, settingName)
return getSettingValue(self.name, settingName)
get = function(self, settingKey)
return getSettingValue(self.key, settingKey)
end,
set = function(self, settingName, value)
setSettingValue(self.name, settingName, value)
set = function(self, settingKey, value)
setSettingValue(self.key, settingKey, value)
end,
onChange = function(self, callback)
table.insert(self.__callbacks, callback)
end,
__changed = function(self, settingName, value)
__changed = function(self, settingKey, value)
for _, callback in ipairs(self.__callbacks) do
callback(settingName, value)
callback(settingKey, value)
end
end,
},
}
local cachedGroups = {}
local function getGroup(groupName)
if not cachedGroups[groupName] then
cachedGroups[groupName] = setmetatable({
name = groupName,
local function getGroup(groupKey)
if not cachedGroups[groupKey] then
cachedGroups[groupKey] = setmetatable({
key = groupKey,
__callbacks = {},
}, groupMeta)
end
return cachedGroups[groupName]
return cachedGroups[groupKey]
end
return {

View File

@ -8,6 +8,7 @@ return {
SCOPE = common.SCOPE,
getGroup = common.getGroup,
registerRenderer = render.registerRenderer,
localizeGroup = render.localizeGroup,
},
engineHandlers = {
onLoad = function(saved)

View File

@ -17,14 +17,8 @@ local function validScope(scope)
end
local function validateSettingOptions(options)
if type(options.name) ~= 'string' then
error('Setting must have a name')
end
if options.default == nil then
error('Setting must have a default value')
end
if type(options.description) ~= 'string' then
error('Setting must have a description')
if type(options.key) ~= 'string' then
error('Setting must have a key')
end
if not validScope(options.scope) then
error(('Invalid setting scope %s'):format(options.scope))
@ -36,14 +30,13 @@ end
local function addSetting(group, options)
validateSettingOptions(options)
if group[options.name] then
error(('Duplicate setting name %s'):format(options.name))
if group[options.key] then
error(('Duplicate setting key %s'):format(options.key))
end
group[options.name] = {
name = options.name,
group[options.key] = {
key = options.key,
scope = options.scope or SCOPE.Global,
default = options.default,
description = options.description,
renderer = options.renderer,
}
end

View File

@ -9,51 +9,88 @@ local function registerRenderer(name, renderFunction)
end
local groupOptions = {}
local localization = {}
local function renderSetting(groupName, setting, value, index)
local function renderSetting(groupKey, setting, value, index)
local renderFunction = renderers[setting.renderer]
if not renderFunction then
error(('Setting %s of %s has unknown renderer %s'):format(setting.name, groupName, setting.renderer))
error(('Setting %s of %s has unknown renderer %s'):format(setting.key, groupKey, setting.renderer))
end
local layout = renderFunction(setting, value or setting.default, function(value)
local group = common.getGroup(groupName)
group:set(setting.name, value)
local element = groupOptions[groupName].element
local settingLayout = renderSetting(groupName, setting, value, index)
settingLayout.name = setting.name
element.layout.content[setting.name] = settingLayout
local loc = localization[groupKey] and localization[groupKey].settings[setting.key] or {
name = setting.key,
description = '',
}
local layout = renderFunction(loc, value or setting.default, function(value)
local group = common.getGroup(groupKey)
group:set(setting.key, value)
local element = groupOptions[groupKey].element
local settingLayout = renderSetting(groupKey, setting, value, index)
settingLayout.name = setting.key
element.layout.content.settings.content[setting.key] = settingLayout
element:update()
end)
layout.name = setting.name
layout.name = setting.key
return layout
end
local function onGroupRegistered(groupName)
local group = common.groups:get(groupName)
local layout = {
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 settingsLayout = {
name = 'settings',
type = ui.TYPE.Flex,
content = ui.content{},
}
local searchHints = { groupName }
local count = 0
for _, setting in pairs(group) do
count = count + 1
local settingLayout = renderSetting(groupName, setting, setting.default, count)
settingLayout.name = setting.name
layout.content:add(settingLayout)
table.insert(searchHints, setting.name)
local settingLayout = renderSetting(groupKey, setting, setting.default, count)
settingLayout.key = setting.key
settingsLayout.content:add(settingLayout)
end
local options = {
name = groupName,
element = ui.create(layout),
searchHints = table.concat(searchHints, ' '),
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),
},
},
settingsLayout,
},
}
groupOptions[groupName] = options
print(('registering group %s'):format(groupName))
local options = {
name = groupKey,
element = ui.create(layout),
searchHints = '',
}
groupOptions[groupKey] = options
updateLocalization(groupKey)
print(('registering group %s'):format(groupKey))
ui.registerSettingsPage(options)
end
local function localizeGroup(groupKey, loc)
localization[groupKey] = loc
updateLocalization(groupKey)
end
return {
onGroupRegistered = onGroupRegistered,
registerRenderer = registerRenderer,
localizeGroup = localizeGroup,
}