add icon button and show/hide method of button

pull/198/head
chrox 11 years ago
parent b174ff2039
commit 4547273d7b

@ -1,10 +1,12 @@
require "ui/widget/image"
require "ui/widget/container"
--[[
a button widget
a button widget that shows text or a icon and handles callback when tapped
--]]
Button = InputContainer:new{
text = nil, -- mandatory
icon = nil,
preselect = false,
callback = nil,
enabled = true,
@ -19,15 +21,22 @@ Button = InputContainer:new{
}
function Button:init()
self.text_widget = TextWidget:new{
text = self.text,
bgcolor = 0.0,
fgcolor = self.enabled and 1.0 or 0.5,
face = Font:getFace(self.text_font_face, self.text_font_size)
}
local text_size = self.text_widget:getSize()
if self.text then
self.label_widget = TextWidget:new{
text = self.text,
bgcolor = 0.0,
fgcolor = self.enabled and 1.0 or 0.5,
face = Font:getFace(self.text_font_face, self.text_font_size)
}
else
self.label_widget = ImageWidget:new{
file = self.icon,
dim = self.enabled,
}
end
local widget_size = self.label_widget:getSize()
if self.width == nil then
self.width = text_size.w
self.width = widget_size.w
end
-- set FrameContainer content
self[1] = FrameContainer:new{
@ -39,9 +48,9 @@ function Button:init()
CenterContainer:new{
dimen = Geom:new{
w = self.width,
h = text_size.h
h = widget_size.h
},
self.text_widget,
self.label_widget,
}
}
if self.preselect then
@ -75,12 +84,48 @@ end
function Button:enable()
self.enabled = true
self.text_widget.fgcolor = 1.0
if self.text then
self.label_widget.fgcolor = self.enabled and 1.0 or 0.5
else
self.label_widget.dim = not self.enabled
end
end
function Button:disable()
self.enabled = false
self.text_widget.fgcolor = 0.5
if self.text then
self.label_widget.fgcolor = self.enabled and 1.0 or 0.5
else
self.label_widget.dim = not self.enabled
end
end
function Button:enableDisable(enable)
if enable then
self:enable()
else
self:disable()
end
end
function Button:hide()
if self.icon then
self.label_widget.hide = true
end
end
function Button:show()
if self.icon then
self.label_widget.hide = false
end
end
function Button:showHide(show)
if show then
self:show()
else
self:hide()
end
end
function Button:onTapSelect()

@ -6,8 +6,10 @@ require "ui/image"
ImageWidget shows an image from a file
--]]
ImageWidget = Widget:new{
invert = nil,
file = nil,
invert = nil,
dim = nil,
hide = nil,
_bb = nil
}
@ -34,10 +36,14 @@ function ImageWidget:paintTo(bb, x, y)
w = size.w,
h = size.h
}
if self.hide then return end
bb:blitFrom(self._bb, x, y, 0, 0, size.w, size.h)
if self.invert then
bb:invertRect(x, y, size.w, size.h)
end
if self.dim then
bb:dimRect(x, y, size.w, size.h)
end
end
function ImageWidget:free()

Loading…
Cancel
Save