ESO Lua File v100010

ingame/stable/stable.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
ZO_StableManager = ZO_Object:Subclass()
function ZO_StableManager:New(...)
    local stables = ZO_Object.New(self)
    stables:Initialize(...)
    return stables
end
function ZO_StableManager:Initialize(control)
    self.control = control
    self:InitializeTabs()
end
function ZO_StableManager:InitializeEvents()
    self.control:RegisterForEvent(EVENT_STABLE_INTERACT_START, function(eventCode, ...) self:OnStablesInteractStart(...) end)
    self.control:RegisterForEvent(EVENT_STABLE_INTERACT_END, function(eventCode, ...) self:OnStablesInteractEnd(...) end)
    self.control:RegisterForEvent(EVENT_MOUNTS_FULL_UPDATE, function(eventCode, ...) self:OnFullMountUpdate(...) end)
    self.control:RegisterForEvent(EVENT_MOUNT_UPDATE, function(eventCode, ...) self:OnMountUpdate(...) end)
    self.control:RegisterForEvent(EVENT_MONEY_UPDATE, function(eventCode, ...) self:OnMoneyUpdated(...) end)
    local STABLES_INTERACTION =
    {
        type = "Stable",
        End =   function()
                    if not IsInGamepadPreferredMode() then
                        self:SetHidden(true)
                    end
                end,
        interactTypes = { INTERACTION_STABLE },
    }
    STABLES_SCENE = ZO_InteractScene:New("stables", SCENE_MANAGER, STABLES_INTERACTION)
    local function OnStateChanged(oldState, newState)
        if newState == SCENE_SHOWING then
            STORE_WINDOW:InitializeStoreInteraction(STORE_WINDOW_MODE_MOUNTS)
            if ZO_Stables_HasAnyMounts() then
                self.modeBar:SelectFragment(SI_STABLE_STABLES_TAB)
            else
                self.modeBar:SelectFragment(SI_STORE_MODE_BUY)
            end
        elseif newState == SCENE_HIDDEN then                         
            self.modeBar:Clear()
            if GetCursorContentType() == MOUSE_CONTENT_STORE_ITEM then 
                ClearCursor()
            end
        end
    end
    STABLES_SCENE:RegisterCallback("StateChange", OnStateChanged)
end
function ZO_StableManager:InitializeNameEditControl()
    self.mountName = ZO_SavingEditBox:New(self.control:GetNamedChild("Name"))
    self.mountName:SetPutTextInQuotes(false)
    local function OnMouseNamedValidation(text)
        local violations = { IsValidMountName(text) }
        local noViolations = #violations == 0
        if noViolations then
            self.nameInstructions:Hide()
        else
            self.nameInstructions:Show(self.mountName:GetEditControl(), violations)
        end
        return noViolations
    end
    self.mountName:RegisterCallback("Save", function(text) RenameMount(ACTIVE_MOUNT_INDEX, text) end)
    self.mountName:RegisterCallback("SetEditing", function() self.nameInstructions:Hide() end)
    SetupEditControlForNameValidation(self.mountName:GetEditControl(), MOUNT_NAME_MAX_LENGTH)
end
function ZO_StableManager:InitializeKeybindStripDescriptors()
    self.keybindStripDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_CENTER,
        -- Buy Stable Space
        {
            name = function()
                local cost = GetNextStableSlotCost()
                if GetCurrentMoney() >= cost then 
                    return zo_strformat(SI_STABLE_UPGRADE_TEXT, ZO_CurrencyControl_FormatCurrency(cost))
                end
                return zo_strformat(SI_STABLE_UPGRADE_TEXT, ZO_ERROR_COLOR:Colorize(ZO_CurrencyControl_FormatCurrency(cost)))
            end,
            keybind = "UI_SHORTCUT_TERTIARY",
            visible = CanBuyStableSlotUpgrade,
            callback = function()
                ZO_Dialogs_ShowDialog("BUY_STABLE_SPACE", {cost = GetNextStableSlotCost()})
            end,
        },
        -- Set Mount Active
        {
            alignment = KEYBIND_STRIP_ALIGN_RIGHT,
            name = GetString(SI_STABLE_SET_ACTIVE),
            keybind = "UI_SHORTCUT_PRIMARY",
            visible = function() 
                local row = self.selectionLockedToRow or self.mousedOverRow
                return row ~= nil and not row.empty
            end,
            callback = function()
                local row = self.selectionLockedToRow or self.mousedOverRow
                SetActiveMount(row.stableIndex)
            end,
        },
    }
