Back to Home

ESO Lua File v101042

ingame/antiquities/keyboard/antiquitylore_keyboard.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
local SCROLL_MAGNITUDE_THRESHOLD = 1
ZO_AntiquityLoreReader_Keyboard = ZO_Object:Subclass()
function ZO_AntiquityLoreReader_Keyboard:New(...)
    local object = ZO_Object.New(self)
    object:Initialize(...)
    return object
end
function ZO_AntiquityLoreReader_Keyboard:Initialize(control)
end
function ZO_AntiquityLoreReader_Keyboard:InitializeControls(control)
    control.owner = self
    self.control = control
    self.loreEntryControls = {}
    self.loreScroll = self.control:GetNamedChild("Content")
    self.loreScrollChild = self.loreScroll:GetNamedChild("ScrollChild")
    -- Allow the lore reader's scroll control to scroll beyond the standard bounds
    -- in order to support the central display of all lore entries.
    self.accumulatedScrollMagnitude = 0
    self.loreScroll.scroll = self.loreScroll
    self.loreScroll:SetScrollBounding(SCROLL_BOUNDING_UNBOUND)
    self.loreScroll:SetFadeGradient(1, 0, 1, 50)
    self.loreScroll:SetFadeGradient(2, 0, -1, 100)
end
function ZO_AntiquityLoreReader_Keyboard:FocusLoreEntry(control)
    control.highlightAnimation:PlayForward()
end
function ZO_AntiquityLoreReader_Keyboard:UnfocusLoreEntry(control)
    control.highlightAnimation:PlayBackward()
end
function ZO_AntiquityLoreReader_Keyboard:Reset()
    for loreEntryControlIndex, loreEntryControl in ipairs(self.loreEntryControls) do
        self:FocusLoreEntry(loreEntryControl)
        loreEntryControl.treeNode = nil
        loreEntryControl:SetHandler("OnMouseDown", nil)
        loreEntryControl:SetMouseEnabled(false)
    end
    ANTIQUITY_LORE_DOCUMENT_MANAGER:ReleaseAllObjects(self.loreScrollChild)
    ZO_ClearNumericallyIndexedTable(self.loreEntryControls)
    self.previouslyAddedLoreEntryIndex = 0
    self.previouslyAddedLoreEntryControl = nil
    self.selectedLoreEntryControl = nil
end
function ZO_AntiquityLoreReader_Keyboard:AddLoreEntry(loreEntryData)
    local loreEntryIndex = self.previouslyAddedLoreEntryIndex + 1
    self.previouslyAddedLoreEntryIndex = loreEntryIndex
    -- Acquire and anchor lore entries in a back-and-forth (left, right, left, etc.) pattern.
    local loreEntryControl = ANTIQUITY_LORE_DOCUMENT_MANAGER:AcquireWideDocumentForLoreEntry(self.loreScrollChild, loreEntryData.antiquityId, loreEntryData.loreEntryIndex)
    loreEntryControl:ClearAnchors()
    if not self.previouslyAddedLoreEntryControl then
        loreEntryControl:SetAnchor(TOP)
    else
        local offsetX
        if loreEntryIndex % 2 == 0 then
            offsetX = 50
        else
            offsetX = -50
        end
        loreEntryControl:SetAnchor(TOPLEFT, self.previouslyAddedLoreEntryControl, BOTTOMLEFT, offsetX, -60)
    end
    loreEntryControl:SetHidden(false)
    loreEntryControl:SetMouseEnabled(true)
    loreEntryControl:SetHandler("OnMouseDown", ZO_AntiquityLoreEntry_OnClicked)
    self:UnfocusLoreEntry(loreEntryControl)
    self.previouslyAddedLoreEntryControl = loreEntryControl
    table.insert(self.loreEntryControls, loreEntryControl)
    return loreEntryControl
