----------------------------- -- Levak ©2011 -------------- -- http://levak.free.fr/ ---- -- levak92@gmail.com -------- ----------------------------- ------ Menu Menu = class(Screen) Menu.__index = Menu function Menu.create(items) local menu = class(Screen) setmetatable(menu, Menu) menu.items = items menu.depth = 1 --format(menu.items) menu.selectedIndex = 1 return menu end function Menu:format() local max = 0 platform.gc():setFont("sansserif", "r", fxxsmall) -- Get the maximum width for i, button in ipairs(self.items) do button.text = tostring(i)..":"..button.text if platform.gc():getStringWidth(button.text) + button.h + 5 + 10 > max then max = platform.gc():getStringWidth(button.text) + button.h + 10 + (button.h+normal)/3 end end local firstX = self.items[1].x local firstY = self.items[1].y for i, button in ipairs(self.items) do button.y = firstY + 2*i*xxsmall button.x = firstX button.w = max if button.child then if #button.child.items < 1 then button.child.items[1] = Button.create(0, 0, 0, 1.5, normal, function() end, "No saved game found", warningButton) end button.child.items[1].x = button.x + button.w + button.s button.child.items[1].y = button.y - button.h - button.s button.child.depth = self.depth + 1 button.child:format() end end end function Menu:paint(gc) for i,button in ipairs(self.items) do button:paint(gc, i == self.selectedIndex) end end function Menu:charIn(ch) if ch >= "1" and ch <= "9" then if self.items[tonumber(ch)] then self.selectedIndex = tonumber(ch) self:enterKey() end end platform.window:invalidate() end function Menu:enterKey() if self.items[self.selectedIndex].child then local submenu = self.items[self.selectedIndex].child PushScreen(submenu) else print(self.depth) for i = 1, self.depth do PullScreen() end self.items[self.selectedIndex].fun() end end function Menu:escapeKey() PullScreen() end function Menu:arrowKey(key) if key == "up" then self.selectedIndex = self.selectedIndex - 1 if self.selectedIndex < 1 then self.selectedIndex = #self.items end elseif key == "down" then self.selectedIndex = self.selectedIndex + 1 if self.selectedIndex > #self.items then self.selectedIndex = 1 end 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() end function Menu:mouseDown(x, y) for i, button in ipairs(self.items) do if button:isActive(x, y) then self.selectedIndex = i self:enterKey() end end platform.window:invalidate() end -- non used events function Menu:tabKey() end function Menu:backtabKey() end function Menu:contextMenu() end function Menu:help() end function Menu:mouseUp() end