end
function ZO_StableManager:InitializeControls()
    STABLES_FRAGMENT = ZO_FadeSceneFragment:New(self.control)
    self.portrait = self.control:GetNamedChild("Portrait")
    self.nameInstructions = ZO_ValidNameInstructions:New(self.control:GetNamedChild("NameInstructions"))
    self.noActiveMount = self.control:GetNamedChild("NoActiveMount")
    self.levelAndBreed = self.control:GetNamedChild("LevelAndBreed")
    self.description = self.control:GetNamedChild("Description")
    self.speedRow = self.control:GetNamedChild("SpeedRow")
    self.speedRow.name:SetText(GetString(SI_MOUNT_ATTRIBUTE_SPEED))
    ZO_StatusBar_SetGradientColor(self.speedRow.bar, ZO_XP_BAR_GRADIENT_COLORS)
    self.staminaRow = self.control:GetNamedChild("StaminaRow")
    self.staminaRow.name:SetText(GetString(SI_MOUNT_ATTRIBUTE_STAMINA))
    ZO_StatusBar_SetGradientColor(self.staminaRow.bar, ZO_XP_BAR_GRADIENT_COLORS)
    self.carryRow = self.control:GetNamedChild("CarryRow")
    self.carryRow.name:SetText(GetString(SI_MOUNT_ATTRIBUTE_CARRY))
    ZO_StatusBar_SetGradientColor(self.carryRow.bar, ZO_XP_BAR_GRADIENT_COLORS)
    self.speedFeedButton = self.control:GetNamedChild("SpeedFeedButton")
    self.speedFeedButton.owner = self
    self.staminaFeedButton = self.control:GetNamedChild("StaminaFeedButton")
    self.staminaFeedButton.owner = self
    self.carryFeedButton = self.control:GetNamedChild("CarryFeedButton")
    self.carryFeedButton.owner = self
    self.timerControl = self.control:GetNamedChild("Timer")
    self.timerOverlayControl = self.timerControl:GetNamedChild("Overlay")
    self.timerText = self.timerControl:GetNamedChild("Text")
    self.freeSlots = self.control:GetNamedChild("InfoBarFreeSlots")
    self.money = self.control:GetNamedChild("InfoBarMoney")
    local function OnTimerUpdate()
        local timeUntilCanBeFed = GetTimeUntilMountCanBeFed(ACTIVE_MOUNT_INDEX)
        local canBeFed = ZO_Stables_CanMountBeFed(timeUntilCanBeFed)
        if canBeFed then
            self:UpdateActiveMount()
        else
            local timeLeft = ZO_FormatTimeMilliseconds(timeUntilCanBeFed, TIME_FORMAT_STYLE_COLONS, TIME_FORMAT_PRECISION_TWELVE_HOUR)
            self.timerText:SetText(timeLeft)
            if self.timerControl.mouseInside then
                InformationTooltip:ClearLines()
                SetTooltipText(InformationTooltip, zo_strformat(SI_STABLE_MOUNT_NOT_HUNGRY, timeLeft))
            end
        end
    end
    self.timerControl:SetHandler("OnUpdate", OnTimerUpdate)
    do
        local function OnUpdate(row)
            if not row.empty then
                local timeUntilCanBeFed = GetTimeUntilMountCanBeFed(row.stableIndex)
                local canBeFed = ZO_Stables_CanMountBeFed(timeUntilCanBeFed)
                if canBeFed then
                    row.readyForFeed:SetHidden(timeUntilCanBeFed == nil)
                    row.timer:SetHidden(true)
                else
                    row.readyForFeed:SetHidden(true)
                    
                    if timeUntilCanBeFed == nil then
                        row.timer:SetHidden(true)
                    else
                        row.timer:SetHidden(false)
                        if row.timer.mouseInside then
                            local timeLeft = ZO_FormatTimeMilliseconds(timeUntilCanBeFed, TIME_FORMAT_STYLE_COLONS, TIME_FORMAT_PRECISION_TWELVE_HOUR)
                            InformationTooltip:ClearLines()
                            SetTooltipText(InformationTooltip, zo_strformat(SI_STABLE_MOUNT_NOT_HUNGRY, timeLeft))
                        end
                    end
                end
            else
                row.readyForFeed:SetHidden(true)
                row.timer:SetHidden(true)
            end
        end
        local stabledMounts = self.control:GetNamedChild("StabledMounts")
        local stabledMountsScrollChild = stabledMounts:GetNamedChild("Scroll"):GetNamedChild("Child")
        self.stableSlotRowPool = ZO_ControlPool:New("ZO_StableSlotRow", stabledMountsScrollChild)
        self.stableSlotRowPool:SetCustomFactoryBehavior(function(control)
                                                            control.owner = self
                                                            control:SetHandler("OnUpdate", OnUpdate)
                                                        end)
    end
