ESO Lua File v100011

libraries/zo_scenegraph/zo_scenegraphnode.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
ZO_SceneGraphNode = ZO_Object:Subclass()
function ZO_SceneGraphNode:New(...)
    local obj = ZO_Object.New(self)
    obj:Initialize(...)
    return obj
end
function ZO_SceneGraphNode:InitializeStaticBehavior()
    --called on the class table, not an instance. one event for all scene nodes
    self.UICustomScale = tonumber(GetSetting(SETTING_TYPE_UI, UI_SETTING_CUSTOM_SCALE))
    EVENT_MANAGER:RegisterForEvent("ZO_SceneGraphNode", EVENT_INTERFACE_SETTING_CHANGED, function(_, settingType, settingId)
        if settingType == SETTING_TYPE_UI and settingId == UI_SETTING_CUSTOM_SCALE then
            self.UICustomScale = tonumber(GetSetting(SETTING_TYPE_UI, UI_SETTING_CUSTOM_SCALE))
        end
    end)
end
function ZO_SceneGraphNode:Initialize(sceneGraph, name)
    self.sceneGraph = sceneGraph
    self.name = name
    self.translateX = 0
    self.translateY = 0
    self.translateZ = 0
    self.rotation = 0
    self.scale = 1
    self.anchorPoint = CENTER
    self:SetDirty(true)
end
function ZO_SceneGraphNode:GetSceneGraph()
    return self.sceneGraph
end
function ZO_SceneGraphNode:GetName()
    return self.name
end
function ZO_SceneGraphNode:SetParent(parent)
    self.parent = parent
    self.parent:OnChildAdded(self)
    self:SetDirty(true)
end
function ZO_SceneGraphNode:GetChildren()
    return self.children
end
function ZO_SceneGraphNode:SetDirty(dirty)
    self.dirty = dirty
    if dirty then
        self.sceneGraph:OnSceneNodeDirty()
    end
end
function ZO_SceneGraphNode:GetX()
    return self.translateX
end
function ZO_SceneGraphNode:AddX(dx)
    self:SetX(self.translateX + dx)
end
function ZO_SceneGraphNode:SetX(x)
    if self.translateX ~= x then
        self.translateX = x
        self:SetDirty(true)
    end
end
function ZO_SceneGraphNode:GetY()
    return self.translateY
end
function ZO_SceneGraphNode:AddY(dy)
    self:SetY(self.translateY + dy)
end
function ZO_SceneGraphNode:SetY(y)
    if self.translateY ~= y then
        self.translateY = y
        self:SetDirty(true)
    end
end
function ZO_SceneGraphNode:GetZ()
    return self.translateZ
end
function ZO_SceneGraphNode:SetZ(z)
    if self.translateZ ~= z then
        self.translateZ = z
        self:SetDirty(true)
    end
end
function ZO_SceneGraphNode:GetRotation()
    return self.rotation
end
function ZO_SceneGraphNode:AddRotation(radians)
    self:SetRotation(self.rotation + radians)
end
function ZO_SceneGraphNode:SetRotation(radians)
    if self.rotation ~= radians then
        self.rotation = radians % (2 * math.pi)
        self:SetDirty(true)
    end
end
function ZO_SceneGraphNode:SetScale(scale)
    if self.scale ~= scale then
        self.scale = scale
        self:SetDirty(true)
    end
end
function ZO_SceneGraphNode:ComputeSizeForDepth(x, y, z)
    local scale = 1 + self.translateZ + z
    local parent = self.parent
    while parent do
        scale = scale + parent.translateZ
        parent = parent.parent
    end
    return x * scale, y * scale
end
function ZO_SceneGraphNode:IsDirty()
    return self.dirty
end
function ZO_SceneGraphNode:AcquireWorkingMatrix()
    if not self.workingMatrix then
       self.workingMatrix = hmake Matrix33 { }
    end
    return self.workingMatrix
end
function ZO_SceneGraphNode:AcquireResultMatrix()
    if not self.resultMatrix then
        self.resultMatrix = hmake Matrix33 { }
    end
    return self.resultMatrix
