From f4de826ec0d2212b8b0d2339ee28f55a578cf93c Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sat, 13 Jan 2024 16:04:00 -0800 Subject: [PATCH 1/2] fix some commands not showing the correct help --- gui/control-panel.lua | 8 ++++---- internal/control-panel/registry.lua | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gui/control-panel.lua b/gui/control-panel.lua index c10b2d2f5c..0b16c4a88c 100644 --- a/gui/control-panel.lua +++ b/gui/control-panel.lua @@ -197,14 +197,14 @@ function CommandTab:init_footer(panel) } end -local function launch_help(command) - dfhack.run_command('gui/launcher', command .. ' ') +local function launch_help(data) + dfhack.run_command('gui/launcher', data.help_command or data.command .. ' ') end function CommandTab:show_help() _,choice = self.subviews.list:getSelected() if not choice then return end - launch_help(choice.data.command) + launch_help(choice.data) end function CommandTab:has_config() @@ -604,7 +604,7 @@ end function OverlaysTab:show_help() _,choice = self.subviews.list:getSelected() if not choice then return end - launch_help(choice.data.command) + launch_help(choice.data) end diff --git a/internal/control-panel/registry.lua b/internal/control-panel/registry.lua index cfa56dc3ea..fd8bd3f208 100644 --- a/internal/control-panel/registry.lua +++ b/internal/control-panel/registry.lua @@ -22,11 +22,11 @@ COMMANDS_BY_IDX = { desc='Enable if you usually farm pig tails for the clothing industry.'}, {command='autofish', group='automation', mode='enable'}, --{command='autolabor', group='automation', mode='enable'}, -- hide until it works better - {command='automilk', group='automation', mode='repeat', + {command='automilk', help_command='workorder', group='automation', mode='repeat', desc='Automatically milk creatures that are ready for milking.', params={'--time', '14', '--timeUnits', 'days', '--command', '[', 'workorder', '"{\\"job\\":\\"MilkCreature\\",\\"item_conditions\\":[{\\"condition\\":\\"AtLeast\\",\\"value\\":2,\\"flags\\":[\\"empty\\"],\\"item_type\\":\\"BUCKET\\"}]}"', ']'}}, {command='autonestbox', group='automation', mode='enable'}, - {command='autoshear', group='automation', mode='repeat', + {command='autoshear', help_command='workorder', group='automation', mode='repeat', desc='Automatically shear creatures that are ready for shearing.', params={'--time', '14', '--timeUnits', 'days', '--command', '[', 'workorder', 'ShearCreature', ']'}}, {command='autoslab', group='automation', mode='enable'}, @@ -39,7 +39,7 @@ COMMANDS_BY_IDX = { desc='Encourage dwarves to drop tattered clothing and grab new ones.', params={'--time', '1', '--timeUnits', 'months', '--command', '[', 'cleanowned', 'X', ']'}}, {command='nestboxes', group='automation', mode='enable'}, - {command='orders-sort', group='automation', mode='repeat', + {command='orders-sort', help_command='orders', group='automation', mode='repeat', desc='Sort manager orders by repeat frequency so one-time orders can be completed.', params={'--time', '1', '--timeUnits', 'days', '--command', '[', 'orders', 'sort', ']'}}, {command='prioritize', group='automation', mode='enable'}, @@ -77,7 +77,7 @@ COMMANDS_BY_IDX = { {command='hide-tutorials', group='gameplay', mode='system_enable'}, {command='light-aquifers-only', group='gameplay', mode='run'}, {command='misery', group='gameplay', mode='enable'}, - {command='orders-reevaluate', group='gameplay', mode='repeat', + {command='orders-reevaluate', help_command='orders', group='gameplay', mode='repeat', desc='Invalidates all work orders once a month, allowing conditions to be rechecked.', params={'--time', '1', '--timeUnits', 'months', '--command', '[', 'orders', 'recheck', ']'}}, {command='starvingdead', group='gameplay', mode='enable'}, From 93f2417686dec8f0ea1f9c6a33c7fa01e719e326 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sat, 13 Jan 2024 18:18:42 -0800 Subject: [PATCH 2/2] invert relationship between command tabs/subtabs --- gui/control-panel.lua | 442 +++++++++++++++++++++++------------------- 1 file changed, 243 insertions(+), 199 deletions(-) diff --git a/gui/control-panel.lua b/gui/control-panel.lua index 0b16c4a88c..b92add9d42 100644 --- a/gui/control-panel.lua +++ b/gui/control-panel.lua @@ -119,135 +119,22 @@ function ConfigPanel:on_select(_, choice) end --- --- CommandTab --- -CommandTab = defclass(CommandTab, ConfigPanel) -local Subtabs = { - automation=1, - bugfix=2, - gameplay=3, -} -local Subtabs_revmap = utils.invert(Subtabs) -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 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 -function CommandTab:init_main_panel(panel) - panel:addviews{ - widgets.TabBar{ - frame={t=5}, - key='CUSTOM_CTRL_N', - key_back='CUSTOM_CTRL_M', - labels={ - 'Automation', - 'Bug Fixes', - 'Gameplay', - }, - on_select=function(val) - self.subpage = val - self:updateLayout() - self:refresh() - end, - get_cur_page=function() return self.subpage end, - }, - widgets.WrappedLabel{ - frame={t=7}, - text_to_wrap=function() return self.blurbs[self.subpage] end, - }, - widgets.FilteredList{ - frame={t=9}, - view_id='list', - on_select=self:callback('on_select'), - on_double_click=self:callback('on_submit'), - row_height=2, - }, - } -end -function CommandTab:init_footer(panel) - panel:addviews{ - widgets.HotkeyLabel{ - frame={t=0, l=0}, - label='Toggle enabled', - key='SELECT', - auto_width=true, - on_activate=self:callback('on_submit') - }, - widgets.HotkeyLabel{ - frame={t=1, l=0}, - label='Show full tool help or run custom command', - auto_width=true, - key='CUSTOM_CTRL_H', - on_activate=self:callback('show_help'), - }, - } -end -local function launch_help(data) - dfhack.run_command('gui/launcher', data.help_command or data.command .. ' ') -end -function CommandTab:show_help() - _,choice = self.subviews.list:getSelected() - if not choice then return end - launch_help(choice.data) -end -function CommandTab:has_config() - _,choice = self.subviews.list:getSelected() - return choice and choice.gui_config -end + -- --- EnabledTab +-- Enabled subtab functions -- -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.', -} - -function EnabledTab:init() - if not dfhack.world.isFortressMode() then - self.subpage = Subtabs.gameplay - end - - self.subviews.list.list.on_double_click2=self:callback('launch_config') -end - -function EnabledTab:init_footer(panel) - EnabledTab.super.init_footer(self, panel) - panel:addviews{ - widgets.HotkeyLabel{ - frame={t=2, l=26}, - label='Launch tool-specific config UI', - key='CUSTOM_CTRL_G', - auto_width=true, - enabled=self:callback('has_config'), - on_activate=self:callback('launch_config'), - }, - } -end - local function get_gui_config(command) command = common.get_first_word(command) local gui_config = 'gui/' .. command @@ -292,16 +179,15 @@ local function make_enabled_text(self, command, mode, gui_config) } end -function EnabledTab:refresh() +local function get_enabled_choices(self) 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 + if not common.command_passes_filters(data, self.group) then goto continue end local gui_config = get_gui_config(data.command) table.insert(choices, { text=make_enabled_text(self, data.command, data.mode, gui_config), @@ -311,49 +197,30 @@ function EnabledTab:refresh() }) ::continue:: end - local list = self.subviews.list - local filter = list:getFilter() - local selected = list:getSelected() - list:setChoices(choices) - list:setFilter(filter, selected) - list.edit:setFocus(true) -end - -function EnabledTab:onInput(keys) - local handled = EnabledTab.super.onInput(self, keys) - if keys._MOUSE_L then - local list = self.subviews.list.list - local idx = list:getIdxUnderMouse() - if idx then - local x = list:getMousePos() - if x <= 2 then - self:on_submit() - elseif x >= 4 and x <= 6 then - self:show_help() - elseif x >= 8 and x <= 10 then - self:launch_config() - end + return choices +end + +local function enabled_onInput(self, keys) + if not keys._MOUSE_L then return end + local list = self.subviews.list.list + local idx = list:getIdxUnderMouse() + if idx then + local x = list:getMousePos() + if x <= 2 then + 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 - return handled end -function EnabledTab:launch_config() - _,choice = self.subviews.list:getSelected() - if not choice or not choice.gui_config then return end - dfhack.run_command(choice.gui_config) -end - -function EnabledTab:on_submit() - _,choice = self.subviews.list:getSelected() - if not choice then return end - local data = choice.data +local function enabled_on_submit(self, data) common.apply_command(data, self.enabled_map, not self.enabled_map[data.command]) - self:refresh() end -function EnabledTab:restore_defaults() - local group = Subtabs_revmap[self.subpage] +local function enabled_restore_defaults(self) 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') @@ -361,33 +228,17 @@ function EnabledTab:restore_defaults() 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) + if not common.command_passes_filters(data, self.group) then goto continue end + common.apply_command(data, self.enabled_map, not not data.default) ::continue:: end - self:refresh() - dialogs.showMessage('Success', - ('Enabled defaults restored for %s tools.'):format(group)) -end - --- pick up enablement changes made from other sources (e.g. gui config tools) -function EnabledTab:onRenderFrame(dc, rect) - self.enabled_map = common.get_enabled_map() - EnabledTab.super.onRenderFrame(self, dc, rect) end -- --- AutostartTab +-- Autostart subtab functions -- -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.', -} - local function make_autostart_text(label, mode, enabled) if mode == 'system_enable' then label = label .. ' (global)' @@ -405,11 +256,10 @@ local function make_autostart_text(label, mode, enabled) } end -function AutostartTab:refresh() +local function get_autostart_choices(self) local choices = {} - local group = Subtabs_revmap[self.subpage] for _,data in ipairs(registry.COMMANDS_BY_IDX) do - if not common.command_passes_filters(data, group) then goto continue end + if not common.command_passes_filters(data, self.group) then goto continue end local enabled = safe_index(common.config.data.commands, data.command, 'autostart') if enabled == nil then enabled = data.default @@ -422,16 +272,10 @@ function AutostartTab:refresh() }) ::continue:: end - local list = self.subviews.list - local filter = list:getFilter() - local selected = list:getSelected() - list:setChoices(choices) - list:setFilter(filter, selected) - list.edit:setFocus(true) + return choices end -function AutostartTab:onInput(keys) - local handled = EnabledTab.super.onInput(self, keys) +local function autostart_onInput(self, keys) if keys._MOUSE_L then local list = self.subviews.list.list local idx = list:getIdxUnderMouse() @@ -444,33 +288,227 @@ function AutostartTab:onInput(keys) end end end - return handled end -function AutostartTab:on_submit() - _,choice = self.subviews.list:getSelected() - if not choice then return end - local data = choice.data +local function autostart_on_submit(self, data) common.set_autostart(data, not choice.enabled) common.config:write() - self:refresh() end -function AutostartTab:restore_defaults() - local group = Subtabs_revmap[self.subpage] +local function autostart_restore_defaults(self) for _,data in ipairs(registry.COMMANDS_BY_IDX) do - if not common.command_passes_filters(data, group) then goto continue end + if not common.command_passes_filters(data, self.group) then goto continue end print(data.command, data.default) common.set_autostart(data, data.default) ::continue:: end common.config:write() +end + + +-- +-- CommandTab +-- + +CommandTab = defclass(CommandTab, ConfigPanel) +CommandTab.ATTRS { + group=DEFAULT_NIL, +} + +local Subtabs = { + enabled=1, + autostart=2, +} + +local subtab = Subtabs.enabled + +function CommandTab:init() + self.blurbs = { + [Subtabs.enabled]='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.', + [Subtabs.autostart]='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.', + } +end + +function CommandTab:init_main_panel(panel) + panel:addviews{ + widgets.TabBar{ + view_id='subtabbar', + frame={t=5}, + key='CUSTOM_CTRL_N', + key_back='CUSTOM_CTRL_M', + labels={ + 'Enabled', + 'Autostart', + }, + on_select=function(val) + subtab = val + self:updateLayout() + self:refresh() + end, + get_cur_page=function() return subtab end, + }, + widgets.WrappedLabel{ + frame={t=7}, + text_to_wrap=function() return self.blurbs[subtab] end, + }, + widgets.FilteredList{ + frame={t=9}, + view_id='list', + on_select=self:callback('on_select'), + on_double_click=self:callback('on_submit'), + on_double_click2=self:callback('launch_config'), + row_height=2, + }, + } +end + +function CommandTab:init_footer(panel) + panel:addviews{ + widgets.HotkeyLabel{ + frame={t=0, l=0}, + label='Toggle enabled', + key='SELECT', + auto_width=true, + on_activate=self:callback('on_submit') + }, + widgets.HotkeyLabel{ + frame={t=1, l=0}, + label='Show full tool help or run custom command', + auto_width=true, + key='CUSTOM_CTRL_H', + on_activate=self:callback('show_help'), + }, + widgets.HotkeyLabel{ + frame={t=2, l=26}, + label='Launch tool-specific config UI', + key='CUSTOM_CTRL_G', + auto_width=true, + enabled=self:callback('has_config'), + visible=function() return subtab == Subtabs.enabled end, + on_activate=self:callback('launch_config'), + }, + } +end + +local function launch_help(data) + dfhack.run_command('gui/launcher', data.help_command or data.command .. ' ') +end + +function CommandTab:show_help() + _,choice = self.subviews.list:getSelected() + if not choice then return end + launch_help(choice.data) +end + +function CommandTab:has_config() + _,choice = self.subviews.list:getSelected() + return choice and choice.gui_config +end + +function CommandTab:launch_config() + if subtab ~= Subtabs.enabled then return end + _,choice = self.subviews.list:getSelected() + if not choice or not choice.gui_config then return end + dfhack.run_command(choice.gui_config) +end + +function CommandTab:refresh() + local choices = subtab == Subtabs.enabled and + get_enabled_choices(self) or get_autostart_choices(self) + local list = self.subviews.list + local filter = list:getFilter() + local selected = list:getSelected() + list:setChoices(choices) + list:setFilter(filter, selected) + list.edit:setFocus(true) +end + +function CommandTab:on_submit() + _,choice = self.subviews.list:getSelected() + if not choice then return end + if subtab == Subtabs.enabled then + enabled_on_submit(self, choice.data) + else + autostart_on_submit(self, choice.data) + end + self:refresh() +end + +-- pick up enablement changes made from other sources (e.g. gui config tools) +function CommandTab:onRenderFrame(dc, rect) + if subtab == Subtabs.enabled then + self.enabled_map = common.get_enabled_map() + end + CommandTab.super.onRenderFrame(self, dc, rect) +end + +function CommandTab:onInput(keys) + local handled = CommandTab.super.onInput(self, keys) + if subtab == Subtabs.enabled then + enabled_onInput(self, keys) + else + autostart_onInput(self, keys) + end + return handled +end + +function CommandTab:restore_defaults() + if subtab == Subtabs.enabled then + enabled_restore_defaults(self) + else + autostart_restore_defaults(self) + end self:refresh() dialogs.showMessage('Success', - ('Autostart defaults restored for %s tools.'):format(group)) + ('%s defaults restored for %s tools.'):format( + self.subviews.subtabbar.labels[subtab], self.group)) end +-- +-- AutomationTab +-- + +AutomationTab = defclass(AutomationTab, CommandTab) +AutomationTab.ATTRS{ + intro_text='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.', + group='automation', +} + + +-- +-- BugFixesTab +-- + +BugFixesTab = defclass(BugFixesTab, CommandTab) +BugFixesTab.ATTRS{ + intro_text='These automatically fix dangerous or annoying vanilla'.. + ' bugs. You should generally have all of these enabled.', + group='bugfix', +} + + +-- +-- GameplayTab +-- + +GameplayTab = defclass(GameplayTab, CommandTab) +GameplayTab.ATTRS{ + intro_text='These change or extend gameplay. Read their help docs to'.. + ' see what they do and enable the ones that appeal to you.', + group='gameplay' +} + + -- -- OverlaysTab -- @@ -820,7 +858,7 @@ end ControlPanel = defclass(ControlPanel, widgets.Window) ControlPanel.ATTRS { frame_title='DFHack Control Panel', - frame={w=68, h=44}, + frame={w=74, h=44}, resizable=true, resize_min={h=39}, autoarrange_subviews=true, @@ -832,8 +870,9 @@ function ControlPanel:init() widgets.TabBar{ frame={t=0}, labels={ - 'Enabled', - 'Autostart', + 'Automation', + 'Bug Fixes', + 'Gameplay', 'UI Overlays', 'Preferences', }, @@ -844,14 +883,19 @@ function ControlPanel:init() view_id='pages', frame={t=5, l=0, b=0, r=0}, subviews={ - EnabledTab{}, - AutostartTab{}, + AutomationTab{}, + BugFixesTab{}, + GameplayTab{}, OverlaysTab{}, PreferencesTab{}, }, }, } + if not dfhack.world.isFortressMode() then + self.subviews.pages:setSelected(3) + end + self:refresh_page() end