ESO Lua File v100010

ingame/mail/mailsend.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
local MailSend = ZO_Object:New()
function MailSend:New(control)
    local manager = ZO_Object.New(self)
    MAIL_SEND = manager
    manager.control = control
    
    local editControlGroup = ZO_EditControlGroup:New()
    manager.sendMoneyMode = true
        
    manager.to = control:GetNamedChild("ToField")
    manager.autoComplete = ZO_AutoComplete:New(manager.to, { AUTO_COMPLETE_FLAG_ALL }, nil, AUTO_COMPLETION_ONLINE_OR_OFFLINE, MAX_AUTO_COMPLETION_RESULTS)
    editControlGroup:AddEditControl(manager.to, manager.autoComplete)
        
    manager.subject = control:GetNamedChild("SubjectField")
    manager.subject:SetMaxInputChars(MAIL_MAX_SUBJECT_CHARACTERS)
    editControlGroup:AddEditControl(manager.subject)
        
    manager.body = control:GetNamedChild("BodyField")
    manager.body:SetMaxInputChars(MAIL_MAX_BODY_CHARACTERS)
    editControlGroup:AddEditControl(manager.body)
    manager.attachMoneyRadioButton = control:GetNamedChild("AttachRadio")
    manager.codRadioButton = control:GetNamedChild("CoDRadio")
    manager.radioButtonGroup = ZO_RadioButtonGroup:New()
    manager.radioButtonGroup:Add(manager.attachMoneyRadioButton)
    manager.radioButtonGroup:Add(manager.codRadioButton)
    manager.radioButtonGroup:SetClickedButton(manager.attachMoneyRadioButton)
        
    manager.postageCurrency = control:GetNamedChild("PostageCurrency")
    manager.sendCurrency = control:GetNamedChild("SendCurrency")
    manager.title = control:GetParent():GetNamedChild("Title")
    local function ChangeMoneyCallback(moneyInput, money)
        manager:AttachMoney(moneyInput, money)
    end
    ZO_DefaultMoneyInputField_SetUsePlayerGoldAsMax(manager.sendCurrency, true)
        
    manager:InitializeKeybindDescriptors()
    manager:CreateAttachmentSlots()
    manager:ClearFields()
    MAIL_SEND_SCENE = ZO_Scene:New("mailSend", SCENE_MANAGER)
    MAIL_SEND_SCENE:RegisterCallback("StateChange", function(oldState, newState)
        if(newState == SCENE_SHOWING) then
            KEYBIND_STRIP:AddKeybindButtonGroup(manager.staticKeybindStripDescriptor)
            manager:RestorePendingMail()
        elseif(newState == SCENE_SHOWN) then
            if(manager.pendingMailChanged) then
                ZO_Dialogs_ShowDialog("MAIL_ATTACHMENTS_CHANGED")
                manager.pendingMailChanged = nil
            end        
        elseif(newState == SCENE_HIDDEN) then
            KEYBIND_STRIP:RemoveKeybindButtonGroup(manager.staticKeybindStripDescriptor)
            manager:SavePendingMail()
        end
    end)
    control:RegisterForEvent(EVENT_MAIL_SEND_SUCCESS, function() manager:OnMailSendSuccess() end)
    control:RegisterForEvent(EVENT_MAIL_ATTACHMENT_ADDED, function(_, attachSlot) manager:OnMailAttachmentAdded(attachSlot) end)
    control:RegisterForEvent(EVENT_MAIL_ATTACHMENT_REMOVED, function(_, attachSlot) manager:OnMailAttachmentRemoved(attachSlot) end)
    control:RegisterForEvent(EVENT_MAIL_COD_CHANGED, function() manager:UpdateCOD() end)
    control:RegisterForEvent(EVENT_MAIL_ATTACHED_MONEY_CHANGED, function() manager:UpdateMoneyAttachment() end)
    control:RegisterForEvent(EVENT_MONEY_UPDATE, function() manager:OnMoneyUpdate() end)
    return manager
