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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
local Textbox = require("textbox")
local HUD = {}
HUD.__index = HUD
HUD.slots = {
{ enabled = false },
{ enabled = false },
{ enabled = false },
{ enabled = false },
}
local SLOT_SIZE = 16
local SLOT_SPACING = 8
local LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
local function createMissingTexture()
local size = SLOT_SIZE
local half = size / 2
local imgData = love.image.newImageData(size, size)
for y = 0, size - 1 do
for x = 0, size - 1 do
local inBlack = (x < half and y < half) or (x >= half and y >= half)
if inBlack then
imgData:setPixel(x, y, 0, 0, 0, 1)
else
imgData:setPixel(x, y, 1, 0, 1, 1)
end
end
end
local img = love.graphics.newImage(imgData)
img:setFilter("nearest", "nearest")
return img
end
function HUD:new()
local self = setmetatable({}, HUD)
self.missingTexture = createMissingTexture()
self.textbox = Textbox:new()
self.hoveredSlot = nil
self.slotRects = {}
self.screenW = 0
self.screenH = 0
self.drawScale = 1
return self
end
function HUD:computeSlotRects(screenW, screenH, drawScale)
local slotW = SLOT_SIZE * drawScale
local slotH = SLOT_SIZE * drawScale
local spacing = SLOT_SPACING * drawScale
local totalW = 4 * slotW + 3 * spacing
local startX = math.floor((screenW - totalW) / 2)
local startY = math.floor(screenH / 2 - slotH / 2)
local rects = {}
for i = 1, 4 do
rects[i] = {
x = startX + (i - 1) * (slotW + spacing),
y = startY,
w = slotW,
h = slotH,
}
end
return rects
end
function HUD:update(dt, screenW, screenH, drawScale)
self.screenW = screenW
self.screenH = screenH
self.drawScale = drawScale
self.slotRects = self:computeSlotRects(screenW, screenH, drawScale)
local mx, my = love.mouse.getPosition()
local prevHovered = self.hoveredSlot
self.hoveredSlot = nil
for i, rect in ipairs(self.slotRects) do
if mx >= rect.x and mx < rect.x + rect.w
and my >= rect.y and my < rect.y + rect.h then
self.hoveredSlot = i
break
end
end
if self.hoveredSlot and self.hoveredSlot ~= prevHovered then
local rect = self.slotRects[self.hoveredSlot]
local centerX = screenW / 2
local topY = rect.y + rect.h + 8 * drawScale
self.textbox:show(LOREM, { centerX, topY, "center" }, "show", {
fontSize = 16,
centeredText = true,
wrapToFit = true,
maxChars = 40,
})
self.textbox.y = topY + self.textbox.height / 2
elseif not self.hoveredSlot and prevHovered then
self.textbox:hide()
end
self.textbox:update(dt)
end
function HUD:mousepressed(mx, my, button)
if button ~= 1 then return end
for i, rect in ipairs(self.slotRects) do
if mx >= rect.x and mx < rect.x + rect.w
and my >= rect.y and my < rect.y + rect.h then
HUD.slots[i].enabled = not HUD.slots[i].enabled
break
end
end
end
function HUD:draw()
local screenW = self.screenW
local screenH = self.screenH
local drawScale = self.drawScale
love.graphics.setColor(0, 0, 0, 0.4)
love.graphics.rectangle("fill", 0, 0, screenW, screenH)
for i, rect in ipairs(self.slotRects) do
love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw(self.missingTexture, rect.x, rect.y, 0, drawScale, drawScale)
if HUD.slots[i].enabled then
love.graphics.setColor(0, 1, 0, 0.3)
love.graphics.rectangle("fill", rect.x, rect.y, rect.w, rect.h)
end
if self.hoveredSlot == i then
love.graphics.setColor(1, 1, 0, 1)
love.graphics.setLineWidth(2)
else
love.graphics.setColor(1, 1, 1, 0.5)
love.graphics.setLineWidth(1)
end
love.graphics.rectangle("line", rect.x, rect.y, rect.w, rect.h)
end
love.graphics.setLineWidth(1)
love.graphics.setColor(1, 1, 1, 1)
self.textbox:draw()
end
return HUD
|