gm-editor fixes and improvements

develop
Warmist 2012-09-23 23:22:14 +03:00
parent bef9e9af7c
commit 77a4058145
1 changed files with 172 additions and 169 deletions

@ -1,5 +1,4 @@
-- Interface powered item editor. -- Interface powered item editor.
-- TODO use this: MechanismList = defclass(MechanismList, guidm.MenuOverlay)
local gui = require 'gui' local gui = require 'gui'
local dialog = require 'gui.dialogs' local dialog = require 'gui.dialogs'
@ -40,19 +39,24 @@ end
local MODE_BROWSE=0 local MODE_BROWSE=0
local MODE_EDIT=1 local MODE_EDIT=1
local item_screen={ GmEditorUi = defclass(GmEditorUi, gui.FramedScreen)
GmEditorUi.ATTRS={
frame_style = gui.GREY_LINE_FRAME, frame_style = gui.GREY_LINE_FRAME,
frame_title = "GameMaster's editor", frame_title = "GameMaster's editor",
stack={}, }
item_count=0, function GmEditorUi:init(args)
mode=MODE_BROWSE, self.stack={}
self.item_count=0
keys={}, self.mode=MODE_BROWSE
self.keys={}
self:pushTarget(args.target)
insertNew=function(self,typename) return self
end
function GmEditorUi:insertNew(typename)
local tp=typename local tp=typename
if typename== nil then if typename== nil then
dialog.showInputPrompt("Class type","Input class type\n:",COLOR_WHITE,"",dfhack.curry(self.insertNew,self)) dialog.showInputPrompt("Class type","Input class type:",COLOR_WHITE,"",dfhack.curry(self.insertNew,self))
return return
end end
local ntype=df[tp] local ntype=df[tp]
@ -67,22 +71,22 @@ local item_screen={
dfhack.call_with_finalizer(1,false,df.delete,thing,trg.target.insert,trg.target,'#',thing) dfhack.call_with_finalizer(1,false,df.delete,thing,trg.target.insert,trg.target,'#',thing)
end end
end, end
deleteSelected=function(self) function GmEditorUi:deleteSelected()
local trg=self:currentTarget() local trg=self:currentTarget()
if trg.target and trg.target._kind and trg.target._kind=="container" then if trg.target and trg.target._kind and trg.target._kind=="container" then
trg.target:erase(trg.keys[trg.selected]) trg.target:erase(trg.keys[trg.selected])
end end
end, end
currentTarget=function(self) function GmEditorUi:currentTarget()
return self.stack[#self.stack] return self.stack[#self.stack]
end, end
changeSelected = function (self,delta) function GmEditorUi:changeSelected(delta)
local trg=self:currentTarget() local trg=self:currentTarget()
if trg.item_count <= 1 then return end if trg.item_count <= 1 then return end
trg.selected = 1 + (trg.selected + delta - 1) % trg.item_count trg.selected = 1 + (trg.selected + delta - 1) % trg.item_count
end, end
editSelected = function(self) function GmEditorUi:editSelected()
local trg=self:currentTarget() local trg=self:currentTarget()
if trg.target and trg.target._kind and trg.target._kind=="bitfield" then if trg.target and trg.target._kind and trg.target._kind=="bitfield" then
trg.target[trg.keys[trg.selected]]= not trg.target[trg.keys[trg.selected]] trg.target[trg.keys[trg.selected]]= not trg.target[trg.keys[trg.selected]]
@ -96,19 +100,19 @@ local item_screen={
trg.target[trg.keys[trg.selected]]= not trg.target[trg.keys[trg.selected]] trg.target[trg.keys[trg.selected]]= not trg.target[trg.keys[trg.selected]]
elseif trg_type=='userdata' then elseif trg_type=='userdata' then
self:pushTarget(trg.target[trg.keys[trg.selected]]) self:pushTarget(trg.target[trg.keys[trg.selected]])
--local screen = mkinstance(gui.FramedScreen,item_screen):init(trg.target[trg.keys[trg.selected]]) -- does not work --local screen = mkinstance(gui.FramedScreen,GmEditorUi):init(trg.target[trg.keys[trg.selected]]) -- does not work
--screen:show() --screen:show()
else else
print("Unknow type:"..trg_type) print("Unknow type:"..trg_type)
print("Subtype:"..tostring(trg.target[trg.keys[trg.selected]]._kind)) print("Subtype:"..tostring(trg.target[trg.keys[trg.selected]]._kind))
end end
end end
end, end
cancelEdit = function(self) function GmEditorUi:cancelEdit()
self.mode=MODE_BROWSE self.mode=MODE_BROWSE
self.input="" self.input=""
end, end
commitEdit = function(self) function GmEditorUi:commitEdit()
local trg=self:currentTarget() local trg=self:currentTarget()
self.mode=MODE_BROWSE self.mode=MODE_BROWSE
if type(trg.target[trg.keys[trg.selected]])=='number' then if type(trg.target[trg.keys[trg.selected]])=='number' then
@ -116,16 +120,19 @@ local item_screen={
elseif type(trg.target[trg.keys[trg.selected]])=='string' then elseif type(trg.target[trg.keys[trg.selected]])=='string' then
trg.target[trg.keys[trg.selected]]=self.input trg.target[trg.keys[trg.selected]]=self.input
end end
end, end
onRenderBody = function(self, dc) function GmEditorUi:onRenderBody( dc)
local trg=self:currentTarget() local trg=self:currentTarget()
dc:seek(2,1):string(tostring(trg.target), COLOR_RED) dc:seek(2,1):string(tostring(trg.target), COLOR_RED)
local offset=2 local offset=2
local page_offset=0 local page_offset=0
local current_item=1 local current_item=1
local t_col local t_col
if math.floor(trg.selected / (self.frame_height-offset-2)) >0 then local width,height=self:getWindowSize()
page_offset=math.floor(trg.selected / (self.frame_height-offset-2))*(self.frame_height-offset-2)-1 local window_height=height-offset-2
local cursor_window=math.floor(trg.selected / window_height)
if cursor_window>0 then
page_offset=cursor_window*window_height-1
end end
for k,v in pairs(trg.target) do for k,v in pairs(trg.target) do
@ -144,12 +151,15 @@ local item_screen={
else else
dc:seek(20,y_pos):string(tostring(v),t_col) dc:seek(20,y_pos):string(tostring(v),t_col)
end end
if y_pos+3>height then
break
end end
current_item=current_item+1
end end
end, current_item=current_item+1
onInput = function(self,keys) end
end
function GmEditorUi:onInput(keys)
if self.mode==MODE_BROWSE then if self.mode==MODE_BROWSE then
if keys.LEAVESCREEN then if keys.LEAVESCREEN then
self:popTarget() self:popTarget()
@ -185,8 +195,8 @@ local item_screen={
end end
end end
end end
end, end
pushTarget=function(self,target_to_push) function GmEditorUi:pushTarget(target_to_push)
local new_tbl={} local new_tbl={}
new_tbl.target=target_to_push new_tbl.target=target_to_push
new_tbl.keys={} new_tbl.keys={}
@ -196,21 +206,14 @@ local item_screen={
end end
new_tbl.item_count=#new_tbl.keys new_tbl.item_count=#new_tbl.keys
table.insert(self.stack,new_tbl) table.insert(self.stack,new_tbl)
end, end
popTarget=function(self) function GmEditorUi:popTarget()
table.remove(self.stack) --removes last element table.remove(self.stack) --removes last element
if #self.stack==0 then if #self.stack==0 then
self:dismiss() self:dismiss()
end end
end, end
init = function(self,item_to_edit)
self:pushTarget(item_to_edit)
self.frame_width,self.frame_height=dfhack.screen.getWindowSize()
return self
end
}
local screen = GmEditorUi{target=my_trg}
local screen = mkinstance(gui.FramedScreen,item_screen):init(my_trg)
screen:show() screen:show()