Skip to content

Commit

Permalink
everything mostly works now
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Dec 30, 2023
1 parent 8057eed commit 38d1a18
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 70 deletions.
160 changes: 93 additions & 67 deletions gui/control-panel.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local common = reqscript('internal/control-panel/common')
local dialogs = require('gui.dialogs')
local gui = require('gui')
local helpdb = require('helpdb')
local textures = require('gui.textures')
local overlay = require('plugins.overlay')
local registry = reqscript('internal/control-panel/registry')
Expand Down Expand Up @@ -135,16 +136,13 @@ function CommandTab:init()
self.subpage = Subtabs.automation

self.blurbs = {
[Subtabs.automation]='These run in the background and'..
' help you manage your fort. They are always safe to enable, and allow'..
' you to avoid paying attention to aspects of gameplay that you find'..
' tedious or unfun.',
[Subtabs.bugfix]='These automatically fix dangerous or'..
' annoying vanilla bugs. You should generally have all of these enabled'..
' unless you have a specific reason not to.',
[Subtabs.gameplay]='These change or extend gameplay. Read'..
' their help docs to see what they do and enable the ones that appeal to'..
' you.',
[Subtabs.automation]='These run in the background and help you manage your'..
' fort. They are always safe to enable, and allow you to concentrate on'..
' other aspects of gameplay that you find more enjoyable.',
[Subtabs.bugfix]='These automatically fix dangerous or annoying vanilla'..
' bugs. You should generally have all of these enabled.',
[Subtabs.gameplay]='These change or extend gameplay. Read their help docs to'..
' see what they do and enable the ones that appeal to you.',
}
end

Expand Down Expand Up @@ -223,21 +221,47 @@ function CommandTab:launch_config()
dfhack.run_command(choice.gui_config)
end


--
-- AutostartTab
-- EnabledTab
--

