From e47799650a24d35019f53f40489cd2cc286f2e86 Mon Sep 17 00:00:00 2001 From: Simon Kellet Date: Wed, 31 Jul 2024 15:20:12 +0100 Subject: [PATCH] added start of gui lib --- gui.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 gui.lua diff --git a/gui.lua b/gui.lua new file mode 100644 index 0000000..ca81f4d --- /dev/null +++ b/gui.lua @@ -0,0 +1,43 @@ +GUI = Object:extend() + +function GUI:new() end + +function GUI:newButton(x, y, w, h, text, selected) + love.graphics.setColor(255, 255, 255) -- reset colours + + --x,y is the top left corner of the button + local rounding = 30 -- used for rounding the buttons + local xoff = x + 10 + local yoff = y - 10 + local textX, textY = 0, 0 + -- Get width and height of text + local tw = MenuFont:getWidth(text) + local th = MenuFont:getHeight(text) + love.graphics.setFont(MenuFont) + + if not selected then + love.graphics.setColor(love.math.colorFromBytes(41, 134, 204)) + love.graphics.rectangle("fill", x, y, w, h, rounding, rounding) + + -- Calculate position to center the text + textX = x + (w - tw) / 2 + textY = y + (h - th) / 2 + -- Place text inside the rectangle + love.graphics.setColor(0, 0, 0) + love.graphics.print(text, textX, textY) + elseif selected then + -- If selected, raise the button and draw a shadow + love.graphics.setColor(love.math.colorFromBytes(0, 0, 0)) + love.graphics.rectangle("fill", x, y, w, h, rounding, rounding) -- shadow + + love.graphics.setColor(love.math.colorFromBytes(244, 67, 54)) + love.graphics.rectangle("fill", xoff, yoff, w, h, rounding, rounding) -- button offset + + -- Calculate position to center the text + textX = xoff + (w - tw) / 2 + textY = yoff + (h - th) / 2 + -- Place text inside the rectangle + love.graphics.setColor(1, 1, 1) + love.graphics.print(text, textX, textY) + end +end