progress_bar as own lib

This commit is contained in:
2026-09-22 15:43:02 -06:00
parent 20dbe60a86
commit 5b897d874f
2 changed files with 38 additions and 38 deletions
+36
View File
@@ -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
+2 -38
View File
@@ -1,46 +1,10 @@
local utility = require "lib.utility"
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,
}
}
local new_progress_bar = 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
local progress_bar = require "lib.progress_bar"
-- test progress_bar
local window_width, window_height = love.graphics.getDimensions()
local progress_bar_height = 30 -- TODO should not be hardcoded
local test = new_progress_bar(0, window_height - progress_bar_height, window_width, progress_bar_height)
local test = progress_bar(0, window_height - progress_bar_height, window_width, progress_bar_height)
test:set_text("This is a test text string of significant length. It exists to test printing over a progress bar.")
test:set_progress(math.random())