helium/core/stack.lua
Elmārs Āboliņš f023959137 commit
2020-08-14 01:59:12 +03:00

109 lines
2.4 KiB
Lua

--Builds the element stack basically
local path = string.sub(..., 1, string.len(...) - string.len(".core.stack"))
local helium = require(path .. ".dummy")
---@class context
local context = {}
context.__index = context
local activeContext
---@param elem element
function context.new(elem)
local ctx = setmetatable({
view = elem.view,
element = elem,
childrenContexts = {},
inputContext = helium.input.newContext(elem),
childRenderTime = 0
}, context)
return ctx
end
function context:bubbleUpdate()
self.element.settings.pendingUpdate = true
self.element.settings.needsRendering = true
if self.parentCtx and self.parentCtx~=self then
self.parentCtx:bubbleUpdate()
end
end
function context:set()
if activeContext then
if not self.parentCtx and activeContext~=self then
self.parentCtx = activeContext
activeContext.childrenContexts[#activeContext.childrenContexts] = self
end
self.absX = self.parentCtx.absX + self.view.x
self.absY = self.parentCtx.absY + self.view.y
activeContext = self
else
self.absX = self.view.x
self.absY = self.view.y
activeContext = self
end
self.inputContext:set()
end
function context:unset()
self.inputContext:unset()
self.inputContext:afterLoad()
if self.parentCtx then
activeContext = self.parentCtx
else
activeContext = nil
end
end
function context:unsuspend()
self.inputContext:unsuspend()
end
function context:startSelfRender()
self.childRenderTime = 0
end
function context:passTimeTo(time)
self.childRenderTime = self.childRenderTime + time
end
function context:endSelfRender(time)
if self.parentCtx then
self.parentCtx:passTimeTo(time)
end
return time-self.childRenderTime
end
function context:destroy()
self.elem:undraw()
for i = 1, #self.childrenContexts do
self.childrenContexts[i]:destroy()
end
end
function context:suspend()
self.inputContext:set()
self.inputContext:suspend()
self.inputContext:unset()
end
function context:getChildrenCount()
return #self.childrenContexts
end
--Function meant for external context capture
function context.getContext()
return activeContext
end
return context