Back to Home

ESO Lua File v101041

ingame/crafting/keyboard/craftingresults_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
local CRAFTING_RESULTS_TEMPLATE = "ZO_CraftingResultItem"
ZO_CraftingResults_Keyboard = ZO_CraftingResults_Base:Subclass()
function ZO_CraftingResults_Keyboard:New(...)
    return ZO_CraftingResults_Base.New(self, ...)
end
local MAX_RUNE_ROWS = 3
local function RegisterEnchantDialog(dialogName, control)
    local function SetupEnchantDialog(dialog)
        local numTranslation = GetNumLastCraftingResultLearnedTranslations()
        for i = 1, numTranslation do
            local translationName, itemName, icon, _, _, _, _, displayQuality = GetLastCraftingResultLearnedTranslationInfo(i)
            local row = control:GetNamedChild("Row" .. i)
            row:SetHidden(false)
            row.icon:SetTexture(icon)
            row.itemName:SetText(zo_strformat(SI_TOOLTIP_ITEM_NAME, itemName))
            local r, g, b = GetInterfaceColor(INTERFACE_COLOR_TYPE_ITEM_QUALITY_COLORS, displayQuality)
            row.itemName:SetColor(r, g, b, 1)
            row.translatedName:SetText(translationName)
        end
        for i = numTranslation + 1, MAX_RUNE_ROWS do
            control:GetNamedChild("Row" .. i):SetHidden(true)
        end
    end
    ZO_Dialogs_RegisterCustomDialog(dialogName,
    {
        customControl = control,
        setup = SetupEnchantDialog,
        title =
        {
            text = SI_ENCHANTING_TRANSLATION_LEARNED_DIALOG_TITLE,
        },
        buttons =
        {
            {
                keybind = "DIALOG_NEGATIVE",
                control = control:GetNamedChild("Exit"),
                text = SI_EXIT_BUTTON,
            },
        }
    })
end
local MAX_ALCHEMY_ROWS = 3
local MAX_ITEM_TRAITS = 4
local function SetDefaultOptionValue(options, optionName, defaultValue)
    if options[optionName] == nil then
        options[optionName] = defaultValue
    end
end
-- The options parameter is a table that can contain any of the following values:
-- colorItemName: Whether to apply the quality color for the reagent name label (defaults to true)
-- centerAlignTraits: Whether to center-align traits relative to reagent label (defaults to true)
-- rowSpacing: The amount of spacing between reagent rows (defaults to 40)
    -- Set default values for options
    if not options then
        options = {}
    end
    SetDefaultOptionValue(options, "colorItemName", true)
    SetDefaultOptionValue(options, "centerAlignTraits", true)
    SetDefaultOptionValue(options, "rowSpacing", 40)
    local numLearnedTraits = GetNumLastCraftingResultLearnedTraits()
    local itemNameToRow = {}
    local currentRowIndex = 1
    for i = 1, numLearnedTraits do
        local traitName, itemName, icon, sellPrice, meetsUsageRequirement, equipType, itemStyle, displayQuality = GetLastCraftingResultLearnedTraitInfo(i)
        if not itemNameToRow[itemName] then
            local row = control:GetNamedChild("Row" .. currentRowIndex)
            currentRowIndex = currentRowIndex + 1
            itemNameToRow[itemName] = row
            row:SetHidden(false)
            row.icon:SetTexture(icon)
            row.itemName:SetText(zo_strformat(SI_TOOLTIP_ITEM_NAME, itemName))
            if options.colorItemName then
                local r, g, b = GetInterfaceColor(INTERFACE_COLOR_TYPE_ITEM_QUALITY_COLORS, displayQuality)
                row.itemName:SetColor(r, g, b, 1)
            end
            row.containerHeight = 0
            for traitIndex = 2, MAX_ITEM_TRAITS do
                row.traits[traitIndex]:SetHidden(true)
            end
            row.currentTraitIndex = 1
        end
        local row = itemNameToRow[itemName]
        local currentTraitIndex = row.currentTraitIndex
        row.traits[currentTraitIndex]:SetText(zo_strformat(SI_ALCHEMY_REAGENT_TRAIT_FORMATTER, traitName))
        row.traits[currentTraitIndex]:SetHidden(false)
        row.currentTraitIndex = currentTraitIndex + 1
        row.containerHeight = row.containerHeight + select(2, row.traits[currentTraitIndex]:GetTextDimensions())
    end
    for i = currentRowIndex, MAX_ALCHEMY_ROWS do
        control:GetNamedChild("Row" .. i):SetHidden(true)
    end
    local prevRow
    for i = 1, currentRowIndex - 1 do
        local row = control:GetNamedChild("Row" .. i)
        row.traitContainer:SetHeight(row.containerHeight)
        local currentOffset = zo_max(row.containerHeight - row:GetHeight(), 0) * .5 + 10
        if i == 1 then
            if options.centerAlignTraits then
                row:SetAnchor(TOP, control:GetNamedChild("Description"), BOTTOM, 0, currentOffset)
            else
                currentOffset = 60
                row:SetAnchor(TOPLEFT, control:GetNamedChild("Description"), BOTTOMLEFT, 0, currentOffset)
            end
        else
            local totalOffset
            if options.centerAlignTraits then
                local prevOffset = zo_max(prevRow.containerHeight - prevRow:GetHeight(), 0) * .5 + 10
                totalOffset = prevOffset + currentOffset
            else
                totalOffset = prevRow.containerHeight + options.rowSpacing
            end
            row:SetAnchor(TOP, prevRow, BOTTOM, 0, totalOffset)
        end
        prevRow = row
    end
    if not options.centerAlignTraits then
        control:GetNamedChild("Exit"):SetAnchor(TOPLEFT, prevRow, BOTTOMLEFT, 0, prevRow.containerHeight + options.rowSpacing)
    end
