0 Members and 1 Guest are viewing this topic.
function option1() --Write the commands hereendfunction option2() --Write the commands hereendmenu = { {"Options Menu", {"option 1", option1}, {"option 2", option2} }} toolpalette.register(menu)
------------------------------- Levak ©2011 ---------------- http://levak.free.fr/ ------ [email protected] ------------------------------------------- MenuMenu = class(Screen())function Menu:init(items, convert) if convert then -- the given format is a non-recursive toopalette, convert it self.items = {} local Btype2 = 1 for j, button in ipairs(items) do local subitems, pic = {}, nil if #button > 1 then local Btype = 1 for i=2, #button do if type(button[i]) == "table" then table.insert(subitems, Button(0, 0, 0, 1.5, normal, button[i][2], button[i][1], button[i][3] or nullButton, nil, Btype)) if not pic and #button[i] > 2 then pic = button[i][3] end if Btype < 3 then Btype = 3 end else Btype = 1 end end subitems[#subitems].Btype = subitems[#subitems].Btype and subitems[#subitems].Btype == 1 and 0 or 2 end self.items[j] = Button(0, 0, 0, 1.5, normal, function() end, button[1], pic or nullButton, Menu(subitems), Btype2) Btype2 = 3 end self.items[#self.items].Btype = 2 else self.items = items end self.depth = 1 self.selectedIndex = 0 self.active = trueendfunction Menu:format() local i, xmax, ymax = 1, 0, 0 local gc = platform.gc() gc:begin() gc:setFont("sansserif", "r", fxxsmall) -- Get the maximum width for _, button in ipairs(self.items) do if type(button.text) ~= "string" or button.text == "" then button.active = false else button.active = true ymax = ymax + button.h + button.s button.text = (i < 10 and tostring(i) or string.uchar(55 + i))..":"..button.text if gc:getStringWidth(button.text) + button.h + 5 + 10 > xmax then xmax = gc:getStringWidth(button.text) + button.h + 10 + (button.h+normal)/3 end i = i + 1 end end if #self.items > 0 then local firstX = self.items[1].x local firstY = self.items[1].y + self.items[1].s if firstY + ymax > platform.window:height() then firstY = platform.window:height() - ymax end for i, button in ipairs(self.items) do button.x = firstX button.y = firstY button.w = xmax if button.child then if #button.child.items < 1 then button.active = false else button.child.items[1].x = button.x + button.w + button.s + 1 button.child.items[1].y = button.y - button.h - button.s/2 button.child.depth = self.depth + 1 button.child:format() end end firstY = firstY + button.h + button.s end end self.selectedIndex = self:escapeNullElement(1) gc:finish()endfunction Menu:paint(gc) for i,button in ipairs(self.items) do button:paint(gc, i == self.selectedIndex) endendfunction Menu:charIn(ch) if ch >= "1" and ch <= "9" then if self.items[tonumber(ch)] and self.items[tonumber(ch)].active then self.selectedIndex = tonumber(ch) self:enterKey() end elseif ch >= "a" and ch <= "z" then local i = string.byte(ch) - 87 if self.items[i] and self.items[i].active then self.selectedIndex = i self:enterKey() end end platform.window:invalidate()endfunction Menu:enterKey() if self.items[self.selectedIndex].child then local submenu = self.items[self.selectedIndex].child PushScreen(submenu) else for i = 1, self.depth do PullScreen() end self.items[self.selectedIndex].fun() endendfunction Menu:escapeKey() PullScreen()endfunction Menu:containsValidItem() for i, button in ipairs(self.items) do if button.active then return true end end return falseendfunction Menu:escapeNullElement(n) local i = (self.selectedIndex + n - 1) % #self.items + 1 -- Is the next item invalid and can we iter anyways ? if self.items[i].active then return i elseif self:containsValidItem() then self.selectedIndex = i return self:escapeNullElement(n) else return self.selectedIndex endendfunction Menu:arrowKey(key) if key == "up" then self.selectedIndex = self:escapeNullElement(-1) elseif key == "down" then self.selectedIndex = self:escapeNullElement(1) elseif key == "left" then self:escapeKey() elseif key == "right" then if self.items[self.selectedIndex].child then local submenu = self.items[self.selectedIndex].child PushScreen(submenu) end end platform.window:invalidate()endfunction Menu:mouseDown(x, y) local push = false for i, button in ipairs(self.items) do if button:isActive(x, y) and button.text ~= "" then self.selectedIndex = i self:enterKey() push = true end end if not push then PullScreen() on.mouseDown(x, y) end platform.window:invalidate()endfunction Menu:mouseMove(x, y) for i, button in ipairs(self.items) do if button:isActive(x, y) and button.text ~= "" then self.selectedIndex = i end end platform.window:invalidate()end-- non used eventsfunction Menu:tabKey() endfunction Menu:backtabKey() endfunction Menu:contextMenu() endfunction Menu:help() endfunction Menu:mouseUp() end