Merge gm-editor changes by warmist into develop

develop
Alexander Gavrilov 2014-05-02 17:57:10 +04:00
commit 6bb57c7b08
1 changed files with 55 additions and 5 deletions

@ -2,6 +2,7 @@
local gui = require 'gui' local gui = require 'gui'
local dialog = require 'gui.dialogs' local dialog = require 'gui.dialogs'
local widgets =require 'gui.widgets' local widgets =require 'gui.widgets'
local guiScript = require 'gui.script'
local args={...} local args={...}
local keybindings={ local keybindings={
@ -10,7 +11,9 @@ local keybindings={
lua_set={key="CUSTOM_ALT_S",desc="Set by using a lua function"}, lua_set={key="CUSTOM_ALT_S",desc="Set by using a lua function"},
insert={key="CUSTOM_ALT_I",desc="Insert a new value to the vector"}, insert={key="CUSTOM_ALT_I",desc="Insert a new value to the vector"},
delete={key="CUSTOM_ALT_D",desc="Delete selected entry"}, delete={key="CUSTOM_ALT_D",desc="Delete selected entry"},
reinterpret={key="CUSTOM_ALT_R",desc="Open selected entry as something else"},
help={key="HELP",desc="Show this help"}, 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() function getTargetFromScreens()
local my_trg local my_trg
@ -75,6 +78,7 @@ function GmEditorUi:init(args)
local helpPage=widgets.Panel{ local helpPage=widgets.Panel{
subviews={widgets.Label{text=helptext,frame = {l=1,t=1,yalign=0}}}} 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"), 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}} text_pen=dfhack.pen.parse{fg=COLOR_DARKGRAY,bg=0},cursor_pen=dfhack.pen.parse{fg=COLOR_YELLOW,bg=0}}
local mainPage=widgets.Panel{ local mainPage=widgets.Panel{
subviews={ subviews={
@ -155,6 +159,37 @@ end
function GmEditorUi:currentTarget() function GmEditorUi:currentTarget()
return self.stack[#self.stack] return self.stack[#self.stack]
end 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:openReinterpret(key)
local trg=self:currentTarget()
dialog.showInputPrompt(tostring(trg_key),"Enter new type:",COLOR_WHITE,
"",function(choice)
local ntype=df[tp]
self:pushTarget(df.reinterpret_cast(ntype,trg.target[key]))
end)
end
function GmEditorUi:editSelected(index,choice) function GmEditorUi:editSelected(index,choice)
local trg=self:currentTarget() local trg=self:currentTarget()
local trg_key=trg.keys[index] local trg_key=trg.keys[index]
@ -206,7 +241,6 @@ function GmEditorUi:set(key,input)
self:updateTarget(true) self:updateTarget(true)
end end
function GmEditorUi:onInput(keys) function GmEditorUi:onInput(keys)
if keys.LEAVESCREEN then if keys.LEAVESCREEN then
if self.subviews.pages:getSelected()==2 then if self.subviews.pages:getSelected()==2 then
self.subviews.pages:setSelected(1) self.subviews.pages:setSelected(1)
@ -218,21 +252,35 @@ function GmEditorUi:onInput(keys)
local _,stoff=df.sizeof(trg.target) local _,stoff=df.sizeof(trg.target)
local size,off=df.sizeof(trg.target:_field(self:getSelectedKey())) local size,off=df.sizeof(trg.target:_field(self:getSelectedKey()))
dialog.showMessage("Offset",string.format("Size hex=%x,%x dec=%d,%d\nRelative hex=%x dec=%d",size,off,size,off,off-stoff,off-stoff),COLOR_WHITE) dialog.showMessage("Offset",string.format("Size hex=%x,%x dec=%d,%d\nRelative hex=%x dec=%d",size,off,size,off,off-stoff,off-stoff),COLOR_WHITE)
--elseif keys.CUSTOM_ALT_F then --filter?
elseif keys[keybindings.find.key] then elseif keys[keybindings.find.key] then
self:find() self:find()
elseif keys[keybindings.lua_set.key] then elseif keys[keybindings.lua_set.key] then
self:set(self:getSelectedKey()) self:set(self:getSelectedKey())
--elseif keys.CUSTOM_I then
-- self:insertSimple()
elseif keys[keybindings.insert.key] then --insert elseif keys[keybindings.insert.key] then --insert
self:insertNew() self:insertNew()
elseif keys[keybindings.delete.key] then --delete elseif keys[keybindings.delete.key] then --delete
self:deleteSelected(self:getSelectedKey()) self:deleteSelected(self:getSelectedKey())
elseif keys[keybindings.reinterpret.key] then
self:openReinterpret(self:getSelectedKey())
end end
self.super.onInput(self,keys) self.super.onInput(self,keys)
end 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
end
function GmEditorUi:updateTarget(preserve_pos,reindex) function GmEditorUi:updateTarget(preserve_pos,reindex)
local trg=self:currentTarget() local trg=self:currentTarget()
if reindex then if reindex then
@ -244,7 +292,7 @@ function GmEditorUi:updateTarget(preserve_pos,reindex)
self.subviews.lbl_current_item:itemById('name').text=tostring(trg.target) self.subviews.lbl_current_item:itemById('name').text=tostring(trg.target)
local t={} local t={}
for k,v in pairs(trg.keys) do 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 end
local last_pos local last_pos
if preserve_pos then if preserve_pos then
@ -292,6 +340,8 @@ if #args~=0 then
show_editor(t) 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
show_editor(df.reinterpret_cast(df[args[2]],args[3]))
else else
local t=load("return "..args[1])() local t=load("return "..args[1])()
show_editor(t) show_editor(t)