Pull some minor stuff from mechanisms into main libs.

develop
Alexander Gavrilov 2012-08-24 13:49:22 +04:00
parent 296f82b02f
commit d52c54cc76
3 changed files with 28 additions and 26 deletions

@ -3,6 +3,8 @@
local _ENV = mkmodule('gui.dwarfmode')
local gui = require('gui')
local utils = require('utils')
local dscreen = dfhack.screen
local world_map = df.global.world.map
@ -187,6 +189,18 @@ function DwarfOverlay:getViewport(old_vp)
end
end
function DwarfOverlay:moveCursorTo(cursor,viewport)
setCursorPos(cursor)
self:getViewport(viewport):reveal(cursor, 5, 0, 10):set()
end
function DwarfOverlay:selectBuilding(building,cursor,viewport)
cursor = cursor or utils.getBuildingCenter(building)
df.global.world.selected_building = building
self:moveCursorTo(cursor, viewport)
end
function DwarfOverlay:propagateMoveKeys(keys)
for code,_ in pairs(MOVEMENT_KEYS) do
if keys[code] then

@ -373,6 +373,14 @@ function call_with_string(obj,methodname,...)
)
end
function getBuildingName(building)
return call_with_string(building, 'getName')
end
function getBuildingCenter(building)
return xyz2pos(building.centerx, building.centery, building.z)
end
-- Ask a yes-no question
function prompt_yes_no(msg,default)
local prompt = msg

@ -4,22 +4,13 @@ local utils = require 'utils'
local gui = require 'gui'
local guidm = require 'gui.dwarfmode'
function getBuildingName(building)
return utils.call_with_string(building, 'getName')
end
function getBuildingCenter(building)
return xyz2pos(building.centerx, building.centery, building.z)
end
function listMechanismLinks(building)
local lst = {}
local function push(item, mode)
if item then
lst[#lst+1] = {
obj = item, mode = mode,
name = getBuildingName(item),
center = getBuildingCenter(item)
name = utils.getBuildingName(item)
}
end
end
@ -64,8 +55,9 @@ end
function MechanismList:fillList(building)
local links = listMechanismLinks(building)
links[1].viewport = self:getViewport()
links[1].cursor = guidm.getCursorPos()
self.old_viewport = self:getViewport()
self.old_cursor = guidm.getCursorPos()
if #links <= 1 then
links[1].mode = 'none'
end
@ -103,22 +95,10 @@ function MechanismList:onRenderBody(dc)
dc:string("Enter", COLOR_LIGHTGREEN):string(": Switch")
end
function MechanismList:zoomToLink(link,back)
df.global.world.selected_building = link.obj
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()
end
end
function MechanismList:changeSelected(delta)
if #self.links <= 1 then return end
self.selected = 1 + (self.selected + delta - 1) % #self.links
self:zoomToLink(self.links[self.selected])
self:selectBuilding(self.links[self.selected].obj)
end
function MechanismList:onInput(keys)
@ -129,7 +109,7 @@ function MechanismList:onInput(keys)
elseif keys.LEAVESCREEN then
self:dismiss()
if self.selected ~= 1 then
self:zoomToLink(self.links[1], true)
self:selectBuilding(self.links[1].obj, self.old_cursor, self.old_view)
end
elseif keys.SELECT_ALL then
if self.selected > 1 then