end
function ZO_AntiquityLoreReader_Keyboard:ShowAntiquityLoreEntry(data)
    if data then
        local loreEntryControl = data.loreEntryControl
        if loreEntryControl then
            local scrollControl = self.loreScroll
            local scrollHeight = scrollControl:GetHeight()
            scrollControl:SetVerticalScroll(0)
            -- Scroll the selected lore entry to the center of the scroll control's view.
            local controlOffset = loreEntryControl:GetTop()
            local controlHeight = loreEntryControl:GetHeight()
            local targetOffset = controlOffset - 0.5 * scrollHeight + 0.5 * controlHeight
            scrollControl:SetVerticalScroll(targetOffset)
            if self.selectedLoreEntryControl then
                self:UnfocusLoreEntry(self.selectedLoreEntryControl)
            end
            if data.unlocked then
                self:FocusLoreEntry(loreEntryControl)
            end
            self.selectedLoreEntryControl = loreEntryControl
            -- Update the lore entries' draw levels such that the focused control (loreEntryControl) is on top
            -- with the adjacent lore entries appearing in descending order in both directions (above and below).
            local currentDrawLevel = 100
            local drawLevelIncrement = 1
            for index, control in ipairs(self.loreEntryControls) do
                control:SetDrawLevel(currentDrawLevel)
                control.backgroundTexture:SetDrawLevel(currentDrawLevel)
                if control == loreEntryControl then
                    drawLevelIncrement = -1
                end
                currentDrawLevel = currentDrawLevel + drawLevelIncrement
            end
            PlaySound(SOUNDS.BOOK_PAGE_TURN)
        end
    end
end
ZO_AntiquityLore_Keyboard = ZO_Object:Subclass()
function ZO_AntiquityLore_Keyboard:New(...)
    local object = ZO_Object.New(self)
    object:Initialize(...)
    return object
end
function ZO_AntiquityLore_Keyboard:Initialize(control)
end
function ZO_AntiquityLore_Keyboard:InitializeControls(control)
    control.owner = self
    self.control = control
    self.antiquityIcon = self.control:GetNamedChild("AntiquityIcon")
    self.antiquityName = self.control:GetNamedChild("AntiquityName")
    self.loreEntryCount = self.control:GetNamedChild("LoreEntryCount")
end
function ZO_AntiquityLore_Keyboard:InitializeScene()
    self.scene = ZO_Scene:New("antiquityLoreKeyboard", SCENE_MANAGER)
    SYSTEMS:RegisterKeyboardObject("antiquityLoreKeyboard", self)
    SYSTEMS:RegisterKeyboardRootScene("antiquityLoreKeyboard", self.scene)
    ANTIQUITY_LORE_KEYBOARD_SCENE = self.scene
    self.scene:RegisterCallback("StateChange", function(oldState, newState)
        if newState == SCENE_SHOWING then
            KEYBIND_STRIP:RemoveDefaultExit()
            KEYBIND_STRIP:AddKeybindButtonGroup(self.keybindStripDescriptor)
        elseif newState == SCENE_HIDING then
            KEYBIND_STRIP:RemoveKeybindButtonGroup(self.keybindStripDescriptor)
            KEYBIND_STRIP:RestoreDefaultExit()
            ANTIQUITY_LORE_READER_KEYBOARD:Reset()
        end
    end)
end
function ZO_AntiquityLore_Keyboard:InitializeKeybindDescriptors()
    self.keybindStripDescriptor =
    {
        {
            alignment = KEYBIND_STRIP_ALIGN_RIGHT,
            name = GetString(SI_ANTIQUITY_BACK_TO_JOURNAL),
            keybind = "UI_SHORTCUT_EXIT",
            order = -10000,
            callback = function()
                SCENE_MANAGER:Show("antiquityJournalKeyboard")
            end,
        },
    }
