ESO Lua File v100010

ingame/zo_moneyinput/zo_moneyinput.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
MONEY_INPUT = nil
ZO_MoneyInputObject = ZO_Object:Subclass()
function ZO_MoneyInputObject:New(...)
    local moneyInput = ZO_Object.New(self)
    moneyInput:Initialize(...)
    return moneyInput
end
function ZO_MoneyInputObject:Initialize(control, pulseAnimTemplate)
    self.control = control
    self.goldField = control:GetNamedChild("Amount")
    self.maxGold = nil
    self.mouseRefCount = 0
    self:Reset()
    local bg = control:GetNamedChild("BG")
    self.pulseTimeline = ANIMATION_MANAGER:CreateTimelineFromVirtual(pulseAnimTemplate, control:GetNamedChild("BG"))
    self.badInputTimeline = ANIMATION_MANAGER:CreateTimelineFromVirtual("MoneyInputBadInput", control:GetNamedChild("BG"))
    self.badInputTimeline:SetHandler("OnPlay", function() bg:SetAlpha(1) self.pulseTimeline:Stop() end)
    self.badInputTimeline:SetHandler("OnStop", function() self.pulseTimeline:PlayFromStart(1000) end)
    self.globalMouseDownHandler =   function(eventCode, button, ctrl, alt, shift)
                                        if(button == 1 and not MouseIsOver(self.control)) then
                                            self:DecrementRefCount()
                                        end
                                    end
end
function ZO_MoneyInputObject:Reset()
    self:SetMoney(0, "update")
end
function ZO_MoneyInputObject:SetUsePlayerGoldAsMax(usePlayerGoldAsMax)
    self.usePlayerGoldAsMax = usePlayerGoldAsMax
end
function ZO_MoneyInputObject:IsUsingPlayerGoldAsMax(usePlayerGoldAsMax)
    return self.usePlayerGoldAsMax
end
function ZO_MoneyInputObject:SetMaxGold(maxGold)
    self:SetUsePlayerGoldAsMax(false)
    self.maxGold = maxGold
end
function ZO_MoneyInputObject:GetMaxGold()
    if(self:IsUsingPlayerGoldAsMax()) then
        return GetCurrentMoney()
    elseif(self.maxGold) then
        return self.maxGold
    end
    return MAX_PLAYER_MONEY
end
function ZO_MoneyInputObject:GetTotalMoney()
     return self.gold
end
function ZO_MoneyInputObject:GetTotalMoneyAsText()
    return self.goldField:GetText()
end
function ZO_MoneyInputObject:DoCallback(eventType)
    if(self.callback) then
        self:callback(self.gold, eventType)
    end
end
local MONEY_INPUT_CURRENCY_OPTIONS =
{
    showTooltips = false,
    font = "ZoFontWinT1",
}
function ZO_MoneyInputObject:SetMoney(gold, eventType)
    if self.gold ~= gold then
        self.gold = zo_min(gold, self:GetMaxGold())
        ZO_CurrencyControl_SetSimpleCurrency(self.goldField, CURRENCY_TYPE_MONEY, self.gold, MONEY_INPUT_CURRENCY_OPTIONS, CURRENCY_SHOW_ALL)
    end
    self:DoCallback(eventType or "update")
end
local CONFIRM_ENTRY = -1
local CANCEL_ENTRY = -2
local BACKSPACE_ENTRY = -3
local CLEAR_ENTRY = -4
local keyMapping =
{
    [KEY_0] = 0,
    [KEY_1] = 1,
    [KEY_2] = 2,
    [KEY_3] = 3,
    [KEY_4] = 4,
    [KEY_5] = 5,
    [KEY_6] = 6,
    [KEY_7] = 7,
    [KEY_8] = 8,
    [KEY_9] = 9,
    [KEY_NUMPAD0] = 0,
    [KEY_NUMPAD1] = 1,
    [KEY_NUMPAD2] = 2,
    [KEY_NUMPAD3] = 3,
    [KEY_NUMPAD4] = 4,
    [KEY_NUMPAD5] = 5,
    [KEY_NUMPAD6] = 6,
    [KEY_NUMPAD7] = 7,
    [KEY_NUMPAD8] = 8,
    [KEY_NUMPAD9] = 9,
    [KEY_NUMPAD_ENTER] = CONFIRM_ENTRY,
    [KEY_ENTER] = CONFIRM_ENTRY,
    [KEY_ESCAPE] = CANCEL_ENTRY,
    [KEY_BACKSPACE] = BACKSPACE_ENTRY,
    [KEY_DELETE] = CLEAR_ENTRY,
}
function ZO_MoneyInputObject:OnKeyDown(key, ctrl, alt, shift)
    local keyValue = keyMapping[key]
    if(keyValue ~= nil) then
        local gold = self.gold
        local eventType = "update"
        local hideAfterEvent = false
        if(keyValue >= 0) then
            if(self.resetAmountOnKeyInput) then
                gold = 0
            end
            gold = (gold * 10) + keyValue
            -- Don't allow users to enter more gold than they have, just reset to what the control used to have
            if (self.maxGold and self.maxGold < gold) or (self:IsUsingPlayerGoldAsMax() and GetCurrentMoney() < gold) then
                self.badInputTimeline:PlayFromStart()
                gold = self.gold
            end
        else
            if(keyValue == CONFIRM_ENTRY) then
                eventType = "confirm"
                hideAfterEvent = true
            elseif(keyValue == CANCEL_ENTRY) then
                eventType = "cancel"
                hideAfterEvent = true
            elseif(keyValue == BACKSPACE_ENTRY) then
                if gold == 0 then
                    self.badInputTimeline:PlayFromStart()
                else
                    gold = zo_floor(gold / 10)
                end
            elseif(keyValue == CLEAR_ENTRY) then
                gold = 0 -- just clear and update, do not cancel
            end            
        end
        -- No matter what, at this point the user has typed something, go ahead and prevent future inputs
        -- from resetting anything
        self.resetAmountOnKeyInput = false
        self:SetMoney(gold, eventType)
        if(hideAfterEvent) then
            self:Hide()
        end
    else
        self.badInputTimeline:PlayFromStart()
    end