end
function ZO_SceneGraphNode:BuildWorldViewMatrix()
    local resultMatrix : Matrix33
    self.finalRotation = self.rotation
    self.finalScale = self.scale
    self.finalTranslateZ = self.translateZ
    if self.scale ~= 1 then
        resultMatrix = self:AcquireResultMatrix()
        zo_setToScaleMatrix2D(resultMatrix, self.scale)
    end
    if self.translateX ~= 0 or self.translateY ~= 0 then
        if resultMatrix then
            local workingMatrix : Matrix33 = self:AcquireWorkingMatrix()
            zo_setToTranslationMatrix2D(workingMatrix, self.translateX, self.translateY)
            zo_matrixMultiply33x33(workingMatrix, resultMatrix, resultMatrix)
        else
            resultMatrix = self:AcquireResultMatrix()
            zo_setToTranslationMatrix2D(resultMatrix, self.translateX, self.translateY)
        end
    end
    if self.rotation ~= 0 then
        if resultMatrix then
            local workingMatrix : Matrix33 = self:AcquireWorkingMatrix()
            zo_setToRotationMatrix2D(workingMatrix, self.rotation)
            zo_matrixMultiply33x33(workingMatrix, resultMatrix, resultMatrix)
        else
            resultMatrix = self:AcquireResultMatrix()
            zo_setToRotationMatrix2D(resultMatrix, self.rotation)
        end
    end
    local parent = self.parent
    if parent then
        if resultMatrix then
            zo_matrixMultiply33x33(parent.worldViewMatrix, resultMatrix, resultMatrix)
        else
            resultMatrix = parent.worldViewMatrix
        end
        self.finalRotation = self.finalRotation + parent.finalRotation
        self.finalScale = self.finalScale * parent.finalScale
        self.finalTranslateZ = self.finalTranslateZ + parent.finalTranslateZ
    end
    if resultMatrix then
        self.worldViewMatrix = resultMatrix
    else
        self.worldViewMatrix = self:AcquireResultMatrix()
        zo_setToIdentityMatrix33(self.worldViewMatrix)
    end
    if self.controls then
        for i, control in ipairs(self.controls) do
            control:SetDrawLevel(self:ComputeDrawLevel(self.finalTranslateZ + self.controlZ[i]))
        end
    end
end
local ANCHOR_TO_NORMALIZED_X =
{
    [TOPLEFT] = 0,
    [LEFT] = 0,
    [BOTTOMLEFT] = 0,
    [TOP] = 0.5,
    [CENTER] = 0.5,
    [BOTTOM] = 0.5,
    [TOPRIGHT] = 1,
    [RIGHT] = 1,
    [BOTTOMRIGHT] = 1,
}
local ANCHOR_TO_NORMALIZED_Y =
{
    [TOPLEFT] = 0,
    [TOP] = 0,
    [TOPRIGHT] = 0,
    [LEFT] = 0.5,
    [CENTER] = 0.5,
    [RIGHT] = 0.5,
    [BOTTOMLEFT] = 1,
    [BOTTOM] = 1,
    [BOTTOMRIGHT] = 1,
}
function ZO_SceneGraphNode:Render()
    if self.controls and #self.controls > 0 then
        for i, control in ipairs(self.controls) do
            local distanceFromCamera = self.finalTranslateZ + self.controlZ[i] - self.sceneGraph:GetCameraZ()
            if distanceFromCamera > 0 then
                local depthScale = 1 / distanceFromCamera
                local positionX = self.controlX[i]
                local positionY = self.controlY[i]
                local worldViewMatrix : Matrix33 = self.worldViewMatrix
                local finalX = worldViewMatrix._11 * positionX + worldViewMatrix._12 * positionY + worldViewMatrix._13
                local finalY = worldViewMatrix._21 * positionX + worldViewMatrix._22 * positionY + worldViewMatrix._23
                finalX = finalX * depthScale
                finalY = finalY * depthScale
                finalX = finalX / self.UICustomScale
                finalY = finalY / self.UICustomScale
                local anchorPoint = self.controlAnchorPoint[i]
                control:SetAnchor(anchorPoint, nil, CENTER, finalX, finalY)
                if self.finalRotation ~= 0 and self.controlUseRotation[i] then
                    control:SetTextureRotation(self.finalRotation, ANCHOR_TO_NORMALIZED_X[anchorPoint], ANCHOR_TO_NORMALIZED_Y[anchorPoint])
                else
                    control:SetTextureRotation(0)
                end
                control:SetScale((self.finalScale * depthScale * self.controlScale[i]) / self.UICustomScale)
                control:SetHidden(self.controlHidden[i])
            else
                control:SetHidden(true)
            end
        end
    end
