Back to Home

ESO Lua File v100022

ingame/crafting/craftingutils.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
--[[ Crafting Slot Base ]]--
ZO_CraftingSlotBase = ZO_Object:Subclass()
function ZO_CraftingSlotBase:New(...)
    local craftingSlot = ZO_Object.New(self)
    craftingSlot:Initialize(...)
    return craftingSlot
end
function ZO_CraftingSlotBase:Initialize(owner, control, slotType, emptyTexture, craftingInventory, emptySlotIcon)
    self.owner = owner
    self.control = control
    self.slotType = slotType
    self.emptyTexture = emptyTexture
    self.craftingInventory = craftingInventory
    self.emptySlotIcon = emptySlotIcon
    if self.emptySlotIcon then
        self.control:GetNamedChild("EmptySlotIcon"):SetTexture(self.emptySlotIcon)
        self:ShowEmptySlotIcon(true)
    end
    self.iconBg = self.control:GetNamedChild("IconBg")
    self.dropCallout = self.control:GetNamedChild("DropCallout")
    self:SetItem(nil)
end
function ZO_CraftingSlotBase:ShowDropCallout(isCorrectType)
    self.dropCallout:SetHidden(false)
    if isCorrectType then
        self.dropCallout:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA())
    else
        self.dropCallout:SetColor(ZO_ERROR_COLOR:UnpackRGBA())
    end
end
function ZO_CraftingSlotBase:HideDropCallout()
    self.dropCallout:SetHidden(true)
end
function ZO_CraftingSlotBase:OnPassedValidation()
    -- intended to be overidden
end
function ZO_CraftingSlotBase:OnFailedValidation()
    -- intended to be overidden
end
-- Use to validate a list of virtually stacked items created from EnumerateInventorySlotsAndAddToScrollData
function ZO_CraftingSlotBase:ValidateItemId(validItemIds)
    if self.bagId and self.slotIndex then
        -- An item might have been used up in a physical stack
        if validItemIds[self.itemInstanceId] then
            -- An item still exists in a physical stack, but might not exist in the virtual stack any more, update the indices
            local itemInfo = validItemIds[self.itemInstanceId]
            if self:IsBagAndSlot(itemInfo.bag, itemInfo.index) then
                self:SetupItem(itemInfo.bag, itemInfo.index)
            else
                self:SetItem(itemInfo.bag, itemInfo.index)
            end
            self:OnPassedValidation()
            return true
        else
            -- Item doesn't exist in a physical stack
            self:SetItem(nil)
            self:OnFailedValidation()
            return false
        end
    end
    return true
end
-- Use to validate a list of slotDatas created from GetIndividualInventorySlotsAndAddToScrollData
function ZO_CraftingSlotBase:ValidateSlottedItem(validItems)
    if self.bagId and self.slotIndex then
        for i, validItem in ipairs(validItems) do
            if self:IsBagAndSlot(validItem.bagId, validItem.slotIndex) then
                self:SetupItem(self.bagId, self.slotIndex)
                self:OnPassedValidation()
                return
            end
        end
        self:SetItem(nil)
        self:OnFailedValidation()
    end
end
function ZO_CraftingSlotBase:SetItem(bagId, slotIndex)
    -- can be overriden for custom functionality, but should still call base or SetupItem
    self:SetupItem(bagId, slotIndex)
end
function ZO_CraftingSlotBase:SetupItem(bagId, slotIndex)
    self.bagId = bagId
    self.slotIndex = slotIndex
    self.itemInstanceId = GetItemInstanceId(self.bagId, self.slotIndex)
    if bagId and slotIndex then
        local icon = GetItemInfo(bagId, slotIndex)
        local stack = self:GetStackCount()
        if self:HasAnimationRefs() then
            ZO_ItemSlot_SetupSlotBase(self.control, stack, icon)
        else
            ZO_ItemSlot_SetupSlot(self.control, stack, icon)
        end
        self:ShowEmptySlotIcon(false)
    else
         if self:HasAnimationRefs() then
            ZO_ItemSlot_SetupSlotBase(self.control, 0, self.emptyTexture)
        else
            ZO_ItemSlot_SetupSlot(self.control, 0, self.emptyTexture)
        end
        self:ShowEmptySlotIcon(true)
    end
    if self.iconBg then
        self.iconBg:SetHidden(self:HasItem())
    end
    ZO_Inventory_BindSlot(self.control, self.slotType, self.slotIndex, self.bagId)
    self:UpdateTooltip()
end
function ZO_CraftingSlotBase:AddAnimationRef()
    self.animationRefs = (self.animationRefs or 0) + 1
