Back to Home

ESO Lua File v101043

libraries/zo_scenegraph/zo_scenegraph.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
ZO_ABOVE_SCENEGRAPH_DRAW_LEVEL = 100000
ZO_SceneGraph = ZO_Object:Subclass()
function ZO_SceneGraph:New(...)
    local obj = ZO_Object.New(self)
    obj:Initialize(...)
    return obj
end
function ZO_SceneGraph:Initialize(canvasControl, debugModeEnabled)
    self.nodes = {}
    self.canvasControl = canvasControl
    canvasControl:SetHandler("OnUpdate", function() self:OnUpdate() end)
    self.debugModeEnabled = debugModeEnabled
    self.debugTextureId = 1
    self.needsRender = false
    self.cameraNode = self:CreateNode("camera")
    self.cameraZ = -1
end
function ZO_SceneGraph:IsHidden()
    return self.canvasControl:IsHidden()
end
function ZO_SceneGraph:GetCameraX()
    return -self.cameraNode:GetX()
end
function ZO_SceneGraph:AddCameraX(dx)
    self.cameraNode:AddX(-dx)
end
function ZO_SceneGraph:SetCameraX(x)
    self.cameraNode:SetX(-x)
end
function ZO_SceneGraph:GetCameraY()
    return -self.cameraNode:GetY()
end
function ZO_SceneGraph:AddCameraY(dy)
    self.cameraNode:AddY(-dy)
end
function ZO_SceneGraph:SetCameraY(y)
    self.cameraNode:SetY(-y)
end
function ZO_SceneGraph:AddCameraRotation(radians)
    self.cameraNode:AddRotation(-radians)
end
function ZO_SceneGraph:SetCameraRotation(radians)
    self.cameraNode:SetRotation(-radians)
end
function ZO_SceneGraph:GetCameraZ()
    return self.cameraZ
end
function ZO_SceneGraph:AddCameraZ(z)
    self:SetCameraZ(self.cameraZ + z)
end
function ZO_SceneGraph:SetCameraZ(z)
    if self.cameraZ ~= z then
        self.cameraZ = z
        self.cameraNode:SetDirty(true)
    end
end
function ZO_SceneGraph:GetCameraNode()
    return self.cameraNode
end
function ZO_SceneGraph:GetCanvasControl()
    return self.canvasControl
end
function ZO_SceneGraph:GetNode(name)
    return self.nodes[name]
end
function ZO_SceneGraph:CreateNode(name)
    local node = ZO_SceneGraphNode:New(self, name)
    if self.debugModeEnabled then
        self:AddDebugTextureToNode(node)
    end
    self.nodes[name] = node
    return node
end
function ZO_SceneGraph:RemoveNode(node)
    -- after this point, this node will no longer participate in rendering, the node should eventually be garbage collected.
    -- it's important that you nil out references in application code too to achieve this.
    -- child controls do not hold references to their scene node, so they will
    -- also become "free-floating". Likewise, it's your responsibility to reuse
    -- those controls to not leak memory.
    node:SetParent(nil)
    self.nodes[node:GetName()] = nil
    local children = node:GetChildren()
    if children then
        for _, childNode in ipairs(children) do
            self:RemoveNode(childNode)
        end
    end
end
function ZO_SceneGraph:AddDebugTextureToNode(node)
    -- shortcut so you can see where a node is placed visually
    -- draws a purple box centered on the node's origin point
    local debugTexture = GetWindowManager():CreateControl(string.format("%sDebugTexture%d", self.canvasControl:GetName(), self.debugTextureId), self.canvasControl, CT_TEXTURE)
    self.debugTextureId = self.debugTextureId + 1
    debugTexture:SetDimensions(20, 20)
    debugTexture:SetColor(0.7, 0.7, 1, 1)
    debugTexture:SetPixelRoundingEnabled(false)
    node:AddTexture(debugTexture, 0, 0, 0)
end
function ZO_SceneGraph:OnSceneNodeDirty()
    self.needsRender = true
end
function ZO_SceneGraph:OnUpdate()
    if self.needsRender then
        self.needsRender = false
        self:Render()
    end
end
do
    local function RenderNode(node, dirtyUpstream)
        local dirty = dirtyUpstream or node:IsDirty()
        if dirty then
            node:BuildWorldViewMatrix()
            node:Render()
        end
        local children = node:GetChildren()
        if children ~= nil then
            for i = 1, #children do
                local child = children[i]
                RenderNode(child, dirty)
            end
        end
    end
    function ZO_SceneGraph:Render()
        local NOT_DIRTY_UPSTREAM = false
        RenderNode(self.cameraNode, NOT_DIRTY_UPSTREAM)
    end
end