From 6e9aa28e70ec842db643a06609454fff54b5d86a Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 22 Aug 2016 02:24:32 +0200 Subject: Werking! --- button.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ config/menumain.lua | 29 +++++++++++++++++++++++++++- config/menustart.lua | 11 +++++++++++ menu.lua | 25 +++++++++++------------- 4 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 button.lua create mode 100644 config/menustart.lua diff --git a/button.lua b/button.lua new file mode 100644 index 0000000..925baf3 --- /dev/null +++ b/button.lua @@ -0,0 +1,54 @@ +-- `Button` +-- Button used in `Menu` + +Button = { + text = "button", + focused = false, + x = 0, + y = 0 +} + +function Button:new(text) + local o = {} + setmetatable(o, self) + self.__index = self + o.text = text or self.text + return o +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() + self.focused = true +end +function Button:blur() + self.focused = false +end +function Button:active() end +function Button:set(name, func) + if type(name) == "string" and type(func) == "function" then + self[name] = func + end + return self +end +function Button:draw(scale) + local x,y = self:getPosition() + if self.focused then + love.graphics.setColor(255, 128, 0, 255) + else + love.graphics.setColor(255, 255, 255, 255) + end + love.graphics.print(self.text, x*scale, y*scale, 0, scale, scale) +end +function Button:update(dt) end +function Button:controlpressed(set, action, key) + if action == "attack" and self.focused then + self:active() + end +end +function Button:controlreleased(set, action, key) end + +return Button \ No newline at end of file diff --git a/config/menumain.lua b/config/menumain.lua index a4374ad..7c8ddf9 100644 --- a/config/menumain.lua +++ b/config/menumain.lua @@ -1 +1,28 @@ -return {"Start", "Join", "Settings", "Credits", "Exit"} \ No newline at end of file +local button = require "button" + +return { + button + :new("start") + :setPosition(10,40) + :set("active", function () + changeScene(Menu:new("menustart")) + end) + , + button + :new("join") + :setPosition(10,50) + , + button + :new("settings") + :setPosition(10,60) + , + button + :new("credits") + :setPosition(10,70) + , + button + :new("exit") + :setPosition(10,80) + :set("active", love.event.quit) + , +} \ No newline at end of file diff --git a/config/menustart.lua b/config/menustart.lua new file mode 100644 index 0000000..03ca2f2 --- /dev/null +++ b/config/menustart.lua @@ -0,0 +1,11 @@ +local button = require "button" + +return { + button + :new("WORKED") + :setPosition(10,40) + :set("active", function () + changeScene(Menu:new("menumain")) + end) + , +} \ No newline at end of file diff --git a/menu.lua b/menu.lua index 34cb539..87369da 100644 --- a/menu.lua +++ b/menu.lua @@ -1,8 +1,8 @@ -- `Menu` (Scene) -- It creates single screen of a menu +-- I do know that model I used here and in `World` loading configuration files is not flawless but I did not want to rewrite `World`s one but wanted to keep things similar at least in project scope. require "selector" -require "button" -- Here it begins Menu = { @@ -23,14 +23,9 @@ function Menu:delete() end -- Load menu from file function Menu:load(name) local name = "config/" .. (name or "menumain") .. ".lua" - print(name) local menu = love.filesystem.load(name) self.elements = menu() -end - --- Creators -function Menu:newButton() - local button = Button:new() + self.elements[self.active]:focus() end -- LÖVE2D callbacks @@ -38,27 +33,29 @@ function Menu:update(dt) end function Menu:draw() local scale = self.scale love.graphics.setFont(Font) - for i,v in ipairs(self.elements) do - if self.active == i then - love.graphics.setColor(255, 128, 0, 255) - else - love.graphics.setColor(255, 255, 255, 255) - end - love.graphics.print(v, 10, (80-5*#self.elements+10*i)*scale, 0, scale, scale) + for _,element in pairs(self.elements) do + element:draw(scale) end 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() 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() + end + for _,element in pairs(self.elements) do + element:controlpressed(set, action, key) end end function Menu:controlreleased(set, action, key) end \ No newline at end of file -- cgit v1.1