end
function ZO_CraftingSlotBase:RemoveAnimationRef()
    self.animationRefs = self.animationRefs - 1
    if self.animationRefs == 0 then
        self:SetItem(self.bagId, self.slotIndex)
    end
end
function ZO_CraftingSlotBase:HasAnimationRefs()
    return self.animationRefs ~= nil and self.animationRefs > 0
end
function ZO_CraftingSlotBase:GetStackCount()
    if self:HasItem() then
        if self.craftingInventory then
            return self.craftingInventory:GetStackCount(self:GetBagAndSlot())
        end
        return 1
    end
    return 0
end
function ZO_CraftingSlotBase:GetBagAndSlot()
    return self.bagId, self.slotIndex
end
function ZO_CraftingSlotBase:IsBagAndSlot(bagId, slotIndex)
    return self.bagId == bagId and self.slotIndex == slotIndex
end
function ZO_CraftingSlotBase:HasItem()
    return self.bagId ~= nil and self.slotIndex ~= nil
end
function ZO_CraftingSlotBase:IsItemId(itemId)
    if self.bagId and self.slotIndex then
        return self.itemInstanceId == itemId
    end
    return false
end
function ZO_CraftingSlotBase:GetItemId()
    if self:HasItem() then
        return self.itemInstanceId
    end
end
function ZO_CraftingSlotBase:IsSlotControl(slotControl)
    return self.control == slotControl
end
function ZO_CraftingSlotBase:GetControl()
    return self.control
end
function ZO_CraftingSlotBase:UpdateTooltip()
    if self.control == WINDOW_MANAGER:GetMouseOverControl() then
    end
end
function ZO_CraftingSlotBase:SetEmptyTexture(emptyTexture)
    self.emptyTexture = emptyTexture
    if not self:HasItem() then
        self:SetItem(self.bagId, self.slotIndex)
    end
end
function ZO_CraftingSlotBase:SetHidden(hidden)
    self.control:SetHidden(hidden)
end
function ZO_CraftingSlotBase:ShowEmptySlotIcon(showIcon)
    if self.emptySlotIcon then
        self.control:GetNamedChild("EmptySlotIcon"):SetHidden(not showIcon)
        self.control:GetNamedChild("Icon"):SetHidden(showIcon)
    end
end
    self.animation = ANIMATION_MANAGER:CreateTimelineFromVirtual("CraftingGlowAlphaAnimation", self:GetNamedChild("Glow"))
    local icon = self:GetNamedChild("Icon")
    icon:ClearAnchors()
    icon:SetAnchor(CENTER, self, CENTER)
end
--[[ Crafting Slot Animation Base ]]--
ZO_CraftingSlotAnimationBase = ZO_Object:Subclass()
function ZO_CraftingSlotAnimationBase:New(...)
    local craftingSlotAnimationBase = ZO_Object.New(self)
    craftingSlotAnimationBase:Initialize(...)
    return craftingSlotAnimationBase
