Skip to content

Commit

Permalink
use toggle widgets for enablement state
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Mar 7, 2024
1 parent 00b791e commit 6b7b65d
Showing 1 changed file with 30 additions and 38 deletions.
68 changes: 30 additions & 38 deletions gui/autobutcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,17 @@ local function sort_by_ordered_asc(a, b)
return a.ordered < b.ordered
end

function nextAutowatchState()
if plugin.autowatch_isEnabled() then
return 'Stop '
end
return 'Start'
end

function nextAutobutcherState()
if plugin.isEnabled() then
return 'Stop '
end
return 'Start'
end

function WatchList:init()
if plugin.isEnabled() then
-- ensure slaughter counts and autowatch are up to date
dfhack.run_command('autobutcher', 'now')
end

local window = widgets.Window{
frame_title = 'Autobutcher Watchlist',
frame = { w=97, h=30 },
resizable = true,
subviews = {
frame_title='Autobutcher',
frame={w=97, h=30},
resizable=true,
subviews={
widgets.CycleHotkeyLabel{
view_id='sort',
frame={l=0, t=0, w=31},
Expand Down Expand Up @@ -416,19 +402,29 @@ function WatchList:init()
auto_width=true,
on_activate=self:callback('onSetRow'),
},
widgets.HotkeyLabel{
frame={t=5, l=0},
widgets.ToggleHotkeyLabel{
view_id='enable_toggle',
frame={t=5, l=0, w=26},
label='Autobutcher is',
key='CUSTOM_SHIFT_A',
label=function() return nextAutobutcherState() .. ' Autobutcher' end,
auto_width=true,
on_activate=self:callback('onToggleAutobutcher'),
options={{value=true, label='Enabled', pen=COLOR_GREEN},
{value=false, label='Disabled', pen=COLOR_RED}},
on_change=function(val)
plugin.setEnabled(val)
self:refresh()
end,
},
widgets.HotkeyLabel{
frame={t=5, l=24},
widgets.ToggleHotkeyLabel{
view_id='autowatch_toggle',
frame={t=5, l=43, w=24},
label='Autowatch is',
key='CUSTOM_SHIFT_W',
label=function() return nextAutowatchState() .. ' Autowatch' end,
auto_width=true,
on_activate=self:callback('onToggleAutowatch'),
options={{value=true, label='Enabled', pen=COLOR_GREEN},
{value=false, label='Disabled', pen=COLOR_RED}},
on_change=function(val)
plugin.autowatch_setEnabled(val)
self:refresh()
end,
},
},
},
Expand All @@ -439,6 +435,12 @@ function WatchList:init()
self:refresh()
end

function WatchList:onRenderFrame(dc, rect)
self.subviews.enable_toggle:setOption(plugin.isEnabled())
self.subviews.autowatch_toggle:setOption(plugin.autowatch_isEnabled())
WatchList.super.onRenderFrame(self, dc, rect)
end

function stringify(number)
if not number then return '' end
-- cap displayed number to 2 characters to fit in the column width
Expand Down Expand Up @@ -707,16 +709,6 @@ function WatchList:onButcherRace()
self:refresh()
end

function WatchList:onToggleAutobutcher()
plugin.setEnabled(not plugin.isEnabled())
self:refresh()
end

function WatchList:onToggleAutowatch()
plugin.autowatch_setEnabled(not plugin.autowatch_isEnabled())
self:refresh()
end

function WatchList:onDismiss()
view = nil
end
Expand Down

0 comments on commit 6b7b65d

Please sign in to comment.