summaryrefslogtreecommitdiffhomepage
path: root/button.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2016-12-20 06:05:56 +0100
committerGitHub <noreply@github.com>2016-12-20 06:05:56 +0100
commit493a89fb354ee6ce667d29a7b96bb4fdd0f6697f (patch)
treed862856d4b62a6c996ea16211ebd321206e236b4 /button.lua
parented6ad6687aeb5359af48bc97c1dbf5dd66842c9c (diff)
parent73bcd524906c51d43b22b3ea68c64b38f130bea6 (diff)
downloadroflnauts-1.0-pre.4.zip
roflnauts-1.0-pre.4.tar.gz
roflnauts-1.0-pre.4.tar.bz2
Merge pull request #16 from nthirtyone/menuv1.0-pre.4
Menu
Diffstat (limited to 'button.lua')
-rw-r--r--button.lua78
1 files changed, 78 insertions, 0 deletions
diff --git a/button.lua b/button.lua
new file mode 100644
index 0000000..c5681b2
--- /dev/null
+++ b/button.lua
@@ -0,0 +1,78 @@
+-- `Button`
+-- Button used in `Menu`
+
+Button = {
+ text = "",
+ focused = false,
+ x = 0,
+ y = 0,
+ sprite,
+ quads,
+ delay = 2,
+ parent,
+}
+
+function Button:new(parent)
+ local o = {}
+ setmetatable(o, self)
+ self.__index = self
+ o.parent = parent
+ o.sprite, o.quads = parent:getSheet()
+ 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(next)
+ self.focused = true
+ return true
+end
+function Button:blur()
+ self.focused = false
+end
+function Button:active() end
+function Button:isEnabled() return true 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()
+ local quad = self.quads
+ local sprite = self.sprite
+ if self:isEnabled() then
+ love.graphics.setColor(255, 255, 255, 255)
+ else
+ 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
+ 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)
+end
+function Button:update(dt)
+ 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 and self:isEnabled() then
+ self:active()
+ end
+end
+function Button:controlreleased(set, action, key) end
+
+return Button \ No newline at end of file