end
function ZO_CraftingSlotAnimationBase:Initialize(sceneName, visibilityPredicate)
    self.slots = {}
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStarted", function()
        if SCENE_MANAGER:IsShowing(sceneName) and (not visibilityPredicate or visibilityPredicate()) then
            self:Play(sceneName)
        end
    end)
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStopped", function() self:Stop() end)
end
function ZO_CraftingSlotAnimationBase:AddSlot(slot)
    self.slots[#self.slots + 1] = slot
end
function ZO_CraftingSlotAnimationBase:Clear()
    if #self.slots > 0 then
        self.slots = {}
    end
end
function ZO_CraftingSlotAnimationBase:Play(sceneName)
    -- intended to be overridden
end
function ZO_CraftingSlotAnimationBase:Stop(sceneName)
    -- intended to be overridden
end
--[[ Global Utils ]]--
    if cost > 0 then
        if GetCurrencyAmount(CURT_MONEY, CURRENCY_LOCATION_CHARACTER) >= cost then
            return zo_strformat(SI_CRAFTING_PERFORM_CRAFT, ZO_Currency_FormatKeyboard(CURT_MONEY, cost, ZO_CURRENCY_FORMAT_AMOUNT_ICON))
        end
        return zo_strformat(SI_CRAFTING_PERFORM_CRAFT, ZO_Currency_FormatKeyboard(CURT_MONEY, cost, ZO_CURRENCY_FORMAT_ERROR_AMOUNT_ICON))
    end
    return GetString(SI_CRAFTING_PERFORM_FREE_CRAFT)
end
    local function OnCraftStarted()
        if not menuBar:IsHidden() then
            ZO_MenuBar_SetAllButtonsEnabled(menuBar, false)
        end
    end
    local function OnCraftCompleted()
        ZO_MenuBar_SetAllButtonsEnabled(menuBar, true)
    end
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStarted", OnCraftStarted)
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStopped", OnCraftCompleted)
end
    local function UpdateKeyBindDescriptorGroup()
        KEYBIND_STRIP:UpdateKeybindButtonGroup(keybindStripDescriptor)
    end
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStarted", UpdateKeyBindDescriptorGroup)
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStopped", UpdateKeyBindDescriptorGroup)
end
local function ConnectStandardObjectToCraftingProcess(object)
    local function OnCraftStarted()
        if not object:GetControl():IsHidden() then
            object:SetEnabled(false)
        end
    end
    local function OnCraftCompleted()
        object:SetEnabled(true)
    end
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStarted", OnCraftStarted)
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStopped", OnCraftCompleted)
end
    ConnectStandardObjectToCraftingProcess(horizontalScrollList)
end
    local function OnCraftStarted()
        if not checkBox:IsHidden() then
            ZO_CheckButton_SetEnableState(checkBox, false)
        end
    end
    local function OnCraftCompleted()
        ZO_CheckButton_SetEnableState(checkBox, true)
    end
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStarted", OnCraftStarted)
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStopped", OnCraftCompleted)
end
end
end
    local traitCategory = GetItemTraitTypeCategory(traitType)
    return traitCategory == ITEM_TRAIT_TYPE_CATEGORY_WEAPON
end
    local traitCategory = GetItemTraitTypeCategory(traitType)
    return traitCategory == ITEM_TRAIT_TYPE_CATEGORY_ARMOR
end
do
    local g_isCrafting = false
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStarted", function()
        g_isCrafting = true
    end)
    CALLBACK_MANAGER:RegisterCallback("CraftingAnimationsStopped", function()
        g_isCrafting = false
    end)
        return g_isCrafting
    end
end
    return SCENE_MANAGER:IsShowing("smithing")
            or SYSTEMS:IsShowing("alchemy")
            or SCENE_MANAGER:IsShowing("enchanting")
            or ZO_Provisioner_IsSceneShowing()
end
--[[ Gamepad Crafting Ingredient Bar ]]--
ZO_GamepadCraftingIngredientBar = ZO_Object:Subclass()
function ZO_GamepadCraftingIngredientBar:New(...)
    local ingredientBar = ZO_Object.New(self)
    ingredientBar:Initialize(...)
    return ingredientBar
end
function ZO_GamepadCraftingIngredientBar:Initialize(control, slotSpacing)
    self.control = control
    self.slotSpacing = slotSpacing
    self.slotCenterControl = self.control:GetNamedChild("SlotCenter")
    self.dataTypes = {}
    self:Clear()
end
function ZO_GamepadCraftingIngredientBar:Clear()
    self.dataList = {}
    if self.dataTypes then
        for key, dataTypeInfo in pairs(self.dataTypes) do
            dataTypeInfo.pool:ReleaseAllObjects()
        end
    end
end
function ZO_GamepadCraftingIngredientBar:AddDataTemplate(templateName, setupFunction)
    if not self.dataTypes[templateName] then
        local dataTypeInfo = {
            pool = ZO_ControlPool:New(templateName, self.slotCenterControl),
            setupFunction = setupFunction,
        }
        self.dataTypes[templateName] = dataTypeInfo
    end
end
function ZO_GamepadCraftingIngredientBar:AddEntry(templateName, data)
    local dataTypeInfo = self.dataTypes[templateName]
    if dataTypeInfo then
        self.dataList[#self.dataList + 1] = data
        
        local control, key = dataTypeInfo.pool:AcquireObject()
        control.key = key
        control.templateName = templateName
        data.control = control
        dataTypeInfo.setupFunction(control, data)
    end
end
function ZO_GamepadCraftingIngredientBar:Commit()
    -- alter x offsets based on number of ingredients (so the slots stay centered relative to parent)
    local numIngredients = #self.dataList  
    local offsetX = (numIngredients - 1) * -self.slotSpacing * 0.5
    for i, data in ipairs(self.dataList) do
        -- adjust the x offset
        data.control:SetAnchor(CENTER, self.slotCenterControl, CENTER, offsetX, 0)
        offsetX = offsetX + self.slotSpacing
    end
end
ZO_CRAFTING_TOOLTIP_STYLES = ZO_DeepTableCopy(ZO_TOOLTIP_STYLES)
for key,value in pairs(ZO_CRAFTING_TOOLTIP_STYLES) do
    value["horizontalAlignment"] = TEXT_ALIGN_CENTER
    if key ~= "topSection" then
        value["layoutPrimaryDirectionCentered"] = true
    end
end