2017-08-29 19:11:16 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
|
|
local Button = require("ui/widget/button")
|
|
|
|
local Device = require("device")
|
|
|
|
local FocusManager = require("ui/widget/focusmanager")
|
2013-10-18 20:38:07 +00:00
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
2017-08-29 19:11:16 +00:00
|
|
|
local LineWidget = require("ui/widget/linewidget")
|
2015-09-13 08:09:00 +00:00
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
2013-10-18 20:38:07 +00:00
|
|
|
local VerticalSpan = require("ui/widget/verticalspan")
|
|
|
|
local Geom = require("ui/geometry")
|
2015-09-13 08:09:00 +00:00
|
|
|
local Screen = Device.screen
|
2013-06-15 15:13:19 +00:00
|
|
|
|
2015-09-13 08:09:00 +00:00
|
|
|
local ButtonTable = FocusManager:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
width = Screen:getWidth(),
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{text="OK", enabled=true, callback=nil},
|
|
|
|
{text="Cancel", enabled=false, callback=nil},
|
|
|
|
},
|
|
|
|
},
|
2014-11-20 22:07:39 +00:00
|
|
|
sep_width = Screen:scaleBySize(1),
|
|
|
|
padding = Screen:scaleBySize(2),
|
2013-10-24 17:47:22 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
zero_sep = false,
|
|
|
|
button_font_face = "cfont",
|
|
|
|
button_font_size = 20,
|
2013-06-15 15:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ButtonTable:init()
|
2016-05-26 06:09:49 +00:00
|
|
|
self.buttons_layout = {}
|
2015-09-13 08:09:00 +00:00
|
|
|
self.container = VerticalGroup:new{ width = self.width }
|
|
|
|
table.insert(self, self.container)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.zero_sep then
|
|
|
|
self:addHorizontalSep()
|
|
|
|
end
|
2016-05-26 06:09:49 +00:00
|
|
|
local row_cnt = #self.buttons
|
|
|
|
for i = 1, row_cnt do
|
|
|
|
self.buttons_layout[i] = {}
|
2014-03-13 13:52:43 +00:00
|
|
|
local horizontal_group = HorizontalGroup:new{}
|
2016-05-26 06:09:49 +00:00
|
|
|
local row = self.buttons[i]
|
|
|
|
local column_cnt = #row
|
|
|
|
local sizer_space = self.sep_width * (column_cnt - 1) + 2
|
|
|
|
for j = 1, column_cnt do
|
|
|
|
local btn_entry = row[j]
|
2014-03-13 13:52:43 +00:00
|
|
|
local button = Button:new{
|
2016-05-26 06:09:49 +00:00
|
|
|
text = btn_entry.text,
|
|
|
|
enabled = btn_entry.enabled,
|
|
|
|
callback = btn_entry.callback,
|
|
|
|
width = (self.width - sizer_space)/column_cnt,
|
2017-08-29 19:11:16 +00:00
|
|
|
max_width = (self.width - sizer_space)/column_cnt - 2*self.sep_width - 2*self.padding,
|
2014-03-13 13:52:43 +00:00
|
|
|
bordersize = 0,
|
|
|
|
margin = 0,
|
|
|
|
padding = 0,
|
|
|
|
text_font_face = self.button_font_face,
|
|
|
|
text_font_size = self.button_font_size,
|
2014-05-01 10:37:12 +00:00
|
|
|
show_parent = self.show_parent,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
local button_dim = button:getSize()
|
|
|
|
local vertical_sep = LineWidget:new{
|
2016-06-09 05:26:30 +00:00
|
|
|
background = Blitbuffer.COLOR_GREY,
|
2014-03-13 13:52:43 +00:00
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.sep_width,
|
|
|
|
h = button_dim.h,
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 06:09:49 +00:00
|
|
|
self.buttons_layout[i][j] = button
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(horizontal_group, button)
|
2016-05-26 06:09:49 +00:00
|
|
|
if j < column_cnt then
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(horizontal_group, vertical_sep)
|
|
|
|
end
|
|
|
|
end -- end for each button
|
2015-09-13 08:09:00 +00:00
|
|
|
table.insert(self.container, horizontal_group)
|
2016-05-26 06:09:49 +00:00
|
|
|
if i < row_cnt then
|
2014-03-13 13:52:43 +00:00
|
|
|
self:addHorizontalSep()
|
|
|
|
end
|
|
|
|
end -- end for each button line
|
2016-05-26 06:09:49 +00:00
|
|
|
if Device:hasDPad() or Device:hasKeyboard() then
|
|
|
|
self.layout = self.buttons_layout
|
2015-09-13 08:09:00 +00:00
|
|
|
self.layout[1][1]:onFocus()
|
|
|
|
self.key_events.SelectByKeyPress = { {{"Press", "Enter"}} }
|
|
|
|
else
|
|
|
|
self.key_events = {} -- deregister all key press event listeners
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2013-07-21 06:11:14 +00:00
|
|
|
function ButtonTable:addHorizontalSep()
|
2015-09-13 08:09:00 +00:00
|
|
|
table.insert(self.container,
|
|
|
|
VerticalSpan:new{ width = Screen:scaleBySize(2) })
|
|
|
|
table.insert(self.container, LineWidget:new{
|
2016-06-09 05:26:30 +00:00
|
|
|
background = Blitbuffer.COLOR_GREY,
|
2014-03-13 13:52:43 +00:00
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.width,
|
|
|
|
h = self.sep_width,
|
|
|
|
}
|
|
|
|
})
|
2015-09-13 08:09:00 +00:00
|
|
|
table.insert(self.container,
|
|
|
|
VerticalSpan:new{ width = Screen:scaleBySize(2) })
|
|
|
|
end
|
|
|
|
|
|
|
|
function ButtonTable:onSelectByKeyPress()
|
|
|
|
self:getFocusItem().callback()
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return ButtonTable
|