Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gui/control-panel] convert keystroke pref to a cpp pref #772

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions gui/control-panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ local PREFERENCES = {
desc='Hide the external DFHack terminal window on startup. Use the "show" command to unhide it.'},
HIDE_ARMOK_TOOLS={label='Hide "armok" tools in command lists', type='bool', default=false,
desc='Don\'t show tools that give you god-like powers wherever DFHack tools are listed.'},
SUPPRESS_DUPLICATE_KEYBOARD_EVENTS={label='Prevent duplicate key events',
type='bool', default=true,
desc='Whether to pass key events through to DF when DFHack keybindings are triggered.'},
},
['gui']={
DEFAULT_INITIAL_PAUSE={label='DFHack tools autopause game', type='bool', default=true,
Expand All @@ -93,6 +90,17 @@ local PREFERENCES = {
desc='Whether to search for a match in the full text (true) or just at the start of words (false).'},
},
}
local CPP_PREFERENCES = {
{
label='Prevent duplicate key events',
type='bool',
default=true,
desc='Whether to pass key events through to DF when DFHack keybindings are triggered.',
init_fmt=':lua dfhack.internal.setSuppressDuplicateKeyboardEvents(%s)',
get_fn=dfhack.internal.getSuppressDuplicateKeyboardEvents,
set_fn=dfhack.internal.setSuppressDuplicateKeyboardEvents,
},
}

local REPEATS = {
['autoMilkCreature']={
Expand Down Expand Up @@ -648,25 +656,33 @@ function Preferences:onInput(keys)
return handled
end

local function make_preference_text(label, value)
return {
{tile=BUTTON_PEN_LEFT},
{tile=CONFIGURE_PEN_CENTER},
{tile=BUTTON_PEN_RIGHT},
' ',
('%s (%s)'):format(label, value),
}
end

function Preferences:refresh()
if self.subviews.input_dlg.visible then return end
local choices = {}
for ctx_name,settings in pairs(PREFERENCES) do
local ctx_env = require(ctx_name)
for id,spec in pairs(settings) do
local label = ('%s (%s)'):format(spec.label, ctx_env[id])
local text = {
{tile=BUTTON_PEN_LEFT},
{tile=CONFIGURE_PEN_CENTER},
{tile=BUTTON_PEN_RIGHT},
' ',
label,
}
local text = make_preference_text(spec.label, ctx_env[id])
table.insert(choices,
{text=text, desc=spec.desc, search_key=label,
{text=text, desc=spec.desc, search_key=text[#text],
ctx_env=ctx_env, id=id, spec=spec})
end
end
for _,spec in ipairs(CPP_PREFERENCES) do
local text = make_preference_text(spec.label, spec.get_fn())
table.insert(choices,
{text=text, desc=spec.desc, search_key=text[#text], spec=spec})
end
table.sort(choices, function(a, b) return a.spec.label < b.spec.label end)
local list = self.subviews.list
local filter = list:getFilter()
Expand All @@ -677,19 +693,28 @@ function Preferences:refresh()
end

local function preferences_set_and_save(self, choice, val)
choice.ctx_env[choice.id] = val
if choice.spec.set_fn then
choice.spec.set_fn(val)
else
choice.ctx_env[choice.id] = val
end
self:do_save()
self:refresh()
end

function Preferences:on_submit()
_,choice = self.subviews.list:getSelected()
if not choice then return end
local cur_val
if choice.spec.get_fn then
cur_val = choice.spec.get_fn()
else
cur_val = choice.ctx_env[choice.id]
end
if choice.spec.type == 'bool' then
preferences_set_and_save(self, choice, not choice.ctx_env[choice.id])
preferences_set_and_save(self, choice, not cur_val)
elseif choice.spec.type == 'int' then
self.subviews.input_dlg:show(choice.id, choice.spec,
choice.ctx_env[choice.id])
self.subviews.input_dlg:show(choice.id or choice.spec.label, choice.spec, cur_val)
end
end

Expand All @@ -708,6 +733,10 @@ function Preferences:do_save()
ctx_name, id, tostring(ctx_env[id])))
end
end
for _,spec in ipairs(CPP_PREFERENCES) do
local line = spec.init_fmt:format(spec.get_fn())
f:write(('%s\n'):format(line))
end
end
save_file(PREFERENCES_INIT_FILE, save_fn)
end
Expand All @@ -719,6 +748,9 @@ function Preferences:restore_defaults()
ctx_env[id] = spec.default
end
end
for _,spec in ipairs(CPP_PREFERENCES) do
spec.set_fn(spec.default)
end
os.remove(PREFERENCES_INIT_FILE)
self:refresh()
dialogs.showMessage('Success', 'Default preferences restored.')
Expand Down