diff --git a/scripts/gui/gm-editor.lua b/scripts/gui/gm-editor.lua index 269d09c31..4d0b09c6a 100644 --- a/scripts/gui/gm-editor.lua +++ b/scripts/gui/gm-editor.lua @@ -2,6 +2,7 @@ local gui = require 'gui' local dialog = require 'gui.dialogs' local widgets =require 'gui.widgets' +local guiScript = require 'gui.script' local args={...} local keybindings={ @@ -10,7 +11,9 @@ local keybindings={ 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"}, 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"}, + NOT_USED={key="SEC_SELECT",desc="Choose an enum value from a list"}, --not a binding... } function getTargetFromScreens() local my_trg @@ -75,6 +78,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 +159,37 @@ 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: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) local trg=self:currentTarget() local trg_key=trg.keys[index] @@ -206,7 +241,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) @@ -218,21 +252,35 @@ function GmEditorUi:onInput(keys) local _,stoff=df.sizeof(trg.target) 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) - --elseif keys.CUSTOM_ALT_F then --filter? + elseif keys[keybindings.find.key] then self:find() elseif keys[keybindings.lua_set.key] then self:set(self:getSelectedKey()) - --elseif keys.CUSTOM_I then - -- self:insertSimple() elseif keys[keybindings.insert.key] then --insert self:insertNew() elseif keys[keybindings.delete.key] then --delete self:deleteSelected(self:getSelectedKey()) + elseif keys[keybindings.reinterpret.key] then + self:openReinterpret(self:getSelectedKey()) end 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 +end function GmEditorUi:updateTarget(preserve_pos,reindex) local trg=self:currentTarget() if reindex then @@ -244,7 +292,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 @@ -292,6 +340,8 @@ if #args~=0 then show_editor(t) end 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 local t=load("return "..args[1])() show_editor(t)