ESO Lua File v100011

ingame/mail/gamepad/mailsend_gamepad.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
-- Settings.
local MAX_SEND_ATTACHMENTS = MAIL_MAX_ATTACHED_ITEMS
local EMPTY_ATTACHMENT_ICON = "EsoUI/Art/Mail/mail_attachment_empty.dds"
local SEND_GOLD_ICON = "EsoUI/Art/Mail/Gamepad/gp_mailMenu_sendGold.dds"
local REQUEST_GOLD_ICON = "EsoUI/Art/Mail/Gamepad/gp_mailMenu_requestGold.dds"
local ATTACHING_GOLD = "attach"
local REQUESTING_GOLD = "request"
-- TODO: This needs to be figured out for localization.
local LETTER_GROUPS =
{
    a = "A-E",
    b = "A-E",
    c = "A-E",
    d = "A-E",
    e = "A-E",
    f = "F-J",
    g = "F-J",
    h = "F-J",
    i = "F-J",
    j = "F-J",
    k = "K-O",
    l = "K-O",
    m = "K-O",
    n = "K-O",
    o = "K-O",
    p = "P-T",
    q = "P-T",
    r = "P-T",
    s = "P-T",
    t = "P-T",
    u = "U-Z",
    v = "U-Z",
    w = "U-Z",
    x = "U-Z",
    y = "U-Z",
    z = "U-Z",
}
-- For a given inventory item, return the attachment slot index, or nil if the item is not attached.
local function GetItemAttachedIndex(bagId, slotIndex)
    for i = 1, MAIL_MAX_ATTACHED_ITEMS do
        local queuedBagId, queuedSlotIndex = GetQueuedItemAttachmentInfo(i)
        if (queuedBagId == bagId) and (queuedSlotIndex == slotIndex) then
            return i
        end
    end
    return nil -- Item not attached.
end
-- Returns whether the specified inventory item is attached.
local function IsItemAttached(bagId, slotIndex)
    if GetItemAttachedIndex(bagId, slotIndex) then
        return true
    else
        return false
    end
end
-- Returns the next open attachment slot index, or nil if all attachment slots are full.
local function GetNextOpenAttachIndex()
    for i = 1, MAIL_MAX_ATTACHED_ITEMS do
        local queuedFromBag = GetQueuedItemAttachmentInfo(i)
        if queuedFromBag == 0 then -- Slot is open.
            return i
        end
    end
    return nil -- No open slots.
end
-- Returns whether there is any item attached.
local function IsAnyItemAttached(bagId, slotIndex)
    for i = 1, MAIL_MAX_ATTACHED_ITEMS do
        local queuedFromBag = GetQueuedItemAttachmentInfo(i)
        if queuedFromBag ~= 0 then -- Slot is filled.
            return true
        end
    end
    return false
end
-- Removes the item attached in the specified slot, behaving as design requested.
local function RemoveQueuedAttachment(attachmentIndex)
    RemoveQueuedItemAttachment(attachmentIndex)
    for i = attachmentIndex+1, MAIL_MAX_ATTACHED_ITEMS do
        local queuedBagId, queuedSlotIndex = GetQueuedItemAttachmentInfo(i)
        if queuedBagId ~= 0 then -- Slot is filled.
            RemoveQueuedItemAttachment(i)
            QueueItemAttachment(queuedBagId, queuedSlotIndex, i-1)
        end 
    end
end
-- The main class.
ZO_MailSend_Gamepad = ZO_Object:Subclass()
function ZO_MailSend_Gamepad:New(...)
    local mailSend = ZO_Object.New(self)
    mailSend:Initialize(...)
    return mailSend
end
function ZO_MailSend_Gamepad:Initialize(control)
    self.control = control
    self.sendControl = self.control:GetNamedChild("Send")
end
function ZO_MailSend_Gamepad:OnShowing()
    self:EnterOutbox()
    if self.initialContact then
        self.mailView:Display(nil, nil, self.initialContact)
        self.initialContact = nil
    end
end
function ZO_MailSend_Gamepad:OnHidden()
    self:Reset()
end
function ZO_MailSend_Gamepad:PerformDeferredInitialization()
    if self.initialized then return end
    self.initialized = true
    self.attachmentSlotIndexToInventoryInfo = {}