end
--Global API
function MailSend:ComposeMailTo(address)
    MAIN_MENU:ShowScene("mailSend")
    self:ClearFields()
    SCENE_MANAGER:CallWhen("mailSend", SCENE_SHOWN, function() self:SetReply(address) end)
end
--Internal
function MailSend:InitializeKeybindDescriptors()
    self.staticKeybindStripDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_CENTER,
        -- Clear
        {
            name = GetString(SI_MAIL_SEND_CLEAR),
            keybind = "UI_SHORTCUT_NEGATIVE",
        
            callback = function()
                ZO_Dialogs_ShowDialog("CONFIRM_CLEAR_MAIL_COMPOSE", { callback = function() self:ClearFields() end })
            end,
        },
        -- Send
        {
            name = GetString(SI_MAIL_SEND_SEND),
            keybind = "UI_SHORTCUT_SECONDARY",
        
            callback = function()
                self:Send()
            end,
        },
    }
end
function MailSend:SavePendingMail()
    if(self.pendingAttachments == nil) then
        self.pendingAttachments = {}
        for _, slot in ipairs(self.attachmentSlots) do
            if(slot.bagId) then
                table.insert(self.pendingAttachments,
                {
                    bagId = slot.bagId,
                    slotIndex = slot.slotIndex,
                    itemInstanceId = GetItemInstanceId(slot.bagId, slot.slotIndex),
                    slotId = slot.id,
                    stackSize = GetSlotStackSize(slot.bagId, slot.slotIndex),
                })
                RemoveQueuedItemAttachment(slot.id)
            end
        end
        self.pendingGold = GetQueuedMoneyAttachment()
    end
end
function MailSend:RestorePendingMail()
    if(self.pendingAttachments) then
        for _, pendingAttachment in ipairs(self.pendingAttachments) do
            local sameItemInSlot = GetItemInstanceId(pendingAttachment.bagId, pendingAttachment.slotIndex) == pendingAttachment.itemInstanceId
            local sameStackSize = GetSlotStackSize(pendingAttachment.bagId, pendingAttachment.slotIndex) == pendingAttachment.stackSize
            if(sameItemInSlot and sameStackSize) then
                QueueItemAttachment(pendingAttachment.bagId, pendingAttachment.slotIndex, pendingAttachment.slotId)
            else
                self.pendingMailChanged = true
            end
        end
        if(self.pendingGold ~= GetQueuedMoneyAttachment()) then
            self.pendingMailChanged = true
        end
        self.pendingAttachments = nil
        self.pendingGold = nil
    end
end
function MailSend:CreateAttachmentSlots()
    self.attachmentSlots = {}
    local parent = GetControl(self.control, "Attachments")
    local previous
    for i = 1, MAIL_MAX_ATTACHED_ITEMS do
        local slot = CreateControlFromVirtual(parent:GetName().."Slot", parent, "ZO_MailSendAttachmentSlot", i)
        if previous then
            slot:SetAnchor(TOPLEFT, previous, TOPRIGHT, 18, 0)
        else
            slot:SetAnchor(TOPLEFT, parent, TOPLEFT, 0, 0)
        end
        
        slot.id = i
        
        slot.icon = slot:GetNamedChild("ItemIcon")
     
        ZO_Inventory_BindSlot(slot, SLOT_TYPE_MAIL_QUEUED_ATTACHMENT, i)
        ZO_Inventory_SetupSlot(slot, 0, ZO_MAIL_EMPTY_SLOT_TEXTURE) 
        self.attachmentSlots[i] = slot
        previous = slot
    end    
end
function MailSend:IsHidden()
    return MAIL_SEND_SCENE:GetState() == SCENE_HIDDEN