end
local FEED_CURRENCY_OPTIONS = { showTooltips = false, iconSide = RIGHT, font = "ZoFontGameLargeBold" }
local NO_LEADING_EDGE = false
local FORCE_VALUE = true
function ZO_StableManager:UpdateActiveMount()
    local hasActiveMount = not IsStableSlotEmpty(ACTIVE_MOUNT_INDEX)
    self.noActiveMount:SetHidden(hasActiveMount)
    self.mountName:GetControl():SetHidden(not hasActiveMount)
    self.levelAndBreed:SetHidden(not hasActiveMount)
    if hasActiveMount then
        local name, icon, portrait, breed, description = GetStableSlotInfo(ACTIVE_MOUNT_INDEX)
        local level, inventoryBonus, maxInventoryBonus, staminaBonus, maxStaminaBonus, speedBonus, maxSpeedBonus = GetStableSlotMountStats(ACTIVE_MOUNT_INDEX)
        self.mountName:SetText(name)
        self.levelAndBreed:SetText(zo_strformat(SI_STABLE_ACTIVE_MOUNT_DESCRIPTION, level, breed))
        self.portrait:SetTexture(portrait)
        self.speedRow.value:SetText(zo_strformat(SI_MOUNT_ATTRIBUTE_SPEED_FORMAT, speedBonus))
        ZO_StatusBar_SmoothTransition(self.speedRow.bar, speedBonus, maxSpeedBonus, FORCE_VALUE)
        self.staminaRow.value:SetText(staminaBonus)
        ZO_StatusBar_SmoothTransition(self.staminaRow.bar, staminaBonus, maxStaminaBonus, FORCE_VALUE)
        self.carryRow.value:SetText(inventoryBonus)
        ZO_StatusBar_SmoothTransition(self.carryRow.bar, inventoryBonus, maxInventoryBonus, FORCE_VALUE)
        local timeUntilCanBeFed, totalFeedWaitDuration = GetTimeUntilMountCanBeFed(ACTIVE_MOUNT_INDEX)
        local canBeFed = ZO_Stables_CanMountBeFed(timeUntilCanBeFed)
        ZO_Stables_SetupFeedButton(self.speedFeedButton, canBeFed, FEED_CURRENCY_OPTIONS)
        ZO_Stables_SetupFeedButton(self.staminaFeedButton, canBeFed, FEED_CURRENCY_OPTIONS)
        ZO_Stables_SetupFeedButton(self.carryFeedButton, canBeFed, FEED_CURRENCY_OPTIONS)
        if timeUntilCanBeFed and timeUntilCanBeFed > 0 then
            self.timerControl:SetHidden(false)
            self.timerOverlayControl:StartCooldown(timeUntilCanBeFed, totalFeedWaitDuration, CD_TYPE_RADIAL, CD_TIME_TYPE_TIME_UNTIL, NO_LEADING_EDGE)
        else
            self.timerControl:SetHidden(true)
        end
    else
        self.levelAndBreed:SetText("")
        self.portrait:SetTexture("EsoUI/Art/Mounts/mountPortait_empty.dds")
        self.description:SetText(GetString(SI_MOUNT_NONE_ACTIVE_HELP))
        self.timerControl:SetHidden(true)
        self.speedRow.value:SetText("")
        ZO_StatusBar_SmoothTransition(self.speedRow.bar, 0, 1, FORCE_VALUE)
        self.staminaRow.value:SetText("")
        ZO_StatusBar_SmoothTransition(self.staminaRow.bar, 0, 1, FORCE_VALUE)
        self.carryRow.value:SetText("")
        ZO_StatusBar_SmoothTransition(self.carryRow.bar, 0, 1, FORCE_VALUE)
        local CANNOT_FEED = false
        ZO_Stables_SetupFeedButton(self.speedFeedButton, CANNOT_FEED, FEED_CURRENCY_OPTIONS)
        ZO_Stables_SetupFeedButton(self.staminaFeedButton, CANNOT_FEED, FEED_CURRENCY_OPTIONS)
        ZO_Stables_SetupFeedButton(self.carryFeedButton, CANNOT_FEED, FEED_CURRENCY_OPTIONS)
    end
