buttons and stuff

This commit is contained in:
Jayden Khan 2026-05-21 18:38:35 -07:00
parent bd4b2f2345
commit 5fff4af053
2 changed files with 47 additions and 7 deletions

View File

@ -3,16 +3,30 @@ local ui = {}
ui.theme = { ui.theme = {
bg = {0.13, 0.13, 0.14}, bg = {0.13, 0.13, 0.14},
panel = {0.17, 0.17, 0.19}, panel = {0.17, 0.17, 0.19},
border = {0.28, 0.28, 0.30}, border = {0.28, 0.28, 0.30}, -- also known as outline
borderwidth = 3, borderwidth = 3,
text = {0.90, 0.90, 0.90}, text = {0.90, 0.90, 0.90},
text_dim = {0.50, 0.50, 0.55}, text_dim = {0.50, 0.50, 0.55}, -- low-key forgot
accent = {0.27, 0.52, 0.90}, accent = {0.27, 0.52, 0.90}, -- low-key also forgot
hover = {0.22, 0.22, 0.25}, hover = {0.22, 0.22, 0.25},
active = {0.20, 0.40, 0.75}, active = {0.20, 0.40, 0.75}, -- i low-key forgot again
} }
function ui.panel(x, y, w, h) ui.events = {}
function ui.emit(event)
ui.events[event] = true
end
function ui.on(event)
return ui.events[event] == true
end
function ui.flush()
ui.events = {}
end
function ui.panel(x, y, w, h) -- this thingy magic panel creator makes a ui with outline. (x pos, y pos, width, height)
love.graphics.setColor(ui.theme.panel) love.graphics.setColor(ui.theme.panel)
love.graphics.rectangle("fill", x, y, w, h) love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(ui.theme.border) love.graphics.setColor(ui.theme.border)
@ -20,8 +34,30 @@ function ui.panel(x, y, w, h)
love.graphics.rectangle("line", x, y, w, h) love.graphics.rectangle("line", x, y, w, h)
end end
function ui.button() function ui.button(x, y, w, h, text)
local mousex, mousey = love.mouse.getPosition()
local hovered = mousex > x and mousex < x + w and mousey > y and mousey < y + h -- calculaters if its touching the box
if hovered then
love.graphics.setColor(ui.theme.hover)
love.graphics.rectangle("fill", x, y , w, h)
love.graphics.setColor(ui.theme.border)
love.graphics.setLineWidth(ui.theme.borderwidth)
love.graphics.rectangle("line", x, y ,w, h)
love.graphics.setColor(ui.theme.text)
love.graphics.printf(text, x, y + h/2-7, w, "center")
else
-- makes a panel and then just adds text to it
ui.panel(x, y, w, h)
love.graphics.setColor(ui.theme.text)
love.graphics.printf(text, x, y + h/2-7, w, "center")
end
if hovered and love.mouse.isDown(1) then
ui.emit(text) -- uses button label as event name for now, change later to avoid same ID stuff
end
return hovered -- returns hovered
end end
return ui return ui

View File

@ -5,9 +5,13 @@ function love.load()
end end
function love.update(dt) function love.update(dt)
ui.flush()
end end
function love.draw() function love.draw()
ui.panel(50,50,50,50) ui.panel(50,50,50,50)
local test = ui.button(100,200,100,100,"hello world")
if ui.on("hello world") then -- detect if the button is pressed.
ui.panel(250,250,250,250)
end
end end