Natively support entry icons in the List widget.

develop
Alexander Gavrilov 2012-10-17 09:41:48 +04:00
parent 023dc82564
commit b14e4e97f5
2 changed files with 30 additions and 10 deletions

@ -46,6 +46,7 @@ function MaterialDialog:init(info)
widgets.List{ widgets.List{
view_id = 'list', view_id = 'list',
frame = { l = 0, r = 0, t = 6, b = 2 }, frame = { l = 0, r = 0, t = 6, b = 2 },
icon_width = 2,
on_submit = self:callback('onSubmitItem'), on_submit = self:callback('onSubmitItem'),
}, },
widgets.EditField{ widgets.EditField{
@ -83,12 +84,12 @@ end
function MaterialDialog:initBuiltinMode() function MaterialDialog:initBuiltinMode()
local choices = { local choices = {
{ text = ' none', mat_type = -1, mat_index = -1 }, { text = 'none', mat_type = -1, mat_index = -1 },
{ text = ARROW..' inorganic', key = 'CUSTOM_SHIFT_I', { icon = ARROW, text = 'inorganic', key = 'CUSTOM_SHIFT_I',
cb = self:callback('initInorganicMode') }, cb = self:callback('initInorganicMode') },
{ text = ARROW..' creature', key = 'CUSTOM_SHIFT_C', { icon = ARROW, text = 'creature', key = 'CUSTOM_SHIFT_C',
cb = self:callback('initCreatureMode')}, cb = self:callback('initCreatureMode')},
{ text = ARROW..' plant', key = 'CUSTOM_SHIFT_P', { icon = ARROW, text = 'plant', key = 'CUSTOM_SHIFT_P',
cb = self:callback('initPlantMode') }, cb = self:callback('initPlantMode') },
} }
@ -138,8 +139,8 @@ function MaterialDialog:addObjectChoice(choices, obj, name, typ, index)
self:addMaterial(choices, obj.material[0], typ, index, true) self:addMaterial(choices, obj.material[0], typ, index, true)
else else
table.insert(choices, { table.insert(choices, {
text = ARROW..' '..name, mat_type = typ, mat_index = index, icon = ARROW, text = name, mat_type = typ, mat_index = index,
ctx = name, obj = obj, cb = self:callback('onSelectObj') obj = obj, cb = self:callback('onSelectObj')
}) })
end end
end end
@ -147,7 +148,7 @@ 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) self:addMaterials(choices, item.obj.material, item.mat_type, item.mat_index)
self:pushContext(item.ctx, choices) self:pushContext(item.text, choices)
end end
function MaterialDialog:addMaterials(choices, vector, tid, index, maxid, field) function MaterialDialog:addMaterials(choices, vector, tid, index, maxid, field)
@ -183,7 +184,7 @@ function MaterialDialog:addMaterial(choices, mat, typ, idx, pfix)
key = mat.prefix key = mat.prefix
end end
table.insert(choices, { table.insert(choices, {
text = ' '..name, text = name,
search_key = key, search_key = key,
material = mat, material = mat,
mat_type = typ, mat_index = idx mat_type = typ, mat_index = idx

@ -222,6 +222,13 @@ function render_text(obj,dc,x0,y0,pen,dpen)
end end
end end
if token.tile then
x = x + 1
if dc then
dc:char(nil, token.tile)
end
end
if token.text or token.key then if token.text or token.key then
local text = getval(token.text) or '' local text = getval(token.text) or ''
local keypen local keypen
@ -356,6 +363,7 @@ List.ATTRS{
on_submit = DEFAULT_NIL, on_submit = DEFAULT_NIL,
row_height = 1, row_height = 1,
scroll_keys = STANDARDSCROLL, scroll_keys = STANDARDSCROLL,
icon_width = DEFAULT_NIL,
} }
function List:init(info) function List:init(info)
@ -399,7 +407,7 @@ function List:getContentWidth()
end end
width = math.max(width, roww) width = math.max(width, roww)
end end
return width return width + (self.icon_width or 0)
end end
function List:getContentHeight() function List:getContentHeight()
@ -449,6 +457,7 @@ function List:onRenderBody(dc)
local choices = self.choices local choices = self.choices
local top = self.page_top local top = self.page_top
local iend = math.min(#choices, top+self.page_size-1) local iend = math.min(#choices, top+self.page_size-1)
local iw = self.icon_width
for i = top,iend do for i = top,iend do
local obj = choices[i] local obj = choices[i]
@ -465,7 +474,17 @@ function List:onRenderBody(dc)
end end
local y = (i - top)*self.row_height local y = (i - top)*self.row_height
render_text(obj, dc, 0, y, cur_pen, cur_dpen)
if iw and obj.icon then
dc:seek(0, y)
if type(obj.icon) == 'table' then
dc:char(nil,obj.icon)
else
dc:string(obj.icon, obj.icon_pen or cur_pen)
end
end
render_text(obj, dc, iw or 0, y, cur_pen, cur_dpen)
if obj.key then if obj.key then
local keystr = gui.getKeyDisplay(obj.key) local keystr = gui.getKeyDisplay(obj.key)