end
function ZO_MailSend_Gamepad:InitializeControls()
    -- Sending Mail
    self.loadingBox = self.control:GetNamedChild("Loading")
    self.loadingLabel = self.loadingBox:GetNamedChild("Loading")
    -- Mail View
    self.mailView = self.sendControl:GetNamedChild("RightPane"):GetNamedChild("Container"):GetNamedChild("MailView")
    local IS_OUTBOX = true
    self.mailView:Initialize(GetString(SI_GAMEPAD_MAIL_SEND_TO), EMPTY_ATTACHMENT_ICON, IS_OUTBOX, ZO_MAIL_COD_MONEY_OPTIONS_GAMEPAD, ZO_MAIL_ATTACHED_MONEY_OPTIONS_GAMEPAD, MAX_SEND_ATTACHMENTS)
    self.mailView:Clear()
    self.mailView.subjectEdit.edit:SetMaxInputChars(MAIL_MAX_SUBJECT_CHARACTERS)
    self.mailView.bodyEdit.edit:SetMaxInputChars(MAIL_MAX_BODY_CHARACTERS)
    -- Gold Slider
    self.goldSliderControl = self.sendControl:GetNamedChild("GoldSliderBox")
    self.goldSlider = ZO_CurrencySelector_Gamepad:New(self.goldSliderControl:GetNamedChild("Selector"))
    self.goldSlider:SetClampValues(true)
    self.goldSlider:RegisterCallback("OnValueChanged", function() MAIL_MANAGER_GAMEPAD:RefreshKeybind() end)
end
function ZO_MailSend_Gamepad:InitializeFragment()
    GAMEPAD_MAIL_SEND_FRAGMENT = ZO_FadeSceneFragment:New(ZO_MailManager_GamepadSend)
    GAMEPAD_MAIL_SEND_FRAGMENT:RegisterCallback("StateChange", function(oldState, newState)
        if newState == SCENE_FRAGMENT_SHOWING then
            self:OnShowing()
        elseif newState == SCENE_FRAGMENT_HIDDEN then
            self:OnHidden()
        end
    end)
end
function ZO_MailSend_Gamepad:ConnectShownEvents()
    self.control:RegisterForEvent(EVENT_MAIL_COD_CHANGED, function() self:UpdateMoneyAttachment() end)
    self.control:RegisterForEvent(EVENT_MAIL_ATTACHED_MONEY_CHANGED, function() self:UpdateMoneyAttachment() end)
    self.control:RegisterForEvent(EVENT_MONEY_UPDATE, function() self:UpdatePostageMoney() end)
end
function ZO_MailSend_Gamepad:DisconnectShownEvent()
    self.control:UnregisterForEvent(EVENT_MAIL_COD_CHANGED)
    self.control:UnregisterForEvent(EVENT_MAIL_ATTACHED_MONEY_CHANGED)
    self.control:UnregisterForEvent(EVENT_MONEY_UPDATE)
end
function ZO_MailSend_Gamepad:InitializeEvents()
    self.control:RegisterForEvent(EVENT_MAIL_ATTACHMENT_ADDED, function(_, attachSlot) self:OnMailAttachmentAdded(attachSlot) end)
    self.control:RegisterForEvent(EVENT_MAIL_ATTACHMENT_REMOVED, function(_, attachSlot) self:OnMailAttachmentRemoved(attachSlot) end)
    local function HandleInventoryChanged()
        self:PopulateMainList()
    end
    self.control:RegisterForEvent(EVENT_INVENTORY_FULL_UPDATE, HandleInventoryChanged)
    self.control:RegisterForEvent(EVENT_INVENTORY_SINGLE_SLOT_UPDATE, HandleInventoryChanged)
    self.control:RegisterForEvent(EVENT_MAIL_SEND_SUCCESS, function() self:OnMailSendSuccess() end)
    self.control:RegisterForEvent(EVENT_MAIL_SEND_FAILED, function(_, ...) self:OnMailSendFailure(...) end)
end
--Global API
function ZO_MailSend_Gamepad:ComposeMailTo(address)
    self.initialContact = address
    local PUSH_SCENE = true
    MAIL_MANAGER_GAMEPAD:ShowTab(SEND_TAB_INDEX, PUSH_SCENE)