end
function ZO_AntiquityLore_Keyboard:InitializeEntryTree()
    local function TreeEntryEquality(left, right)
        return left.loreEntryIndex == right.loreEntryIndex
    end
    local function SetTreeEntryState(control, unlocked, selected)
        local iconTexture
        if unlocked then
            if selected then
                iconTexture = "EsoUI/Art/Antiquities/bullet_active.dds"
            else
                iconTexture = "EsoUI/Art/Antiquities/bullet.dds"
            end
        else
            iconTexture = "EsoUI/Art/Antiquities/bullet_empty.dds"
        end
        control.icon:SetTexture(iconTexture)
        control.iconHighlight:SetTexture(iconTexture)
    end
    local function TreeEntrySetup(node, control, data, open)
        control.text:SetModifyTextType(MODIFY_TEXT_TYPE_UPPERCASE)
        control.text:SetFont("ZoFontWinH3")
        control.text:SetText(data.title)
        node:SetEnabled(data.unlocked)
        SetTreeEntryState(control, data.unlocked, node == node:IsSelected())
        control.subText = control:GetNamedChild("SubText")
        if control.subText then
            if data.subTitle then
                control.subText:SetText(data.subTitle)
            end
            control.subText:SetHidden(not data.subTitle)
            control.subText:SetSelected(open)
            control.subText:SetEnabled(data.unlocked)
            local DISABLE_SCALING = false
            ZO_IconHeader_Setup(control, open, data.unlocked, DISABLE_SCALING, ZO_AntiquityLore_IconHeader_UpdateSize)
        else
            ZO_IconHeader_Setup(control, open, data.unlocked)
        end
    end
    local function TreeEntryOnSelected(control, data, selected, reselectingDuringRebuild)
        control:SetSelected(selected)
        if selected then
            ANTIQUITY_LORE_READER_KEYBOARD:ShowAntiquityLoreEntry(data)
        end
        SetTreeEntryState(control, data.unlocked, selected)
    end
    self.selectedLoreEntryIndex = 0
    local DEFAULT_INDENT = 0
    local DEFAULT_SPACING = -10
    local TREE_WIDTH = 400
    self.loreEntryTreeScroll = GetControl(self.control, "LoreEntryContainer")
    self.loreEntryTree = ZO_Tree:New(GetControl(self.control, "LoreEntryContainerScrollChild"), DEFAULT_INDENT, DEFAULT_SPACING, TREE_WIDTH)
    self.loreEntryTree:AddTemplate("ZO_IconChildlessHeader", TreeEntrySetup, TreeEntryOnSelected, TreeEntryEquality)
    self.loreEntryTree:AddTemplate("ZO_AntiquityLore_SetIconChildlessHeader", TreeEntrySetup, TreeEntryOnSelected, TreeEntryEquality)
    self.loreEntryTree:SetExclusive(true)
    self.loreEntryTree:SetOpenAnimation("ZO_TreeOpenAnimation")
end
function ZO_AntiquityLore_Keyboard:GetData()
    if self.antiquityId then
        return ANTIQUITY_DATA_MANAGER:GetAntiquityData(self.antiquityId)
    else
        return ANTIQUITY_DATA_MANAGER:GetAntiquitySetData(self.antiquitySetId)
    end
end
function ZO_AntiquityLore_Keyboard:GetLoreEntryData()
    local data = self:GetData()
    return data:GetLoreEntries()
end
function ZO_AntiquityLore_Keyboard:Reset()
    self.antiquityId = nil
    self.antiquitySetId = nil
    self.accumulatedScrollMagnitude = 0
    self.loreEntryTree:Reset()
    ANTIQUITY_LORE_READER_KEYBOARD:Reset()
end
function ZO_AntiquityLore_Keyboard:Refresh()
    local antiquityData = self:GetData()
    local loreEntries = self:GetLoreEntryData()
    self.antiquityIcon:SetTexture(antiquityData:GetIcon())
    self.antiquityName:SetText(antiquityData:GetColorizedFormattedName())
    if loreEntries then
        local numUnlockedLoreEntries = antiquityData:GetNumUnlockedLoreEntries()
        local numLoreEntries = antiquityData:GetNumLoreEntries()
        self.loreEntries = loreEntries
        self.loreEntryCount:SetText(zo_strformat(SI_ANTIQUITY_CODEX_ENTRIES_FOUND, numUnlockedLoreEntries, numLoreEntries))
        local firstNode
        for loreEntryIndex, loreEntryData in ipairs(loreEntries) do
            local loreEntryControl
            if loreEntryData.unlocked then
                loreEntryControl = ANTIQUITY_LORE_READER_KEYBOARD:AddLoreEntry(loreEntryData)
            end
            local data =
            {
                title = loreEntryData.displayName,
                loreEntryIndex = loreEntryIndex,
                loreEntryControl = loreEntryControl,
                unlocked = loreEntryData.unlocked,
                subTitle = loreEntryData.fragmentName and ZO_CachedStrFormat(SI_ANTIQUITY_NAME_FORMATTER, loreEntryData.fragmentName),
            }
            local template = antiquityData.antiquitySetId and "ZO_AntiquityLore_SetIconChildlessHeader" or "ZO_IconChildlessHeader"
            local node = self.loreEntryTree:AddNode(template, data)
            if loreEntryControl then
                loreEntryControl.treeNode = node
                   firstNode = firstNode or node
            end
        end
        -- Select the first node in the tree.
        self.loreEntryTree:Commit(firstNode)
        -- Force the first node to be focused, even if it is locked.
        ANTIQUITY_LORE_READER_KEYBOARD:ShowAntiquityLoreEntry(firstNode)
    end