end
function ZO_StableManager:InitializeTabs()
    local function CreateButtonData(normal, pressed, highlight, clickSound, tutorialTrigger)
        return {
            normal = normal,
            pressed = pressed,
            highlight = highlight,
            clickSound = clickSound,
            callback = function()
                TriggerTutorial(tutorialTrigger)
            end
        }
    end
    
    self.modeBar = ZO_SceneFragmentBar:New(ZO_StableWindowMenuBar)
    --Buy Button
    local buyButtonData = CreateButtonData("EsoUI/Art/Vendor/vendor_tabIcon_buy_up.dds",
                                            "EsoUI/Art/Vendor/vendor_tabIcon_buy_down.dds",
                                            "EsoUI/Art/Vendor/vendor_tabIcon_buy_over.dds",
                                            SOUNDS.STABLE_WINDOW_BUY_CLICKED,
                                            TUTORIAL_TRIGGER_STORE_OPENED)
    self.modeBar:Add(SI_STORE_MODE_BUY, { STORE_FRAGMENT }, buyButtonData)
    --Sell Button
    local sellButtonData = CreateButtonData("EsoUI/Art/Vendor/vendor_tabIcon_sell_up.dds",
                                            "EsoUI/Art/Vendor/vendor_tabIcon_sell_down.dds",
                                            "EsoUI/Art/Vendor/vendor_tabIcon_sell_over.dds",
                                            SOUNDS.STABLE_WINDOW_SELL_CLICKED,
                                            TUTORIAL_TRIGGER_STORE_OPENED)
    self.modeBar:Add(SI_STORE_MODE_SELL, { SELL_MOUNT_FRAGMENT }, sellButtonData)
    --Stables Button
    local stablesButtonData = CreateButtonData("EsoUI/Art/Mounts/tabIcon_mounts_up.dds",
                                               "EsoUI/Art/Mounts/tabIcon_mounts_down.dds",
                                               "EsoUI/Art/Mounts/tabIcon_mounts_over.dds",
                                               SOUNDS.STABLE_WINDOW_MANAGE_CLICKED,
                                               TUTORIAL_TRIGGER_STABLE_MANAGEMENT_OPENED)
    self.modeBar:Add(SI_STABLE_STABLES_TAB, { STABLES_FRAGMENT }, stablesButtonData, self.keybindStripDescriptor)
end
do
    local function StableSlotIndexComparator(leftStableIndex, rightStableIndex)
        local leftIsEmpty = IsStableSlotEmpty(leftStableIndex)
        local rightIsEmpty = IsStableSlotEmpty(rightStableIndex)
        if leftIsEmpty and rightIsEmpty then
            return false
        elseif leftIsEmpty then
            return false
        elseif rightIsEmpty then
            return true
        end
        local leftName = GetStableSlotInfo(leftStableIndex)
        local rightName = GetStableSlotInfo(rightStableIndex)
        if leftName == rightName then
            return leftStableIndex < rightStableIndex
        end
        return leftName < rightName
    end
    function ZO_StableManager:UpdateStabledMounts()
        local mousedOverRow = self.mousedOverRow
        if mousedOverRow then            
            zo_callHandler(mousedOverRow, "OnMouseExit")
        end
        self.stableSlotRowPool:ReleaseAllObjects()
        local stableSlotIndices = {}
        for stableIndex = ACTIVE_MOUNT_INDEX, GetNumStableSlots() + ACTIVE_MOUNT_INDEX do
            stableSlotIndices[stableIndex] = stableIndex
        end
        table.sort(stableSlotIndices, StableSlotIndexComparator)
        local prevRow
        for i, stableIndex in ipairs(stableSlotIndices) do
            if stableIndex ~= ACTIVE_MOUNT_INDEX then
                local row = self.stableSlotRowPool:AcquireObject()
                row.stableIndex = stableIndex
                if IsStableSlotEmpty(stableIndex) then
                    row.empty = true
                    row.name:SetText(GetString(SI_STABLE_SLOT_EMPTY))
                    row.breed:SetText("")
                    row.icon:SetTexture("EsoUI/Art/Inventory/inventory_slot.dds")
                else
                    row.empty = false
                    local name, icon, portrait, breed = GetStableSlotInfo(stableIndex)
                    row.name:SetText(name)
                    row.breed:SetText(zo_strformat(SI_MOUNT_BREED_FORMAT, breed))
                    row.icon:SetTexture(portrait)
                    local timeUntilCanBeFed, totalFeedWaitDuration = GetTimeUntilMountCanBeFed(stableIndex)
                    if timeUntilCanBeFed and timeUntilCanBeFed > 0 then
                        row.timerOverlay:StartCooldown(timeUntilCanBeFed, totalFeedWaitDuration, CD_TYPE_RADIAL, CD_TIME_TYPE_TIME_UNTIL, NO_LEADING_EDGE)
                    end
                end
                row:SetAnchor(TOP, prevRow, prevRow and BOTTOM or TOP)
                row:SetHidden(false)
                prevRow = row
            end
        end
        if mousedOverRow then
            -- Force tooltip and highlights to update, the row might have changed
            zo_callHandler(mousedOverRow, "OnMouseEnter")
        end
    end
