2012-05-18 23:10:57 +00:00
|
|
|
require "ui/widget"
|
2012-06-10 15:52:09 +00:00
|
|
|
require "ui/focusmanager"
|
|
|
|
require "ui/infomessage"
|
2012-05-18 23:10:57 +00:00
|
|
|
require "ui/font"
|
2012-04-15 00:28:48 +00:00
|
|
|
|
2012-04-29 15:53:48 +00:00
|
|
|
--[[
|
|
|
|
Widget that displays a shortcut icon for menu item
|
|
|
|
]]
|
|
|
|
ItemShortCutIcon = WidgetContainer:new{
|
2012-06-10 15:36:19 +00:00
|
|
|
dimen = Geom:new{ w = 22, h = 22 },
|
2012-04-29 15:53:48 +00:00
|
|
|
key = nil,
|
|
|
|
bordersize = 2,
|
2012-05-01 09:36:01 +00:00
|
|
|
radius = 0,
|
|
|
|
style = "square"
|
2012-04-29 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ItemShortCutIcon:init()
|
|
|
|
if not self.key then
|
|
|
|
return
|
|
|
|
end
|
2012-05-01 09:36:01 +00:00
|
|
|
|
|
|
|
local radius = 0
|
|
|
|
local background = 0
|
|
|
|
if self.style == "rounded_corner" then
|
|
|
|
radius = math.floor(self.width/2)
|
|
|
|
elseif self.style == "grey_square" then
|
|
|
|
background = 3
|
|
|
|
end
|
|
|
|
|
|
|
|
--@TODO calculate font size by icon size 01.05 2012 (houqp)
|
|
|
|
if self.key:len() > 1 then
|
|
|
|
sc_face = Font:getFace("ffont", 14)
|
|
|
|
else
|
|
|
|
sc_face = Font:getFace("scfont", 22)
|
|
|
|
end
|
|
|
|
|
2012-05-02 12:37:38 +00:00
|
|
|
self[1] = FrameContainer:new{
|
|
|
|
padding = 0,
|
|
|
|
bordersize = self.bordersize,
|
|
|
|
radius = radius,
|
|
|
|
background = background,
|
2012-06-10 15:36:19 +00:00
|
|
|
dimen = self.dimen,
|
2012-05-02 12:37:38 +00:00
|
|
|
CenterContainer:new{
|
2012-06-10 15:36:19 +00:00
|
|
|
dimen = self.dimen,
|
2012-05-02 12:37:38 +00:00
|
|
|
TextWidget:new{
|
|
|
|
text = self.key,
|
|
|
|
face = sc_face,
|
2012-04-29 15:53:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-12-07 15:30:15 +00:00
|
|
|
--[[
|
|
|
|
NOTICE:
|
|
|
|
@menu entry must be provided in order to close the menu
|
|
|
|
--]]
|
|
|
|
MenuCloseButton = InputContainer:new{
|
|
|
|
align = "right",
|
|
|
|
menu = nil,
|
|
|
|
dimen = Geom:new{},
|
|
|
|
}
|
|
|
|
|
|
|
|
function MenuCloseButton:init()
|
|
|
|
self[1] = TextWidget:new{
|
|
|
|
text = "x ",
|
|
|
|
face = Font:getFace("cfont", 22),
|
|
|
|
}
|
|
|
|
|
|
|
|
local text_size = self[1]:getSize()
|
|
|
|
self.dimen.w, self.dimen.h = text_size.w, text_size.h
|
|
|
|
|
|
|
|
self.ges_events.Close = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = self.dimen,
|
|
|
|
},
|
|
|
|
doc = "Close menu",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function MenuCloseButton:onClose()
|
|
|
|
self.menu:onClose()
|
|
|
|
return true
|
|
|
|
end
|
2012-04-29 15:53:48 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
Widget that displays an item for menu
|
2012-12-07 15:30:15 +00:00
|
|
|
--]]
|
2012-05-27 22:14:08 +00:00
|
|
|
MenuItem = InputContainer:new{
|
2012-04-29 15:53:48 +00:00
|
|
|
text = nil,
|
|
|
|
detail = nil,
|
|
|
|
face = Font:getFace("cfont", 22),
|
2012-06-10 15:36:19 +00:00
|
|
|
dimen = nil,
|
2012-04-29 15:53:48 +00:00
|
|
|
shortcut = nil,
|
2012-05-01 09:36:01 +00:00
|
|
|
shortcut_style = "square",
|
2012-05-27 22:14:08 +00:00
|
|
|
_underline_container = nil,
|
2012-04-29 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function MenuItem:init()
|
2012-06-10 15:36:19 +00:00
|
|
|
local shortcut_icon_dimen = Geom:new()
|
2012-04-29 15:53:48 +00:00
|
|
|
if self.shortcut then
|
2012-06-10 15:36:19 +00:00
|
|
|
shortcut_icon_dimen.w = math.floor(self.dimen.h*4/5)
|
|
|
|
shortcut_icon_dimen.h = shortcut_icon_dimen.w
|
2012-04-29 15:53:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self.detail = self.text
|
2012-05-02 12:37:38 +00:00
|
|
|
-- 15 for HorizontalSpan,
|
2012-06-10 15:36:19 +00:00
|
|
|
self.content_width = self.dimen.w - shortcut_icon_dimen.w - 15
|
2012-05-02 12:37:38 +00:00
|
|
|
|
2012-05-28 16:59:16 +00:00
|
|
|
-- we need this table per-instance, so we declare it here
|
2012-12-07 15:30:15 +00:00
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
|
|
|
TapSelect = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = self.dimen,
|
|
|
|
},
|
|
|
|
doc = "Select Menu Item",
|
2012-11-11 06:00:52 +00:00
|
|
|
},
|
2012-12-07 15:30:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
self.active_key_events = {
|
|
|
|
Select = { {"Press"}, doc = "chose selected item" },
|
|
|
|
}
|
|
|
|
end
|
2012-05-28 16:59:16 +00:00
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
w = sizeUtf8Text(0, self.dimen.w, self.face, self.text, true).x
|
2012-05-02 12:37:38 +00:00
|
|
|
if w >= self.content_width then
|
2012-12-07 15:30:15 +00:00
|
|
|
if Device:isTouchDevice() then
|
|
|
|
else
|
|
|
|
self.active_key_events.ShowItemDetail = {
|
|
|
|
{"Right"}, doc = "show item detail"
|
|
|
|
}
|
|
|
|
end
|
2012-04-29 15:53:48 +00:00
|
|
|
indicator = " >>"
|
2012-06-10 15:36:19 +00:00
|
|
|
indicator_w = sizeUtf8Text(0, self.dimen.w, self.face, indicator, true).x
|
2012-04-29 15:53:48 +00:00
|
|
|
self.text = getSubTextByWidth(self.text, self.face,
|
2012-05-02 12:37:38 +00:00
|
|
|
self.content_width - indicator_w, true) .. indicator
|
2012-04-29 15:53:48 +00:00
|
|
|
end
|
|
|
|
|
2012-05-27 16:23:01 +00:00
|
|
|
self._underline_container = UnderlineContainer:new{
|
2012-06-10 15:36:19 +00:00
|
|
|
dimen = Geom:new{
|
2012-05-27 16:23:01 +00:00
|
|
|
w = self.content_width,
|
2012-06-10 15:36:19 +00:00
|
|
|
h = self.dimen.h
|
2012-05-27 16:23:01 +00:00
|
|
|
},
|
|
|
|
HorizontalGroup:new {
|
|
|
|
align = "center",
|
|
|
|
TextWidget:new{
|
|
|
|
text = self.text,
|
|
|
|
face = self.face,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2012-04-29 15:53:48 +00:00
|
|
|
self[1] = HorizontalGroup:new{
|
2012-05-02 12:37:38 +00:00
|
|
|
HorizontalSpan:new{ width = 5 },
|
2012-04-29 15:53:48 +00:00
|
|
|
ItemShortCutIcon:new{
|
2012-06-10 15:36:19 +00:00
|
|
|
dimen = shortcut_icon_dimen,
|
2012-04-29 15:53:48 +00:00
|
|
|
key = self.shortcut,
|
2012-05-01 09:36:01 +00:00
|
|
|
radius = shortcut_icon_r,
|
|
|
|
style = self.shortcut_style,
|
2012-04-29 15:53:48 +00:00
|
|
|
},
|
2012-05-02 12:37:38 +00:00
|
|
|
HorizontalSpan:new{ width = 10 },
|
2012-05-27 16:23:01 +00:00
|
|
|
self._underline_container
|
2012-04-29 15:53:48 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function MenuItem:onFocus()
|
2012-06-23 14:23:43 +00:00
|
|
|
self._underline_container.color = 15
|
2012-05-28 16:59:16 +00:00
|
|
|
self.key_events = self.active_key_events
|
2012-04-29 15:53:48 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function MenuItem:onUnfocus()
|
2012-05-27 16:23:01 +00:00
|
|
|
self._underline_container.color = 0
|
2012-05-28 16:59:16 +00:00
|
|
|
self.key_events = { }
|
2012-04-29 15:53:48 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-05-28 16:59:16 +00:00
|
|
|
function MenuItem:onShowItemDetail()
|
2012-04-29 15:53:48 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text=self.detail,
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-11-11 06:00:52 +00:00
|
|
|
function MenuItem:onTapSelect()
|
|
|
|
self.menu:onMenuSelect(self.table)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-04-29 15:53:48 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
Widget that displays menu
|
2012-12-07 15:30:15 +00:00
|
|
|
--]]
|
2012-04-29 15:53:48 +00:00
|
|
|
Menu = FocusManager:new{
|
|
|
|
-- face for displaying item contents
|
|
|
|
cface = Font:getFace("cfont", 22),
|
|
|
|
-- face for menu title
|
|
|
|
tface = Font:getFace("tfont", 25),
|
|
|
|
-- face for paging info display
|
|
|
|
fface = Font:getFace("ffont", 16),
|
|
|
|
-- font for item shortcut
|
|
|
|
sface = Font:getFace("scfont", 20),
|
|
|
|
|
|
|
|
title = "No Title",
|
2012-12-10 17:59:24 +00:00
|
|
|
-- default width and height
|
|
|
|
width = 500,
|
|
|
|
-- height will be calculated according to item number if not given
|
|
|
|
height = nil,
|
2012-12-08 17:18:40 +00:00
|
|
|
dimen = Geom:new{},
|
2012-04-29 15:53:48 +00:00
|
|
|
item_table = {},
|
|
|
|
item_shortcuts = {
|
|
|
|
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
|
|
|
|
"A", "S", "D", "F", "G", "H", "J", "K", "L", "Del",
|
|
|
|
"Z", "X", "C", "V", "B", "N", "M", ".", "Sym", "Enter",
|
|
|
|
},
|
2012-06-11 05:46:19 +00:00
|
|
|
item_table_stack = {},
|
2012-04-29 15:53:48 +00:00
|
|
|
is_enable_shortcut = true,
|
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
item_dimen = nil,
|
2012-04-29 15:53:48 +00:00
|
|
|
page = 1,
|
2012-05-27 06:03:13 +00:00
|
|
|
|
2012-05-27 16:23:01 +00:00
|
|
|
item_group = nil,
|
|
|
|
page_info = nil,
|
2012-06-10 15:36:19 +00:00
|
|
|
|
|
|
|
-- set this to true to not paint as popup menu
|
|
|
|
is_borderless = false,
|
2012-12-08 17:18:40 +00:00
|
|
|
-- close_callback is a function, which is executed when menu is closed
|
|
|
|
-- it is usually set by the widget which creates the menu
|
|
|
|
close_callback = nil
|
2012-04-29 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
2012-12-08 17:18:40 +00:00
|
|
|
function Menu:_recalculateDimen()
|
|
|
|
self.dimen.w = self.width
|
2012-12-10 17:59:24 +00:00
|
|
|
-- if height not given, dynamically calculate it
|
|
|
|
self.dimen.h = self.height or (#self.item_table + 2) * 36
|
2012-12-10 18:30:50 +00:00
|
|
|
if self.dimen.h > Screen:getHeight() then
|
|
|
|
self.dimen.h = Screen:getHeight()
|
|
|
|
end
|
2012-06-10 15:36:19 +00:00
|
|
|
self.item_dimen = Geom:new{
|
|
|
|
w = self.dimen.w,
|
|
|
|
h = 36, -- hardcoded for now
|
|
|
|
}
|
2012-12-08 17:18:40 +00:00
|
|
|
if not self.is_borderless then
|
|
|
|
-- we need to substract border, margin and padding
|
|
|
|
self.item_dimen.w = self.item_dimen.w - 14
|
|
|
|
end
|
2012-06-10 15:36:19 +00:00
|
|
|
self.perpage = math.floor(self.dimen.h / self.item_dimen.h) - 2
|
2012-05-27 21:43:00 +00:00
|
|
|
self.page_num = math.ceil(#self.item_table / self.perpage)
|
2012-12-08 17:18:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:init()
|
|
|
|
self:_recalculateDimen()
|
|
|
|
self.page = 1
|
2012-04-29 15:53:48 +00:00
|
|
|
|
2012-12-07 15:30:15 +00:00
|
|
|
-----------------------------------
|
|
|
|
-- start to set up widget layout --
|
|
|
|
-----------------------------------
|
2012-06-11 05:46:19 +00:00
|
|
|
self.menu_title = TextWidget:new{
|
2012-12-07 15:30:15 +00:00
|
|
|
align = "center",
|
2012-06-11 05:46:19 +00:00
|
|
|
text = self.title,
|
|
|
|
face = self.tface,
|
|
|
|
}
|
2012-12-07 15:30:15 +00:00
|
|
|
-- group for title bar
|
|
|
|
self.title_bar = OverlapGroup:new{
|
|
|
|
dimen = {w = self.dimen.w},
|
|
|
|
self.menu_title,
|
|
|
|
}
|
2012-05-27 16:23:01 +00:00
|
|
|
-- group for items
|
|
|
|
self.item_group = VerticalGroup:new{}
|
2012-12-07 15:30:15 +00:00
|
|
|
self.page_info = TextWidget:new{
|
|
|
|
face = self.fface,
|
|
|
|
}
|
2012-11-26 07:25:28 +00:00
|
|
|
-- group for menu layout
|
2012-06-10 15:36:19 +00:00
|
|
|
local content = VerticalGroup:new{
|
2012-12-07 15:30:15 +00:00
|
|
|
self.title_bar,
|
2012-06-10 15:36:19 +00:00
|
|
|
self.item_group,
|
|
|
|
self.page_info,
|
2012-12-07 15:30:15 +00:00
|
|
|
}
|
|
|
|
-- maintain reference to content so we can change it later
|
2012-11-26 07:25:28 +00:00
|
|
|
self.content_group = content
|
2012-06-10 15:36:19 +00:00
|
|
|
|
|
|
|
if not self.is_borderless then
|
2012-12-08 17:18:40 +00:00
|
|
|
self[1] = FrameContainer:new{
|
|
|
|
dimen = self.dimen,
|
|
|
|
background = 0,
|
|
|
|
radius = math.floor(self.dimen.w/20),
|
|
|
|
content
|
2012-06-10 15:36:19 +00:00
|
|
|
}
|
|
|
|
else
|
2012-12-07 15:30:15 +00:00
|
|
|
-- no border for the menu
|
2012-06-10 15:36:19 +00:00
|
|
|
self[1] = FrameContainer:new{
|
2012-04-29 15:53:48 +00:00
|
|
|
background = 0,
|
2012-06-10 15:36:19 +00:00
|
|
|
bordersize = 0,
|
|
|
|
padding = 0,
|
|
|
|
margin = 0,
|
2012-12-08 17:18:40 +00:00
|
|
|
dimen = self.dimen,
|
2012-06-10 15:36:19 +00:00
|
|
|
content
|
|
|
|
}
|
|
|
|
end
|
2012-04-29 15:53:48 +00:00
|
|
|
|
2012-12-07 15:30:15 +00:00
|
|
|
------------------------------------------
|
|
|
|
-- start to set up input event callback --
|
|
|
|
------------------------------------------
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
table.insert(self.title_bar,
|
|
|
|
MenuCloseButton:new{
|
|
|
|
menu = self,
|
|
|
|
})
|
2012-12-08 17:18:40 +00:00
|
|
|
self.ges_events.TapCloseAllMenus = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-07 15:30:15 +00:00
|
|
|
else
|
|
|
|
-- set up keyboard events
|
|
|
|
self.key_events.Close = { {"Back"}, doc = "close menu" }
|
|
|
|
self.key_events.NextPage = {
|
|
|
|
{Input.group.PgFwd}, doc = "goto next page of the menu"
|
|
|
|
}
|
|
|
|
self.key_events.PrevPage = {
|
|
|
|
{Input.group.PgBack}, doc = "goto previous page of the menu"
|
|
|
|
}
|
|
|
|
-- we won't catch presses to "Right"
|
|
|
|
self.key_events.FocusRight = nil
|
|
|
|
-- shortcut icon is not needed for touch device
|
|
|
|
if self.is_enable_shortcut then
|
|
|
|
self.key_events.SelectByShortCut = { {self.item_shortcuts} }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.key_events.Select = { {"Press"}, doc = "select current menu item"}
|
|
|
|
|
|
|
|
|
2012-05-27 21:43:00 +00:00
|
|
|
if #self.item_table > 0 then
|
|
|
|
-- if the table is not yet initialized, this call
|
|
|
|
-- must be done manually:
|
2012-06-10 18:14:29 +00:00
|
|
|
self:updateItems(1)
|
2012-05-27 21:43:00 +00:00
|
|
|
end
|
2012-04-29 15:53:48 +00:00
|
|
|
end
|
|
|
|
|
2012-06-10 18:14:29 +00:00
|
|
|
function Menu:updateItems(select_number)
|
2012-11-29 15:48:57 +00:00
|
|
|
-- self.layout must be updated for focusmanager
|
|
|
|
self.layout = {}
|
2012-05-27 16:23:01 +00:00
|
|
|
self.item_group:clear()
|
2012-11-26 07:25:28 +00:00
|
|
|
self.content_group:resetLayout()
|
2012-12-08 17:18:40 +00:00
|
|
|
self:_recalculateDimen()
|
2012-04-29 15:53:48 +00:00
|
|
|
|
|
|
|
for c = 1, self.perpage do
|
2012-12-10 17:59:24 +00:00
|
|
|
-- calculate index in item_table
|
2012-04-29 15:53:48 +00:00
|
|
|
local i = (self.page - 1) * self.perpage + c
|
2012-05-27 21:43:00 +00:00
|
|
|
if i <= #self.item_table then
|
2012-04-29 15:53:48 +00:00
|
|
|
local item_shortcut = nil
|
2012-05-01 09:36:01 +00:00
|
|
|
local shortcut_style = "square"
|
2012-04-29 15:53:48 +00:00
|
|
|
if self.is_enable_shortcut then
|
2012-05-01 09:36:01 +00:00
|
|
|
-- give different shortcut_style to keys in different
|
|
|
|
-- lines of keyboard
|
|
|
|
if c >= 11 and c <= 20 then
|
|
|
|
--shortcut_style = "rounded_corner"
|
|
|
|
shortcut_style = "grey_square"
|
|
|
|
end
|
2012-04-29 15:53:48 +00:00
|
|
|
item_shortcut = self.item_shortcuts[c]
|
|
|
|
if item_shortcut == "Enter" then
|
|
|
|
item_shortcut = "Ent"
|
|
|
|
end
|
|
|
|
end
|
2012-06-10 15:36:19 +00:00
|
|
|
local item_tmp = MenuItem:new{
|
2012-04-29 15:53:48 +00:00
|
|
|
text = self.item_table[i].text,
|
|
|
|
face = self.cface,
|
2012-11-11 06:00:52 +00:00
|
|
|
dimen = self.item_dimen:new(),
|
2012-05-01 09:36:01 +00:00
|
|
|
shortcut = item_shortcut,
|
|
|
|
shortcut_style = shortcut_style,
|
2012-11-11 06:00:52 +00:00
|
|
|
table = self.item_table[i],
|
|
|
|
menu = self,
|
2012-04-29 15:53:48 +00:00
|
|
|
}
|
2012-05-27 16:23:01 +00:00
|
|
|
table.insert(self.item_group, item_tmp)
|
2012-12-10 17:59:24 +00:00
|
|
|
-- this is for focus manager
|
2012-11-29 15:48:57 +00:00
|
|
|
table.insert(self.layout, {item_tmp})
|
2012-12-10 17:59:24 +00:00
|
|
|
else
|
|
|
|
-- item not enough to fill the whole page, break out of loop
|
|
|
|
table.insert(self.item_group,
|
|
|
|
VerticalSpan:new{
|
|
|
|
width = (self.item_dimen.h * (self.perpage - c + 1))
|
|
|
|
})
|
|
|
|
break
|
2012-04-29 15:53:48 +00:00
|
|
|
end -- if i <= self.items
|
|
|
|
end -- for c=1, self.perpage
|
2012-06-10 15:36:19 +00:00
|
|
|
if self.item_group[1] then
|
|
|
|
-- reset focus manager accordingly
|
2012-06-10 18:14:29 +00:00
|
|
|
self.selected = { x = 1, y = select_number }
|
|
|
|
-- set focus to requested menu item
|
|
|
|
self.item_group[select_number]:onFocus()
|
2012-06-10 15:36:19 +00:00
|
|
|
-- update page information
|
|
|
|
self.page_info.text = "page "..self.page.."/"..self.page_num
|
|
|
|
else
|
|
|
|
self.page_info.text = "no choices available"
|
|
|
|
end
|
2012-05-27 21:43:00 +00:00
|
|
|
|
2012-12-08 17:18:40 +00:00
|
|
|
-- FIXME: this is a dirty hack to clear previous menus
|
2012-12-07 15:30:15 +00:00
|
|
|
UIManager.repaint_all = true
|
|
|
|
--UIManager:setDirty(self)
|
2012-04-29 15:53:48 +00:00
|
|
|
end
|
|
|
|
|
2012-06-11 05:46:19 +00:00
|
|
|
function Menu:swithItemTable(new_title, new_item_table)
|
|
|
|
self.menu_title.text = new_title
|
|
|
|
self.item_table = new_item_table
|
|
|
|
self:updateItems(1)
|
|
|
|
end
|
|
|
|
|
2012-04-29 15:53:48 +00:00
|
|
|
function Menu:onSelectByShortCut(_, keyevent)
|
|
|
|
for k,v in ipairs(self.item_shortcuts) do
|
2012-05-27 06:03:13 +00:00
|
|
|
if k > self.perpage then
|
|
|
|
break
|
|
|
|
elseif v == keyevent.key then
|
2012-06-10 15:36:19 +00:00
|
|
|
if self.item_table[(self.page-1)*self.perpage + k] then
|
|
|
|
self:onMenuSelect(self.item_table[(self.page-1)*self.perpage + k])
|
|
|
|
end
|
2012-04-29 15:53:48 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2012-05-27 06:03:13 +00:00
|
|
|
return true
|
2012-04-29 15:53:48 +00:00
|
|
|
end
|
|
|
|
|
2012-06-10 18:14:29 +00:00
|
|
|
function Menu:onWrapFirst()
|
|
|
|
if self.page > 1 then
|
|
|
|
self.page = self.page - 1
|
|
|
|
local end_position = self.perpage
|
|
|
|
if self.page == self.page_num then
|
|
|
|
end_position = #self.item_table % self.perpage
|
|
|
|
end
|
|
|
|
self:updateItems(end_position)
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:onWrapLast()
|
|
|
|
if self.page < self.page_num then
|
|
|
|
self:onNextPage()
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
--[[
|
|
|
|
override this function to process the item selected in a different manner
|
|
|
|
]]--
|
|
|
|
function Menu:onMenuSelect(item)
|
2012-06-11 05:46:19 +00:00
|
|
|
if item.sub_item_table == nil then
|
|
|
|
UIManager:close(self)
|
|
|
|
self:onMenuChoice(item)
|
|
|
|
else
|
|
|
|
-- save menu title for later resume
|
|
|
|
self.item_table.title = self.title
|
|
|
|
table.insert(self.item_table_stack, self.item_table)
|
|
|
|
self:swithItemTable(item.text, item.sub_item_table)
|
|
|
|
end
|
2012-06-10 15:36:19 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
override this function to handle the choice
|
|
|
|
]]--
|
|
|
|
function Menu:onMenuChoice(item)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-04-29 15:53:48 +00:00
|
|
|
function Menu:onNextPage()
|
|
|
|
if self.page < self.page_num then
|
|
|
|
self.page = self.page + 1
|
2012-06-10 18:14:29 +00:00
|
|
|
self:updateItems(1)
|
|
|
|
elseif self.page == self.page_num then
|
|
|
|
-- on the last page, we check if we're on the last item
|
|
|
|
local end_position = #self.item_table % self.perpage
|
2012-06-12 07:59:35 +00:00
|
|
|
if end_position == 0 then
|
|
|
|
end_position = self.perpage
|
|
|
|
end
|
2012-06-10 18:14:29 +00:00
|
|
|
if end_position ~= self.selected.y then
|
|
|
|
self:updateItems(end_position)
|
|
|
|
end
|
2012-04-29 15:53:48 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:onPrevPage()
|
|
|
|
if self.page > 1 then
|
|
|
|
self.page = self.page - 1
|
|
|
|
end
|
2012-06-12 08:02:08 +00:00
|
|
|
self:updateItems(1)
|
2012-04-29 15:53:48 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-05-27 06:03:13 +00:00
|
|
|
function Menu:onSelect()
|
2012-06-10 15:36:19 +00:00
|
|
|
self:onMenuSelect(self.item_table[(self.page-1)*self.perpage+self.selected.y])
|
2012-05-27 06:03:13 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-04-29 15:53:48 +00:00
|
|
|
function Menu:onClose()
|
2012-06-11 05:46:19 +00:00
|
|
|
local table_length = #self.item_table_stack
|
|
|
|
if table_length == 0 then
|
2012-12-08 17:18:40 +00:00
|
|
|
self:onCloseAllMenus()
|
2012-06-11 05:46:19 +00:00
|
|
|
else
|
|
|
|
-- back to parent menu
|
|
|
|
parent_item_table = table.remove(self.item_table_stack, table_length)
|
|
|
|
self:swithItemTable(parent_item_table.title, parent_item_table)
|
|
|
|
end
|
2012-04-29 15:53:48 +00:00
|
|
|
return true
|
|
|
|
end
|
2012-06-10 15:36:19 +00:00
|
|
|
|
2012-12-08 17:18:40 +00:00
|
|
|
function Menu:onCloseAllMenus()
|
|
|
|
UIManager:close(self)
|
|
|
|
if self.close_callback then
|
|
|
|
self.close_callback()
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:onTapCloseAllMenus(arg, ges_ev)
|
|
|
|
if ges_ev.pos:notIntersectWith(self.dimen) then
|
|
|
|
self:onCloseAllMenus()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|