88 lines
2.7 KiB
Lua
88 lines
2.7 KiB
Lua
local ui = {}
|
|
|
|
ui.theme = {
|
|
bg = {0.13, 0.13, 0.14},
|
|
panel = {0.17, 0.17, 0.19},
|
|
border = {0.28, 0.28, 0.30}, -- also known as outline
|
|
borderwidth = 3,
|
|
text = {0.90, 0.90, 0.90},
|
|
text_dim = {0.50, 0.50, 0.55}, -- low-key forgot
|
|
accent = {0.27, 0.52, 0.90}, -- low-key also forgot
|
|
hover = {0.22, 0.22, 0.25},
|
|
active = {0.20, 0.40, 0.75}, -- i low-key forgot again, probably made for when a toggle or something is turned on
|
|
}
|
|
|
|
ui.events = {}
|
|
ui._wasDown = false
|
|
ui._focused = nil
|
|
ui._inputs = {}
|
|
|
|
|
|
function ui.emit(event)
|
|
ui.events[event] = true -- should change to a custom ID soon to prevent conflicts.
|
|
end
|
|
|
|
function ui.on(event)
|
|
return ui.events[event] == true
|
|
end
|
|
|
|
function ui.flush()
|
|
ui._wasDown = love.mouse.isDown(1)
|
|
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.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)
|
|
end
|
|
|
|
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
|
|
-- makes dimed panel then text ontop
|
|
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)
|
|
ui.text(x, y, w, h, text)
|
|
else
|
|
ui.text(x, y, w, h, text) -- just normal text
|
|
end
|
|
|
|
if hovered and love.mouse.isDown(1) and not ui._wasDown then
|
|
ui.emit(text)
|
|
end
|
|
|
|
return hovered -- returns hovered
|
|
end
|
|
|
|
function ui.text(x, y, w, h, text) -- expand on this, havent fully read the text documentation so i dont know if i should have more
|
|
ui.panel(x,y,w,h)
|
|
love.graphics.setColor(ui.theme.text)
|
|
love.graphics.printf(text, x, y + h/2-7, w, "center")
|
|
end
|
|
|
|
function ui.textInput(x,y,w,h,placeholder)
|
|
if ui.on(placeholder) then
|
|
ui.button(x,y,w,h,tostring(ui._inputs[ui._focused]))
|
|
else
|
|
ui.button(x,y,w,h,placeholder)
|
|
end
|
|
end
|
|
|
|
function love.textinput(t)
|
|
ui._inputs[ui._focused] = (tostring(ui._inputs[ui._focused] or "")) .. t
|
|
end
|
|
|
|
function love.keypressed(key)
|
|
if key == "backspace" then
|
|
ui._inputs[ui._focused] = ui._inputs[ui._focused]:sub(tostring(ui._inputs[ui._focused]):len())
|
|
end
|
|
end
|
|
|
|
return ui |