end
function ZO_MailSend_Gamepad:InitializeKeybindDescriptors()
    -- Main list.
    self.mainKeybindDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Back
        KEYBIND_STRIP:GetDefaultGamepadBackButtonDescriptor(),
        -- Edit
        {
            name = GetString(SI_GAMEPAD_SELECT_OPTION),
            keybind = "UI_SHORTCUT_PRIMARY",
            callback = function()
                        local selectedData = self.mainList:GetSelectedData()
                        if selectedData and selectedData.actionFunction then
                            selectedData:actionFunction()
                        end
                   end,
        },
        -- Send Mail
        {
            name = GetString(SI_MAIL_SEND_SEND),
            keybind = "UI_SHORTCUT_SECONDARY",
            callback = function()
                            local to = self.mailView:GetAddress()
                            local subject = self.mailView:GetSubject()
                            local body = self.mailView:GetBody()
                            SendMail(to, subject, body)
                            self:EnterSending()
                       end,
            visible = function()
                            -- In order to send mail, the following must be true:
                            -- - have an address,
                            -- - and one of: subject, body, attached gold, or attached items.
                            local to = self.mailView:GetAddress()
                            if (not to) or (to == "") then
                                return false
                            end
                            local subject = self.mailView:GetSubject()
                            local hasSubject = subject and (subject ~= "")
                            local body = self.mailView:GetBody()
                            local hasBody = body and (body ~= "")
                            return hasSubject or hasBody or (GetQueuedMoneyAttachment() > 0) or IsAnyItemAttached()
                        end,
        },
        -- Clear
        {
            name = GetString(SI_GAMEPAD_MAIL_SEND_CLEAR),
            keybind = "UI_SHORTCUT_TERTIARY",
            callback = function() ZO_Dialogs_ShowGamepadDialog("CONFIRM_CLEAR_MAIL_COMPOSE", { callback = function() self:ClearFields() end }) end
        },
    }
    ZO_Gamepad_AddListTriggerKeybindDescriptors(self.mainKeybindDescriptor, self.mainList)
    -- Slider Edit
    self.sliderKeybindDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Accept
        {
            name = GetString(SI_GAMEPAD_MAIL_SEND_ACCEPT_MONEY),
            keybind = "UI_SHORTCUT_PRIMARY",
            callback = function()
                            if self.goldMode == ATTACHING_GOLD then
                                QueueCOD(0)
                                QueueMoneyAttachment(self.goldSlider:GetValue())
                            elseif self.goldMode == REQUESTING_GOLD then
                                QueueMoneyAttachment(0)
                                QueueCOD(self.goldSlider:GetValue())
                            end
                            PlaySound(SOUNDS.ITEM_MONEY_CHANGED)
                            self:UpdatePostageMoney()
                            self:EnterOutbox()
                            local selectedControl = self.mainList:GetSelectedControl()
                            selectedControl:SetHidden(false)
                       end,
            visible = function() return self.goldSlider:GetValue() <= self.goldSlider:GetMaxValue() end,
        },
        -- Cancel
        KEYBIND_STRIP:GenerateGamepadBackButtonDescriptor(function()
                            self:UpdateMoneyAttachment()
                            self:EnterOutbox()
                            if self.hiddenControl then
                                self.hiddenControl:SetHidden(false)
                                self.hiddenControl = nil
                            end
                       end),
    }
    
    -- Contacts List
    self.contactsKeybindDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Accept
        {
            name = GetString(SI_GAMEPAD_MAIL_SEND_ACCEPT_MONEY),
            keybind = "UI_SHORTCUT_PRIMARY",
            callback = function()
                            local selectedItem = self.contactsList:GetSelectedData()
                            if selectedItem.actionFunction then
                                selectedItem.actionFunction(selectedItem)
                            end
                            self:EnterOutbox()
                       end,
        },
        -- Cancel
        KEYBIND_STRIP:GenerateGamepadBackButtonDescriptor(function()
                            self:UpdateMoneyAttachment()
                            self:EnterOutbox()
                       end),
    }
    ZO_Gamepad_AddListTriggerKeybindDescriptors(self.contactsKeybindDescriptor, self.contactsList)
    -- Inventory
    self.inventoryKeybindDescriptor = 
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Add to Mail/Remove from Mail
        {
            name = function()
                        local selectedItem = self.inventoryList:GetSelectedData()
                        if IsItemAttached(selectedItem.bagId, selectedItem.slotIndex) then
                            return GetString(SI_GAMEPAD_MAIL_SEND_DETACH_ITEM)
                        else
                            return GetString(SI_GAMEPAD_MAIL_SEND_ATTACH_ITEM)
                        end
                    end,
            keybind = "UI_SHORTCUT_PRIMARY",
            callback = function()
                        local selectedItem = self.inventoryList:GetSelectedData()
                        local bagId = selectedItem.bagId
                        local slotIndex = selectedItem.slotIndex
                        local attachedSlotIndex = GetItemAttachedIndex(bagId, slotIndex)
                        if attachedSlotIndex then -- Item is attached, detach it.
                            RemoveQueuedAttachment(attachedSlotIndex)
                            local soundCategory = GetItemSoundCategory(bagId, slotIndex)
                             PlayItemSound(soundCategory, ITEM_SOUND_ACTION_UNEQUIP)
                        else -- Item is not attached, attach it.
                            attachedSlotIndex = GetNextOpenAttachIndex()
                            if attachedSlotIndex then
                                QueueItemAttachment(bagId, slotIndex, attachedSlotIndex)
                                local soundCategory = GetItemSoundCategory(bagId, slotIndex)
                                 PlayItemSound(soundCategory, ITEM_SOUND_ACTION_EQUIP)
                            end
                        end
                    end,
            visible = function()
                        local selectedItem = self.inventoryList:GetSelectedData()
                        if not selectedItem then
                            return false
                        end
                        local bagId = selectedItem.bagId
                        local slotIndex = selectedItem.slotIndex
                        if IsItemAttached(bagId, slotIndex) then
                            return true -- Can always remove an attached item.
                        end
                        local attachedSlotIndex = GetNextOpenAttachIndex()
                        if not attachedSlotIndex then
                            return false
                        end
                        local canAttach = CanQueueItemAttachment(bagId, slotIndex, attachedSlotIndex)
                        return canAttach
                   end,
        },
        -- Back
        KEYBIND_STRIP:GenerateGamepadBackButtonDescriptor(function()
                            self:UpdatePostageMoney()
                            self:EnterOutbox()
                       end),
    }
