From 5b897d874fc01672a5b12bf4f56bccc0330423c1 Mon Sep 17 00:00:00 2001 From: Rose Liverman Date: Tue, 22 Sep 2026 15:43:02 -0600 Subject: [PATCH] progress_bar as own lib --- src/lib/progress_bar.lua | 36 ++++++++++++++++++++++++++++++++++++ src/main.lua | 40 ++-------------------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) create mode 100644 src/lib/progress_bar.lua diff --git a/src/lib/progress_bar.lua b/src/lib/progress_bar.lua new file mode 100644 index 0000000..0541f33 --- /dev/null +++ b/src/lib/progress_bar.lua @@ -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 diff --git a/src/main.lua b/src/main.lua index 9e06725..2fc5706 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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())