add enable/disable method for Button widget

pull/122/head
chrox 11 years ago
parent e947c9b4b2
commit c246696de9

@ -7,6 +7,7 @@ Button = InputContainer:new{
text = nil, -- mandatory
preselect = false,
callback = nil,
enabled = true,
margin = 0,
bordersize = 3,
background = 0,
@ -18,11 +19,13 @@ Button = InputContainer:new{
}
function Button:init()
local text_widget = TextWidget:new{
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 = text_widget:getSize()
local text_size = self.text_widget:getSize()
if self.width == nil then
self.width = text_size.w
end
@ -35,7 +38,7 @@ function Button:init()
padding = self.padding,
HorizontalGroup:new{
HorizontalSpan:new{ width = (self.width - text_size.w)/2 },
text_widget,
self.text_widget,
HorizontalSpan:new{ width = (self.width - text_size.w)/2 },
}
}
@ -68,7 +71,19 @@ function Button:onUnfocus()
return true
end
function Button:enable()
self.enabled = true
self.text_widget.fgcolor = 1.0
end
function Button:disable()
self.enabled = false
self.text_widget.fgcolor = 0.5
end
function Button:onTapSelect()
self.callback()
if self.enabled then
self.callback()
end
return true
end

@ -4,8 +4,8 @@ require "ui/widget/line"
ButtonTable = InputContainer:new{
buttons = {
{
{text="OK", callback=nil},
{text="Cancel", callback=nil},
{text="OK", enabled=true, callback=nil},
{text="Cancel", enabled=false, callback=nil},
},
},
tap_close_callback = nil,
@ -43,6 +43,7 @@ function ButtonTable:init()
for j = 1, #line do
local button = Button:new{
text = line[j].text,
enabled = line[j].enabled,
callback = line[j].callback,
width = Screen:getWidth()*0.9/#line,
bordersize = 0,

Loading…
Cancel
Save