2012-08-20 13:04:01 -06:00
|
|
|
-- Shows mechanisms linked to the current building.
|
|
|
|
|
2012-08-21 01:35:39 -06:00
|
|
|
local utils = require 'utils'
|
2012-08-20 13:04:01 -06:00
|
|
|
local gui = require 'gui'
|
|
|
|
local guidm = require 'gui.dwarfmode'
|
|
|
|
|
|
|
|
function getBuildingName(building)
|
2012-08-21 01:35:39 -06:00
|
|
|
return utils.call_with_string(building, 'getName')
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
|
|
|
|
2012-08-22 12:28:52 -06:00
|
|
|
function getBuildingCenter(building)
|
|
|
|
return xyz2pos(building.centerx, building.centery, building.z)
|
|
|
|
end
|
|
|
|
|
2012-08-20 13:04:01 -06:00
|
|
|
function listMechanismLinks(building)
|
|
|
|
local lst = {}
|
|
|
|
local function push(item, mode)
|
|
|
|
if item then
|
2012-08-22 12:28:52 -06:00
|
|
|
lst[#lst+1] = {
|
|
|
|
obj = item, mode = mode,
|
|
|
|
name = getBuildingName(item),
|
|
|
|
center = getBuildingCenter(item)
|
|
|
|
}
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
push(building, 'self')
|
|
|
|
|
|
|
|
if not df.building_actual:is_instance(building) then
|
|
|
|
return lst
|
|
|
|
end
|
|
|
|
|
|
|
|
local item, tref, tgt
|
|
|
|
for _,v in ipairs(building.contained_items) do
|
|
|
|
item = v.item
|
|
|
|
if df.item_trappartsst:is_instance(item) then
|
|
|
|
tref = dfhack.items.getGeneralRef(item, df.general_ref_type.BUILDING_TRIGGER)
|
|
|
|
if tref then
|
|
|
|
push(tref:getBuilding(), 'trigger')
|
|
|
|
end
|
|
|
|
tref = dfhack.items.getGeneralRef(item, df.general_ref_type.BUILDING_TRIGGERTARGET)
|
|
|
|
if tref then
|
|
|
|
push(tref:getBuilding(), 'target')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return lst
|
|
|
|
end
|
|
|
|
|
2012-08-21 01:35:39 -06:00
|
|
|
MechanismList = defclass(MechanismList, guidm.MenuOverlay)
|
2012-08-20 13:04:01 -06:00
|
|
|
|
|
|
|
MechanismList.focus_path = 'mechanisms'
|
|
|
|
|
|
|
|
function MechanismList.new(building)
|
|
|
|
local self = {
|
2012-08-21 10:34:15 -06:00
|
|
|
links = {},
|
2012-08-20 13:04:01 -06:00
|
|
|
selected = 1
|
|
|
|
}
|
2012-08-21 10:34:15 -06:00
|
|
|
return mkinstance(MechanismList, self):init(building)
|
|
|
|
end
|
|
|
|
|
|
|
|
function MechanismList:init(building)
|
|
|
|
local links = listMechanismLinks(building)
|
|
|
|
|
2012-08-22 12:28:52 -06:00
|
|
|
links[1].viewport = self:getViewport()
|
2012-08-21 01:35:39 -06:00
|
|
|
links[1].cursor = guidm.getCursorPos()
|
|
|
|
if #links <= 1 then
|
|
|
|
links[1].mode = 'none'
|
|
|
|
end
|
2012-08-21 10:34:15 -06:00
|
|
|
|
|
|
|
self.links = links
|
|
|
|
self.selected = 1
|
|
|
|
return self
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local colors = {
|
2012-08-21 01:35:39 -06:00
|
|
|
self = COLOR_CYAN, none = COLOR_CYAN,
|
|
|
|
trigger = COLOR_GREEN, target = COLOR_GREEN
|
2012-08-20 13:04:01 -06:00
|
|
|
}
|
|
|
|
local icons = {
|
2012-08-21 01:35:39 -06:00
|
|
|
self = 128, none = 63, trigger = 27, target = 26
|
2012-08-20 13:04:01 -06:00
|
|
|
}
|
|
|
|
|
2012-08-21 01:35:39 -06:00
|
|
|
function MechanismList:onRenderBody(dc)
|
|
|
|
dc:clear()
|
|
|
|
dc:seek(1,1):string("Mechanism Links", COLOR_WHITE):newline()
|
2012-08-20 13:04:01 -06:00
|
|
|
|
|
|
|
for i,v in ipairs(self.links) do
|
|
|
|
local pen = { fg=colors[v.mode], bold = (i == self.selected) }
|
2012-08-21 01:35:39 -06:00
|
|
|
dc:newline(1):pen(pen):char(icons[v.mode])
|
|
|
|
dc:advance(1):string(v.name)
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local nlinks = #self.links
|
|
|
|
|
|
|
|
if nlinks <= 1 then
|
2012-08-21 01:35:39 -06:00
|
|
|
dc:newline():newline(1):string("This building has no links", COLOR_LIGHTRED)
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
|
|
|
|
2012-08-21 01:35:39 -06:00
|
|
|
dc:newline():newline(1):pen(COLOR_WHITE)
|
|
|
|
dc:string("Esc", COLOR_LIGHTGREEN):string(": Back, ")
|
|
|
|
dc:string("Enter", COLOR_LIGHTGREEN):string(": Switch")
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
|
|
|
|
2012-08-22 12:28:52 -06:00
|
|
|
function MechanismList:zoomToLink(link,back)
|
2012-08-20 13:04:01 -06:00
|
|
|
df.global.world.selected_building = link.obj
|
|
|
|
|
2012-08-22 12:28:52 -06:00
|
|
|
if back then
|
|
|
|
guidm.setCursorPos(link.cursor)
|
|
|
|
self:getViewport(link.viewport):set()
|
|
|
|
else
|
|
|
|
guidm.setCursorPos(link.center)
|
|
|
|
self:getViewport():reveal(link.center, 5, 0, 10):set()
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MechanismList:changeSelected(delta)
|
2012-08-20 13:59:54 -06:00
|
|
|
if #self.links <= 1 then return end
|
2012-08-20 13:04:01 -06:00
|
|
|
self.selected = 1 + (self.selected + delta - 1) % #self.links
|
|
|
|
self:zoomToLink(self.links[self.selected])
|
|
|
|
end
|
|
|
|
|
|
|
|
function MechanismList:onInput(keys)
|
2012-08-22 12:28:52 -06:00
|
|
|
if keys.SECONDSCROLL_UP then
|
2012-08-20 13:04:01 -06:00
|
|
|
self:changeSelected(-1)
|
2012-08-22 12:28:52 -06:00
|
|
|
elseif keys.SECONDSCROLL_DOWN then
|
2012-08-20 13:04:01 -06:00
|
|
|
self:changeSelected(1)
|
|
|
|
elseif keys.LEAVESCREEN then
|
2012-08-21 01:35:39 -06:00
|
|
|
self:dismiss()
|
2012-08-20 13:04:01 -06:00
|
|
|
if self.selected ~= 1 then
|
2012-08-22 12:28:52 -06:00
|
|
|
self:zoomToLink(self.links[1], true)
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
2012-08-21 10:34:15 -06:00
|
|
|
elseif keys.SELECT_ALL then
|
|
|
|
if self.selected > 1 then
|
|
|
|
self:init(self.links[self.selected].obj)
|
|
|
|
end
|
2012-08-20 13:04:01 -06:00
|
|
|
elseif keys.SELECT then
|
|
|
|
self:dismiss()
|
2012-08-22 12:28:52 -06:00
|
|
|
elseif self:simulateViewScroll(keys) then
|
|
|
|
return
|
2012-08-20 13:04:01 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not df.viewscreen_dwarfmodest:is_instance(dfhack.gui.getCurViewscreen()) then
|
|
|
|
qerror("This script requires the main dwarfmode view")
|
|
|
|
end
|
|
|
|
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding then
|
|
|
|
qerror("This script requires the 'q' interface mode")
|
|
|
|
end
|
|
|
|
|
2012-08-20 13:59:54 -06:00
|
|
|
local list = MechanismList.new(df.global.world.selected_building)
|
|
|
|
list:show()
|
|
|
|
list:changeSelected(1)
|