-
Notifications
You must be signed in to change notification settings - Fork 15
/
menu.lua
116 lines (102 loc) · 3.29 KB
/
menu.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local Component = require("component")
local Menu = {}
Menu.__index = Menu
setmetatable(Menu, Component)
Menu.TEXT_BUTTON = 0
Menu.IMAGE_BUTTON = 1
function Menu.create(x, y, width, height, spacing, listener)
local self = setmetatable({}, Menu)
self.x, self.y = x, y
self.nexty = y
self.width = width
self.height = height
self.spacing = spacing
self.listener = listener
self.id = ""
self.imgButton = ResMgr.getImage("button.png")
self.quadTopLeft = love.graphics.newQuad(0, 0, 3, 3, 7, 7)
self.quadTopRight = love.graphics.newQuad(4, 0, 3, 3, 7, 7)
self.quadBottomLeft = love.graphics.newQuad(0, 4, 3, 3, 7, 7)
self.quadBottomRight = love.graphics.newQuad(4, 4, 3, 3, 7, 7)
self.quadTopEdge = love.graphics.newQuad(3, 0, 1, 3, 7, 7)
self.quadBottomEdge = love.graphics.newQuad(3, 4, 1, 3, 7, 7)
self.quadLeftEdge = love.graphics.newQuad(0, 3, 3, 1, 7, 7)
self.quadRightEdge = love.graphics.newQuad(4, 3, 3, 1, 7, 7)
self.buttons = {}
return self
end
function Menu:addButton(text, id, x, y, width, height)
local e = {}
e.type = Menu.TEXT_BUTTON
e.text = text
e.id = id
e.enabled = true
e.visible = true
e.width = width or self.width
e.height = height or self.height
if x and y then
e.x = x
e.y = y
else
e.x = self.x
e.y = self.nexty
self.nexty = self.nexty + e.height + self.spacing
end
table.insert(self.buttons, e)
return e
end
function Menu:addImageButton(img, quad, id, x, y, width, height)
local e = {}
e.type = Menu.IMAGE_BUTTON
e.img = img
e.quad = quad
e.id = id
e.x, e.y = x,y
e.enabled = true
e.visible = true
e.width, e.height = width, height
table.insert(self.buttons, e)
return e
end
function Menu:draw()
love.graphics.setFont(ResMgr.getFont("menu"))
-- Draw button graphics
for i,v in ipairs(self.buttons) do
if v.visible == true then
if v.type == Menu.TEXT_BUTTON then
love.graphics.draw(self.imgButton, self.quadTopLeft, v.x, v.y)
love.graphics.draw(self.imgButton, self.quadTopRight, v.x+v.width-3, v.y)
love.graphics.draw(self.imgButton, self.quadBottomLeft, v.x, v.y+v.height-3)
love.graphics.draw(self.imgButton, self.quadBottomRight, v.x+v.width-3, v.y+v.height-3)
love.graphics.draw(self.imgButton, self.quadTopEdge, v.x+3, v.y, 0, v.width-6, 1)
love.graphics.draw(self.imgButton, self.quadBottomEdge, v.x+3, v.y+v.height-3, 0, v.width-6, 1)
love.graphics.draw(self.imgButton, self.quadLeftEdge, v.x, v.y+3, 0, 1, v.height-6)
love.graphics.draw(self.imgButton, self.quadRightEdge, v.x+v.width-3, v.y+3, 0, 1, v.height-6)
love.graphics.setColor(255, 194, 49, 255)
love.graphics.rectangle("fill", v.x+3, v.y+3, v.width-6, v.height-6)
love.graphics.setColor(255, 255, 255, 255)
if v.enabled then
love.graphics.setColor(80, 49, 0, 255)
else
love.graphics.setColor(80, 49, 0, 128)
end
love.graphics.printf(v.text, v.x, (v.y+v.height/2-9), v.width, "center")
love.graphics.setColor(255,255,255,255)
elseif v.type == Menu.IMAGE_BUTTON then
love.graphics.draw(v.img, v.quad, v.x, v.y)
end
end
end
end
function Menu:click(x, y)
for i,v in ipairs(self.buttons) do
if v.enabled == true and v.visible == true
and x >= v.x and x <= v.x + v.width
and y >= v.y and y <= v.y + v.height then
self.listener:buttonPressed(v.id, self)
return true
end
end
return false
end
return Menu