cc-utilities/gfx/padding.lua
2024-10-04 00:29:14 +02:00

91 lines
2.1 KiB
Lua

local Event = require("gfx.event")
local Element = require("gfx.element")
local Padding = Element:new{
left = 0,
right = 0,
top = 0,
bottom = 0,
element = nil
}
function Padding:new(opts)
local obj = Element.new(self, opts)
obj.element:setPos(obj:getPaddingLeft(), obj:getPaddingTop())
obj:resize{
width = obj:getWidth(),
height = obj:getHeight()
}
obj.element:setParent(obj:_getWindow())
return obj
end
function Padding:resize(opts)
-- Un-pad dimensions and pass to child element
return self.element:resize{
width = (opts.width and opts.width - self:getPaddingLeft() - self:getPaddingRight()) or self:getWidth(),
height = (opts.height and opts.height - self:getPaddingTop() - self:getPaddingBottom()) or self:getHeight()
}
end
function Padding:draw()
if Element.draw(self) or self.element:_isDirty() then
local win = self:_getWindow()
win.clear()
self.element:draw()
return true
end
return false
end
function Padding:getPaddingLeft()
return self.left
end
function Padding:getPaddingRight()
return self.right
end
function Padding:getPaddingTop()
return self.top
end
function Padding:getPaddingBottom()
return self.bottom
end
function Padding:getInnerWidth()
return self.element:getWidth()
end
function Padding:getInnerHeight()
return self.element:getHeight()
end
function Padding:getWidth()
return self:getInnerWidth() + self:getPaddingLeft() + self:getPaddingRight()
end
function Padding:getHeight()
return self:getInnerHeight() + self:getPaddingTop() + self:getPaddingBottom()
end
function Padding:findById(id)
return Element.findById(self, id) or self.element:findById(id)
end
function Padding:handleEvent(evt)
if Element.handleEvent(self, evt) then
return true
end
if Event.isClickEvent(evt) then
if Event.containsClick(self.element, evt, -self:getPaddingLeft(), -self:getPaddingTop()) then
self.element:handleEvent({Event.repositionEvent(evt, -self:getPaddingLeft(), -self:getPaddingTop())})
end
else
return self.element:handleEvent(evt)
end
return false
end
return Padding