2012-02-26 00:28:03 +00:00
|
|
|
require "rendertext"
|
|
|
|
require "keys"
|
|
|
|
require "graphics"
|
2012-03-10 08:41:23 +00:00
|
|
|
require "font"
|
2012-03-29 13:10:25 +00:00
|
|
|
require "commands"
|
2012-02-26 00:28:03 +00:00
|
|
|
|
|
|
|
SelectMenu = {
|
|
|
|
-- font for displaying item names
|
|
|
|
fsize = 22,
|
|
|
|
-- font for page title
|
|
|
|
tfsize = 25,
|
|
|
|
-- font for paging display
|
|
|
|
ffsize = 16,
|
2012-03-06 04:25:04 +00:00
|
|
|
-- font for item shortcut
|
2012-04-09 17:04:26 +00:00
|
|
|
sface = Font:getFace("scfont", 22),
|
2012-02-26 00:28:03 +00:00
|
|
|
|
|
|
|
-- title height
|
|
|
|
title_H = 40,
|
|
|
|
-- spacing between lines
|
|
|
|
spacing = 36,
|
|
|
|
-- foot height
|
|
|
|
foot_H = 27,
|
|
|
|
|
2012-04-09 17:04:26 +00:00
|
|
|
menu_title = "No Title",
|
2012-02-26 01:47:54 +00:00
|
|
|
no_item_msg = "No items found.",
|
2012-02-26 00:28:03 +00:00
|
|
|
item_array = {},
|
2012-03-06 13:37:00 +00:00
|
|
|
items = 0,
|
2012-02-26 01:47:54 +00:00
|
|
|
|
2012-03-06 04:27:19 +00:00
|
|
|
item_shortcuts = {
|
2012-03-06 11:28:12 +00:00
|
|
|
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
|
|
|
|
"A", "S", "D", "F", "G", "H", "J", "K", "L", "Del",
|
2012-03-06 13:37:00 +00:00
|
|
|
"Z", "X", "C", "V", "B", "N", "M", ".", "Sym", "Ent",
|
2012-03-06 04:25:04 +00:00
|
|
|
},
|
2012-03-06 18:49:44 +00:00
|
|
|
last_shortcut = 0,
|
2012-03-29 13:10:25 +00:00
|
|
|
|
2012-02-26 01:47:54 +00:00
|
|
|
-- state buffer
|
2012-02-26 00:28:03 +00:00
|
|
|
page = 1,
|
|
|
|
current = 1,
|
|
|
|
oldcurrent = 0,
|
2012-03-29 13:10:25 +00:00
|
|
|
selected_item = nil,
|
|
|
|
|
|
|
|
commands = nil,
|
2012-02-26 00:28:03 +00:00
|
|
|
}
|
|
|
|
|
2012-02-26 01:47:54 +00:00
|
|
|
function SelectMenu:new(o)
|
|
|
|
o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
o.items = #o.item_array
|
|
|
|
o.page = 1
|
|
|
|
o.current = 1
|
|
|
|
o.oldcurrent = 0
|
2012-03-29 13:10:25 +00:00
|
|
|
o.selected_item = nil
|
2012-03-06 13:37:00 +00:00
|
|
|
-- increase spacing for DXG so we don't have more than 30 shortcuts
|
|
|
|
if fb.bb:getHeight() == 1200 then
|
|
|
|
o.spacing = 37
|
|
|
|
end
|
2012-03-29 13:10:25 +00:00
|
|
|
o:addAllCommands()
|
2012-02-26 01:47:54 +00:00
|
|
|
return o
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
|
|
|
|
2012-03-06 04:25:04 +00:00
|
|
|
function SelectMenu:getItemIndexByShortCut(c, perpage)
|
2012-03-06 18:49:44 +00:00
|
|
|
if c == nil then return end -- unused key
|
2012-03-06 04:27:19 +00:00
|
|
|
for _k,_v in ipairs(self.item_shortcuts) do
|
2012-03-06 18:49:44 +00:00
|
|
|
if _v == c and _k <= self.last_shortcut then
|
2012-03-06 04:25:04 +00:00
|
|
|
return (perpage * (self.page - 1) + _k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-29 13:10:25 +00:00
|
|
|
function SelectMenu:addAllCommands()
|
|
|
|
self.commands = Commands:new{}
|
|
|
|
|
|
|
|
self.commands:add(KEY_FW_UP, nil, "",
|
|
|
|
"previous item",
|
|
|
|
function(sm)
|
|
|
|
if sm.current == 1 then
|
|
|
|
if sm.page > 1 then
|
|
|
|
sm.current = sm.perpage
|
|
|
|
sm.page = sm.page - 1
|
|
|
|
sm.pagedirty = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
sm.current = sm.current - 1
|
|
|
|
sm.markerdirty = true
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add(KEY_FW_DOWN, nil, "",
|
|
|
|
"next item",
|
|
|
|
function(sm)
|
|
|
|
if sm.current == sm.perpage then
|
|
|
|
if sm.page < (sm.items / sm.perpage) then
|
|
|
|
sm.current = 1
|
|
|
|
sm.page = sm.page + 1
|
|
|
|
sm.pagedirty = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if sm.page ~= math.floor(sm.items / sm.perpage) + 1
|
|
|
|
or sm.current + (sm.page - 1) * sm.perpage < sm.items then
|
|
|
|
sm.current = sm.current + 1
|
|
|
|
sm.markerdirty = true
|
|
|
|
end
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add({KEY_PGFWD, KEY_LPGFWD}, nil, "",
|
|
|
|
"next page",
|
|
|
|
function(sm)
|
|
|
|
if sm.page < (sm.items / sm.perpage) then
|
|
|
|
if sm.current + sm.page * sm.perpage > sm.items then
|
|
|
|
sm.current = sm.items - sm.page * sm.perpage
|
|
|
|
end
|
|
|
|
sm.page = sm.page + 1
|
|
|
|
sm.pagedirty = true
|
|
|
|
else
|
|
|
|
sm.current = sm.items - (sm.page - 1) * sm.perpage
|
|
|
|
sm.markerdirty = true
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add({KEY_PGBCK, KEY_LPGBCK}, nil, "",
|
|
|
|
"previous page",
|
|
|
|
function(sm)
|
|
|
|
if sm.page > 1 then
|
|
|
|
sm.page = sm.page - 1
|
|
|
|
sm.pagedirty = true
|
|
|
|
else
|
|
|
|
sm.current = 1
|
|
|
|
sm.markerdirty = true
|
|
|
|
end
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add(KEY_FW_PRESS, nil, "",
|
|
|
|
"select menu item",
|
|
|
|
function(sm)
|
2012-04-22 01:40:43 +00:00
|
|
|
if sm.items == 0 then
|
|
|
|
return "break"
|
|
|
|
else
|
|
|
|
self.selected_item = (sm.perpage * (sm.page - 1) + sm.current)
|
2012-03-29 13:10:25 +00:00
|
|
|
end
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-04-09 09:38:50 +00:00
|
|
|
local KEY_Q_to_P = {}
|
2012-03-29 13:10:25 +00:00
|
|
|
for i = KEY_Q, KEY_P do
|
2012-04-09 09:38:50 +00:00
|
|
|
table.insert(KEY_Q_to_P, Keydef:new(i, nil, ""))
|
2012-03-29 13:10:25 +00:00
|
|
|
end
|
2012-04-09 09:38:50 +00:00
|
|
|
self.commands:addGroup("Q to P", KEY_Q_to_P,
|
2012-03-29 13:10:25 +00:00
|
|
|
"Select menu item with Q to E key as shortcut",
|
|
|
|
function(sm, keydef)
|
|
|
|
sm.selected_item = sm:getItemIndexByShortCut(
|
|
|
|
sm.item_shortcuts[ keydef.keycode - KEY_Q + 1 ], sm.perpage)
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
local KEY_A_to_L = {}
|
|
|
|
for i = KEY_A, KEY_L do
|
|
|
|
table.insert(KEY_A_to_L, Keydef:new(i, nil, ""))
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:addGroup("A to L", KEY_A_to_L,
|
|
|
|
"Select menu item with A to L key as shortcut",
|
|
|
|
function(sm, keydef)
|
|
|
|
sm.selected_item = sm:getItemIndexByShortCut(
|
|
|
|
sm.item_shortcuts[ keydef.keycode - KEY_A + 11 ], sm.perpage)
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
local KEY_Z_to_M = {}
|
|
|
|
for i = KEY_Z, KEY_M do
|
|
|
|
table.insert(KEY_Z_to_M, Keydef:new(i, nil, ""))
|
|
|
|
end
|
|
|
|
self.commands:addGroup("Z to M", KEY_Z_to_M,
|
|
|
|
"Select menu item with Z to M key as shortcut",
|
|
|
|
function(sm, keydef)
|
|
|
|
sm.selected_item = sm:getItemIndexByShortCut(
|
|
|
|
sm.item_shortcuts[ keydef.keycode - KEY_Z + 21 ], sm.perpage)
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add(KEY_DEL, nil, "",
|
|
|
|
"Select menu item with del key as shortcut",
|
|
|
|
function(sm)
|
|
|
|
sm.selected_item = sm:getItemIndexByShortCut("Del", sm.perpage)
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add(KEY_DOT, nil, "",
|
|
|
|
"Select menu item with dot key as shortcut",
|
|
|
|
function(sm)
|
|
|
|
sm.selected_item = sm:getItemIndexByShortCut(".", sm.perpage)
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add({KEY_SYM, KEY_SLASH}, nil, "",
|
|
|
|
"Select menu item with sym/slash key as shortcut",
|
|
|
|
function(sm)
|
|
|
|
-- DXG has slash after dot
|
|
|
|
sm.selected_item = sm:getItemIndexByShortCut("Sym", sm.perpage)
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add(KEY_ENTER, nil, "",
|
|
|
|
"Select menu item with enter key as shortcut",
|
|
|
|
function(sm)
|
|
|
|
sm.selected_item = sm:getItemIndexByShortCut("Ent", sm.perpage)
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.commands:add(KEY_BACK, nil, "",
|
|
|
|
"Exit menu",
|
|
|
|
function(sm)
|
|
|
|
return "break"
|
2012-04-08 03:57:25 +00:00
|
|
|
end
|
|
|
|
)
|
2012-03-29 13:10:25 +00:00
|
|
|
end
|
|
|
|
|
2012-03-29 13:16:09 +00:00
|
|
|
function SelectMenu:clearCommands()
|
|
|
|
self.commands = Commands:new{}
|
|
|
|
|
|
|
|
self.commands:add(KEY_BACK, nil, "",
|
|
|
|
"Exit menu",
|
|
|
|
function(sm)
|
|
|
|
return "break"
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2012-03-29 13:10:25 +00:00
|
|
|
------------------------------------------------
|
|
|
|
-- return the index of selected item
|
|
|
|
------------------------------------------------
|
|
|
|
function SelectMenu:choose(ypos, height)
|
|
|
|
self.perpage = math.floor(height / self.spacing) - 2
|
|
|
|
self.pagedirty = true
|
|
|
|
self.markerdirty = false
|
2012-03-06 18:49:44 +00:00
|
|
|
self.last_shortcut = 0
|
2012-02-26 00:28:03 +00:00
|
|
|
|
|
|
|
while true do
|
2012-04-09 17:04:26 +00:00
|
|
|
local cface = Font:getFace("cfont", 22)
|
|
|
|
local tface = Font:getFace("tfont", 25)
|
|
|
|
local fface = Font:getFace("ffont", 16)
|
2012-03-10 08:41:23 +00:00
|
|
|
|
2012-03-29 13:10:25 +00:00
|
|
|
if self.pagedirty then
|
|
|
|
self.markerdirty = true
|
2012-02-26 00:28:03 +00:00
|
|
|
-- draw menu title
|
|
|
|
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), self.title_H + 10, 0)
|
2012-03-06 07:50:00 +00:00
|
|
|
fb.bb:paintRect(10, ypos + 10, fb.bb:getWidth() - 20, self.title_H, 5)
|
|
|
|
|
2012-03-10 08:41:23 +00:00
|
|
|
local x = 20
|
|
|
|
local y = ypos + self.title_H
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, x, y, tface, self.menu_title, true)
|
2012-02-26 00:28:03 +00:00
|
|
|
|
|
|
|
-- draw items
|
|
|
|
fb.bb:paintRect(0, ypos + self.title_H + 10, fb.bb:getWidth(), height - self.title_H, 0)
|
|
|
|
if self.items == 0 then
|
|
|
|
y = ypos + self.title_H + (self.spacing * 2)
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 30, y, cface,
|
2012-02-26 00:28:03 +00:00
|
|
|
"Oops... Bad news for you:", true)
|
|
|
|
y = y + self.spacing
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 30, y, cface,
|
2012-02-26 01:47:54 +00:00
|
|
|
self.no_item_msg, true)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.markerdirty = false
|
2012-03-29 13:16:09 +00:00
|
|
|
self:clearCommands()
|
2012-02-26 00:28:03 +00:00
|
|
|
else
|
|
|
|
local c
|
2012-03-29 13:10:25 +00:00
|
|
|
for c = 1, self.perpage do
|
|
|
|
local i = (self.page - 1) * self.perpage + c
|
2012-02-26 00:28:03 +00:00
|
|
|
if i <= self.items then
|
|
|
|
y = ypos + self.title_H + (self.spacing * c)
|
2012-03-06 11:28:12 +00:00
|
|
|
|
|
|
|
-- paint shortcut indications
|
2012-03-06 13:37:00 +00:00
|
|
|
if c <= 10 or c > 20 then
|
2012-03-06 11:28:12 +00:00
|
|
|
blitbuffer.paintBorder(fb.bb, 10, y-22, 29, 29, 2, 15)
|
|
|
|
else
|
|
|
|
fb.bb:paintRect(10, y-22, 29, 29, 3)
|
|
|
|
end
|
2012-03-10 08:41:23 +00:00
|
|
|
if self.item_shortcuts[c] ~= nil and
|
|
|
|
string.len(self.item_shortcuts[c]) == 3 then
|
2012-04-18 16:16:49 +00:00
|
|
|
-- debug "Del", "Sym and "Ent"
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 13, y, fface,
|
2012-03-06 11:28:12 +00:00
|
|
|
self.item_shortcuts[c], true)
|
|
|
|
else
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 18, y, self.sface,
|
2012-03-06 11:28:12 +00:00
|
|
|
self.item_shortcuts[c], true)
|
|
|
|
end
|
|
|
|
|
2012-03-06 18:49:44 +00:00
|
|
|
self.last_shortcut = c
|
|
|
|
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 50, y, cface,
|
2012-02-26 00:28:03 +00:00
|
|
|
self.item_array[i], true)
|
2012-04-22 01:40:43 +00:00
|
|
|
end -- if i <= self.items
|
|
|
|
end -- for c=1, self.perpage
|
|
|
|
end -- if self.items == 0
|
2012-02-26 00:28:03 +00:00
|
|
|
|
|
|
|
-- draw footer
|
2012-03-29 13:10:25 +00:00
|
|
|
y = ypos + self.title_H + (self.spacing * self.perpage)
|
|
|
|
+ self.foot_H + 5
|
2012-02-26 00:28:03 +00:00
|
|
|
x = (fb.bb:getWidth() / 2) - 50
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, x, y, fface,
|
2012-03-29 13:10:25 +00:00
|
|
|
"Page "..self.page.." of "..
|
|
|
|
(math.ceil(self.items / self.perpage)), true)
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
|
|
|
|
2012-03-29 13:10:25 +00:00
|
|
|
if self.markerdirty then
|
|
|
|
if not self.pagedirty then
|
2012-02-26 00:28:03 +00:00
|
|
|
if self.oldcurrent > 0 then
|
|
|
|
y = ypos + self.title_H + (self.spacing * self.oldcurrent) + 8
|
2012-03-06 04:25:04 +00:00
|
|
|
fb.bb:paintRect(45, y, fb.bb:getWidth() - 60, 3, 0)
|
|
|
|
fb:refresh(1, 45, y, fb.bb:getWidth() - 60, 3)
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- draw new marker line
|
|
|
|
y = ypos + self.title_H + (self.spacing * self.current) + 8
|
2012-03-06 04:25:04 +00:00
|
|
|
fb.bb:paintRect(45, y, fb.bb:getWidth() - 60, 3, 15)
|
2012-03-29 13:10:25 +00:00
|
|
|
if not self.pagedirty then
|
2012-03-06 04:25:04 +00:00
|
|
|
fb:refresh(1, 45, y, fb.bb:getWidth() - 60, 3)
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
|
|
|
self.oldcurrent = self.current
|
2012-03-29 13:10:25 +00:00
|
|
|
self.markerdirty = false
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
|
|
|
|
2012-03-29 13:10:25 +00:00
|
|
|
if self.pagedirty then
|
2012-02-26 00:28:03 +00:00
|
|
|
fb:refresh(0, 0, ypos, fb.bb:getWidth(), height)
|
2012-03-29 13:10:25 +00:00
|
|
|
self.pagedirty = false
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
|
|
|
|
2012-04-12 19:00:44 +00:00
|
|
|
local ev = input.saveWaitForEvent()
|
2012-03-02 10:38:34 +00:00
|
|
|
ev.code = adjustKeyEvents(ev)
|
2012-02-26 00:28:03 +00:00
|
|
|
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
|
2012-03-29 13:10:25 +00:00
|
|
|
keydef = Keydef:new(ev.code, getKeyModifier())
|
2012-04-18 16:16:49 +00:00
|
|
|
debug("key pressed: "..tostring(keydef))
|
2012-03-29 13:10:25 +00:00
|
|
|
|
|
|
|
command = self.commands:getByKeydef(keydef)
|
|
|
|
if command ~= nil then
|
2012-04-18 16:16:49 +00:00
|
|
|
debug("command to execute: "..tostring(command))
|
2012-03-29 13:10:25 +00:00
|
|
|
ret_code = command.func(self, keydef)
|
|
|
|
else
|
2012-04-18 16:16:49 +00:00
|
|
|
debug("command not found: "..tostring(command))
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|
2012-03-29 13:10:25 +00:00
|
|
|
|
|
|
|
if ret_code == "break" then
|
|
|
|
break
|
2012-03-06 18:49:44 +00:00
|
|
|
end
|
2012-03-29 13:10:25 +00:00
|
|
|
|
|
|
|
if self.selected_item ~= nil then
|
2012-04-18 16:16:49 +00:00
|
|
|
debug("# selected "..self.selected_item)
|
2012-04-09 17:04:26 +00:00
|
|
|
return self.selected_item, self.item_array[self.selected_item]
|
2012-03-29 13:10:25 +00:00
|
|
|
end
|
|
|
|
end -- EOF if
|
|
|
|
end -- EOF while
|
|
|
|
return nil
|
2012-02-26 00:28:03 +00:00
|
|
|
end
|