end
function MailSend:ClearFields()
    ClearQueuedMail()
    self.to:SetText("")
    self.subject:SetText("")
    self.body:SetText("")
    self:UpdateCOD()
    self.radioButtonGroup:SetClickedButton(self.attachMoneyRadioButton)
end
function MailSend:UpdateMoneyAttachment()
    if(self.sendMoneyMode) then
        self:UpdatePostageMoney()
    end
end
function MailSend:UpdateCOD()
    if(not self.sendMoneyMode) then
        ZO_DefaultMoneyInputField_SetMoney(self.sendCurrency, GetQueuedCOD())
    end
end
function MailSend:UpdatePostageMoney()
    local postageAmount = GetQueuedMailPostage()
    ZO_CurrencyControl_SetSimpleCurrency(self.postageCurrency, CURRENCY_TYPE_MONEY, postageAmount, nil, CURRENCY_SHOW_ALL, postageAmount > GetCurrentMoney())
end
function MailSend:SetSendMoneyMode(sendMoneyMode)
    if self.sendMoneyMode ~= sendMoneyMode then
        self.sendMoneyMode = sendMoneyMode
        ZO_DefaultMoneyInputField_SetMoney(self.sendCurrency, 0)
        ZO_DefaultMoneyInputField_SetUsePlayerGoldAsMax(self.sendCurrency, sendMoneyMode)
        if(sendMoneyMode) then
            QueueMoneyAttachment(ZO_DefaultMoneyInputField_GetMoney(self.sendCurrency))
        else
            QueueCOD(ZO_DefaultMoneyInputField_GetMoney(self.sendCurrency))
        end
        self:UpdatePostageMoney()
    end
end
function MailSend:AttachMoney(moneyInput, money)
    if self.sendMoneyMode then
        QueueMoneyAttachment(money)
    else
        QueueCOD(money)
    end
end
function MailSend:Send()
    WINDOW_MANAGER:SetFocusByName("")
    if not self.sendMoneyMode and GetQueuedCOD() == 0 then
        ZO_Alert(UI_ALERT_CATEGORY_ERROR, SOUNDS.NEGATIVE_CLICK, GetString(SI_MAIL_MUST_SET_REQUIRED_MONEY_IN_COD))
    else
        SendMail(self.to:GetText(), self.subject:GetText(), self.body:GetText())
    end
end
function MailSend:SetReply(to, subject)
    self.to:SetText(to or "")
    self.body:SetText("")
    if(subject and subject ~= "") then
        self.subject:SetText(zo_strformat(SI_MAIL_REPLY_SUBJECT, subject))
        self.body:TakeFocus()
    else
        self.subject:SetText("")
        self.subject:TakeFocus()
    end   
end
--Events
function MailSend:OnMoneyUpdate()
end
function MailSend:OnMailSendSuccess()
     PlaySound(SOUNDS.MAIL_SENT)
    self:ClearFields()
end     
function MailSend:OnMailAttachmentAdded(attachSlot)
    ZO_MailSendShared_AddAttachedItem(attachSlot, self.attachmentSlots[attachSlot])
end
function MailSend:OnMailAttachmentRemoved(attachSlot)
    ZO_MailSendShared_RemoveAttachedItem(attachSlot, self.attachmentSlots[attachSlot])
end
--Local XML
function MailSend:SetCoDMode()
    if self.radioButtonGroup:GetClickedButton() ~= self.codRadioButton then
        self.radioButtonGroup:SetClickedButton(self.codRadioButton)
    end
    self:SetSendMoneyMode(false)
end
function MailSend:SetMoneyAttachmentMode()
    if self.radioButtonGroup:GetClickedButton() ~= self.attachMoneyRadioButton then
        self.radioButtonGroup:SetClickedButton(self.attachMoneyRadioButton)
    end
    self:SetSendMoneyMode(true)
end
--Global XML
    MAIL_SEND:SetMoneyAttachmentMode()
end
    MAIL_SEND:SetCoDMode()
end
    MailSend:New(self)
end