-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.lua
54 lines (45 loc) · 1.53 KB
/
Button.lua
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
Button = Class{}
--[[
File stores code for generation and operation of buttons in the game
]]
function Button:init(x, y, width, height, text)
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.mouseDown = false
self.hover = false
end
-- checks if a button is pressed and hovered
-- if button is pressed, returns true, else returns false
function Button:pressed(cursorX, cursorY)
if cursorX >= self.x and cursorX <= self.x + self.width and cursorY >= self.y and cursorY <= self.y + self.height then
self.hover = true
if love.mouse.isDown(1) then
self.mouseDown = true
end
if love.mouse.isDown(1) == false and self.mouseDown == true then
self.mouseDown = false
return true
end
else
self.hover = false
end
return false
end
-- renders button
function Button:render()
-- default button color
love.graphics.setColor(186 / 255, 186 / 255, 186 / 255, 1)
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
-- when hovered upon
if self.hover then
love.graphics.setColor(50 / 255, 50 / 255, 50 / 255, 0.25)
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
-- displays text within button
love.graphics.setColor(69 / 255, 81 / 255, 94 / 255, 1)
love.graphics.setFont(love.graphics.newFont(self.height / 2))
love.graphics.printf(self.text, self.x, self.y + self.height / 4, self.width, 'center')
end