end
local function UpdatePlayerGold(control)
    ZO_CurrencyControl_SetSimpleCurrency(control, CURRENCY_TYPE_MONEY, GetCurrentMoney(), ZO_MAIL_HEADER_MONEY_OPTIONS_GAMEPAD)
    return true
end
local function UpdatePostage(control)
    ZO_CurrencyControl_SetSimpleCurrency(control, CURRENCY_TYPE_MONEY, GetQueuedMailPostage(), ZO_MAIL_HEADER_MONEY_OPTIONS_GAMEPAD)
    return true
end
function ZO_MailSend_Gamepad:InitializeHeader()
    self.mainHeaderData = {
            data1HeaderText = GetString(SI_GAMEPAD_MAIL_INBOX_PLAYER_GOLD),
            data1Text = UpdatePlayerGold,
            data2HeaderText = GetString(SI_GAMEPAD_MAIL_SEND_POSTAGE_LABEL),
            data2Text = UpdatePostage,
            tabBarEntries = MAIL_MANAGER_GAMEPAD.tabBarEntries,
        }
    self.setFieldHeaderData = {
            data1HeaderText = GetString(SI_GAMEPAD_MAIL_INBOX_PLAYER_GOLD),
            data1Text = UpdatePlayerGold,
            data2HeaderText = GetString(SI_GAMEPAD_MAIL_SEND_POSTAGE_LABEL),
            data2Text = UpdatePostage,
        }
end
local function InventorySetupFunction(entryData)
    entryData.isMailAttached = IsItemAttached(entryData.bagId, entryData.slotIndex)
end
local function ItemFilterFunction(entryData)
    local bagId = entryData.bagId
    local slotIndex = entryData.slotIndex
    return CanQueueItemAttachment(bagId, slotIndex) or IsItemAttached(bagId, slotIndex)