end
local function RegisterAlchemyDialog(dialogName, control, options)
    local function SetupAlchemyDialog(dialog)
    end
    ZO_Dialogs_RegisterCustomDialog(dialogName,
    {
        customControl = control,
        setup = SetupAlchemyDialog,
        title =
        {
            text = SI_ALCHEMY_REAGENT_TRAIT_LEARNED_DIALOG_TITLE,
        },
        buttons =
        {
            {
                keybind = "DIALOG_NEGATIVE",
                control = control:GetNamedChild("Exit"),
                text = SI_EXIT_BUTTON,
            },
        }
    })
end
function ZO_CraftingResults_Keyboard:Initialize(control)
    local DONT_SHOW_IN_GAMEPAD_PREFERRED_MODE = false
    ZO_CraftingResults_Base.Initialize(self, control, DONT_SHOW_IN_GAMEPAD_PREFERRED_MODE)
    RegisterEnchantDialog("ENCHANT_TRANSLATION_LEARNED", ZO_CraftingResultEnchantDialog)
    local options = {
        colorItemName = true,
        centerAlignTraits = true,
        rowSpacing = 40,
    }
    RegisterAlchemyDialog("ALCHEMY_TRAITS_LEARNED", ZO_CraftingResultAlchemyDialog, options)
end
function ZO_CraftingResults_Keyboard:ModifyAnchor(newAnchor)
    ZO_CraftingResults_Base.ModifyAnchor(self, ZO_CraftingResultsTopLevel, newAnchor)
end
function ZO_CraftingResults_Keyboard:RestoreAnchor()
    ZO_CraftingResults_Base.RestoreAnchor(self, ZO_CraftingResultsTopLevel)
end
do
    local function SetupItem(control, data)
        if data.color then
            control:SetColor(data.color:UnpackRGB())
        else
            control:SetColor(ZO_SELECTED_TEXT:UnpackRGB())
        end
        control:SetText(data.text)
        local inventorySlot = GetControl(control, "Button")
        ZO_Inventory_SetupSlot(inventorySlot, data.stack, data.icon, data.meetsUsageRequirement)
    end
    local function EntryEqualitySetup(fadingControlBuffer, oldEntry, newEntry)
        local oldData = oldEntry.lines[1]
        local newData = newEntry.lines[1]
        oldData.stack = oldData.stack + newData.stack
        local inventorySlot = ZO_FadingControlBuffer_GetLineControl(oldData):GetNamedChild("Button")
        ZO_Inventory_SetupSlot(inventorySlot, oldData.stack, newData.icon, newData.meetsUsageRequirement)
        ZO_CraftingResults_Base_PlayPulse(inventorySlot)
    end
    function ZO_CraftingResults_Keyboard:AreCraftingResultLinesEqual(oldLines, newLines)
        return self:AreCraftingResultsEqual(oldLines[1], newLines[1])
    end
    function ZO_CraftingResults_Keyboard:InitializeResultBuffer()
        local anchor = ZO_Anchor:New(TOP)
        self.notifier = ZO_FadingControlBuffer:New(self.control, 5, nil, nil, "CraftingResultFade", "CraftingResultTranslate", anchor)
        self.notifier:SetTranslateDuration(500)
        self.notifier:SetHoldTimes(5000)
        self.notifier:SetFadesInImmediately(true)
        self.notifier:SetAdditionalVerticalSpacing(5)
        self.notifier:SetDisplayOlderEntriesFirst(true)
        local templateData =
        {
            setup = SetupItem,
            equalityCheck = function(...) return self:AreCraftingResultLinesEqual(...) end,
            equalitySetup = EntryEqualitySetup,
        }
        self.notifier:AddTemplate(CRAFTING_RESULTS_TEMPLATE, templateData)
        local function OnLastControlReleased()
            if self.isCurrentlyRegisteredWithCSAs then
                --Tell CSA to release handle
                self.isCurrentlyRegisteredWithCSAs = false
                CENTER_SCREEN_ANNOUNCE:ReleaseExternalHandle()
            end
        end
        self.notifier:RegisterCallback("LastControlReleased", OnLastControlReleased)
    end
