-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectmenu.lua
463 lines (432 loc) · 13.1 KB
/
selectmenu.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
require "rendertext"
require "keys"
require "graphics"
require "font"
require "commands"
require "lbrstrings"
require "defaults"
SelectMenu = {
fsize = DSM_FSIZE, -- font for displaying item names
tfsize = DSM_TFSIZE, -- font for page title
ffsize = DSM_FFSIZE,-- font for paging display
title_H = DSM_TITLE_H, -- title height
spacing = DSM_SPACING, -- spacing between lines
foot_H = DSM_FOOT_H, -- foot height
margin_H = DSM_MARGIN_H, -- horisontal margin
shortcut_width = DSM_SHORTCUT_WIDTH, -- width used to draw shortcut key
current_entry = 0,
menu_title = SNo_Title,
no_item_msg = SNo_items_found_,
item_array = {},
items = 0,
item_shortcuts = {
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
"A", "S", "D", "F", "G", "H", "J", "K", "L", "Sym",
"Z", "X", "C", "V", "B", "N", "M", ".", "/", "Ent",
},
last_shortcut = 0,
-- state buffer
page = 1,
current = 1,
oldcurrent = 0,
selected_item = nil,
onpage = 0, -- number of items displayed on the current page - added by Kai771
commands = nil,
expandable = false, -- if true handle Right/Left FW selector keys
deletable = false, -- if true handle Del key as a request to delete item
-- note that currently expandable and deletable are mutually exclusive
-- NuPogodi, 30.08.12: define font to render menu items
own_glyph = 0, -- render menu items with default "cfont"
-- own_glyph = 1 => own glyphs for items like "Droid/DroidSans.ttf"
-- own_glyph = 2 => own glyphs for Font.fontmap._index like "ffont", "tfont", etc.
}
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
o.selected_item = nil
o.onpage = 0
-- increase spacing for DXG so we don't have more than 30 shortcuts
if fb.bb:getHeight() == 1200 then
o.spacing = 37
end
o:addAllCommands()
return o
end
function SelectMenu:getItemIndexByShortCut(c, perpage)
if c == nil then return end -- unused key
for _k,_v in ipairs(self.item_shortcuts) do
if _v == c and _k <= self.last_shortcut then
return (perpage * (self.page - 1) + _k)
end
end
end
function SelectMenu:addAllCommands()
self.commands = Commands:new{}
local numeric_keydefs, i = {}
for i=1, 10 do numeric_keydefs[i]=Keydef:new(KEY_1+i-1, nil, tostring(i%10)) end
self.commands:addGroup("[1, 2 .. 9, 0]", numeric_keydefs,
Sitem_at_position_0_10__90_100_,
function(sm)
local target_item = math.ceil(sm.items * (keydef.keycode-KEY_1) / 9)
sm.current, sm.page, sm.markerdirty, sm.pagedirty =
gotoTargetItem(target_item, sm.items, sm.current, sm.page, sm.perpage)
end
)
self.commands:add(KEY_FW_UP, nil, Sjoypad_up,
Sprevious_item,
function(sm)
if sm.current == 1 then
sm.current = sm.onpage
sm.markerdirty = true
else
sm.current = sm.current - 1
sm.markerdirty = true
end
end
)
self.commands:add(KEY_FW_UP, MOD_SHIFT, Sjoypad_up,
Sprevious_..DSM_SHIFT_UP_DOWN..S_items,
function(sm)
if sm.current > DSM_SHIFT_UP_DOWN then
sm.current = sm.current - DSM_SHIFT_UP_DOWN
else
sm.current = 1
end
sm.markerdirty = true
end
)
self.commands:add(KEY_FW_DOWN, nil, Sjoypad_down,
Snext_item,
function(sm)
if sm.current == sm.onpage then
sm.current = 1
sm.markerdirty = true
else
sm.current = sm.current + 1
sm.markerdirty = true
end
end
)
self.commands:add(KEY_FW_DOWN, MOD_SHIFT, Sjoypad_down,
Snext_..DSM_SHIFT_UP_DOWN..S_items,
function(sm)
if sm.page < (sm.items / sm.perpage) then
if sm.current <= sm.perpage - DSM_SHIFT_UP_DOWN then
sm.current = sm.current + DSM_SHIFT_UP_DOWN
else
sm.current = sm.perpage
end
else
-- lpitems = number of items on the last page
local lpitems = sm.items % sm.perpage
if sm.current <= lpitems - DSM_SHIFT_UP_DOWN then
sm.current = sm.current + DSM_SHIFT_UP_DOWN
else
sm.current = lpitems
end
end
sm.markerdirty = true
end
)
self.commands:add({KEY_PGFWD, KEY_LPGFWD}, nil, ">",
Snext_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
end
end
)
self.commands:add({KEY_PGBCK, KEY_LPGBCK}, nil, "<",
Sprevious_page,
function(sm)
if sm.page > 1 then
sm.page = sm.page - 1
sm.pagedirty = true
else
sm.current = 1
sm.markerdirty = true
end
end
)
self.commands:add(KEY_FW_PRESS, nil, Sjoypad_press,
Sselect_item,
function(sm)
if sm.items == 0 then
return "break"
else
self.selected_item = (sm.perpage * (sm.page - 1) + sm.current)
end
end
)
if self.deletable then
self.commands:add({KEY_DEL, KEY_SCREENKB}, nil, "Del",
Sdelete_item,
function(sm)
self.selected_item = (sm.perpage * (sm.page - 1) + sm.current)
return "delete"
end
)
end
if self.expandable then
self.commands:add(KEY_FW_RIGHT, nil, Sjoypad_right,
Sexpand_item,
function(sm)
self.selected_item = (sm.perpage * (sm.page - 1) + sm.current)
return "expand"
end
)
self.commands:add(KEY_FW_LEFT, nil, Sjoypad_left,
Scollapse_item,
function(sm)
self.selected_item = (sm.perpage * (sm.page - 1) + sm.current)
return "collapse"
end
)
self.commands:add(KEY_FW_RIGHT, MOD_SHIFT, Sjoypad_right,
Sexpand_all_subitems,
function(sm)
self.selected_item = (sm.perpage * (sm.page - 1) + sm.current)
return "expand all"
end
)
end
-- Left out if not DKPV_STYLE_LISTS ------------------------------
if DKPV_STYLE_LISTS then
local KEY_Q_to_P = {}
for i = KEY_Q, KEY_P do
table.insert(KEY_Q_to_P, Keydef:new(i, nil, ""))
end
self.commands:addGroup("Q to P", KEY_Q_to_P,
Sselect_item_with_Q_to_P_key_as_shortcut,
function(sm, keydef)
sm.selected_item = sm:getItemIndexByShortCut(
sm.item_shortcuts[ keydef.keycode - KEY_Q + 1 ], sm.perpage)
end
)
local KEY_A_to_L = {}
for i = KEY_A, KEY_L do
table.insert(KEY_A_to_L, Keydef:new(i, nil, ""))
end
self.commands:addGroup("A to L", KEY_A_to_L,
Sselect_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)
end
)
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 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)
end
)
self.commands:add(KEY_SLASH, nil, "/",
Sselect_item_with_Z_to_M_key_as_shortcut,
function(sm)
sm.selected_item = sm:getItemIndexByShortCut("/", sm.perpage)
end
)
self.commands:add(KEY_DOT, nil, ".",
Sselect_item_with_dot_key_as_shortcut,
function(sm)
sm.selected_item = sm:getItemIndexByShortCut(".", sm.perpage)
end
)
self.commands:add(KEY_SYM, nil, "Sym",
Sselect_item_with_Sym_key_as_shortcut,
function(sm)
sm.selected_item = sm:getItemIndexByShortCut("Sym", sm.perpage)
end
)
self.commands:add(KEY_ENTER, nil, "Enter",
Sselect_item_with_Enter_key_as_shortcut,
function(sm)
sm.selected_item = sm:getItemIndexByShortCut("Ent", sm.perpage)
end
)
end
-- ^^Left out if not DKPV_STYLE_LISTS ---------------
self.commands:add(KEY_H,MOD_ALT,"H",
Sshow_help_page,
function(sm)
HelpPage:show(0, G_height, sm.commands)
sm.pagedirty = true
end)
self.commands:add(KEY_BACK, nil, "Back",
Sexit_menu,
function(sm)
return "break"
end
)
end
function SelectMenu:clearCommands()
self.commands = Commands:new{}
self.commands:add(KEY_BACK, nil, "Back",
Sexit_menu,
function(sm)
return "break"
end)
end
------------------------------------------------
-- 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
self.last_shortcut = 0
self.current_entry = math.min(self.current_entry,self.items)
-- now calculating the page & cursor
self.page = math.floor(self.current_entry / self.perpage) + 1
self.page = math.max(1, self.page)
self.current = self.current_entry - (self.page - 1) * self.perpage + 1
self.current = math.max(1, self.current)
local own_face
while true do
local cface = Font:getFace("cfont", 22)
local tface = Font:getFace("tfont", 25)
local fface = Font:getFace("ffont", 16)
local sface = Font:getFace("scfont", 22)
local lx
if DKPV_STYLE_LISTS then
lx = self.margin_H + self.shortcut_width
else
lx = self.margin_H
end
local fw = fb.bb:getWidth() - lx - self.margin_H
if self.pagedirty then
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0)
self.markerdirty = true
-- draw menu title
DrawTitle(self.menu_title,self.margin_H,0,self.title_H,3,tface)
-- draw items
fb.bb:paintRect(0, ypos + self.title_H + self.margin_H, fb.bb:getWidth(), height - self.title_H, 0)
if self.items == 0 then
y = ypos + self.title_H + (self.spacing * 2)
renderUtf8Text(fb.bb, self.margin_H + 20, y, cface,
"Oops... Bad news for you:", true)
y = y + self.spacing
renderUtf8Text(fb.bb, self.margin_H + 20, y, cface,
self.no_item_msg, true)
self.markerdirty = false
self:clearCommands()
else
local c
for c = 1, self.perpage do
local i = (self.page - 1) * self.perpage + c
if i <= self.items then
y = ypos + self.title_H + (self.spacing * c) + 4
if DKPV_STYLE_LISTS then
-- paint shortcut indications
if c <= 10 or c > 20 then
blitbuffer.paintBorder(fb.bb, self.margin_H, y-22, 29, 29, 2, 15)
else
fb.bb:paintRect(self.margin_H, y-22, 29, 29, 3)
end
if self.item_shortcuts[c] ~= nil and
string.len(self.item_shortcuts[c]) == 3 then
-- debug "Del", "Sym and "Ent"
renderUtf8Text(fb.bb, self.margin_H + 3, y, fface,
self.item_shortcuts[c], true)
else
renderUtf8Text(fb.bb, self.margin_H + 8, y, sface,
self.item_shortcuts[c], true)
end
end
self.onpage = c
self.last_shortcut = c
-- NuPogodi, 30.08.12: improved method to use own fontface for each menu item
if self.own_glyph == 1 then -- Font.fontmap[_index], like "Droid/DroidSans.ttf"
own_face = Font:getFace(self.item_array[i], 22)
elseif self.own_glyph == 2 then -- Font.fontmap._index, like "[cfont] description"
own_face = Font:getFace(string.sub(string.match(self.item_array[i],"%b[]"), 2, -2), 22)
else
own_face = cface
end
-- rendering menu items
if sizeUtf8Text(lx,fb.bb:getWidth(),own_face,self.item_array[i],false).x < (fw - 10) then
renderUtf8Text(fb.bb,lx,y,own_face,self.item_array[i],false)
else
local gapx = sizeUtf8Text(0,fb.bb:getWidth(),own_face,"...", true).x
gapx = lx + renderUtf8TextWidth(fb.bb,lx,y,own_face,self.item_array[i],false,fw-gapx-15).x
renderUtf8Text(fb.bb,gapx,y,own_face,"...",true)
end
-- end of changes (NuPogodi)
end -- if i <= self.items
end -- for c=1, self.perpage
end -- if self.items == 0
local footer = "Page "..self.page.." of "..(math.ceil(self.items / self.perpage)).." - Press Alt-H for help"
renderUtf8Text(fb.bb, self.margin_H, height-7, fface, footer, true)
end
if self.markerdirty then
if not self.pagedirty then
if self.oldcurrent > 0 then
y = ypos + self.title_H + (self.spacing * self.oldcurrent) + 12
fb.bb:paintRect( lx, y, fw, 3, 0)
fb:refresh(1, lx, y, fw, 3)
end
end
-- draw new marker line
y = ypos + self.title_H + (self.spacing * self.current) + 12
fb.bb:paintRect(lx, y, fw, 3, 15)
if not self.pagedirty then
fb:refresh(1, lx, y, fw, 3)
end
self.oldcurrent = self.current
self.markerdirty = false
end
if self.pagedirty then
fb:refresh(0, 0, ypos, fb.bb:getWidth(), height)
self.pagedirty = false
end
local ev = input.saveWaitForEvent()
ev.code = adjustKeyEvents(ev)
if ev.type == EV_KEY and ev.value ~= EVENT_VALUE_KEY_RELEASE then
keydef = Keydef:new(ev.code, getKeyModifier())
Debug("key pressed: "..tostring(keydef))
command = self.commands:getByKeydef(keydef)
if command ~= nil then
Debug("command to execute: "..tostring(command))
ret_code = command.func(self, keydef)
else
Debug("command not found: "..tostring(command))
end
if ret_code == "break" then
break
end
if self.selected_item ~= nil then
if self.expandable then
if ret_code == "expand" then
return nil, self.selected_item
elseif ret_code == "collapse" then
return nil, -self.selected_item
elseif ret_code == "expand all" then
return nil, self.selected_item, "all"
end
elseif self.deletable and ret_code == "delete" then
return nil, self.selected_item
end
Debug("# selected "..self.selected_item)
return self.selected_item, self.item_array[self.selected_item]
end
end -- EOF if
end -- EOF while
return nil
end