test progress bar

This commit is contained in:
2026-09-22 15:31:42 -06:00
parent 71038321b3
commit 20dbe60a86
3 changed files with 688 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
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
-- 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)
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())
love.draw = function()
test:draw()
end
love.keypressed = function(key)
if key == "escape" then
-- TODO save any remaining log
love.event.quit()
end
end