Back to Home

ESO Lua File v101044

libraries/zo_editboxes/zo_savingeditbox.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
ZO_SavingEditBox = ZO_CallbackObject:Subclass()
function ZO_SavingEditBox:New(...)
    local object = ZO_CallbackObject.New(self)
    object:Initialize(...)
    return object
end
function ZO_SavingEditBox:Initialize(control)
    self.control = control
    self.editBackdrop = control:GetNamedChild("Saving")
    self.edit = control:GetNamedChild("SavingEdit")
    self.edit:SetHandler("OnTextChanged", function() self:OnTextChanged() end)
    self.edit:SetHandler("OnEnter", function() self:OnEnter() end)
    self.modifyButton = control:GetNamedChild("Modify")
    self.modifyButton:SetHandler("OnClicked", function() self:OnModifyClicked() end)
    self.saveButton = control:GetNamedChild("Save")
    self.saveButton:SetHandler("OnClicked", function() self:OnSaveClicked() end)
    self.cancelButton = control:GetNamedChild("Cancel")
    self.cancelButton:SetHandler("OnClicked", function() self:OnCancelClicked() end)    
    self.display = control:GetNamedChild("Display")
    self.empty = control:GetNamedChild("Empty")
    self.modified = false
    self.enabled = true
    self.editing = false
    self.putTextInQuotes = true
    self.shouldEscapeMarkup = false
    self:RefreshButtons()
end
function ZO_SavingEditBox:SetDefaultText(defaultText)
    self.edit:SetDefaultText(defaultText)
end
function ZO_SavingEditBox:SetEmptyText(emptyText)
    self.empty:SetText(emptyText)
end
function ZO_SavingEditBox:SetEditing(editing, forceUpdate)
    if forceUpdate or self.editing ~= editing then
        self.editing = editing
        self.editBackdrop:SetHidden(not editing)
        self:RefreshButtons()
        if editing then
            self.edit:TakeFocus()
            self.display:SetHidden(true)
            self.empty:SetHidden(true)
        else
            self.edit:LoseFocus()
            if self.resetText == "" then
                self.empty:SetHidden(false)
            else
                self.display:SetHidden(false)
            end
        end
        self:FireCallbacks("SetEditing", self, editing)
    end
end
function ZO_SavingEditBox:IsEditing()
    return self.editing
end
function ZO_SavingEditBox:SetShouldEscapeNonColorMarkup(shouldEscapeMarkup)
    self.shouldEscapeMarkup = shouldEscapeMarkup
end
function ZO_SavingEditBox:SetEnabled(enabled)
    if enabled ~= self.enabled then
        self.enabled = enabled
        if not self.enabled then
            self:SetEditing(false)
            self:ResetText()
        end
        self:RefreshButtons()
    end
end
function ZO_SavingEditBox:SetHidden(hidden)
    if hidden then
        self:Cancel()
        self.modifyButton:SetHidden(true)
    else
        self.modifyButton:SetHidden(false)
    end
end
function ZO_SavingEditBox:SetCustomTextValidator(validator)
    self.validator = validator
end
function ZO_SavingEditBox:SetPutTextInQuotes(putTextInQuotes)
    self.putTextInQuotes = putTextInQuotes
end
function ZO_SavingEditBox:GetText()
    return self.edit:GetText()
end
function ZO_SavingEditBox:SetText(text, dontSetResetText)
    if not self.editing then
        if text == "" then
            self.display:SetHidden(true)
            self.empty:SetHidden(false)
        else
            self.empty:SetHidden(true)
            self.display:SetHidden(false)
            local displayText = self.shouldEscapeMarkup and EscapeMarkup(text, ALLOW_MARKUP_TYPE_COLOR_ONLY) or text
            if self.putTextInQuotes then
                self.display:SetText(zo_strformat(SI_SAVING_EDIT_BOX_QUOTES, displayText))
            else
                self.display:SetText(displayText)
            end
        end
    end
    self.edit:SetText(text)
    if not dontSetResetText then
        self.resetText = text
        self.modified = false
    end
    self:RefreshButtons()
end
function ZO_SavingEditBox:GetControl()
    return self.control
end
function ZO_SavingEditBox:GetEditControl()
    return self.edit
