2023-01-22 04:08:36 -07:00
|
|
|
local _ENV = mkmodule('plugins.orders')
|
|
|
|
|
|
|
|
local dialogs = require('gui.dialogs')
|
|
|
|
local gui = require('gui')
|
|
|
|
local overlay = require('plugins.overlay')
|
|
|
|
local widgets = require('gui.widgets')
|
|
|
|
|
|
|
|
--
|
|
|
|
-- OrdersOverlay
|
|
|
|
--
|
|
|
|
|
|
|
|
local function do_sort()
|
|
|
|
dfhack.run_command('orders', 'sort')
|
|
|
|
end
|
|
|
|
|
|
|
|
local function do_clear()
|
|
|
|
dialogs.showYesNoPrompt('Clear manager orders?',
|
|
|
|
'Are you sure you want to clear the manager orders?', nil,
|
|
|
|
function() dfhack.run_command('orders', 'clear') end)
|
|
|
|
end
|
|
|
|
|
2023-03-27 04:52:12 -06:00
|
|
|
local function get_import_choices()
|
|
|
|
return dfhack.run_command_silent('orders', 'list'):split('\n')
|
|
|
|
end
|
|
|
|
|
2023-01-22 04:08:36 -07:00
|
|
|
local function do_import()
|
2023-03-27 04:52:12 -06:00
|
|
|
local dlg
|
|
|
|
local function get_dlg() return dlg end
|
|
|
|
dlg = dialogs.ListBox{
|
|
|
|
frame_title='Import/Delete Manager Orders',
|
2023-01-22 04:08:36 -07:00
|
|
|
with_filter=true,
|
2023-03-27 04:52:12 -06:00
|
|
|
choices=get_import_choices(),
|
|
|
|
on_select=function(_, choice)
|
2023-01-22 04:08:36 -07:00
|
|
|
dfhack.run_command('orders', 'import', choice.text)
|
|
|
|
end,
|
2023-03-27 04:52:12 -06:00
|
|
|
dismiss_on_select2=false,
|
|
|
|
on_select2=function(_, choice)
|
|
|
|
if choice.text:startswith('library/') then return end
|
|
|
|
local fname = 'dfhack-config/orders/'..choice.text..'.json'
|
|
|
|
if not dfhack.filesystem.isfile(fname) then return end
|
|
|
|
dialogs.showYesNoPrompt('Delete orders file?',
|
|
|
|
'Are you sure you want to delete "' .. fname .. '"?', nil,
|
|
|
|
function()
|
|
|
|
print('deleting ' .. fname)
|
|
|
|
os.remove(fname)
|
|
|
|
local list = get_dlg().subviews.list
|
|
|
|
local filter = list:getFilter()
|
|
|
|
list:setChoices(get_import_choices(), list:getSelected())
|
|
|
|
list:setFilter(filter)
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
select2_hint='Delete file',
|
2023-01-22 04:08:36 -07:00
|
|
|
}:show()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function do_export()
|
|
|
|
dialogs.InputBox{
|
|
|
|
frame_title='Export Manager Orders',
|
|
|
|
on_input=function(text)
|
|
|
|
dfhack.run_command('orders', 'export', text)
|
|
|
|
end
|
|
|
|
}:show()
|
|
|
|
end
|
|
|
|
|
2023-09-18 04:41:02 -06:00
|
|
|
local function do_recheck()
|
|
|
|
dfhack.run_command('orders', 'recheck')
|
2023-09-15 16:24:47 -06:00
|
|
|
end
|
|
|
|
|
2023-01-22 04:08:36 -07:00
|
|
|
OrdersOverlay = defclass(OrdersOverlay, overlay.OverlayWidget)
|
|
|
|
OrdersOverlay.ATTRS{
|
2023-01-23 09:01:45 -07:00
|
|
|
default_pos={x=53,y=-6},
|
2023-01-26 01:53:57 -07:00
|
|
|
default_enabled=true,
|
2023-06-19 17:48:07 -06:00
|
|
|
viewscreens='dwarfmode/Info/WORK_ORDERS/Default',
|
2023-09-29 08:34:48 -06:00
|
|
|
frame={w=46, h=4},
|
2023-01-22 04:08:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function OrdersOverlay:init()
|
2023-03-23 03:17:36 -06:00
|
|
|
self.minimized = false
|
|
|
|
|
|
|
|
local main_panel = widgets.Panel{
|
|
|
|
frame={t=0, l=0, r=0, h=4},
|
|
|
|
frame_style=gui.MEDIUM_FRAME,
|
|
|
|
frame_background=gui.CLEAR_PEN,
|
|
|
|
visible=function() return not self.minimized end,
|
|
|
|
subviews={
|
|
|
|
widgets.HotkeyLabel{
|
|
|
|
frame={t=0, l=0},
|
|
|
|
label='import',
|
|
|
|
key='CUSTOM_CTRL_I',
|
|
|
|
auto_width=true,
|
|
|
|
on_activate=do_import,
|
|
|
|
},
|
|
|
|
widgets.HotkeyLabel{
|
|
|
|
frame={t=1, l=0},
|
|
|
|
label='export',
|
|
|
|
key='CUSTOM_CTRL_E',
|
|
|
|
auto_width=true,
|
|
|
|
on_activate=do_export,
|
|
|
|
},
|
|
|
|
widgets.HotkeyLabel{
|
|
|
|
frame={t=0, l=15},
|
2023-09-18 04:41:02 -06:00
|
|
|
label='recheck',
|
2023-10-01 00:31:56 -06:00
|
|
|
key='CUSTOM_CTRL_K',
|
2023-09-16 05:18:06 -06:00
|
|
|
auto_width=true,
|
2023-09-18 04:41:02 -06:00
|
|
|
on_activate=do_recheck,
|
2023-09-16 05:18:06 -06:00
|
|
|
},
|
|
|
|
widgets.HotkeyLabel{
|
|
|
|
frame={t=1, l=15},
|
2023-09-29 08:34:48 -06:00
|
|
|
label='sort',
|
2023-03-23 03:17:36 -06:00
|
|
|
key='CUSTOM_CTRL_O',
|
|
|
|
auto_width=true,
|
|
|
|
on_activate=do_sort,
|
|
|
|
},
|
|
|
|
widgets.HotkeyLabel{
|
2023-09-29 08:34:48 -06:00
|
|
|
frame={t=0, l=31},
|
|
|
|
label='clear',
|
|
|
|
key='CUSTOM_CTRL_C',
|
2023-09-15 16:24:47 -06:00
|
|
|
auto_width=true,
|
2023-09-29 08:34:48 -06:00
|
|
|
on_activate=do_clear,
|
2023-09-15 16:24:47 -06:00
|
|
|
},
|
2023-01-22 04:08:36 -07:00
|
|
|
},
|
2023-03-23 03:17:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
local minimized_panel = widgets.Panel{
|
|
|
|
frame={t=0, r=0, w=3, h=1},
|
|
|
|
subviews={
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=0, l=0, w=1, h=1},
|
|
|
|
text='[',
|
|
|
|
text_pen=COLOR_RED,
|
|
|
|
visible=function() return self.minimized end,
|
|
|
|
},
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=0, l=1, w=1, h=1},
|
|
|
|
text={{text=function() return self.minimized and string.char(31) or string.char(30) end}},
|
|
|
|
text_pen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_GREY},
|
|
|
|
text_hpen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_WHITE},
|
|
|
|
on_click=function() self.minimized = not self.minimized end,
|
|
|
|
},
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=0, r=0, w=1, h=1},
|
|
|
|
text=']',
|
|
|
|
text_pen=COLOR_RED,
|
|
|
|
visible=function() return self.minimized end,
|
|
|
|
},
|
2023-01-22 04:08:36 -07:00
|
|
|
},
|
|
|
|
}
|
2023-03-23 03:17:36 -06:00
|
|
|
|
|
|
|
self:addviews{
|
|
|
|
main_panel,
|
|
|
|
minimized_panel,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function OrdersOverlay:onInput(keys)
|
2023-06-19 17:48:07 -06:00
|
|
|
if df.global.game.main_interface.job_details.open then return end
|
2023-03-23 03:17:36 -06:00
|
|
|
if keys.CUSTOM_ALT_M then
|
|
|
|
self.minimized = not self.minimized
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if OrdersOverlay.super.onInput(self, keys) then
|
|
|
|
return true
|
|
|
|
end
|
2023-01-22 04:08:36 -07:00
|
|
|
end
|
|
|
|
|
2023-06-19 17:48:07 -06:00
|
|
|
function OrdersOverlay:render(dc)
|
|
|
|
if df.global.game.main_interface.job_details.open then return end
|
|
|
|
OrdersOverlay.super.render(self, dc)
|
|
|
|
end
|
|
|
|
|
2023-09-22 09:29:45 -06:00
|
|
|
-- Resets the selected work order to the `Checking` state
|
|
|
|
|
|
|
|
local function set_current_inactive()
|
|
|
|
local scrConditions = df.global.game.main_interface.info.work_orders.conditions
|
|
|
|
if scrConditions.open then
|
|
|
|
dfhack.run_command('orders', 'recheck', 'this')
|
|
|
|
else
|
|
|
|
qerror("Order conditions is not open")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_current_active()
|
|
|
|
local scrConditions = df.global.game.main_interface.info.work_orders.conditions
|
|
|
|
local order = scrConditions.wq
|
|
|
|
return order.status.active
|
|
|
|
end
|
|
|
|
|
|
|
|
-- -------------------
|
|
|
|
-- RecheckOverlay
|
|
|
|
--
|
|
|
|
|
|
|
|
local focusString = 'dwarfmode/Info/WORK_ORDERS/Conditions'
|
|
|
|
|
|
|
|
RecheckOverlay = defclass(RecheckOverlay, overlay.OverlayWidget)
|
|
|
|
RecheckOverlay.ATTRS{
|
|
|
|
default_pos={x=6,y=8},
|
|
|
|
default_enabled=true,
|
|
|
|
viewscreens=focusString,
|
|
|
|
-- width is the sum of lengths of `[` + `Ctrl+A` + `: ` + button.label + `]`
|
|
|
|
frame={w=1 + 6 + 2 + 16 + 1, h=3},
|
|
|
|
}
|
|
|
|
|
|
|
|
local function areTabsInTwoRows()
|
|
|
|
-- get the tile above the order status icon
|
|
|
|
local pen = dfhack.screen.readTile(7, 7, false)
|
|
|
|
-- in graphics mode, `0` when one row, something else when two (`67` aka 'C' from "Creatures")
|
|
|
|
-- in ASCII mode, `32` aka ' ' when one row, something else when two (`196` aka '-' from tab frame's top)
|
|
|
|
return (pen.ch ~= 0 and pen.ch ~= 32)
|
|
|
|
end
|
|
|
|
|
|
|
|
function RecheckOverlay:updateTextButtonFrame()
|
|
|
|
local twoRows = areTabsInTwoRows()
|
|
|
|
if (self._twoRows == twoRows) then return false end
|
|
|
|
|
|
|
|
self._twoRows = twoRows
|
|
|
|
local frame = twoRows
|
|
|
|
and {b=0, l=0, r=0, h=1}
|
|
|
|
or {t=0, l=0, r=0, h=1}
|
|
|
|
self.subviews.button.frame = frame
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function RecheckOverlay:init()
|
|
|
|
self:addviews{
|
|
|
|
widgets.TextButton{
|
|
|
|
view_id = 'button',
|
|
|
|
-- frame={t=0, l=0, r=0, h=1}, -- is set in `updateTextButtonFrame()`
|
|
|
|
label='request re-check',
|
|
|
|
key='CUSTOM_CTRL_A',
|
|
|
|
on_activate=set_current_inactive,
|
|
|
|
enabled=is_current_active,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
self:updateTextButtonFrame()
|
|
|
|
end
|
|
|
|
|
|
|
|
function RecheckOverlay:onRenderBody(dc)
|
|
|
|
if (self.frame_rect.y1 == 7) then
|
|
|
|
-- only apply this logic if the overlay is on the same row as
|
|
|
|
-- originally thought: just above the order status icon
|
|
|
|
|
|
|
|
if self:updateTextButtonFrame() then
|
|
|
|
self:updateLayout()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RecheckOverlay.super.onRenderBody(self, dc)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- -------------------
|
|
|
|
|
2023-01-22 04:08:36 -07:00
|
|
|
OVERLAY_WIDGETS = {
|
2023-09-22 09:29:45 -06:00
|
|
|
recheck=RecheckOverlay,
|
2023-01-22 04:08:36 -07:00
|
|
|
overlay=OrdersOverlay,
|
|
|
|
}
|
|
|
|
|
|
|
|
return _ENV
|