2015-04-13 06:45:02 +00:00
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2015-04-13 06:45:02 +00:00
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local ImageWidget = require("ui/widget/imagewidget")
|
2015-04-13 06:45:02 +00:00
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
2013-10-18 20:38:07 +00:00
|
|
|
local GestureRange = require("ui/gesturerange")
|
2015-04-13 06:45:02 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2014-05-01 10:37:12 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2015-04-13 06:45:02 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2015-04-13 06:45:02 +00:00
|
|
|
local Font = require("ui/font")
|
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,
|
2014-10-22 13:34:11 +00:00
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
2014-03-13 13:52:43 +00:00
|
|
|
radius = 15,
|
|
|
|
padding = 2,
|
|
|
|
width = nil,
|
|
|
|
text_font_face = "cfont",
|
|
|
|
text_font_size = 20,
|
2015-04-13 06:45:02 +00:00
|
|
|
text_font_bold = true,
|
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,
|
2016-06-09 05:26:30 +00:00
|
|
|
fgcolor = self.enabled and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_GREY,
|
2015-04-13 06:45:02 +00:00
|
|
|
bold = self.text_font_bold,
|
2014-03-13 13:52:43 +00:00
|
|
|
face = Font:getFace(self.text_font_face, self.text_font_size)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
self.label_widget = ImageWidget:new{
|
|
|
|
file = self.icon,
|
2014-10-14 13:34:09 +00:00
|
|
|
dim = not self.enabled,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
local widget_size = self.label_widget:getSize()
|
|
|
|
if self.width == nil then
|
|
|
|
self.width = widget_size.w
|
|
|
|
end
|
|
|
|
-- set FrameContainer content
|
2014-09-14 03:42:12 +00:00
|
|
|
self.frame = FrameContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2015-09-13 08:09:00 +00:00
|
|
|
self:onFocus()
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-09-14 03:42:12 +00:00
|
|
|
self.dimen = self.frame:getSize()
|
|
|
|
self[1] = self.frame
|
2014-03-13 13:52:43 +00:00
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
2014-10-20 14:19:31 +00:00
|
|
|
TapSelectButton = {
|
2014-03-13 13:52:43 +00:00
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = self.dimen,
|
|
|
|
},
|
2014-06-05 06:58:53 +00:00
|
|
|
doc = "Tap Button",
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2014-10-20 14:19:31 +00:00
|
|
|
HoldSelectButton = {
|
2014-08-17 16:32:09 +00:00
|
|
|
GestureRange:new{
|
|
|
|
ges = "hold",
|
|
|
|
range = self.dimen,
|
|
|
|
},
|
|
|
|
doc = "Hold Button",
|
|
|
|
}
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
end
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
2015-04-13 06:45:02 +00:00
|
|
|
function Button:setText(text)
|
|
|
|
self.text = text
|
|
|
|
self.width = nil
|
|
|
|
self:init()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Button:setIcon(icon)
|
|
|
|
self.icon = icon
|
|
|
|
self.width = nil
|
|
|
|
self:init()
|
|
|
|
end
|
|
|
|
|
2012-06-10 15:52:09 +00:00
|
|
|
function Button:onFocus()
|
2015-09-13 08:09:00 +00:00
|
|
|
self.frame.invert = true
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Button:onUnfocus()
|
2015-09-13 08:09:00 +00:00
|
|
|
self.frame.invert = false
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2016-06-09 05:26:30 +00:00
|
|
|
if self.enabled then
|
|
|
|
self.label_widget.fgcolor = Blitbuffer.COLOR_BLACK
|
|
|
|
else
|
|
|
|
self.label_widget.fgcolor = Blitbuffer.COLOR_GREY
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2016-06-09 05:26:30 +00:00
|
|
|
if self.enabled then
|
|
|
|
self.label_widget.fgcolor = Blitbuffer.COLOR_BLACK
|
|
|
|
else
|
|
|
|
self.label_widget.fgcolor = Blitbuffer.COLOR_GREY
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2014-09-14 03:42:12 +00:00
|
|
|
self.frame.orig_background = self[1].background
|
|
|
|
self.frame.background = nil
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2014-09-14 03:42:12 +00:00
|
|
|
self.frame.background = self[1].old_background
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
|
|
|
|
2014-10-20 14:19:31 +00:00
|
|
|
function Button:onTapSelectButton()
|
2014-08-20 06:45:11 +00:00
|
|
|
if self.enabled and self.callback then
|
2014-11-14 14:07:14 +00:00
|
|
|
UIManager:scheduleIn(0.0, function()
|
|
|
|
self[1].invert = true
|
2014-11-30 00:12:00 +00:00
|
|
|
UIManager:setDirty(self.show_parent, function()
|
2015-04-26 18:07:17 +00:00
|
|
|
return "ui", self[1].dimen
|
2014-11-30 00:12:00 +00:00
|
|
|
end)
|
2014-11-14 14:07:14 +00:00
|
|
|
end)
|
2014-05-01 10:37:12 +00:00
|
|
|
UIManager:scheduleIn(0.1, function()
|
|
|
|
self.callback()
|
|
|
|
self[1].invert = false
|
2014-11-30 00:12:00 +00:00
|
|
|
UIManager:setDirty(self.show_parent, function()
|
2015-04-26 18:07:17 +00:00
|
|
|
return "ui", self[1].dimen
|
2014-11-30 00:12:00 +00:00
|
|
|
end)
|
2014-05-01 10:37:12 +00:00
|
|
|
end)
|
2015-04-13 06:45:02 +00:00
|
|
|
elseif self.tap_input then
|
|
|
|
self:onInput(self.tap_input)
|
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
|
|
|
|
2014-10-20 14:19:31 +00:00
|
|
|
function Button:onHoldSelectButton()
|
2014-08-17 16:32:09 +00:00
|
|
|
if self.enabled and self.hold_callback then
|
|
|
|
self.hold_callback()
|
2015-04-13 06:45:02 +00:00
|
|
|
elseif self.hold_input then
|
|
|
|
self:onInput(self.hold_input)
|
2014-08-17 16:32:09 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return Button
|