1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 03:32:36 +00:00

Simplify renderers, standard setting reset

This commit is contained in:
uramer 2022-04-13 20:08:33 +02:00
parent a0c0c39a8c
commit 711f982e19
3 changed files with 72 additions and 27 deletions

View File

@ -1,5 +1,23 @@
local common = require('scripts.omw.settings.common') local common = require('scripts.omw.settings.common')
local render = require('scripts.omw.settings.render') local render = require('scripts.omw.settings.render')
local ui = require('openmw.ui')
local async = require('openmw.async')
local util = require('openmw.util')
render.registerRenderer('text', function(value, set, arg)
return {
type = ui.TYPE.TextEdit,
props = {
size = util.vector2(arg and arg.size or 300, 100),
text = value,
textColor = util.color.rgb(1, 1, 1),
textSize = 30,
},
events = {
textChanged = async:callback(function(s) set(s) end),
},
}
end)
local saveScope = common.scopes[common.SCOPE.SavePlayer] local saveScope = common.scopes[common.SCOPE.SavePlayer]
return { return {

View File

@ -38,6 +38,7 @@ local function addSetting(group, options)
scope = options.scope or SCOPE.Global, scope = options.scope or SCOPE.Global,
default = options.default, default = options.default,
renderer = options.renderer, renderer = options.renderer,
argument = options.argument,
} }
end end

View File

@ -1,5 +1,6 @@
local ui = require('openmw.ui') local ui = require('openmw.ui')
local util = require('openmw.util') local util = require('openmw.util')
local async = require('openmw.async')
local common = require('scripts.omw.settings.common') local common = require('scripts.omw.settings.common')
@ -11,26 +12,55 @@ end
local groupOptions = {} local groupOptions = {}
local localization = {} local localization = {}
local function renderSetting(groupKey, setting, value, index) local function renderSetting(groupKey, setting, value)
local renderFunction = renderers[setting.renderer] local renderFunction = renderers[setting.renderer]
if not renderFunction then if not renderFunction then
error(('Setting %s of %s has unknown renderer %s'):format(setting.key, groupKey, setting.renderer)) error(('Setting %s of %s has unknown renderer %s'):format(setting.key, groupKey, setting.renderer))
end end
local loc = localization[groupKey] and localization[groupKey].settings[setting.key] or { local settingName = localization[groupKey]
name = setting.key, and localization[groupKey].settings[setting.key].name
description = '', or setting.key
} local set = function(value)
local layout = renderFunction(loc, value or setting.default, function(value)
local group = common.getGroup(groupKey) local group = common.getGroup(groupKey)
group:set(setting.key, value) group:set(setting.key, value)
local element = groupOptions[groupKey].element renderSetting(groupKey, setting, value)
local settingLayout = renderSetting(groupKey, setting, value, index) end
settingLayout.name = setting.key local element = groupOptions[groupKey].element
element.layout.content.settings.content[setting.key] = settingLayout local settingsLayout = element.layout.content.settings
element:update() settingsLayout.content[setting.key] = {
end) name = setting.key,
layout.name = setting.key type = ui.TYPE.Flex,
return layout 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()
end end
local function updateLocalization(groupKey) local function updateLocalization(groupKey)
@ -46,18 +76,6 @@ end
local function onGroupRegistered(groupKey) local function onGroupRegistered(groupKey)
local group = common.groups:get(groupKey) local group = common.groups:get(groupKey)
local settingsLayout = {
name = 'settings',
type = ui.TYPE.Flex,
content = ui.content{},
}
local count = 0
for _, setting in pairs(group) do
count = count + 1
local settingLayout = renderSetting(groupKey, setting, setting.default, count)
settingLayout.key = setting.key
settingsLayout.content:add(settingLayout)
end
local layout = { local layout = {
type = ui.TYPE.Flex, type = ui.TYPE.Flex,
content = ui.content { content = ui.content {
@ -70,7 +88,11 @@ local function onGroupRegistered(groupKey)
textColor = util.color.rgb(1, 1, 1), textColor = util.color.rgb(1, 1, 1),
}, },
}, },
settingsLayout, {
name = 'settings',
type = ui.TYPE.Flex,
content = ui.content{},
},
}, },
} }
local options = { local options = {
@ -79,6 +101,10 @@ local function onGroupRegistered(groupKey)
searchHints = '', searchHints = '',
} }
groupOptions[groupKey] = options 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) updateLocalization(groupKey)
print(('registering group %s'):format(groupKey)) print(('registering group %s'):format(groupKey))
ui.registerSettingsPage(options) ui.registerSettingsPage(options)