Refactor to more abstract field accesses

This commit is contained in:
Gabriel Tofvesson 2024-10-02 13:47:58 +02:00
parent 8b1228fa9e
commit f47a839341
3 changed files with 32 additions and 11 deletions

View File

@ -17,7 +17,7 @@ local Element = {
Element.__index = Element
local function createWindow(element)
return window.create(element.render.parent, element:getX(), element:getY(), element:getWidth(), element:getHeight(), element:isVisible())
return window.create(element.parent, element:getX(), element:getY(), element:getWidth(), element:getHeight(), element:isVisible())
end
@ -83,7 +83,7 @@ function Element:setFgColor(color)
if color ~= self.fgColor then
self:setDirty()
self.fgColor = color
self.render.window.setTextColor(color)
self:_getWindow().setTextColor(color)
end
end
@ -91,12 +91,28 @@ function Element:setBgColor(color)
if color ~= self.bgColor then
self:setDirty()
self.bgColor = color
self.render.window.setBackgroundColor(color)
self:_getWindow().setBackgroundColor(color)
end
end
function Element:getFgColor()
return self.fgColor
end
function Element:getBgColor()
return self.bgColor
end
function Element:_getWindow()
return self.window
end
function Element:_setWindow(window)
self.window = window
end
function Element:setVisible(visible)
self.render.window.setVisible(visible)
self:_getWindow().setVisible(visible)
end
function Element:resize(opts)
@ -157,7 +173,7 @@ function Element:getId()
end
function Element:redraw()
self.render.window.redraw()
self:_getWindow().redraw()
end
function Element:handleEvent(evt)
@ -173,7 +189,7 @@ function Element:setOnClick(onClick)
end
function Element:_reload()
self.render.window = createWindow(self)
self:_setWindow(createWindow(self))
end
return Element

View File

@ -68,7 +68,7 @@ end
function List:draw()
Element.draw(self)
self.render.window.clear()
self:_getWindow().clear()
for _,v in ipairs(self.children) do
v:draw()
end

View File

@ -9,10 +9,11 @@ function Text:new(o)
end
function Text:setText(text)
if self.text ~= text then
local current = self:getText()
if current ~= text then
self:setDirty()
local needReload = #self.text ~= #text
local needReload = #current ~= #text
self.text = text
if needReload then
self:_reload()
@ -20,9 +21,13 @@ function Text:setText(text)
end
end
function Text:getText()
return self.text
end
function Text:draw()
Element.draw(self)
self.render.window.write(self.text)
self:_getWindow().write(self:getText())
end
function Text:getHeight()
@ -30,7 +35,7 @@ function Text:getHeight()
end
function Text:getWidth()
return #self.text
return #self:getText()
end
function Text:setWidth()