70 lines
2.2 KiB
Lua
70 lines
2.2 KiB
Lua
local Constraints = require "lib.simple_ui.core.constraints"
|
|
local Rect = require "lib.simple_ui.core.rect"
|
|
local StatefulElement = require "lib.simple_ui.core.stateful_element"
|
|
|
|
|
|
--- @class ScreenArea : StatefulElement
|
|
--- @field dimensions Rect
|
|
--- @field filter "nearest" | "linear"
|
|
local element = setmetatable({}, StatefulElement)
|
|
element.__index = element
|
|
element.type = "ScreenArea"
|
|
|
|
function element:build()
|
|
local cw, ch = self.state.canvas:getDimensions()
|
|
if cw ~= self.dimensions.width or ch ~= self.dimensions.height then
|
|
self.state.canvas = self:initState().canvas -- в реальном мире оно не будет пересоздаваться каждый кадр
|
|
end
|
|
return self.child
|
|
end
|
|
|
|
function element:layout()
|
|
local screenW, screenH = self.state.canvas:getDimensions()
|
|
|
|
self._constraints_ = Constraints {
|
|
maxWidth = screenW,
|
|
maxHeight = screenH
|
|
}
|
|
self._size_ = Vec3 { screenW, screenH }
|
|
|
|
if not self._child_ then return end
|
|
self._child_._constraints_ = Constraints(self._constraints_)
|
|
self._child_:layout()
|
|
self._child_._offset_ = Vec3 {}
|
|
end
|
|
|
|
function element:draw()
|
|
local oldCanvas = love.graphics.getCanvas()
|
|
|
|
love.graphics.push()
|
|
love.graphics.origin()
|
|
love.graphics.setCanvas(self.state.canvas)
|
|
love.graphics.clear()
|
|
if self._child_ then self._child_:draw() end
|
|
love.graphics.pop()
|
|
|
|
love.graphics.setCanvas(oldCanvas)
|
|
love.graphics.push("transform")
|
|
love.graphics.translate(self._offset_.x, self._offset_.y)
|
|
love.graphics.draw(self.state.canvas)
|
|
love.graphics.pop()
|
|
end
|
|
|
|
function element:initState()
|
|
local canvas = love.graphics.newCanvas(self.dimensions.width, self.dimensions.height)
|
|
canvas:setFilter(self.filter, self.filter)
|
|
return {
|
|
canvas = canvas
|
|
}
|
|
end
|
|
|
|
--- @return ScreenArea
|
|
--- @param values {key: any, dimensions: Rect?, filter: "nearest" | "linear" | nil, child: UIElement?}
|
|
function element:new(values)
|
|
if not values.dimensions then values.dimensions = Rect { width = love.graphics.getWidth(), height = love.graphics.getHeight() } end
|
|
if not values.filter then values.filter = "linear" end
|
|
return StatefulElement.new(self, values)
|
|
end
|
|
|
|
return element
|