end
function ZO_SavingEditBox:ResetText()
    self.edit:SetText(self.resetText)
    self.modified = false
    self:RefreshButtons()
end
function ZO_SavingEditBox:OnTextChanged()
    if not self.modified or self.validator then
        if self.validator then
            local text = self.edit:GetText()
            self.validText = self.validator(text)
        else
            self.validText = true
        end
        self.modified = true
        self:RefreshButtons()
    end
end
function ZO_SavingEditBox:OnEnter()
    if not self.edit:IsMultiLine() then
        if self.validText then
            self:OnSaveClicked()
        else
            self:Cancel()
        end
    end
end
function ZO_SavingEditBox:OnSaveClicked()
    local newText = self:GetText()
    self:FireCallbacks("Save", newText)
    self:SetEditing(false)
    self:SetText(newText)
end
function ZO_SavingEditBox:Cancel()
    if self.editing then
        self:SetEditing(false)
        self:ResetText()
    end
end
function ZO_SavingEditBox:OnCancelClicked()
    self:Cancel()
end
function ZO_SavingEditBox:OnModifyClicked()
    self:SetEditing(true)
end
function ZO_SavingEditBox:RefreshButtons()
    if self.enabled then
        if self.editing then
            self.modifyButton:SetHidden(true)
            self.saveButton:SetHidden(false)
            self.cancelButton:SetHidden(false)
            if self.modified and self.validText then
                self.saveButton:SetState(BSTATE_NORMAL, false)
            else
                self.saveButton:SetState(BSTATE_DISABLED, true)
            end
            if self.validText then
                self.edit:SetColor(GetInterfaceColor(INTERFACE_COLOR_TYPE_TEXT_COLORS, INTERFACE_TEXT_COLOR_SELECTED))
            else
                self.edit:SetColor(GetInterfaceColor(INTERFACE_COLOR_TYPE_TEXT_COLORS, INTERFACE_TEXT_COLOR_FAILED))
            end
        else
            self.modifyButton:SetHidden(false)
            self.modifyButton:SetState(BSTATE_NORMAL, false)
            self.saveButton:SetHidden(true)
            self.cancelButton:SetHidden(true)
        end
    else
        self.modifyButton:SetHidden(false)
        self.modifyButton:SetState(BSTATE_DISABLED, true)
        self.saveButton:SetHidden(true)
        self.cancelButton:SetHidden(true)
    end
end
--Saving Edit Box Group
ZO_SavingEditBoxGroup = ZO_Object:Subclass()
function ZO_SavingEditBoxGroup:New()
    local group = ZO_Object.New(self)
    group.savingEditBoxes = {}
    group.setEditingCallback =  function(savingEditBox, isEditing)
                                    if isEditing then
                                        for i = 1, #group.savingEditBoxes do
                                            if group.savingEditBoxes[i] ~= savingEditBox then
                                                group.savingEditBoxes[i]:Cancel()
                                            end
                                        end
                                    end
                                end
    return group
end
function ZO_SavingEditBoxGroup:Add(savingEditBox)
    table.insert(self.savingEditBoxes, savingEditBox)
    savingEditBox:RegisterCallback("SetEditing", self.setEditingCallback)
end
-- Scrolling Saving Edit Box
ZO_ScrollingSavingEditBox = ZO_SavingEditBox:Subclass()
function ZO_ScrollingSavingEditBox:New(...)
    return ZO_SavingEditBox.New(self, ...)
end
function ZO_ScrollingSavingEditBox:Initialize(control)
    ZO_SavingEditBox.Initialize(self, control)
    self.pane = control:GetNamedChild("Pane")
    self.paneScroll = self.pane:GetNamedChild("Scroll")
    self.display:ClearAnchors()
    self.display:SetParent(self.pane:GetNamedChild("ScrollChild"))
    self.display:SetAnchor(TOPLEFT)
    self.display:SetWidth(self.control:GetWidth() - 34)
end
function ZO_ScrollingSavingEditBox:SetEditing(editing, forceUpdate)
    ZO_SavingEditBox.SetEditing(self, editing, forceUpdate)
    self.paneScroll:SetMouseEnabled(editing)
end