end
local SETUP_LOCALLY = true
function ZO_MailSend_Gamepad:InitializeInventoryList()
    self.inventoryList = MAIL_MANAGER_GAMEPAD:AddList("Inventory", SETUP_LOCALLY, ZO_GamepadInventoryList, BAG_BACKPACK, SLOT_TYPE_ITEM, function(...) self:InventorySelectionChanged(...) end, InventorySetupFunction)
    self.inventoryListControl = self.inventoryList:GetControl()
end
function ZO_MailSend_Gamepad:AddMainListEntry(text, header, icon, callback)
    local newEntry = ZO_GamepadEntryData:New(text, icon)
    newEntry.actionFunction = callback
    local template
    if header then
        newEntry:SetHeader(header)
        template = "ZO_GamepadMenuEntryTemplateWithHeader"
    else
        template = "ZO_GamepadMenuEntryTemplate"
    end
    self.mainList:AddEntry(template, newEntry)
end
function ZO_MailSend_Gamepad:PopulateMainList()
    local function RefreshKeybind()
          MAIL_MANAGER_GAMEPAD:RefreshKeybind()
     end
     self.mainList:Clear()
    self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_TO), nil, nil, function() self:EnterContactsList() end)
    self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SUBJECT_LABEL), nil, nil, function() self:ActivateEditbox(self.mailView.subjectEdit) end)
    self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_BODY_LABEL), nil, nil, function() self:ActivateEditbox(self.mailView.bodyEdit) end)
    self:AddMainListEntry(GetString(SI_MAIL_SEND_ATTACH_MONEY), GetString(SI_GAMEPAD_MAIL_SEND_GOLD_HEADER), SEND_GOLD_ICON, function() self:ShowSliderControl(ATTACHING_GOLD, GetQueuedMoneyAttachment(), GetCurrentMoney()) end)
    self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_COD), nil, REQUEST_GOLD_ICON, function() self:ShowSliderControl(REQUESTING_GOLD, GetQueuedCOD(), MAX_PLAYER_MONEY) end)
     self.mailView.subjectEdit.edit:SetHandler("OnFocusLost", RefreshKeybind)
     self.mailView.addressEdit.edit:SetHandler("OnFocusLost", RefreshKeybind)
    self.mailView.bodyEdit.edit:SetHandler("OnFocusLost", RefreshKeybind)
    if not self.inventoryList:IsEmpty() then
        self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_ATTACH), GetString(SI_GAMEPAD_MAIL_SEND_ITEMS_HEADER), EMPTY_ATTACHMENT_ICON, function() self:EnterInventoryList() end)
    end
    self.mainList:Commit()
end
function ZO_MailSend_Gamepad:InitializeMainList()
    self.mainList = MAIL_MANAGER_GAMEPAD:GetMainList()
    self.mainList:SetOnMovementChangedCallback(function(...) self:OnListMovement(...) end)
end
function ZO_MailSend_Gamepad:OnListMovement(list, isMoving)
    self.mailView.addressEdit.edit:LoseFocus()
    self.mailView.subjectEdit.edit:LoseFocus()
    self.mailView.bodyEdit.edit:LoseFocus()
end
function ZO_MailSend_Gamepad:InitializeContactsList()
    self.contactsList = MAIL_MANAGER_GAMEPAD:AddList("Contacts")
    self.contactsListControl = self.contactsList:GetControl()
end
function ZO_MailSend_Gamepad:ClearFields()
    self:Clear()
    PlaySound(SOUNDS.MAIL_CANCELLED)
    self:EnterOutbox()
end
function ZO_MailSend_Gamepad:Reset()
    if (not self.inventoryListControl:IsHidden()) or (not self.goldSliderControl:IsHidden()) or (not self.contactsListControl:IsHidden()) then
        PlaySound(SOUNDS.GAMEPAD_MENU_BACK)
    end
    MAIL_MANAGER_GAMEPAD:DisableCurrentList()
    self.goldSliderControl:SetHidden(true)
    if self.hiddenControl then
        self.hiddenControl:SetHidden(false)
        self.hiddenControl = nil
    end
    GAMEPAD_TOOLTIPS:HideBg(GAMEPAD_LEFT_TOOLTIP)
    GAMEPAD_TOOLTIPS:ClearLines(GAMEPAD_LEFT_TOOLTIP)
    GAMEPAD_TOOLTIPS:Reset(GAMEPAD_LEFT_TOOLTIP)
    self.loadingBox:SetHidden(true)
    self.goldSlider:Deactivate()
    self.goldMode = nil
    MAIL_MANAGER_GAMEPAD:SwitchToHeader(self.mainHeaderData)