end
function ZO_MoneyInputObject:Show(callback, playerGoldAsMaxOrMaxGold, initialGold, anchorTo, offsetX, offsetY)
    self.mouseRefCount = 1 -- assume it's always shown because of mouse input for now
    self.resetAmountOnKeyInput = true
    local control = self.control
    if(control:IsHidden() == false) then return end
    control:SetHidden(false)
    control:RegisterForEvent(EVENT_GLOBAL_MOUSE_DOWN, self.globalMouseDownHandler)
    if(anchorTo) then
        control:ClearAnchors()
        control:SetAnchorFill(anchorTo)
        local inputField = self.goldField
        inputField:ClearAnchors()
        inputField:SetAnchor(LEFT)
        inputField:SetAnchor(RIGHT, nil, RIGHT, offsetX, offsetY)
    end
    self.callback = callback
    if(type(playerGoldAsMaxOrMaxGold) == "number") then
        self:SetMaxGold(playerGoldAsMaxOrMaxGold)
    else
        self:SetUsePlayerGoldAsMax(playerGoldAsMaxOrMaxGold)
    end
    self:SetMoney(initialGold or self:GetTotalMoney(), "update")
    self.pulseTimeline:PlayFromStart()
    if not self.isHoldingDisabledReference then
        self.isHoldingDisabledReference = true
    end
end
function ZO_MoneyInputObject:SetContext(context)
    self.context = context
end
function ZO_MoneyInputObject:GetContext()
    return self.context
end
function ZO_MoneyInputObject:Hide()
    self.callback = nil
    self.control:SetHidden(true)
    self.control:UnregisterForEvent(EVENT_GLOBAL_MOUSE_DOWN)
    self:SetMaxGold(nil)
    self:SetContext(nil)
    self:Reset() -- make sure callback is cleared before this, otherwise it gets called with an "update" event.
    self.mouseRefCount = 0
    self.pulseTimeline:Stop()
    if self.isHoldingDisabledReference then
        self.isHoldingDisabledReference = false
    end
end
function ZO_MoneyInputObject:Confirm()
    self:DoCallback("confirm")
    self:Hide()
end
function ZO_MoneyInputObject:Cancel()
    self:DoCallback("cancel")
    self:Hide()
end
function ZO_MoneyInputObject:DecrementRefCount()
    self.mouseRefCount = self.mouseRefCount - 1
    if(self.mouseRefCount <= 0) then
        self:Confirm()
    end
end
--Default Money Input Field
local function ClampSetMoney(self, money)
    local max = money
    local min = self.goldMin or 0
    if(self.goldMax) then
        max = self.goldMax
    elseif(self.usePlayerGoldAsMax) then
        max = GetCurrentMoney()
    end
    return zo_clamp(money, min, max)
end
    self.currencyControl = GetControl(self, "Amount")
    self.usePlayerGoldAsMax = false
    self.currentMoney = 0
    ZO_CurrencyControl_SetSimpleCurrency(self.currencyControl, CURRENCY_TYPE_MONEY, 0)
    self.OnMoneyChanged = function(moneyInput, money, eventType)
        money = ClampSetMoney(self, money)
        if(eventType == "confirm") then
            if onMoneyChanged then
                onMoneyChanged(moneyInput, money)
            end
            self.currentMoney = money
            self.currencyControl:SetHidden(false)
        elseif(eventType == "cancel") then
            self.currencyControl:SetHidden(false)
        end
        ZO_CurrencyControl_SetSimpleCurrency(self.currencyControl, CURRENCY_TYPE_MONEY, money)
    end
    self:SetHandler("OnEffectivelyHidden", function(control)
        MONEY_INPUT:Hide()
    end)
    self.OnBeginInput = function()
        self.currencyControl:SetHidden(true)
        MONEY_INPUT:Show(self.OnMoneyChanged, self.goldMax or self.usePlayerGoldAsMax, self.currentMoney or 0, self, 18)
    end
end
local function RefreshMoneyForPlayerLimit(self)
    local playerMoney = GetCurrentMoney()
    if(self.currentMoney > playerMoney) then
        ZO_DefaultMoneyInputField_SetMoney(self, playerMoney)
    end
end
function ZO_DefaultMoneyInputField_SetUsePlayerGoldAsMax(self, usePlayerGoldAsMax)
    if(self.usePlayerGoldAsMax ~= usePlayerGoldAsMax) then
        self.usePlayerGoldAsMax = usePlayerGoldAsMax
        if(self.usePlayerGoldAsMax) then
            self.goldMax = nil
            RefreshMoneyForPlayerLimit(self)
            if(self.onMoneyUpdate == nil) then
                self.onMoneyUpdate = function() RefreshMoneyForPlayerLimit(self) end
            end
            self:RegisterForEvent(EVENT_MONEY_UPDATE, self.onMoneyUpdate)
        else
            if(self.onMoneyUpdate) then
                self:UnregisterForEvent(EVENT_MONEY_UPDATE, self.onMoneyUpdate)
            end
        end
    end
end
    self.goldMax = goldMax
end
    self.goldMin = goldMin
end
    money = ClampSetMoney(self, money)
    if money ~= self.currentMoney then
        self.OnMoneyChanged(nil, money, "confirm")
    end
end
    return self.currentMoney
end