2013-03-12 17:18:53 +00:00
|
|
|
require "ui/widget/container"
|
2012-06-10 15:52:09 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
a button widget
|
2013-03-14 02:52:09 +00:00
|
|
|
--]]
|
2013-02-21 14:29:54 +00:00
|
|
|
Button = InputContainer:new{
|
2012-06-10 15:52:09 +00:00
|
|
|
text = nil, -- mandatory
|
2013-02-21 14:29:54 +00:00
|
|
|
preselect = false,
|
|
|
|
callback = nil,
|
|
|
|
margin = 0,
|
|
|
|
bordersize = 3,
|
|
|
|
background = 0,
|
|
|
|
radius = 15,
|
|
|
|
padding = 2,
|
|
|
|
width = nil,
|
|
|
|
text_font_face = "cfont",
|
|
|
|
text_font_size = 20,
|
2012-06-10 15:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Button:init()
|
2013-02-21 14:29:54 +00:00
|
|
|
local text_widget = TextWidget:new{
|
|
|
|
text = self.text,
|
|
|
|
face = Font:getFace(self.text_font_face, self.text_font_size)
|
|
|
|
}
|
|
|
|
local text_size = text_widget:getSize()
|
2013-03-12 05:08:48 +00:00
|
|
|
if self.width == nil then
|
|
|
|
self.width = text_size.w
|
|
|
|
end
|
2012-06-10 15:52:09 +00:00
|
|
|
-- set FrameContainer content
|
|
|
|
self[1] = FrameContainer:new{
|
2013-02-21 14:29:54 +00:00
|
|
|
margin = self.margin,
|
|
|
|
bordersize = self.bordersize,
|
|
|
|
background = self.background,
|
|
|
|
radius = self.radius,
|
|
|
|
padding = self.padding,
|
2012-06-10 15:52:09 +00:00
|
|
|
HorizontalGroup:new{
|
2013-02-21 14:29:54 +00:00
|
|
|
HorizontalSpan:new{ width = (self.width - text_size.w)/2 },
|
|
|
|
text_widget,
|
|
|
|
HorizontalSpan:new{ width = (self.width - text_size.w)/2 },
|
2012-06-10 15:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.preselect then
|
|
|
|
self[1].color = 15
|
|
|
|
else
|
|
|
|
self[1].color = 5
|
|
|
|
end
|
2013-02-21 14:29:54 +00:00
|
|
|
self.dimen = self[1]:getSize()
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
|
|
|
TapSelect = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = self.dimen,
|
|
|
|
},
|
|
|
|
doc = "Tap Button",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Button:onFocus()
|
|
|
|
self[1].color = 15
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Button:onUnfocus()
|
|
|
|
self[1].color = 5
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-02-21 14:29:54 +00:00
|
|
|
function Button:onTapSelect()
|
|
|
|
self.callback()
|
|
|
|
return true
|
|
|
|
end
|