73 lines
1.9 KiB
Lua
73 lines
1.9 KiB
Lua
require "lib.utils.vec3"
|
|
local baseElement = require "lib.ui.element"
|
|
|
|
local W = {}
|
|
--- @alias Alignment "topLeft" | "topCenter" | "topRight" | "centerLeft" | "center" | "centerRight" | "bottomLeft" | "bottomCenter" | "bottomRight"
|
|
--- @type {[Alignment]: Vec3}
|
|
local alignments = {
|
|
topLeft = Vec3 { 0, 0 },
|
|
topCenter = Vec3 { 0.5, 0 },
|
|
topRight = Vec3 { 1, 0 },
|
|
centerLeft = Vec3 { 0, 0.5 },
|
|
center = Vec3 { 0.5, 0.5 },
|
|
centerRight = Vec3 { 1, 0.5 },
|
|
bottomLeft = Vec3 { 0, 1 },
|
|
bottomCenter = Vec3 { 0.5, 1 },
|
|
bottomRight = Vec3 { 1, 1 }
|
|
}
|
|
|
|
|
|
--- @class UIRoot : Element
|
|
local Root = {}
|
|
setmetatable(Root, { __index = baseElement })
|
|
|
|
function Root.new(data)
|
|
return setmetatable(data, { __index = Root })
|
|
end
|
|
|
|
function Root:update(dt)
|
|
self.size = Vec3 { love.graphics.getWidth(), love.graphics.getHeight() }
|
|
end
|
|
|
|
W.Root = Root.new
|
|
--------------------------------------------------
|
|
|
|
--- @class Align : Element
|
|
--- @field alignment Alignment
|
|
local Align = {}
|
|
setmetatable(Align, { __index = baseElement })
|
|
|
|
function Align.new(data)
|
|
data.alignment = data.alignment or "center"
|
|
return setmetatable(data, { __index = Align })
|
|
end
|
|
|
|
function Align:update(dt)
|
|
local parent = self.parent --[[@as Element]]
|
|
local shift = alignments[self.alignment]
|
|
self.origin = Vec3 { parent.size.x * shift.x, parent.size.y * shift.y }
|
|
end
|
|
|
|
W.Align = Align.new
|
|
--------------------------------------------------
|
|
|
|
--- @class Rectangle : Element
|
|
--- @field color number[]
|
|
local Rectangle = {}
|
|
setmetatable(Rectangle, { __index = baseElement })
|
|
|
|
function Rectangle.new(data)
|
|
return setmetatable(data, { __index = Rectangle })
|
|
end
|
|
|
|
function Rectangle:draw()
|
|
love.graphics.setColor(self.color or { 1, 1, 1 })
|
|
love.graphics.rectangle("fill", self.origin.x - self.size.x / 2, self.origin.y - self.size.y / 2, self.size.x,
|
|
self.size.y)
|
|
end
|
|
|
|
W.Rectangle = Rectangle.new
|
|
---------------------------------------------------
|
|
|
|
return W
|