Support restricting the set of materials presented in the dialog.

develop
Alexander Gavrilov 2012-10-17 18:29:15 +04:00
parent 5f4f540bfa
commit 4f7895f571
2 changed files with 66 additions and 38 deletions

@ -173,6 +173,7 @@ function ListBox:init(info)
view_id = 'list', view_id = 'list',
selected = info.selected, selected = info.selected,
choices = info.choices, choices = info.choices,
icon_width = info.icon_width,
text_pen = spen, text_pen = spen,
cursor_pen = cpen, cursor_pen = cpen,
on_submit = function(sel,obj) on_submit = function(sel,obj)

@ -21,6 +21,11 @@ MaterialDialog.ATTRS{
frame_inset = 1, frame_inset = 1,
frame_title = 'Select Material', frame_title = 'Select Material',
-- new attrs -- new attrs
none_caption = 'none',
use_inorganic = true,
use_creature = true,
use_plant = true,
mat_filter = DEFAULT_NIL,
on_select = DEFAULT_NIL, on_select = DEFAULT_NIL,
on_cancel = DEFAULT_NIL, on_cancel = DEFAULT_NIL,
on_close = DEFAULT_NIL, on_close = DEFAULT_NIL,
@ -73,20 +78,32 @@ end
function MaterialDialog:initBuiltinMode() function MaterialDialog:initBuiltinMode()
local choices = { local choices = {
{ text = 'none', mat_type = -1, mat_index = -1 }, { text = self.none_caption, mat_type = -1, mat_index = -1 },
{ icon = ARROW, text = 'inorganic', key = 'CUSTOM_SHIFT_I',
cb = self:callback('initInorganicMode') },
{ icon = ARROW, text = 'creature', key = 'CUSTOM_SHIFT_C',
cb = self:callback('initCreatureMode')},
{ icon = ARROW, text = 'plant', key = 'CUSTOM_SHIFT_P',
cb = self:callback('initPlantMode') },
} }
self:addMaterials( if self.use_inorganic then
choices, table.insert(choices, {
df.global.world.raws.mat_table.builtin, 0, -1, icon = ARROW, text = 'inorganic', key = 'CUSTOM_SHIFT_I',
df.builtin_mats._last_item cb = self:callback('initInorganicMode')
) })
end
if self.use_creature then
table.insert(choices, {
icon = ARROW, text = 'creature', key = 'CUSTOM_SHIFT_C',
cb = self:callback('initCreatureMode')
})
end
if self.use_plant then
table.insert(choices, {
icon = ARROW, text = 'plant', key = 'CUSTOM_SHIFT_P',
cb = self:callback('initPlantMode')
})
end
local table = df.global.world.raws.mat_table.builtin
for i=0,df.builtin_mats._last_item do
self:addMaterial(choices, table[i], i, -1, false, nil)
end
self:pushContext('Any material', choices) self:pushContext('Any material', choices)
end end
@ -94,11 +111,9 @@ end
function MaterialDialog:initInorganicMode() function MaterialDialog:initInorganicMode()
local choices = {} local choices = {}
self:addMaterials( for i,mat in ipairs(df.global.world.raws.inorganics) do
choices, self:addMaterial(choices, mat.material, 0, i, false, mat)
df.global.world.raws.inorganics, 0, nil, end
nil, 'material'
)
self:pushContext('Inorganic materials', choices) self:pushContext('Inorganic materials', choices)
end end
@ -124,8 +139,26 @@ function MaterialDialog:initPlantMode()
end end
function MaterialDialog:addObjectChoice(choices, obj, name, typ, index) function MaterialDialog:addObjectChoice(choices, obj, name, typ, index)
if #obj.material == 1 then -- Check if any eligible children
self:addMaterial(choices, obj.material[0], typ, index, true) local count = #obj.material
local idx = 0
if self.mat_filter then
count = 0
for i,v in ipairs(obj.material) do
if self.mat_filter(v, obj, typ+i, index) then
count = count + 1
if count > 1 then break end
idx = i
end
end
end
-- Create an entry
if count < 1 then
return
elseif count == 1 then
self:addMaterial(choices, obj.material[idx], typ+idx, index, true, obj)
else else
table.insert(choices, { table.insert(choices, {
icon = ARROW, text = name, mat_type = typ, mat_index = index, icon = ARROW, text = name, mat_type = typ, mat_index = index,
@ -136,29 +169,19 @@ end
function MaterialDialog:onSelectObj(item) function MaterialDialog:onSelectObj(item)
local choices = {} local choices = {}
self:addMaterials(choices, item.obj.material, item.mat_type, item.mat_index) for i,v in ipairs(item.obj.material) do
self:addMaterial(choices, v, item.mat_type+i, item.mat_index, false, item.obj)
end
self:pushContext(item.text, choices) self:pushContext(item.text, choices)
end end
function MaterialDialog:addMaterials(choices, vector, tid, index, maxid, field) function MaterialDialog:addMaterial(choices, mat, typ, idx, pfix, parent)
for i=0,(maxid or #vector-1) do -- Check the filter
local mat = vector[i] if self.mat_filter and not self.mat_filter(mat, parent, typ, idx) then
if mat and field then return
mat = mat[field]
end
if mat then
local typ, idx
if index then
typ, idx = tid+i, index
else
typ, idx = tid, i
end
self:addMaterial(choices, mat, typ, idx)
end
end end
end
function MaterialDialog:addMaterial(choices, mat, typ, idx, pfix) -- Find the material name
local state = 0 local state = 0
if mat.heat.melting_point <= 10015 then if mat.heat.melting_point <= 10015 then
state = 1 state = 1
@ -167,11 +190,14 @@ function MaterialDialog:addMaterial(choices, mat, typ, idx, pfix)
name = string.gsub(name, '^frozen ','') name = string.gsub(name, '^frozen ','')
name = string.gsub(name, '^molten ','') name = string.gsub(name, '^molten ','')
name = string.gsub(name, '^condensed ','') name = string.gsub(name, '^condensed ','')
-- Add prefix if requested
local key local key
if pfix and mat.prefix ~= '' then if pfix and mat.prefix ~= '' then
name = mat.prefix .. ' ' .. name name = mat.prefix .. ' ' .. name
key = mat.prefix key = mat.prefix
end end
table.insert(choices, { table.insert(choices, {
text = name, text = name,
search_key = key, search_key = key,
@ -239,10 +265,11 @@ function MaterialDialog:onInput(keys)
end end
end end
function showMaterialPrompt(title, prompt, on_select, on_cancel) function showMaterialPrompt(title, prompt, on_select, on_cancel, mat_filter)
MaterialDialog{ MaterialDialog{
frame_title = title, frame_title = title,
prompt = prompt, prompt = prompt,
mat_filter = mat_filter,
on_select = on_select, on_select = on_select,
on_cancel = on_cancel, on_cancel = on_cancel,
}:show() }:show()