yoo, really fucking big update. its uhh, TEXT. really difficult

This commit is contained in:
Jayden Khan 2026-05-26 17:05:56 -07:00
parent 0f17b262e3
commit b46bfc32d6
2 changed files with 78 additions and 24 deletions

View File

@ -8,7 +8,7 @@ ui.theme = {
text = {0.90, 0.90, 0.90}, text = {0.90, 0.90, 0.90},
text_dim = {0.50, 0.50, 0.55}, -- low-key forgot text_dim = {0.50, 0.50, 0.55}, -- low-key forgot
accent = {0.27, 0.52, 0.90}, -- low-key also forgot accent = {0.27, 0.52, 0.90}, -- low-key also forgot
hover = {0.22, 0.22, 0.25}, hover = {0.22, 0.25, 0.35},
active = {0.20, 0.40, 0.75}, -- i low-key forgot again, probably made for when a toggle or something is turned on active = {0.20, 0.40, 0.75}, -- i low-key forgot again, probably made for when a toggle or something is turned on
} }
@ -16,7 +16,8 @@ ui.events = {}
ui._wasDown = false ui._wasDown = false
ui._focused = nil ui._focused = nil
ui._inputs = {} ui._inputs = {}
ui._claimedFocus = false
ui._clickConsumed = false
function ui.emit(event) function ui.emit(event)
ui.events[event] = true -- should change to a custom ID soon to prevent conflicts. ui.events[event] = true -- should change to a custom ID soon to prevent conflicts.
@ -26,9 +27,15 @@ function ui.on(event)
return ui.events[event] == true return ui.events[event] == true
end end
function ui.flush() function ui.flush() -- all issues is because of this btw (joke, kinda. bunch of bugs from it)
ui._wasDown = love.mouse.isDown(1) local isDown = love.mouse.isDown(1)
if isDown and not ui._wasDown and not ui._claimedFocus then
ui._focused = nil
end
ui._wasDown = isDown -- update AFTER using it
ui._claimedFocus = false
ui.events = {} ui.events = {}
ui._clickConsumed = false
end end
function ui.panel(x, y, w, h) -- this thingy magic panel creator makes a ui with outline. (x pos, y pos, width, height) function ui.panel(x, y, w, h) -- this thingy magic panel creator makes a ui with outline. (x pos, y pos, width, height)
@ -51,37 +58,74 @@ function ui.button(x, y, w, h, text)
love.graphics.rectangle("line", x, y, w, h) love.graphics.rectangle("line", x, y, w, h)
ui.text(x, y, w, h, text) ui.text(x, y, w, h, text)
else else
ui.text(x, y, w, h, text) -- just normal text ui.textPanel(x, y, w, h, text) -- just normal text with a background
end end
if hovered and love.mouse.isDown(1) and not ui._wasDown then if hovered and love.mouse.isDown(1) and not ui._wasDown and not ui._clickConsumed then
ui.emit(text) ui.emit(text)
ui._clickConsumed = true
end end
return hovered -- returns hovered return hovered -- returns hovered, this means hovered is returned. and hovered being returned means it returns what hovered is
end 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 function ui.textPanel(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) ui.panel(x, y, w, h)
ui.text(x, y, w, h, text)
end
function ui.text(x, y, w, h, text)
local font = love.graphics.getFont()
local _, lines = font:getWrap(text, w)
local lineCount = #lines
local lineHeight = font:getHeight()
local totalHeight = lineCount * lineHeight
local startY = y + (h - totalHeight) / 2
love.graphics.setScissor(x, y, w, h)
love.graphics.setColor(ui.theme.text) love.graphics.setColor(ui.theme.text)
love.graphics.printf(text, x, y + h/2-7, w, "center") love.graphics.printf(text, x, startY, w, "center")
love.graphics.setScissor() -- limits drawing space.
end end
function ui.textInput(x,y,w,h,placeholder) function ui.textInput(x, y, w, h, placeholder) -- two days for this to work...
if ui.on(placeholder) then local text = ui._inputs[placeholder] or ""
ui.button(x,y,w,h,tostring(ui._inputs[ui._focused])) local focused = ui._focused == placeholder
local mousex, mousey = love.mouse.getPosition()
local clickedInside = mousex > x and mousex < x + w and mousey > y and mousey < y + h
if love.mouse.isDown(1) and not ui._wasDown and clickedInside then
ui._focused = placeholder
ui._claimedFocus = true
end
if focused then
ui.panel(x, y, w, h)
if text == "" then
local font = love.graphics.getFont()
local lineHeight = font:getHeight()
local startY = y + (h - lineHeight) / 2
love.graphics.setColor(ui.theme.text_dim)
love.graphics.printf(placeholder, x + 4, startY, w - 8, "left")
else else
ui.button(x,y,w,h,placeholder) ui.text(x, y, w, h, text)
end
else
ui.button(x, y, w, h, text == "" and placeholder or text)
end end
end end
function love.textinput(t)
ui._inputs[ui._focused] = (tostring(ui._inputs[ui._focused] or "")) .. t function ui.textinput_char(t)
if ui._focused then
ui._inputs[ui._focused] = (ui._inputs[ui._focused] or "") .. t
end
end end
function love.keypressed(key) function ui.textinput_backspace()
if key == "backspace" then if ui._focused then
ui._inputs[ui._focused] = ui._inputs[ui._focused]:sub(tostring(ui._inputs[ui._focused]):len()) local str = ui._inputs[ui._focused] or ""
ui._inputs[ui._focused] = str:sub(1, -2)
end end
end end

View File

@ -4,6 +4,16 @@ function love.load()
end end
function love.textinput(t)
ui.textinput_char(t)
end
function love.keypressed(key)
if key == "backspace" then
ui.textinput_backspace()
end
end
function love.update(dt) function love.update(dt)
end end