2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
|
|
|
local ImageWidget = require("ui/widget/imagewidget")
|
|
|
|
local Font = require("ui/font")
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
2014-05-01 10:37:12 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Device = require("ui/device")
|
2014-05-01 10:37:12 +00:00
|
|
|
local DEBUG = require("dbg")
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
2012-06-10 15:52:09 +00:00
|
|
|
|
|
|
|
--[[
|
2013-08-02 14:41:39 +00:00
|
|
|
a button widget that shows text or a icon and handles callback when tapped
|
2013-03-14 02:52:09 +00:00
|
|
|
--]]
|
2013-10-18 20:38:07 +00:00
|
|
|
local Button = InputContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
text = nil, -- mandatory
|
|
|
|
icon = nil,
|
|
|
|
preselect = false,
|
|
|
|
callback = nil,
|
|
|
|
enabled = true,
|
|
|
|
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()
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.text then
|
|
|
|
self.label_widget = TextWidget:new{
|
|
|
|
text = self.text,
|
|
|
|
bgcolor = 0.0,
|
|
|
|
fgcolor = self.enabled and 1.0 or 0.5,
|
|
|
|
bold = true,
|
|
|
|
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 = widget_size.w
|
|
|
|
end
|
|
|
|
-- set FrameContainer content
|
|
|
|
self[1] = FrameContainer:new{
|
|
|
|
margin = self.margin,
|
|
|
|
bordersize = self.bordersize,
|
|
|
|
background = self.background,
|
|
|
|
radius = self.radius,
|
|
|
|
padding = self.padding,
|
|
|
|
CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.width,
|
|
|
|
h = widget_size.h
|
|
|
|
},
|
|
|
|
self.label_widget,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.preselect then
|
|
|
|
self[1].color = 15
|
|
|
|
else
|
|
|
|
self[1].color = 5
|
|
|
|
end
|
|
|
|
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()
|
2014-03-13 13:52:43 +00:00
|
|
|
self[1].color = 15
|
|
|
|
return true
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Button:onUnfocus()
|
2014-03-13 13:52:43 +00:00
|
|
|
self[1].color = 5
|
|
|
|
return true
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
2013-06-16 03:13:54 +00:00
|
|
|
function Button:enable()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.enabled = true
|
|
|
|
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
|
2013-06-16 03:13:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Button:disable()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.enabled = false
|
|
|
|
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
|
2013-08-02 14:41:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Button:enableDisable(enable)
|
2014-03-13 13:52:43 +00:00
|
|
|
if enable then
|
|
|
|
self:enable()
|
|
|
|
else
|
|
|
|
self:disable()
|
|
|
|
end
|
2013-08-02 14:41:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Button:hide()
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.icon then
|
|
|
|
self.label_widget.hide = true
|
|
|
|
end
|
2013-08-02 14:41:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Button:show()
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.icon then
|
|
|
|
self.label_widget.hide = false
|
|
|
|
end
|
2013-08-02 14:41:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Button:showHide(show)
|
2014-03-13 13:52:43 +00:00
|
|
|
if show then
|
|
|
|
self:show()
|
|
|
|
else
|
|
|
|
self:hide()
|
|
|
|
end
|
2013-06-16 03:13:54 +00:00
|
|
|
end
|
|
|
|
|
2013-02-21 14:29:54 +00:00
|
|
|
function Button:onTapSelect()
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.enabled then
|
2014-05-01 10:37:12 +00:00
|
|
|
self[1].invert = true
|
|
|
|
UIManager:setDirty(self.show_parent, "partial")
|
|
|
|
UIManager:scheduleIn(0.1, function()
|
|
|
|
self.callback()
|
|
|
|
self[1].invert = false
|
|
|
|
UIManager:setDirty(self.show_parent, "partial")
|
|
|
|
end)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
return true
|
2013-02-21 14:29:54 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return Button
|