AutostartTab = defclass(AutostartTab, CommandTab)
AutostartTab.ATTRS{
intro_text='Tools that are enabled on this page will be auto-run or auto-enabled'..
' for you when you start a new fort (or, for "global" tools, when you start the game). To see tools that are enabled'..
' right now, please click on the "Enabled" tab.',
EnabledTab = defclass(EnabledTab, CommandTab)
EnabledTab.ATTRS{
intro_text='These are the tools that can be enabled right now. Most tools can'..
' only be enabled when you have a fort loaded. Once enabled, tools'..
' will stay enabled when you save and reload your fort. If you want'..
' them to be auto-enabled for new forts, please see the "Autostart"'..
' tab.',
}

local function make_autostart_text(label, mode, enabled)
function EnabledTab:init()
if not dfhack.world.isFortressMode() then
self.subpage = Subtabs.gameplay
end
end

-- TODO
local function get_gui_config(command)
command = common.get_first_word(command)
local gui_config = 'gui/' .. command
if helpdb.is_entry(gui_config) then
return gui_config
end
end

local function make_enabled_text(label, mode, enabled, gui_config)
if mode == 'system_enable' then
label = label .. ' (global)'
end

local function get_config_button_token(tile)
return {
tile=gui_config and tile or nil,
text=not gui_config and ' ' or nil,
}
end

return {
{tile=enabled and ENABLED_PEN_LEFT or DISABLED_PEN_LEFT},
{tile=enabled and ENABLED_PEN_CENTER or DISABLED_PEN_CENTER},
Expand All @@ -247,21 +271,32 @@ local function make_autostart_text(label, mode, enabled)
{tile=HELP_PEN_CENTER},
{tile=BUTTON_PEN_RIGHT},
' ',
get_config_button_token(BUTTON_PEN_LEFT),
get_config_button_token(CONFIGURE_PEN_CENTER),
get_config_button_token(BUTTON_PEN_RIGHT),
' ',
label,
}
end

function AutostartTab:refresh()
function EnabledTab:refresh()
local choices = {}
self.enabled_map = common.get_enabled_map()
local group = Subtabs_revmap[self.subpage]
for _,data in ipairs(registry.COMMANDS_BY_IDX) do
if data.mode == 'run' then goto continue end
if data.mode ~= 'system_enable' and not dfhack.world.isFortressMode() then
goto continue
end
if not common.command_passes_filters(data, group) then goto continue end
local enabled = safe_index(common.config.data.commands, data.command, 'autostart')
local enabled = self.enabled_map[data.command]
local gui_config = get_gui_config(data.command)
table.insert(choices, {
text=make_autostart_text(data.command, data.mode, enabled),
text=make_enabled_text(data.command, data.mode, enabled, gui_config),
search_key=data.command,
data=data,
enabled=enabled,
gui_config=gui_config,
})
::continue::
end
Expand All @@ -273,7 +308,7 @@ function AutostartTab:refresh()
list.edit:setFocus(true)
end

function AutostartTab:onInput(keys)
function EnabledTab:onInput(keys)
local handled = EnabledTab.super.onInput(self, keys)
if keys._MOUSE_L then
local list = self.subviews.list.list
Expand All @@ -284,25 +319,33 @@ function AutostartTab:onInput(keys)
self:on_submit()
elseif x >= 4 and x <= 6 then
self:show_help()
elseif x >= 8 and x <= 10 then
self:launch_config()
end
end
end
return handled
end

function AutostartTab:on_submit()
function EnabledTab:on_submit()
_,choice = self.subviews.list:getSelected()
if not choice then return end
local data = choice.data
common.set_autostart(data, not data.enabled)
common.apply_command(data, self.enabled_map, not choice.enabled)
self:refresh()
end

function AutostartTab:restore_defaults()
local group = Subtabs_revmap[self.subtab]
function EnabledTab:restore_defaults()
local group = Subtabs_revmap[self.subpage]
for _,data in ipairs(registry.COMMANDS_BY_IDX) do
if data.mode == 'run' then goto continue end
if (data.mode == 'enable' or data.mode == 'repeat')
and not dfhack.world.isFortressMode()
then
goto continue
end
if not common.command_passes_filters(data, group) then goto continue end
common.set_autostart(data, data.default)
common.apply_command(data, self.enabled_map, data.default)
::continue::
end
self:refresh()
Expand All @@ -311,24 +354,17 @@ end


--
-- EnabledTab
-- AutostartTab
--

EnabledTab = defclass(EnabledTab, CommandTab)
EnabledTab.ATTRS{
intro_text='These are the tools that are enabled right now. Note that if a'..
' tool is not marked as "global", then it can only be enabled when you have a fort loaded.'..
' Once enabled, tools will stay enabled when you'..
' save and reload your fort. If you want them to be'..
' auto-enabled for new forts, please see the "Autostart" tab.',
AutostartTab = defclass(AutostartTab, CommandTab)
AutostartTab.ATTRS{
intro_text='Tools that are enabled on this page will be auto-run or auto-enabled'..
' for you when you start a new fort (or, for "global" tools, when you start the game). To see tools that are enabled'..
' right now, please click on the "Enabled" tab.',
}

-- TODO
local function get_gui_config(command)
return 'gui/confirm'
end

local function make_enabled_text(label, mode, enabled, gui_config)
local function make_autostart_text(label, mode, enabled)
if mode == 'system_enable' then
label = label .. ' (global)'
end
Expand All @@ -341,29 +377,24 @@ local function make_enabled_text(label, mode, enabled, gui_config)
{tile=HELP_PEN_CENTER},
{tile=BUTTON_PEN_RIGHT},
' ',
{tile=gui_config and BUTTON_PEN_LEFT or gui.CLEAR_PEN},
{tile=gui_config and CONFIGURE_PEN_CENTER or gui.CLEAR_PEN},
{tile=gui_config and BUTTON_PEN_RIGHT or gui.CLEAR_PEN},
' ',
label,
}
end

function EnabledTab:refresh()
function AutostartTab:refresh()
local choices = {}
self.enabled_map = common.get_enabled_map()
local group = Subtabs_revmap[self.subpage]
for _,data in ipairs(registry.COMMANDS_BY_IDX) do
if data.mode == 'run' then goto continue end
if not common.command_passes_filters(data, group) then goto continue end
local enabled = self.enabled_map[data.command]
local gui_config = get_gui_config(data.command)
local enabled = safe_index(common.config.data.commands, data.command, 'autostart')
if enabled == nil then
enabled = data.default
end
table.insert(choices, {
text=make_enabled_text(data.command, data.mode, enabled, gui_config),
text=make_autostart_text(data.command, data.mode, enabled),
search_key=data.command,
data=data,
enabled=enabled,
gui_config=gui_config,
})
::continue::
end
Expand All @@ -375,7 +406,7 @@ function EnabledTab:refresh()
list.edit:setFocus(true)
end

function EnabledTab:onInput(keys)
function AutostartTab:onInput(keys)
local handled = EnabledTab.super.onInput(self, keys)
if keys._MOUSE_L then
local list = self.subviews.list.list
Expand All @@ -386,35 +417,30 @@ function EnabledTab:onInput(keys)
self:on_submit()
elseif x >= 4 and x <= 6 then
self:show_help()
elseif x >= 8 and x <= 10 then
self:launch_config()
end
end
end
return handled
end

function EnabledTab:on_submit()
function AutostartTab:on_submit()
_,choice = self.subviews.list:getSelected()
if not choice then return end
local data = choice.data
common.apply_command(data, self.enabled_map, not data.enabled)
common.set_autostart(data, not choice.enabled)
common.config:write()
self:refresh()
end

function EnabledTab:restore_defaults()
local group = Subtabs_revmap[self.subtab]
function AutostartTab:restore_defaults()
local group = Subtabs_revmap[self.subpage]
for _,data in ipairs(registry.COMMANDS_BY_IDX) do
if data.mode == 'run' then goto continue end
if (data.mode == 'enable' or data.mode == 'repeat')
and not dfhack.world.isFortressMode()
then
goto continue
end
if not common.command_passes_filters(data, group) then goto continue end
common.apply_command(data, self.enabled_map, data.default)
print(data.command, data.default)
common.set_autostart(data, data.default)
::continue::
end
common.config:write()
self:refresh()
dialogs.showMessage('Success', 'Defaults restored.')
end
Expand Down Expand Up @@ -501,8 +527,8 @@ function OverlaysTab:refresh()
name=name,
command='overlay',
desc=state.db[name].desc,
enabled=enabled,
},
enabled=enabled,
})
end
local list = self.subviews.list
Expand All @@ -524,7 +550,7 @@ function OverlaysTab:on_submit()
_,choice = self.subviews.list:getSelected()
if not choice then return end
local data = choice.data
enable_overlay(data.name, not data.enabled)
enable_overlay(data.name, not choice.enabled)
self:refresh()
end

Expand Down
4 changes: 2 additions & 2 deletions internal/control-panel/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function get_enabled_map()
return enabled_map
end

local function get_first_word(str)
function get_first_word(str)
local word = str:trim():split(' +')[1]
if word:startswith(':') then word = word:sub(2) end
return word
Expand Down Expand Up @@ -170,7 +170,7 @@ function set_preference(data, in_value)
qerror(('value too small: got: %s; minimum: %s'):format(value, data.min))
end
data.set_fn(value)
if data.default ~= safe_index(config.data.preferences, data.name, 'val') then
if data.default ~= value then
config.data.preferences[data.name] = {
val=value,
version=data.version,
Expand Down
2 changes: 1 addition & 1 deletion internal/control-panel/registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ COMMANDS_BY_IDX = {
{command='seedwatch', group='automation', mode='enable'},
{command='suspendmanager', group='automation', mode='enable'},
{command='tailor', group='automation', mode='enable'},
{command='work-now', group='automation', mode='system_enable'},
{command='work-now', group='automation', mode='enable'},

-- bugfix tools
{command='fix/blood-del', group='bugfix', mode='run', default=true},
Expand Down

0 comments on commit 38d1a18

Please sign in to comment.