Back to Home

ESO Lua File v100019

libraries/zo_savingeditbox/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
ZO_SavingEditBox = ZO_CallbackObject:Subclass()
function ZO_SavingEditBox:New(control)
    local manager = ZO_CallbackObject.New(self)
    manager.control = control
    manager.editBackdrop = GetControl(control, "Saving")
    manager.edit = GetControl(control, "SavingEdit")
    manager.edit:SetHandler("OnTextChanged", function() manager:OnTextChanged() end)
    manager.edit:SetHandler("OnEnter", function() manager:OnEnter() end)
    manager.modifyButton = GetControl(control, "Modify")
    manager.modifyButton:SetHandler("OnClicked", function() manager:OnModifyClicked() end)
    manager.saveButton = GetControl(control, "Save")
    manager.saveButton:SetHandler("OnClicked", function() manager:OnSaveClicked() end)
    manager.cancelButton = GetControl(control, "Cancel")
    manager.cancelButton:SetHandler("OnClicked", function() manager:OnCancelClicked() end)    
    manager.display = GetControl(control, "Display")
    manager.empty = GetControl(control, "Empty")
    manager.modified = false
    manager.enabled = true
    manager.editing = false
    manager.putTextInQuotes = true
    manager:RefreshButtons()
    return manager
end
function ZO_SavingEditBox:SetDefaultText(defaultText)
    ZO_EditDefaultText_Initialize(self.edit, defaultText)
end
function ZO_SavingEditBox:SetEmptyText(emptyText)
    self.empty:SetText(emptyText)
end
function ZO_SavingEditBox:SetEditing(editing)
    if(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: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)
    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)
            if self.putTextInQuotes then
                self.display:SetText(zo_strformat(SI_SAVING_EDIT_BOX_QUOTES, text))
            else
                self.display:SetText(text)
            end
        end
    end
    self.edit:SetText(text)
    self.resetText = text
    self.modified = false
    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