end
function ZO_MailSend_Gamepad:EnterSending()
    self.inSendMode = true
    self:Reset()
    MAIL_MANAGER_GAMEPAD:SwitchToKeybind(nil) -- Remove keybinds as they are invaild when sending.
    self.loadingLabel:Show()
    self.loadingLabel:SetText(GetString(SI_GAMEPAD_MAIL_SEND_SENDING))
    self.loadingBox:SetHidden(false)
end
function ZO_MailSend_Gamepad:EnterOutbox()
    self:Reset()
    if self.inSendMode then
        self:EnterSending()
    else
        MAIL_MANAGER_GAMEPAD:SetCurrentList(self.mainList)
        MAIL_MANAGER_GAMEPAD:SwitchToKeybind(self.mainKeybindDescriptor)
    end
end
function ZO_MailSend_Gamepad:AddContact(text, header, callback)
    local newEntry = ZO_GamepadEntryData:New(text)
    newEntry.actionFunction = callback
    local template
    if header then
        newEntry:SetHeader(header)
        template = "ZO_GamepadMenuEntryTemplateWithHeader"
    else
        template = "ZO_GamepadMenuEntryTemplate"
    end
    self.contactsList:AddEntry(template, newEntry)
end
local function GetHeaderForName(name)
    for i=1, #name do
        local letter = zo_strlower(string.sub(name, i, i))
        local header = LETTER_GROUPS[letter]
        if header then
            return header
        end
    end
end
local function FriendSort(a,b)
    return a < b
end
local RECIPIENT_HEADER_TEXT = GetString(SI_GAMEPAD_MAIL_SEND_RECIPIENT)
function ZO_MailSend_Gamepad:EnterContactsList()
    local function FillWithName(selectedData)
        self.mailView:Display(nil, nil, selectedData.text)
    end
    self.contactsList:Clear()
    -- Text edit
    self:AddContact(GetString(SI_GAMEPAD_MAIL_SEND_ENTER_NAME), nil, function() self:ActivateEditbox(self.mailView.addressEdit) end)
    -- Recent
    local NO_MIN_SCORE = true
    local recentContacts = {GetAutoCompletion("", nil, nil, {AUTO_COMPLETE_FLAG_RECENT}, nil, NO_MIN_SCORE)}
    local isFirst = true
    for i = 1, #recentContacts do
        self:AddContact(recentContacts[i], isFirst and GetString(SI_GAMEPAD_MAIL_SEND_RECENT_CONTACTS), FillWithName)
        isFirst = false
    end
    -- Friends
    local friends = {}
    local numFriends = GetNumFriends()
    for i=1, numFriends do  
        local displayName = GetFriendInfo(i)
        table.insert(friends, displayName)
    end
    table.sort(friends, FriendSort)
    local lastHeader = nil
    for i= 1, #friends do  
        local displayName = friends[i]
        local header = GetHeaderForName(displayName)
        header = (header ~= lastHeader) and header
        lastHeader = header or lastHeader
        self:AddContact(displayName, header, FillWithName)
    end
    self.contactsList:Commit()
    self.setFieldHeaderData.titleText = RECIPIENT_HEADER_TEXT
    MAIL_MANAGER_GAMEPAD:SwitchToHeader(self.setFieldHeaderData)
    MAIL_MANAGER_GAMEPAD:SwitchToKeybind(self.contactsKeybindDescriptor)
    MAIL_MANAGER_GAMEPAD:SetCurrentList(self.contactsList)
    PlaySound(SOUNDS.GAMEPAD_MENU_FORWARD)