end
function ZO_StableManager:ShowContextMenu(row)
    if not row.empty then
        ClearMenu()
        row.lockSelection = true
        self.selectionLockedToRow = row
        local function OnHiddenCallback()
            self.selectionLockedToRow = nil
            row.lockSelection = false 
            if self.mousedOverRow ~= row then
                zo_callHandler(row, "OnMouseExit")
                if self.mousedOverRow then
                    zo_callHandler(self.mousedOverRow, "OnMouseEnter")
                end
            end
        end
        AddMenuItem(GetString(SI_STABLE_SET_ACTIVE), function() SetActiveMount(row.stableIndex) end)
        ShowMenu(row)
    end
end
function ZO_StableManager:OnStableSlotRowMouseEnter(row)
    self.mousedOverRow = row
    if not self.selectionLockedToRow then
        KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptor)
    end
end
function ZO_StableManager:OnStableSlotRowMouseExit(row)
    if self.mousedOverRow == row then
        self.mousedOverRow = nil
    end
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptor)
end
function ZO_StableManager:UpdateFreeSlots()
    local numUsedSlots, numSlots = ZO_Stables_GetNumMounts(), GetNumStableSlots() + ACTIVE_MOUNT_INDEX
    if numUsedSlots < numSlots then
        self.freeSlots:SetText(zo_strformat(SI_STABLE_SLOTS_REMAINING_SPACES, numUsedSlots, numSlots))
    else
        self.freeSlots:SetText(zo_strformat(SI_STABLE_SLOTS_COMPLETELY_FULL, numUsedSlots, numSlots))
    end
end
function ZO_StableManager:OnShow()
    self:UpdateMoney()
end
function ZO_StableManager:OnFullMountUpdate()
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptor)
end
function ZO_StableManager:OnMountUpdate(stableIndex)
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptor)
    if stableIndex == ACTIVE_MOUNT_INDEX then
        self:UpdateActiveMount()
    else
        self:UpdateStabledMounts()
    end
end
function ZO_StableManager:UpdateMoney()
    ZO_CurrencyControl_SetSimpleCurrency(self.money, CURRENCY_TYPE_MONEY, GetCurrentMoney(), CURRENCY_OPTIONS)
end
function ZO_StableManager:OnMoneyUpdated()
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptor)
    self:UpdateMoney()
end
function ZO_StableManager:OnFeedButtonClicked(button)
     PlaySound(button.feedSound)
    FeedMount(button.feed)
end
function ZO_StableManager:SetHidden(hidden)
    if hidden then
        SCENE_MANAGER:Hide("stables") 
    else
        SCENE_MANAGER:Show("stables") 
    end
end
function ZO_StableManager:OnStablesInteractStart()
    if not IsInGamepadPreferredMode() then
        self:SetHidden(false)
        self:UpdateActiveMount()
    end
end
function ZO_StableManager:OnStablesInteractEnd()
    self:SetHidden(true)
end
    STABLE = ZO_StableManager:New(control)
end
function ZO_Stable_OnShown()
    STABLE:OnShow()
end
    row.owner:ShowContextMenu(row)
end