2013-10-18 20:38:07 +00:00
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
|
|
|
local Font = require("ui/font")
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local RenderText = require("ui/rendertext")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Screen = require("device").screen
|
|
|
|
local Device = require("device")
|
2013-10-18 20:38:07 +00:00
|
|
|
local GestureRange = require("ui/gesturerange")
|
2013-10-22 18:51:29 +00:00
|
|
|
local DEBUG = require("dbg")
|
2014-10-22 13:34:11 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
2013-03-16 11:48:32 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local ToggleLabel = TextWidget:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
bold = true,
|
2014-10-22 13:34:11 +00:00
|
|
|
bgcolor = Blitbuffer.COLOR_WHITE,
|
|
|
|
fgcolor = Blitbuffer.COLOR_BLACK,
|
2013-03-16 11:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ToggleLabel:paintTo(bb, x, y)
|
2014-10-04 13:53:30 +00:00
|
|
|
RenderText:renderUtf8Text(bb, x, y+self._height*0.75, self.face, self.text, true, self.bold, self.fgcolor)
|
2013-03-16 11:48:32 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local ToggleSwitch = InputContainer:new{
|
2014-11-20 22:07:39 +00:00
|
|
|
width = Screen:scaleBySize(216),
|
|
|
|
height = Screen:scaleBySize(30),
|
2014-10-22 13:34:11 +00:00
|
|
|
bgcolor = Blitbuffer.COLOR_WHITE, -- unfoused item color
|
|
|
|
fgcolor = Blitbuffer.gray(0.5), -- focused item color
|
2014-11-23 08:52:08 +00:00
|
|
|
font_face = "cfont",
|
|
|
|
font_size = 16,
|
2015-03-16 13:49:53 +00:00
|
|
|
enabled = true,
|
2013-03-16 11:48:32 +00:00
|
|
|
}
|
2013-02-19 03:44:44 +00:00
|
|
|
|
|
|
|
function ToggleSwitch:init()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.n_pos = #self.toggle
|
|
|
|
self.position = nil
|
2013-04-08 07:29:23 +00:00
|
|
|
|
2014-10-22 13:34:11 +00:00
|
|
|
self.toggle_frame = FrameContainer:new{
|
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
|
|
|
color = Blitbuffer.gray(0.5),
|
|
|
|
radius = 7,
|
|
|
|
bordersize = 1,
|
|
|
|
padding = 2,
|
2015-03-16 13:49:53 +00:00
|
|
|
dim = not self.enabled,
|
2014-10-22 13:34:11 +00:00
|
|
|
}
|
2014-03-13 13:52:43 +00:00
|
|
|
self.toggle_content = HorizontalGroup:new{}
|
2013-04-08 07:29:23 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
for i=1,#self.toggle do
|
|
|
|
local label = ToggleLabel:new{
|
|
|
|
align = "center",
|
|
|
|
text = self.toggle[i],
|
2014-11-23 08:52:08 +00:00
|
|
|
face = Font:getFace(self.font_face, self.font_size),
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
local content = CenterContainer:new{
|
|
|
|
dimen = Geom:new{w = self.width/self.n_pos, h = self.height},
|
|
|
|
label,
|
|
|
|
}
|
|
|
|
local button = FrameContainer:new{
|
2014-10-22 13:34:11 +00:00
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
|
|
|
color = Blitbuffer.gray(0.5),
|
2014-03-13 13:52:43 +00:00
|
|
|
margin = 0,
|
|
|
|
radius = 5,
|
|
|
|
bordersize = 1,
|
|
|
|
padding = 0,
|
|
|
|
content,
|
|
|
|
}
|
|
|
|
table.insert(self.toggle_content, button)
|
|
|
|
end
|
2013-04-08 07:29:23 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
self.toggle_frame[1] = self.toggle_content
|
|
|
|
self[1] = self.toggle_frame
|
|
|
|
self.dimen = Geom:new(self.toggle_frame:getSize())
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
|
|
|
TapSelect = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = self.dimen,
|
|
|
|
},
|
2014-06-05 06:58:53 +00:00
|
|
|
doc = "Toggle switch",
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2014-07-03 08:30:24 +00:00
|
|
|
HoldSelect = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "hold",
|
|
|
|
range = self.dimen,
|
|
|
|
},
|
|
|
|
doc = "Hold switch",
|
|
|
|
},
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
end
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ToggleSwitch:update()
|
2014-03-13 13:52:43 +00:00
|
|
|
local pos = self.position
|
|
|
|
for i=1,#self.toggle_content do
|
|
|
|
if pos == i then
|
|
|
|
self.toggle_content[i].color = self.fgcolor
|
|
|
|
self.toggle_content[i].background = self.fgcolor
|
2014-10-22 13:34:11 +00:00
|
|
|
self.toggle_content[i][1][1].fgcolor = Blitbuffer.COLOR_WHITE
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
|
|
|
self.toggle_content[i].color = self.bgcolor
|
|
|
|
self.toggle_content[i].background = self.bgcolor
|
2014-10-22 13:34:11 +00:00
|
|
|
self.toggle_content[i][1][1].fgcolor = Blitbuffer.COLOR_BLACK
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ToggleSwitch:setPosition(position)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.position = position
|
|
|
|
self:update()
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ToggleSwitch:togglePosition(position)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.n_pos == 2 and self.alternate ~= false then
|
|
|
|
self.position = (self.position+1)%self.n_pos
|
|
|
|
self.position = self.position == 0 and self.n_pos or self.position
|
|
|
|
elseif self.n_pos == 1 then
|
|
|
|
self.position = self.position == 1 and 0 or 1
|
|
|
|
else
|
|
|
|
self.position = position
|
|
|
|
end
|
|
|
|
self:update()
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ToggleSwitch:onTapSelect(arg, gev)
|
2015-03-16 13:49:53 +00:00
|
|
|
if not self.enabled then return true end
|
2014-03-13 13:52:43 +00:00
|
|
|
local position = math.ceil(
|
|
|
|
(gev.pos.x - self.dimen.x) / self.dimen.w * self.n_pos
|
|
|
|
)
|
|
|
|
--DEBUG("toggle position:", position)
|
|
|
|
self:togglePosition(position)
|
|
|
|
--[[
|
|
|
|
if self.values then
|
|
|
|
self.values = self.values or {}
|
|
|
|
self.config:onConfigChoice(self.name, self.values[self.position])
|
|
|
|
end
|
|
|
|
if self.event then
|
|
|
|
self.args = self.args or {}
|
|
|
|
self.config:onConfigEvent(self.event, self.args[self.position])
|
|
|
|
end
|
|
|
|
if self.events then
|
|
|
|
self.config:onConfigEvents(self.events, self.position)
|
|
|
|
end
|
|
|
|
--]]
|
2014-07-03 08:30:24 +00:00
|
|
|
self.config:onConfigChoose(self.values, self.name,
|
|
|
|
self.event, self.args, self.events, self.position)
|
2014-11-30 00:12:00 +00:00
|
|
|
UIManager:setDirty(self.config, function()
|
2015-04-26 18:07:17 +00:00
|
|
|
return "ui", self.dimen
|
2014-11-30 00:12:00 +00:00
|
|
|
end)
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2013-02-19 03:44:44 +00:00
|
|
|
end
|
|
|
|
|
2014-07-03 08:30:24 +00:00
|
|
|
function ToggleSwitch:onHoldSelect(arg, gev)
|
|
|
|
local position = math.ceil(
|
|
|
|
(gev.pos.x - self.dimen.x) / self.dimen.w * self.n_pos
|
|
|
|
)
|
|
|
|
self.config:onMakeDefault(self.name, self.name_text,
|
|
|
|
self.values or self.args, self.toggle, position)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return ToggleSwitch
|