end
function ZO_MailSend_Gamepad:ShowSliderControl(mode, value, maxValue)
    self.hiddenControl = self.mainList:GetSelectedControl()
    self.hiddenControl:SetHidden(true)
    self.mainList:Deactivate()
    self.goldSlider:SetMaxValue(maxValue)
    self.goldSlider:SetValue(value)
    MAIL_MANAGER_GAMEPAD:SwitchToKeybind(self.sliderKeybindDescriptor)
    self.goldSlider:Activate()
    self.goldMode = mode
    self.goldSliderControl:SetHidden(false)
    PlaySound(SOUNDS.GAMEPAD_MENU_FORWARD)
end
local ATTACHMENT_HEADER_TEXT = GetString(SI_GAMEPAD_MAIL_SEND_ATTACH)
function ZO_MailSend_Gamepad:EnterInventoryList()
    self:Reset()
    self.setFieldHeaderData.titleText = ATTACHMENT_HEADER_TEXT
    MAIL_MANAGER_GAMEPAD:SwitchToHeader(self.setFieldHeaderData)
    MAIL_MANAGER_GAMEPAD:SwitchToKeybind(self.inventoryKeybindDescriptor)
    MAIL_MANAGER_GAMEPAD:SetCurrentList(self.inventoryList)
    GAMEPAD_TOOLTIPS:SetBgType(GAMEPAD_LEFT_TOOLTIP, GAMEPAD_TOOLTIP_DARK_BG)
    GAMEPAD_TOOLTIPS:ShowBg(GAMEPAD_LEFT_TOOLTIP)
    PlaySound(SOUNDS.GAMEPAD_MENU_FORWARD)
end
function ZO_MailSend_Gamepad:InventorySelectionChanged(inventoryData)
    GAMEPAD_TOOLTIPS:ClearLines(GAMEPAD_LEFT_TOOLTIP)
    if inventoryData then
        GAMEPAD_TOOLTIPS:LayoutBagItem(GAMEPAD_LEFT_TOOLTIP, inventoryData.bagId, inventoryData.slotIndex)
    end
    MAIL_MANAGER_GAMEPAD:RefreshKeybind()
end
function ZO_MailSend_Gamepad:ActivateEditbox(editbox)
    local SAVE_EXISTING_TEXT = true
    editbox.edit:OpenVirtualKeyboard(SAVE_EXISTING_TEXT)
end
function ZO_MailSend_Gamepad:Clear()
    ClearQueuedMail()
    self.mailView:Clear()
end
function ZO_MailSend_Gamepad:UpdateMoneyAttachment()
    local queuedCOD = GetQueuedCOD()
    local queuedMoney = GetQueuedMoneyAttachment()
    self.mailView:Display(queuedCOD, queuedMoney)
end
function ZO_MailSend_Gamepad:IsAttachingItems()
    return (not self.sendControl:IsHidden())
end
function ZO_MailSend_Gamepad:UpdatePostageMoney()
    MAIL_MANAGER_GAMEPAD:RefreshHeader()
end
function ZO_MailSend_Gamepad:OnMailAttachmentAdded(attachSlot)
    local bagId, slotIndex, icon, stack = GetQueuedItemAttachmentInfo(attachSlot)
    self.mailView:SetAttachment(attachSlot, stack, icon)
    self.attachmentSlotIndexToInventoryInfo[attachSlot] = { stack = stack, icon = icon }
    MAIL_MANAGER_GAMEPAD:RefreshKeybind()
    self.inventoryList:RefreshList()
end
function ZO_MailSend_Gamepad:OnMailAttachmentRemoved(attachSlot)
    self.mailView:ClearAttachment(attachSlot)
    self.attachmentSlotIndexToInventoryInfo[attachSlot] = nil
    MAIL_MANAGER_GAMEPAD:RefreshKeybind()
    self.inventoryList:RefreshList()
end
function ZO_MailSend_Gamepad:OnMailSendSuccess()
    PlaySound(SOUNDS.MAIL_SENT)
    self.inSendMode = false
    self:Clear()
    if not self.sendControl:IsHidden() then
        self:EnterOutbox()
    end
end
function ZO_MailSend_Gamepad:OnMailSendFailure(failureReason)
    self.inSendMode = false
    if not self.sendControl:IsHidden() then
        self:EnterOutbox()
    end
end