gm-editor: Added enum printing and selection.

gm-editor: a bugfix for some entries not having fields
develop
Warmist 2014-03-08 10:09:53 +02:00
parent 415cdad489
commit 401c8f3026
1 changed files with 47 additions and 2 deletions

@ -11,6 +11,7 @@ local keybindings={
insert={key="CUSTOM_ALT_I",desc="Insert a new value to the vector"},
delete={key="CUSTOM_ALT_D",desc="Delete selected entry"},
help={key="HELP",desc="Show this help"},
NOT_USED={key="SEC_SELECT",desc="Choose an enum value from a list"}, --not a binding...
}
function getTargetFromScreens()
local my_trg
@ -75,6 +76,7 @@ function GmEditorUi:init(args)
local helpPage=widgets.Panel{
subviews={widgets.Label{text=helptext,frame = {l=1,t=1,yalign=0}}}}
local mainList=widgets.List{view_id="list_main",choices={},frame = {l=1,t=3,yalign=0},on_submit=self:callback("editSelected"),
on_submit2=self:callback("editSelectedEnum"),
text_pen=dfhack.pen.parse{fg=COLOR_DARKGRAY,bg=0},cursor_pen=dfhack.pen.parse{fg=COLOR_YELLOW,bg=0}}
local mainPage=widgets.Panel{
subviews={
@ -155,6 +157,29 @@ end
function GmEditorUi:currentTarget()
return self.stack[#self.stack]
end
function GmEditorUi:editSelectedEnum(index,choice)
local trg=self:currentTarget()
local trg_key=trg.keys[index]
if trg.target._field==nil then qerror("not an enum") end
local enum=trg.target:_field(trg_key)._type
if enum._kind=="enum-type" then
local list={}
for i=enum._first_item, enum._last_item do
table.insert(list,{text=tostring(enum[i]),value=i})
end
guiScript.start(function()
local ret,idx,choice=guiScript.showListPrompt("Choose item:",nil,3,list)
if ret then
trg.target[trg_key]=choice.value
self:updateTarget(true)
end
end)
else
qerror("not an enum")
end
end
function GmEditorUi:editSelected(index,choice)
local trg=self:currentTarget()
local trg_key=trg.keys[index]
@ -206,7 +231,6 @@ function GmEditorUi:set(key,input)
self:updateTarget(true)
end
function GmEditorUi:onInput(keys)
if keys.LEAVESCREEN then
if self.subviews.pages:getSelected()==2 then
self.subviews.pages:setSelected(1)
@ -233,6 +257,27 @@ function GmEditorUi:onInput(keys)
self.super.onInput(self,keys)
end
function getStringValue(trg,field)
local obj=trg.target
local text=tostring(obj[field])
pcall(function()
if obj._field ~= nil then
local enum=obj:_field(field)._type
if enum._kind=="enum-type" then
text=text.."("..tostring(enum[obj[field]])..")"
end
end
end)
return text
--[[local ok,ret=pcall(function() return trg.target[field]._enum end)
if ok then
print(trg.target[field]._kind,ok,ret)
text=text.."("..tostring(ret[field])..")"
end
return text]]
end
function GmEditorUi:updateTarget(preserve_pos,reindex)
local trg=self:currentTarget()
if reindex then
@ -244,7 +289,7 @@ function GmEditorUi:updateTarget(preserve_pos,reindex)
self.subviews.lbl_current_item:itemById('name').text=tostring(trg.target)
local t={}
for k,v in pairs(trg.keys) do
table.insert(t,{text={{text=string.format("%-25s",tostring(v))},{gap=1,text=tostring(trg.target[v]),}}})
table.insert(t,{text={{text=string.format("%-25s",tostring(v))},{gap=1,text=getStringValue(trg,v)}}})
end
local last_pos
if preserve_pos then