gm-editor: Define some aliases for useful variables

develop
lethosor 2015-07-24 13:58:09 -04:00
parent f1a6e5fbe9
commit 726ffd6417
2 changed files with 34 additions and 5 deletions

@ -49,7 +49,10 @@ DFHack Future
- weather display now separated from the date display - weather display now separated from the date display
- New mouse cursor widget - New mouse cursor widget
full-heal: "-r" option removes corpses full-heal: "-r" option removes corpses
gui/gm-editor: Pointers can now be displaced gui/gm-editor
- Pointers can now be displaced
- Added some useful aliases: "item" for the selected item, "screen" for the current screen, etc.
- Now avoids errors with unrecognized types
gui/hack-wish: renamed to gui/create-item gui/hack-wish: renamed to gui/create-item
"keybinding list" accepts a context "keybinding list" accepts a context
lever: lever:

@ -228,6 +228,8 @@ function GmEditorUi:editSelected(index,choice)
self:updateTarget(true) self:updateTarget(true)
elseif trg_type == 'userdata' or trg_type == 'table' then elseif trg_type == 'userdata' or trg_type == 'table' then
self:pushTarget(trg.target[trg_key]) self:pushTarget(trg.target[trg_key])
elseif trg_type == 'nil' or trg_type == 'function' then
-- ignore
else else
print("Unknown type:"..trg_type) print("Unknown type:"..trg_type)
pcall(function() print("Subtype:"..tostring(trg.target[trg_key]._kind)) end) pcall(function() print("Subtype:"..tostring(trg.target[trg_key]._kind)) end)
@ -379,21 +381,45 @@ function GmEditorUi:popTarget()
self:updateTarget() self:updateTarget()
end end
function show_editor(trg) function show_editor(trg)
if not trg then
qerror('Target not found')
end
local screen = GmEditorUi{target=trg} local screen = GmEditorUi{target=trg}
screen:show() screen:show()
end end
eval_env = {}
setmetatable(eval_env, {__index = function(_, k)
if k == 'scr' or k == 'screen' then
return dfhack.gui.getCurViewscreen()
elseif k == 'bld' or k == 'building' then
return dfhack.gui.getSelectedBuilding()
elseif k == 'item' then
return dfhack.gui.getSelectedItem()
elseif k == 'job' then
return dfhack.gui.getSelectedJob()
elseif k == 'wsjob' or k == 'workshop_job' then
return dfhack.gui.getSelectedWorkshopJob()
elseif k == 'unit' then
return dfhack.gui.getSelectedUnit()
else
return _G[k]
end
end})
function eval(s)
local f, err = load("return " .. s, "expression", "t", eval_env)
if err then qerror(err) end
return f()
end
if #args~=0 then if #args~=0 then
if args[1]=="dialog" then if args[1]=="dialog" then
function thunk(entry) function thunk(entry)
local t=load("return "..entry)() show_editor(eval(entry))
show_editor(t)
end end
dialog.showInputPrompt("Gm Editor", "Object to edit:", COLOR_GRAY, "",thunk) dialog.showInputPrompt("Gm Editor", "Object to edit:", COLOR_GRAY, "",thunk)
elseif args[1]=="free" then elseif args[1]=="free" then
show_editor(df.reinterpret_cast(df[args[2]],args[3])) show_editor(df.reinterpret_cast(df[args[2]],args[3]))
else else
local t=load("return "..args[1])() show_editor(eval(args[1]))
show_editor(t)
end end
else else
show_editor(getTargetFromScreens()) show_editor(getTargetFromScreens())