uuhh, text? and i guess 'optimized', although its like 4 bytes less for a function
This commit is contained in:
parent
5fff4af053
commit
56014fd8f6
@ -13,9 +13,13 @@ ui.theme = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ui.events = {}
|
ui.events = {}
|
||||||
|
ui._wasDown = false
|
||||||
|
ui._focused = nil
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function ui.emit(event)
|
function ui.emit(event)
|
||||||
ui.events[event] = true
|
ui.events[event] = true -- should change to a custom ID soon to prevent conflicts.
|
||||||
end
|
end
|
||||||
|
|
||||||
function ui.on(event)
|
function ui.on(event)
|
||||||
@ -23,6 +27,7 @@ function ui.on(event)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function ui.flush()
|
function ui.flush()
|
||||||
|
ui._wasDown = love.mouse.isDown(1)
|
||||||
ui.events = {}
|
ui.events = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -38,26 +43,28 @@ function ui.button(x, y, w, h, text)
|
|||||||
local mousex, mousey = love.mouse.getPosition()
|
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
|
local hovered = mousex > x and mousex < x + w and mousey > y and mousey < y + h -- calculaters if its touching the box
|
||||||
if hovered then
|
if hovered then
|
||||||
|
-- makes dimed panel then text ontop
|
||||||
love.graphics.setColor(ui.theme.hover)
|
love.graphics.setColor(ui.theme.hover)
|
||||||
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)
|
||||||
love.graphics.setLineWidth(ui.theme.borderwidth)
|
love.graphics.setLineWidth(ui.theme.borderwidth)
|
||||||
love.graphics.rectangle("line", x, y, w, h)
|
love.graphics.rectangle("line", x, y, w, h)
|
||||||
love.graphics.setColor(ui.theme.text)
|
ui.text(x, y, w, h, text)
|
||||||
love.graphics.printf(text, x, y + h/2-7, w, "center")
|
|
||||||
else
|
else
|
||||||
-- makes a panel and then just adds text to it
|
ui.text(x, y, w, h, text) -- just normal text
|
||||||
ui.panel(x, y, w, h)
|
|
||||||
love.graphics.setColor(ui.theme.text)
|
|
||||||
love.graphics.printf(text, x, y + h/2-7, w, "center")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if hovered and love.mouse.isDown(1) then
|
if hovered and love.mouse.isDown(1) and not ui._wasDown then
|
||||||
ui.emit(text) -- uses button label as event name for now, change later to avoid same ID stuff
|
ui.emit(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
return hovered -- returns hovered
|
return hovered -- returns hovered
|
||||||
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
|
||||||
|
ui.panel(x,y,w,h)
|
||||||
|
love.graphics.setColor(ui.theme.text)
|
||||||
|
love.graphics.printf(text, x, y + h/2-7, w, "center")
|
||||||
|
end
|
||||||
|
|
||||||
return ui
|
return ui
|
||||||
6
main.lua
6
main.lua
@ -5,13 +5,15 @@ 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")
|
ui.button(100,200,100,100,"hello world")
|
||||||
if ui.on("hello world") then -- detect if the button is pressed.
|
if ui.on("hello world") then -- detect if the button is pressed.
|
||||||
ui.panel(250,250,250,250)
|
ui.panel(250,250,250,250)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ui.flush() -- KEEP AT END
|
||||||
end
|
end
|
||||||
Loading…
Reference in New Issue
Block a user