From b14e4e97f543b5b2a107ce0a808f507c68716e41 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Wed, 17 Oct 2012 09:41:48 +0400 Subject: [PATCH] Natively support entry icons in the List widget. --- library/lua/gui/materials.lua | 17 +++++++++-------- library/lua/gui/widgets.lua | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/library/lua/gui/materials.lua b/library/lua/gui/materials.lua index 0cf3c21d9..17fcf2928 100644 --- a/library/lua/gui/materials.lua +++ b/library/lua/gui/materials.lua @@ -46,6 +46,7 @@ function MaterialDialog:init(info) widgets.List{ view_id = 'list', frame = { l = 0, r = 0, t = 6, b = 2 }, + icon_width = 2, on_submit = self:callback('onSubmitItem'), }, widgets.EditField{ @@ -83,12 +84,12 @@ end function MaterialDialog:initBuiltinMode() local choices = { - { text = ' none', mat_type = -1, mat_index = -1 }, - { text = ARROW..' inorganic', key = 'CUSTOM_SHIFT_I', + { text = 'none', mat_type = -1, mat_index = -1 }, + { icon = ARROW, text = 'inorganic', key = 'CUSTOM_SHIFT_I', cb = self:callback('initInorganicMode') }, - { text = ARROW..' creature', key = 'CUSTOM_SHIFT_C', + { icon = ARROW, text = 'creature', key = 'CUSTOM_SHIFT_C', cb = self:callback('initCreatureMode')}, - { text = ARROW..' plant', key = 'CUSTOM_SHIFT_P', + { icon = ARROW, text = 'plant', key = 'CUSTOM_SHIFT_P', 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) else table.insert(choices, { - text = ARROW..' '..name, mat_type = typ, mat_index = index, - ctx = name, obj = obj, cb = self:callback('onSelectObj') + icon = ARROW, text = name, mat_type = typ, mat_index = index, + obj = obj, cb = self:callback('onSelectObj') }) end end @@ -147,7 +148,7 @@ end function MaterialDialog:onSelectObj(item) local choices = {} self:addMaterials(choices, item.obj.material, item.mat_type, item.mat_index) - self:pushContext(item.ctx, choices) + self:pushContext(item.text, choices) end function MaterialDialog:addMaterials(choices, vector, tid, index, maxid, field) @@ -183,7 +184,7 @@ function MaterialDialog:addMaterial(choices, mat, typ, idx, pfix) key = mat.prefix end table.insert(choices, { - text = ' '..name, + text = name, search_key = key, material = mat, mat_type = typ, mat_index = idx diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index eb2e8d05d..16568aa16 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -222,6 +222,13 @@ function render_text(obj,dc,x0,y0,pen,dpen) 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 local text = getval(token.text) or '' local keypen @@ -356,6 +363,7 @@ List.ATTRS{ on_submit = DEFAULT_NIL, row_height = 1, scroll_keys = STANDARDSCROLL, + icon_width = DEFAULT_NIL, } function List:init(info) @@ -399,7 +407,7 @@ function List:getContentWidth() end width = math.max(width, roww) end - return width + return width + (self.icon_width or 0) end function List:getContentHeight() @@ -449,6 +457,7 @@ function List:onRenderBody(dc) local choices = self.choices local top = self.page_top local iend = math.min(#choices, top+self.page_size-1) + local iw = self.icon_width for i = top,iend do local obj = choices[i] @@ -465,7 +474,17 @@ function List:onRenderBody(dc) end 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 local keystr = gui.getKeyDisplay(obj.key)