end
function ZO_AntiquityLore_Keyboard:ShowAntiquity(antiquityId)
    self:Reset()
    self.antiquityId = antiquityId
    self:Refresh()
    SCENE_MANAGER:Show("antiquityLoreKeyboard")
end
function ZO_AntiquityLore_Keyboard:ShowAntiquitySet(antiquitySetId)
    self:Reset()
    self.antiquitySetId = antiquitySetId
    self:Refresh()
    SCENE_MANAGER:Show("antiquityLoreKeyboard")
end
function ZO_AntiquityLore_Keyboard:SelectTreeNodeControl(control)
    if control and control.treeNode then
        self.loreEntryTree:SelectNode(control.treeNode)
        ZO_Scroll_ScrollControlIntoCentralView(self.loreEntryTreeScroll, control.treeNode.control)
    end
end
function ZO_AntiquityLore_Keyboard:ScrollLoreEntries(directionMagnitude)
    self.accumulatedScrollMagnitude = self.accumulatedScrollMagnitude + directionMagnitude
    local direction
    if self.accumulatedScrollMagnitude >= SCROLL_MAGNITUDE_THRESHOLD then
        direction = 1
        self.accumulatedScrollMagnitude = 0
    elseif self.accumulatedScrollMagnitude <= -SCROLL_MAGNITUDE_THRESHOLD then
        direction = -1
        self.accumulatedScrollMagnitude = 0
    end
    if direction then
        local currentNode = self.loreEntryTree:GetSelectedNode()
        if currentNode then
            local targetNode
            if direction > 0 then
                targetNode = currentNode:GetPreviousSiblingNode()
            else
                targetNode = currentNode:GetNextSiblingNode()
            end
            if targetNode then
                self.loreEntryTree:SelectNode(targetNode)
                ZO_Scroll_ScrollControlIntoCentralView(self.loreEntryTreeScroll, currentNode.control)
            end
        end
    end
end
-- Global XML --
function ZO_AntiquityLoreReader_OnMouseWheel(control, delta, ctrl, alt, shift)
    ANTIQUITY_LORE_KEYBOARD:ScrollLoreEntries(delta)
end
    ANTIQUITY_LORE_KEYBOARD:SelectTreeNodeControl(control)
end
    local textWidth, textHeight = control.text:GetTextDimensions()
    local height = textHeight + ZO_TREE_ENTRY_ICON_HEADER_TEXT_PADDING_Y * 2
    local subTextWidth, subTextHeight = control.subText:GetTextDimensions()
    height = height + subTextHeight
    height = zo_max(height, ZO_TREE_ENTRY_ICON_HEADER_ICON_MAX_DIMENSIONS)
    local width = zo_max(textWidth, subTextWidth) + ZO_TREE_ENTRY_ICON_HEADER_TEXT_OFFSET_X
    control:SetDimensions(width, height)
end
    control.SetSelected = function(control, open, enabled, disableScaling)
        ZO_IconHeader_Setup(control, open, enabled, disableScaling, ZO_AntiquityLore_IconHeader_UpdateSize)
        control.subText:SetSelected(open)
    end
    control.OnMouseEnter = function(...)
        ZO_IconHeader_OnMouseEnter(...)
        ZO_SelectableLabel_OnMouseEnter(control.subText)
    end
    control.OnMouseExit = function(...)
        ZO_IconHeader_OnMouseExit(...)
        ZO_SelectableLabel_OnMouseExit(control.subText)
    end
end
    ANTIQUITY_LORE_KEYBOARD = ZO_AntiquityLore_Keyboard:New(control)
end
    ANTIQUITY_LORE_READER_KEYBOARD = ZO_AntiquityLoreReader_Keyboard:New(control)
end