From e57a5b0b77c1ba3a2f05371c551d0e61fc9ce709 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 22 Aug 2016 17:10:20 +0200 Subject: Graphics, animation, general improvements --- assets/menu.png | Bin 0 -> 279 bytes button.lua | 34 ++++++++++++++++++++++++---------- config/menumain.lua | 46 +++++++++++++++++++++++++++++++--------------- config/menustart.lua | 6 ++++-- menu.lua | 36 ++++++++++++++++++++++++------------ 5 files changed, 83 insertions(+), 39 deletions(-) create mode 100644 assets/menu.png diff --git a/assets/menu.png b/assets/menu.png new file mode 100644 index 0000000..ec5a398 Binary files /dev/null and b/assets/menu.png differ diff --git a/button.lua b/button.lua index 925baf3..fc4ca8b 100644 --- a/button.lua +++ b/button.lua @@ -2,26 +2,37 @@ -- Button used in `Menu` Button = { - text = "button", + text = "", focused = false, x = 0, - y = 0 + y = 0, + sprite, + quad = love.graphics.newQuad(0, 0, 58,15, 68,15), + arrow_l = love.graphics.newQuad(58, 0, 5, 5, 68,15), + arrow_r = love.graphics.newQuad(63, 0, 5, 5, 68,15), + delay = 0, + parent } -function Button:new(text) +function Button:new(parent) local o = {} setmetatable(o, self) self.__index = self - o.text = text or self.text + o.parent = parent + self.sprite = love.graphics.newImage("assets/menu.png") return o end +function Button:setText(text) + self.text = text or "" + return self +end function Button:setPosition(x, y) self.x = x or 0 self.y = y or 0 return self end function Button:getPosition() return self.x,self.y end -function Button:focus() +function Button:focus(next) self.focused = true end function Button:blur() @@ -36,14 +47,17 @@ function Button:set(name, func) end function Button:draw(scale) local x,y = self:getPosition() + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(self.sprite, self.quad, x*scale, y*scale, 0, scale, scale) if self.focused then - love.graphics.setColor(255, 128, 0, 255) - else - love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(self.sprite, self.arrow_l, (x+54+math.floor(self.delay))*scale, (y+5)*scale, 0, scale, scale) + love.graphics.draw(self.sprite, self.arrow_r, (x-1-math.floor(self.delay))*scale, (y+5)*scale, 0, scale, scale) end - love.graphics.print(self.text, x*scale, y*scale, 0, scale, scale) + love.graphics.printf(string.upper(self.text), (x+2)*scale, (y+4)*scale, 54, "center", 0, scale, scale) +end +function Button:update(dt) + self.delay = (self.delay + dt)%2 end -function Button:update(dt) end function Button:controlpressed(set, action, key) if action == "attack" and self.focused then self:active() diff --git a/config/menumain.lua b/config/menumain.lua index 7c8ddf9..543a858 100644 --- a/config/menumain.lua +++ b/config/menumain.lua @@ -1,28 +1,44 @@ +local menu = ... + local button = require "button" +local width, height = love.graphics.getWidth()/getRealScale(), love.graphics.getHeight()/getRealScale() +local button_x = width/2-29 + return { - button - :new("start") - :setPosition(10,40) + button:new(menu) + :setText("start") + :setPosition(button_x,60) :set("active", function () changeScene(Menu:new("menustart")) end) , - button - :new("join") - :setPosition(10,50) + button:new(menu) + :setText("join") + :setPosition(button_x,76) , - button - :new("settings") - :setPosition(10,60) + button:new(menu) + :setText("settings") + :setPosition(button_x,92) , - button - :new("credits") - :setPosition(10,70) + button:new(menu) + :setText("credits") + :setPosition(button_x,108) , - button - :new("exit") - :setPosition(10,80) + button:new(menu) + :setText("exit") + :setPosition(button_x,124) :set("active", love.event.quit) , + button:new(menu) + :setText("NEVER") + :setPosition(button_x,140) + :set("focus", function (self, next) + if next then + self.parent:next() + else + self.parent:previous() + end + end) + , } \ No newline at end of file diff --git a/config/menustart.lua b/config/menustart.lua index 03ca2f2..46015a9 100644 --- a/config/menustart.lua +++ b/config/menustart.lua @@ -1,8 +1,10 @@ +local menu = ... + local button = require "button" return { - button - :new("WORKED") + button:new(menu) + :setText("Go back") :setPosition(10,40) :set("active", function () changeScene(Menu:new("menumain")) diff --git a/menu.lua b/menu.lua index 87369da..9c7481b 100644 --- a/menu.lua +++ b/menu.lua @@ -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) -- cgit v1.1