-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.lua
83 lines (69 loc) · 2.16 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
require "misc"
function MenuItem(menu, img, x, y, type)
local m = {}
m.menu = menu
m.img = img
m.x = x -- x relative to menu x
m.y = y -- y relative to menu y
m.type = type
m.sz = 16
m.hovered = false
function m:checkHovered()
local ox = self.menu.x
local oy = self.menu.y
local x = love.mouse.getX()
local y = love.mouse.getY()
if dist(self.x+ox+(self.sz/2), self.y+oy+(self.sz/2), x, y-8) < self.sz then
return true
else
return false
end
end
function m:update()
self.hovered = self:checkHovered()
end
function m:draw()
local ox = self.menu.x
local oy = self.menu.y
if self.hovered then
love.graphics.setColor(1, 1, 1)
love.graphics.circle("fill", self.x+ox+(self.sz/2 + 4), self.y+oy+(self.sz/2 + 4), self.sz)
end
love.graphics.draw(self.img, self.x+ox, self.y+oy)
end
return m
end
function BuildMenu(x, y, w, h)
local m = {}
m.x = x
m.y = y
m.w = w
m.h = h
m.tabs = {"Rooms"}
m.open_tab = "Rooms"
m.items = {["Rooms"] = {
MenuItem(m, love.graphics.newImage("images/build_menu/empty.png"), 8, 16, "empty"),
MenuItem(m, love.graphics.newImage("images/build_menu/plant.png"), 8, 48, "food"),
MenuItem(m, love.graphics.newImage("images/build_menu/power.png"), 8, 80, "power"),
MenuItem(m, love.graphics.newImage("images/build_menu/oxygen.png"), 8, 112, "oxygen"),
MenuItem(m, love.graphics.newImage("images/build_menu/storage.png"), 8, 144, "storage")
}}
function m:checkHovered(o)
return o.x > self.x and o.x < self.x + self.w and
o.y > self.y and o.y < self.y + self.h
end
function m:update()
for i=1, #self.items[self.open_tab] do
self.items[self.open_tab][i]:update()
end
end
function m:draw()
love.graphics.setColor(0.8, 0.8, 0.8)
love.graphics.rectangle("fill", self.x, self.y, self.w, self.h, 16, 16, 16)
love.graphics.setColor(1, 1, 1)
for i=1, #self.items[self.open_tab] do
self.items[self.open_tab][i]:draw(self.x, self.y)
end
end
return m
end