diff options
Diffstat (limited to 'menu.lua')
-rw-r--r-- | menu.lua | 36 |
1 files changed, 24 insertions, 12 deletions
@@ -24,12 +24,32 @@ function Menu:delete() end function Menu:load(name) local name = "config/" .. (name or "menumain") .. ".lua" local menu = love.filesystem.load(name) - self.elements = menu() + self.elements = menu(self) + self.elements[self.active]:focus() +end + +-- Cycle elements +function Menu:next() + self.elements[self.active]:blur() + self.active = (self.active%#self.elements)+1 + self.elements[self.active]:focus(true) +end +function Menu:previous() + self.elements[self.active]:blur() + if self.active == 1 then + self.active = #self.elements + else + self.active = self.active - 1 + end self.elements[self.active]:focus() end -- LÖVE2D callbacks -function Menu:update(dt) end +function Menu:update(dt) + for _,element in pairs(self.elements) do + element:update(dt) + end +end function Menu:draw() local scale = self.scale love.graphics.setFont(Font) @@ -41,18 +61,10 @@ end -- Controller callbacks function Menu:controlpressed(set, action, key) if action == "down" then - self.elements[self.active]:blur() - self.active = (self.active%#self.elements)+1 - self.elements[self.active]:focus() + self:next() end if action == "up" then - self.elements[self.active]:blur() - if self.active == 1 then - self.active = #self.elements - else - self.active = self.active - 1 - end - self.elements[self.active]:focus() + self:previous() end for _,element in pairs(self.elements) do element:controlpressed(set, action, key) |