end
function ZO_SceneGraphNode:OnChildAdded(child)
    if self.children == nil then
        self.children = {}
    end
    table.insert(self.children, child)
end
function ZO_SceneGraphNode:ComputeDrawLevel(z)
    return 10000 - z * 100
end
function ZO_SceneGraphNode:AddControl(control, x, y, z)
    if x and y and z then
        if not self.controls then
            self.controls = {}
            self.controlX = {}
            self.controlY = {}
            self.controlZ = {}
            self.controlScale = {}
            self.controlHidden = {}
            self.controlAnchorPoint = {}
            self.controlUseRotation = {}
        end
        table.insert(self.controls, control)
        table.insert(self.controlX, x)
        table.insert(self.controlY, y)
        table.insert(self.controlZ, z)
        table.insert(self.controlScale, 1)
        table.insert(self.controlHidden, false)
        table.insert(self.controlAnchorPoint, CENTER)
        table.insert(self.controlUseRotation, true)
        self:SetDirty(true)
        self:RefreshControlIndices()
    end
end
local function RemoveFromTables(index, ...)
    for i = 1, select("#", ...) do
        table.remove(select(i, ...), index)
    end
end
function ZO_SceneGraphNode:RemoveControl(control)
    local controlIndex = self:GetControlIndex(control)
    if controlIndex then
        RemoveFromTables(controlIndex, self.controls, self.controlX, self.controlY, self.controlZ, self.controlScale, self.controlHidden, self.controlAnchorPoint, self.controlUseRotation)
        self:RefreshControlIndices()
    end
end
function ZO_SceneGraphNode:GetControlIndex(control)
    return control.index
end
function ZO_SceneGraphNode:RefreshControlIndices()
    for i, currentControl in ipairs(self.controls) do
        currentControl.index = i
    end
end
function ZO_SceneGraphNode:GetControl(i)
    return self.controls[i]
end
function ZO_SceneGraphNode:SetControlPosition(control, x, y, z)
    local index = self:GetControlIndex(control)
    self.controlX[index] = x
    self.controlY[index] = y
    self.controlZ[index] = z
    self:SetDirty(true)
end
function ZO_SceneGraphNode:SetControlHidden(control, hidden)
    local index = self:GetControlIndex(control)
    if self.controlHidden[index] ~= hidden then
        self.controlHidden[index] = hidden
        self:SetDirty(true)
    end
end
function ZO_SceneGraphNode:SetControlScale(control, scale)
    local index = self:GetControlIndex(control)
    if self.controlScale[index] ~= scale then
        self.controlScale[index] = scale
        self:SetDirty(true)
    end
end
function ZO_SceneGraphNode:GetControlScale(control, scale)
    local index = self:GetControlIndex(control)
    return self.controlScale[index]
end
function ZO_SceneGraphNode:SetControlAnchorPoint(control, anchorPoint)
    local index = self:GetControlIndex(control)
    if self.controlAnchorPoint[index] ~= anchorPoint then
        self.controlAnchorPoint[index] = anchorPoint
        self:SetDirty(true)
    end
end
function ZO_SceneGraphNode:SetControlUseRotation(control, useRotation)
    local index = self:GetControlIndex(control)
    if self.controlAnchorPoint[index] ~= anchorPoint then
        self.controlUseRotation[index] = useRotation
        self:SetDirty(true)
    end
end
ZO_SceneGraphNode:InitializeStaticBehavior()