summaryrefslogtreecommitdiffhomepage
path: root/not/Button.lua
blob: 72823d6c4893f077108f8a92fbf0703b05d6c45f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require "not.Element"

--- `Button`
-- Menu element that can be activated by user.
Button = Element:extends()

Button.text = ""
Button.focused = false
Button.sprite = --[[]]nil
Button.quads = --[[]]nil
Button.delay = 2

function Button:new (parent)
	Button.__super.new(self, parent)
	self.sprite, self.quads = parent:getSheet()
end

function Button:getSize ()
	return 58, 15
end

function Button:setText (text)
	self.text = text or ""
	return self
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:draw (scale)
	local x,y = self:getPosition()
	local quad = self.quads
	local sprite = self.sprite
	if self:isEnabled() then
		love.graphics.setColor(1, 1, 1, 1)
	else
		love.graphics.setColor(.6, .6, .6, 1)
	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

return Button