progress_bar as own lib
This commit is contained in:
1 parent
20dbe60a86
commit
5b897d874f
2 files changed
+38
-38
No files matched your search
@@ -0,0 +1,36 @@
|
||||
local progress_bar_metatable = {
|
||||
__index = {
|
||||
font = love.graphics.newFont(18), -- TODO customizable
|
||||
set_progress = function(self, progress_percentage)
|
||||
self.progress_percentage = progress_percentage
|
||||
end,
|
||||
set_text = function(self, new_text)
|
||||
self.text = new_text
|
||||
end,
|
||||
draw = function(self)
|
||||
local cyan = {0, 0.5, 0.5, 1} -- TODO make customizable; consider percentage range colors
|
||||
love.graphics.setColor(cyan)
|
||||
love.graphics.rectangle("fill", self.x_position, self.y_position, self.total_width * self.progress_percentage, self.total_height)
|
||||
|
||||
local text_color = {1, 1, 1, 1} -- TODO customizable; default auto contrast with background color
|
||||
love.graphics.setColor(text_color)
|
||||
|
||||
if self.text then
|
||||
local font_height = self.font:getHeight()
|
||||
local buffer_width = 8 -- TODO this should not be hardcoded
|
||||
love.graphics.setFont(self.font)
|
||||
love.graphics.print(self.text, self.x_position + buffer_width, self.y_position + self.total_height / 2 - font_height / 2)
|
||||
end
|
||||
end,
|
||||
}
|
||||
}
|
||||
|
||||
return function(x_position, y_position, total_width, total_height, progress_percentage)
|
||||
return setmetatable({
|
||||
x_position = x_position,
|
||||
y_position = y_position,
|
||||
total_width = total_width,
|
||||
total_height = total_height,
|
||||
progress_percentage = progress_percentage or 0,
|
||||
}, progress_bar_metatable)
|
||||
end
|
||||
Reference in new issue
Block a user