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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 button.lua (limited to 'button.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 -- cgit v1.1 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 --- button.lua | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'button.lua') 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() -- cgit v1.1 From 402e19b8bf19fb24af0fdc5b77f44088f48814ed Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 22 Aug 2016 17:24:07 +0200 Subject: initial frame delay --- button.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index fc4ca8b..7262813 100644 --- a/button.lua +++ b/button.lua @@ -10,7 +10,7 @@ Button = { 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, + delay = 2, parent } @@ -56,7 +56,7 @@ function Button:draw(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 + self.delay = (self.delay + dt)%Button.delay -- Button.delay is initial end function Button:controlpressed(set, action, key) if action == "attack" and self.focused then -- cgit v1.1 From 4bcaf1770c0aa693e24359ee52f7552f301917d3 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 22 Aug 2016 18:17:08 +0200 Subject: Faster --- button.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 7262813..f221cda 100644 --- a/button.lua +++ b/button.lua @@ -56,7 +56,10 @@ function Button:draw(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)%Button.delay -- Button.delay is initial + self.delay = self.delay + dt + if self.delay < Button.delay then -- Button.delay is initial + self.delay = self.delay - Button.delay + end end function Button:controlpressed(set, action, key) if action == "attack" and self.focused then -- cgit v1.1 From 7d9d6a04c0bf916da50b5ab6dbafcee4a492398f Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 22 Aug 2016 18:17:47 +0200 Subject: it even works now --- button.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index f221cda..de9ef56 100644 --- a/button.lua +++ b/button.lua @@ -57,7 +57,7 @@ function Button:draw(scale) end function Button:update(dt) self.delay = self.delay + dt - if self.delay < Button.delay then -- Button.delay is initial + if self.delay > Button.delay then -- Button.delay is initial self.delay = self.delay - Button.delay end end -- cgit v1.1 From 3279bd5a968111260eb3f488976a1024958dc283 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 22 Aug 2016 18:27:26 +0200 Subject: case --- button.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index de9ef56..9c33885 100644 --- a/button.lua +++ b/button.lua @@ -53,7 +53,7 @@ function Button:draw(scale) 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.printf(string.upper(self.text), (x+2)*scale, (y+4)*scale, 54, "center", 0, scale, scale) + love.graphics.printf(self.text, (x+2)*scale, (y+4)*scale, 54, "center", 0, scale, scale) end function Button:update(dt) self.delay = self.delay + dt -- cgit v1.1 From 63d90193f50f5fec561f725d8bda1fcb7ad6c3b8 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 23 Aug 2016 18:15:29 +0200 Subject: just in case --- button.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 9c33885..d7caf74 100644 --- a/button.lua +++ b/button.lua @@ -53,6 +53,7 @@ function Button:draw(scale) 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.setFont(Font) love.graphics.printf(self.text, (x+2)*scale, (y+4)*scale, 54, "center", 0, scale, scale) end function Button:update(dt) -- cgit v1.1 From cd72b080bd949d9ec0b42c29e0ea31e300464dad Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 24 Aug 2016 00:10:33 +0200 Subject: Focus() return bool --- button.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'button.lua') diff --git a/button.lua b/button.lua index d7caf74..fd12e0b 100644 --- a/button.lua +++ b/button.lua @@ -34,6 +34,7 @@ end function Button:getPosition() return self.x,self.y end function Button:focus(next) self.focused = true + return true end function Button:blur() self.focused = false -- cgit v1.1 From 8b0698abc1290cbdbfef3c8b48a2dc7a0388c9d1 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 24 Aug 2016 14:19:18 +0200 Subject: Blink button, fix empty error --- button.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index fd12e0b..6b8ef25 100644 --- a/button.lua +++ b/button.lua @@ -11,6 +11,7 @@ Button = { arrow_l = love.graphics.newQuad(58, 0, 5, 5, 68,15), arrow_r = love.graphics.newQuad(63, 0, 5, 5, 68,15), delay = 2, + blinker = 1, parent } @@ -40,6 +41,9 @@ function Button:blur() self.focused = false end function Button:active() end +function Button:blink() + self.blinker = 0 +end function Button:set(name, func) if type(name) == "string" and type(func) == "function" then self[name] = func @@ -48,7 +52,12 @@ function Button:set(name, func) end function Button:draw(scale) local x,y = self:getPosition() - love.graphics.setColor(255, 255, 255, 255) + local blinker = math.floor(self.blinker*4) + if blinker%2 == 0 then + love.graphics.setColor(255, 255, 255, 255) + else + love.graphics.setColor(255, 100, 100, 255) + end love.graphics.draw(self.sprite, self.quad, x*scale, y*scale, 0, scale, scale) if self.focused then love.graphics.draw(self.sprite, self.arrow_l, (x+54+math.floor(self.delay))*scale, (y+5)*scale, 0, scale, scale) @@ -62,6 +71,9 @@ function Button:update(dt) if self.delay > Button.delay then -- Button.delay is initial self.delay = self.delay - Button.delay end + if self.blinker < Button.blinker then -- Button.blink is initial + self.blinker = self.blinker + dt + end end function Button:controlpressed(set, action, key) if action == "attack" and self.focused then -- cgit v1.1 From ac6d4307fc9f7713b996eb8d36a8d0d3756efc00 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 25 Aug 2016 16:44:13 +0200 Subject: THIS NEED REAL CLEANUP --- button.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 6b8ef25..4b397cf 100644 --- a/button.lua +++ b/button.lua @@ -7,9 +7,7 @@ Button = { x = 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), + quads, delay = 2, blinker = 1, parent @@ -20,7 +18,7 @@ function Button:new(parent) setmetatable(o, self) self.__index = self o.parent = parent - self.sprite = love.graphics.newImage("assets/menu.png") + o.sprite, o.quads = parent:getSheet() return o end function Button:setText(text) @@ -53,15 +51,17 @@ end function Button:draw(scale) local x,y = self:getPosition() local blinker = math.floor(self.blinker*4) + local quad = self.quads + local sprite = self.sprite if blinker%2 == 0 then love.graphics.setColor(255, 255, 255, 255) else love.graphics.setColor(255, 100, 100, 255) end - love.graphics.draw(self.sprite, self.quad, x*scale, y*scale, 0, scale, scale) + love.graphics.draw(sprite, quad.button.normal, x*scale, y*scale, 0, scale, scale) if self.focused then - 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) + love.graphics.draw(sprite, quad.arrow_l, (x+54+math.floor(self.delay))*scale, (y+5)*scale, 0, scale, scale) + love.graphics.draw(sprite, quad.arrow_r, (x-2-math.floor(self.delay))*scale, (y+5)*scale, 0, scale, scale) end love.graphics.setFont(Font) love.graphics.printf(self.text, (x+2)*scale, (y+4)*scale, 54, "center", 0, scale, scale) -- cgit v1.1 From 96b4407ae6410affd0db1e6d6d657d4f65d32235 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 28 Aug 2016 20:41:48 +0200 Subject: No blinking; buttons enabled/disabled --- button.lua | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'button.lua') diff --git a/button.lua b/button.lua index 4b397cf..c5681b2 100644 --- a/button.lua +++ b/button.lua @@ -9,8 +9,7 @@ Button = { sprite, quads, delay = 2, - blinker = 1, - parent + parent, } function Button:new(parent) @@ -39,9 +38,7 @@ function Button:blur() self.focused = false end function Button:active() end -function Button:blink() - self.blinker = 0 -end +function Button:isEnabled() return true end function Button:set(name, func) if type(name) == "string" and type(func) == "function" then self[name] = func @@ -50,13 +47,12 @@ function Button:set(name, func) end function Button:draw(scale) local x,y = self:getPosition() - local blinker = math.floor(self.blinker*4) local quad = self.quads local sprite = self.sprite - if blinker%2 == 0 then + if self:isEnabled() then love.graphics.setColor(255, 255, 255, 255) else - love.graphics.setColor(255, 100, 100, 255) + love.graphics.setColor(140, 140, 140, 255) end love.graphics.draw(sprite, quad.button.normal, x*scale, y*scale, 0, scale, scale) if self.focused then @@ -71,12 +67,9 @@ function Button:update(dt) if self.delay > Button.delay then -- Button.delay is initial self.delay = self.delay - Button.delay end - if self.blinker < Button.blinker then -- Button.blink is initial - self.blinker = self.blinker + dt - end end function Button:controlpressed(set, action, key) - if action == "attack" and self.focused then + if action == "attack" and self.focused and self:isEnabled() then self:active() end end -- cgit v1.1