end
function ZO_CraftingResults_Keyboard:IsActive()
    return not IsInGamepadPreferredMode()
end
function ZO_CraftingResults_Keyboard:ResumeCraftingResults()
    if self.isCurrentlyRegisteredWithCSAs then
        self.notifier:SetHoldDisplayingEntries(false)
        return true
    else
        return false
    end
end
function ZO_CraftingResults_Keyboard:DisplayCraftingResult(resultData)
    if not self.isCurrentlyRegisteredWithCSAs then
        self.isCurrentlyRegisteredWithCSAs = true
        self.notifier:SetHoldDisplayingEntries(true)
        --Hook the crafting results into the CSA system
        local messageParams = CENTER_SCREEN_ANNOUNCE:CreateMessageParams(CSA_CATEGORY_EXTERNAL_HANDLE)
        messageParams:SetCSAType(CENTER_SCREEN_ANNOUNCE_TYPE_CRAFTING_RESULTS)
        messageParams:SetExternalHandleCallback(function() return self:ResumeCraftingResults() end)
        CENTER_SCREEN_ANNOUNCE:AddMessageWithParams(messageParams)
    end
    local resultEntryData = ZO_EntryData:New(resultData)
    -- Alias for backwards compatibility:
    resultEntryData.entryColor = resultData.color
    if ZO_RETRAIT_STATION_MANAGER:IsRetraitFragmentShowing() then
        local itemTrait = GetItemLinkTraitInfo(resultData.itemLink)
        local itemTraitString = GetString("SI_ITEMTRAITTYPE", itemTrait)
        local itemNameString = resultData.color:Colorize(resultData.name)
        resultEntryData.text = zo_strformat(SI_KEYBOARD_RETRAIT_COMPLETED_RESULT_FORMATTER, itemNameString, itemTraitString)
    else
        resultEntryData.text = zo_strformat(SI_TOOLTIP_ITEM_NAME, resultData.name)
    end
    local displayData =
    {
        lines =
        {
            resultEntryData
        }
    }
    self.notifier:AddEntry(CRAFTING_RESULTS_TEMPLATE, displayData)
end
function ZO_CraftingResults_Keyboard:ClearAll()
    self.notifier:ClearAll()
end
function ZO_CraftingResults_Keyboard:HasEntries()
    return self.notifier:HasEntries()
end
function ZO_CraftingResults_Keyboard:FadeAll()
    self.notifier:FadeAll()
end
function ZO_CraftingResults_Keyboard:DisplayDiscoveredTraits()
    ZO_Dialogs_ShowDialog("ALCHEMY_TRAITS_LEARNED")
end
function ZO_CraftingResults_Keyboard:DisplayTranslatedRunes()
    ZO_Dialogs_ShowDialog("ENCHANT_TRANSLATION_LEARNED")
end
function ZO_CraftingResults_Keyboard:ShouldDisplayMessages()
    return true
end
function ZO_CraftingResults_Keyboard:OnHidden()
    self:ClearAll()
    --If the crafting results were already registered with the CSA system, we need to make sure we properly release everything
    if self.isCurrentlyRegisteredWithCSAs then
        self.notifier:SetHoldDisplayingEntries(false)
        self.isCurrentlyRegisteredWithCSAs = false
        CENTER_SCREEN_ANNOUNCE:ReleaseExternalHandle()
    end
end
    CRAFTING_RESULTS = ZO_CraftingResults_Keyboard:New(control)
    SYSTEMS:RegisterKeyboardObject("craftingResults", CRAFTING_RESULTS)
    CRAFTING_RESULTS_FRAGMENT = ZO_FadeSceneFragment:New(control)
    CRAFTING_RESULTS_FRAGMENT:RegisterCallback("StateChange", function(oldState, newState)
        if newState == SCENE_HIDDEN then
            CRAFTING_RESULTS:OnHidden()
        end
    end)
end