Back to Home

ESO Lua File v101036

ingame/mail/keyboard/mailsend_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
local MailSend = ZO_InitializingObject:Subclass()
function MailSend:Initialize(control)
    self.control = control
    local editControlGroup = ZO_EditControlGroup:New()
    self.sendMoneyMode = true
    self.to = control:GetNamedChild("ToField")
    self.autoComplete = ZO_AutoComplete:New(self.to, { AUTO_COMPLETE_FLAG_ALL }, { AUTO_COMPLETE_FLAG_GUILD_NAMES }, AUTO_COMPLETION_ONLINE_OR_OFFLINE, MAX_AUTO_COMPLETION_RESULTS)
    editControlGroup:AddEditControl(self.to, self.autoComplete)
    self.to:SetDefaultText(GetString(SI_REQUEST_NAME_DEFAULT_TEXT))
    self.subject = control:GetNamedChild("SubjectField")
    self.subject:SetMaxInputChars(MAIL_MAX_SUBJECT_CHARACTERS)
    editControlGroup:AddEditControl(self.subject)
    self.subject:SetDefaultText(GetString(SI_MAIL_SUBJECT_DEFAULT_TEXT))
    self.body = control:GetNamedChild("BodyField")
    self.body:SetMaxInputChars(MAIL_MAX_BODY_CHARACTERS)
    editControlGroup:AddEditControl(self.body)
    self.attachMoneyRadioButton = control:GetNamedChild("AttachRadio")
    self.codRadioButton = control:GetNamedChild("CoDRadio")
    self.radioButtonGroup = ZO_RadioButtonGroup:New()
    self.radioButtonGroup:Add(self.attachMoneyRadioButton)
    self.radioButtonGroup:Add(self.codRadioButton)
    self.radioButtonGroup:SetClickedButton(self.attachMoneyRadioButton)
    self.postageCurrency = control:GetNamedChild("PostageCurrency")
    self.sendCurrency = control:GetNamedChild("SendCurrency")
    self.title = control:GetParent():GetNamedChild("Title")
    local function ChangeMoneyCallback(moneyInput, money)
        self:AttachMoney(moneyInput, money)
    end
    self:ClearFields()
    local MoneyEvents =
    {
        [EVENT_MAIL_COD_CHANGED] = function() self:UpdateCOD() end,
        [EVENT_MAIL_ATTACHED_MONEY_CHANGED] = function() self:UpdateMoneyAttachment() end,
        [EVENT_MONEY_UPDATE] = function() self:OnMoneyUpdate() end,
    }
    local function ConnectMoneyEvents()
        for event, callback in pairs(MoneyEvents) do
            control:RegisterForEvent(event, callback)
        end
    end
    local function DisconnectMoneyEvents()
        for event in pairs(MoneyEvents) do
            control:UnregisterForEvent(event)
        end
    end
    local INVENTORY_TYPE_LIST = { INVENTORY_BACKPACK }
    MAIL_SEND_SCENE = ZO_Scene:New("mailSend", SCENE_MANAGER)
    MAIL_SEND_SCENE:RegisterCallback("StateChange", function(oldState, newState)
        if newState == SCENE_SHOWING then
            PLAYER_INVENTORY:SetContextForInventories("mailTextSearch", INVENTORY_TYPE_LIST)
            TEXT_SEARCH_MANAGER:ActivateTextSearch("mailTextSearch")
            ConnectMoneyEvents()
            KEYBIND_STRIP:AddKeybindButtonGroup(self.staticKeybindStripDescriptor)
            ZO_MailSend_Shared.RestorePendingMail(self)
        elseif newState == SCENE_SHOWN then
            if self.pendingMailChanged then
                ZO_Dialogs_ShowDialog("MAIL_ATTACHMENTS_CHANGED")
                self.pendingMailChanged = nil
            end
        elseif newState == SCENE_HIDDEN then
            TEXT_SEARCH_MANAGER:DeactivateTextSearch("mailTextSearch")
            local REMOVE_CONTEXT = nil
            PLAYER_INVENTORY:SetContextForInventories(REMOVE_CONTEXT, INVENTORY_TYPE_LIST)
            KEYBIND_STRIP:RemoveKeybindButtonGroup(self.staticKeybindStripDescriptor)
            ZO_MailSend_Shared.SavePendingMail()
            DisconnectMoneyEvents()
        end
    end)
    control:RegisterForEvent(EVENT_MAIL_SEND_SUCCESS, function() self:OnMailSendSuccess() end)
    control:RegisterForEvent(EVENT_MAIL_ATTACHMENT_ADDED, function(_, attachSlot) self:OnMailAttachmentAdded(attachSlot) end)
    control:RegisterForEvent(EVENT_MAIL_ATTACHMENT_REMOVED, function(_, attachSlot) self:OnMailAttachmentRemoved(attachSlot) end)
end
--Global API
function MailSend:ComposeMailTo(address, subject)
    MAIN_MENU_KEYBOARD:ShowScene("mailSend")
    SCENE_MANAGER:CallWhen("mailSend", SCENE_SHOWN, function() self:SetReply(address, subject) 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: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
    end
end
function MailSend:UpdatePostageMoney()
    local postageAmount = GetQueuedMailPostage()
    ZO_CurrencyControl_SetSimpleCurrency(self.postageCurrency, CURT_MONEY, postageAmount, nil, CURRENCY_SHOW_ALL, postageAmount > GetCurrencyAmount(CURT_MONEY, CURRENCY_LOCATION_CHARACTER))
end
function MailSend:SetSendMoneyMode(sendMoneyMode)
    if self.sendMoneyMode ~= sendMoneyMode then
        self.sendMoneyMode = sendMoneyMode
        ZO_DefaultCurrencyInputField_SetCurrencyAmount(self.sendCurrency, 0)
        ZO_DefaultCurrencyInputField_SetUsePlayerCurrencyAsMax(self.sendCurrency, sendMoneyMode)
        if sendMoneyMode then
        else
            QueueCOD(ZO_DefaultCurrencyInputField_GetCurrency(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, formattedReplySubject)
    self:ClearFields()
    self.to:SetText(to or "")
    self.body:SetText("")
    if formattedReplySubject and formattedReplySubject ~= "" then
        self.subject:SetText(formattedReplySubject)
        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_MailSend_Shared.AddAttachedItem(attachSlot, self.attachmentSlots[attachSlot])
end
function MailSend:OnMailAttachmentRemoved(attachSlot)
    ZO_MailSend_Shared.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
    if MAIL_SEND then
        MAIL_SEND:SetMoneyAttachmentMode()
    end
end
    if MAIL_SEND then
        MAIL_SEND:SetCoDMode()
    end
end
    MAIL_SEND = MailSend:New(self)
end