Allow elements to have multiple parents

This commit is contained in:
Fierelier 2022-12-01 23:59:42 +01:00
parent cccc8d1b4d
commit 3460748bc8
1 changed files with 33 additions and 25 deletions

View File

@ -41,14 +41,17 @@ function engine.element.create(elementType)
local element = { local element = {
memlo = { memlo = {
_type = engine.elementTypes[elementType],
_children = {}, _children = {},
_type = engine.elementTypes[elementType] _parents = {}
}, },
memhi = { memhi = {
_id = id, _id = id,
_type = elementType, _type = elementType,
_children = {}, _children = {},
_childrenCount = 0 _childrenCount = 0,
_parents = {},
_parentsCount = 0
} }
} }
@ -59,43 +62,48 @@ function engine.element.create(elementType)
end end
function engine.element.attach(child,parent) function engine.element.attach(child,parent)
if child.memlo._parent ~= nil then if child.memlo._parents[parent.memhi._id] ~= nil then return end
child.memlo._parent.memlo._children[child.memhi._id] = nil child.memlo._parents[parent.memhi._id] = parent
child.memlo._parent.memhi._children[child.memhi._id] = nil child.memhi._parents[parent.memhi._id] = true
child.memlo._parent.memhi._childrenCount = child.memlo._parent.memhi._childrenCount - 1 child.memhi._parentsCount = child.memhi._parentsCount + 1
end
child.memlo._parent = parent
if parent == nil then
child.memhi._parent = nil
return
end
child.memhi._parent = parent.memhi._id
parent.memhi._children[child.memhi._id] = true
parent.memlo._children[child.memhi._id] = child parent.memlo._children[child.memhi._id] = child
parent.memhi._children[child.memhi._id] = true
parent.memhi._childrenCount = parent.memhi._childrenCount + 1 parent.memhi._childrenCount = parent.memhi._childrenCount + 1
end end
function engine.element.detach(child,parent)
if child.memlo._parents[parent.memhi._id] == nil then return end
child.memlo._parents[parent.memhi._id] = nil
child.memhi._parents[parent.memhi._id] = nil
child.memhi._parentsCount = child.memhi._parentsCount - 1
parent.memlo._children[child.memhi._id] = nil
parent.memhi._children[child.memhi._id] = nil
parent.memhi._childrenCount = parent.memhi._childrenCount - 1
end
function engine.element.destroy(element,destroyChildren) function engine.element.destroy(element,destroyChildren)
if destroyChildren == nil then destroyChildren = true end if destroyChildren == nil then destroyChildren = true end
engine.element.attach(element)
if destroyChildren == true then for _,parent in pairs(element.memlo._parents) do
if element.memhi._childrenCount > 0 then engine.element.detach(element,parent)
for i,v in pairs(element.memlo._children) do end
engine.element.destroy(v)
for _,child in pairs(element.memlo._children) do
engine.element.detach(child,element)
if destroyChildren == true then
if child.memhi._parentsCount == 0 then
engine.element.destroy(child,true)
end end
end end
end end
engine.elementTypes[element.memhi._type].destroy(element) element.memlo._type.destroy(element)
engine.elements[element.memhi._id] = nil engine.elements[element.memhi._id] = nil
engine.elementsByType[element.memhi._type][element.memhi._id] = nil engine.elementsByType[element.memhi._type][element.memhi._id] = nil
engine.elementTypes[element.memhi._type]._references = engine.elementTypes[element.memhi._type]._references - 1 element.memlo._type._references = engine.elementTypes[element.memhi._type]._references - 1
if engine.elementTypes[element.memhi._type]._references == 0 then if element.memlo._type._references == 0 then
engine.elementTypes[element.memhi._type].unload() element.memlo._type.unload()
engine.elementTypes[element.memhi._type] = nil engine.elementTypes[element.memhi._type] = nil
engine.elementsByType[element.memhi._type] = nil engine.elementsByType[element.memhi._type] = nil
end end