Back to Home

ESO Lua File v101042

libraries/zo_dialog/zo_listdialog.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
-----------------
-- ZO_ListDialog
-----------------
ZO_ListDialog = ZO_InitializingObject:Subclass()
LIST_DIALOG_CUSTOM_CONTROL_LOCATION_TOP = 1
LIST_DIALOG_CUSTOM_CONTROL_LOCATION_BOTTOM = 2
local SCROLL_TYPE_ITEM = 1
local listDialogId = 1
function ZO_ListDialog:Initialize(listTemplate, listItemHeight, listSetupFunction)
    self.control = CreateControlFromVirtual("ZO_ListDialog", GuiRoot, "ZO_ListDialogTemplate", listDialogId)
    self.control.owner = self
    listDialogId = listDialogId + 1
    
    self.aboveText = self.control:GetNamedChild("AboveText")
    self.belowText = self.control:GetNamedChild("BelowText")
    self.firstButton = self.control:GetNamedChild("Button1")
    self.secondButton = self.control:GetNamedChild("Button2")
    self.list = self.control:GetNamedChild("List")
    self.emptyListText = self.control:GetNamedChild("EmptyListText")
    self.topCustomControlContainer = self.control:GetNamedChild("TopCustomControlContainer")
    self.bottomCustomControlContainer = self.control:GetNamedChild("BottomCustomControlContainer")
    self.customControls = {}
    self:SetupList(listTemplate, listItemHeight, listSetupFunction)
end
function ZO_ListDialog:SetupList(listTemplate, listItemHeight, listSetupFunction)
    local function OnMouseUp(rowControl, button, upInside)
        if upInside then
            local data = ZO_ScrollList_GetData(rowControl)
            ZO_ScrollList_SelectData(self.list, data, rowControl)
        end
    end
    local function Setup(rowControl, ...)
        rowControl:SetHandler("OnMouseUp", OnMouseUp)
        listSetupFunction(rowControl, ...)
    end
    ZO_ScrollList_AddDataType(self.list, SCROLL_TYPE_ITEM, listTemplate, listItemHeight, Setup)
    self.listItemHeight = listItemHeight
    self.minVisibleItems = zo_max(zo_floor(220 / listItemHeight), 2)
    self.maxVisibleItems = zo_max(zo_floor(300 / listItemHeight), self.minVisibleItems)
    local function OnListSelection(previouslySelected, selected)
        self.selectedItem = selected
        if self.onSelectedCallback then
            self.onSelectedCallback(selected)
        end
    end
end
function ZO_ListDialog:SetAboveText(text)
    self.aboveText:SetText(text)
end
function ZO_ListDialog:SetBelowText(text)
    self.belowText:SetText(text)
end
function ZO_ListDialog:GetButton(index)
    if index == 1 then
        return self.firstButton
    elseif index == 2 then
        return self.secondButton
    end
end
function ZO_ListDialog:SetFirstButtonEnabled(enabled)
    self.firstButton:SetEnabled(enabled)
end
function ZO_ListDialog:SetSecondButtonEnabled(enabled)
    self.secondButton:SetEnabled(enabled)
end
function ZO_ListDialog:SetEmptyListText(text)
    self.emptyListText:SetText(text)
end
function ZO_ListDialog:ClearList()
end
function ZO_ListDialog:CommitList(sortFunction)
    local scrollData = ZO_ScrollList_GetDataList(self.list)
    local numListItems = #scrollData
    self.list:SetHeight(self.listItemHeight * zo_clamp(numListItems, self.minVisibleItems, self.maxVisibleItems))
    if sortFunction then
        table.sort(scrollData, sortFunction)
    end
    self.emptyListText:SetHidden(numListItems > 0)
end
function ZO_ListDialog:AddListItem(itemData)
    local scrollData = ZO_ScrollList_GetDataList(self.list)
    scrollData[#scrollData + 1] = ZO_ScrollList_CreateDataEntry(SCROLL_TYPE_ITEM, itemData)
end
function ZO_ListDialog:AddCustomControl(control, location)
    if self.customControls[location] then
        self.customControls[location]:ClearAnchors()
    end
    local container = self:GetCustomContainerFromLocation(location)
    control:SetAnchor(TOP, container, TOP)
    control:SetParent(container)
    self.customControls[location] = control
end
function ZO_ListDialog:GetCustomContainerFromLocation(location)
    if location == LIST_DIALOG_CUSTOM_CONTROL_LOCATION_TOP then
        return self.topCustomControlContainer
    elseif location == LIST_DIALOG_CUSTOM_CONTROL_LOCATION_BOTTOM then
        return self.bottomCustomControlContainer
    end
end
function ZO_ListDialog:GetSelectedItem()
    return self.selectedItem
end
function ZO_ListDialog:SetOnSelectedCallback(selectedCallback)
end
function ZO_ListDialog:SetHidden(hidden)
    self.control:SetHidden(hidden)
end
function ZO_ListDialog:GetControl()
    return self.control
end
local function ClearCustomControl(control)
    if control then
        control:ClearAnchors()
        control:SetParent(nil)
        control:SetHidden(true)
    end
end
function ZO_ListDialog:OnHide()
    self.aboveText:SetText("")
    self.belowText:SetText("")
    self.firstButton:SetEnabled(true)
    self.secondButton:SetEnabled(true)
    self.emptyListText:SetText("")
    self.selectedItem = nil
    self.onSelectedCallback = nil
    self.customControls[LIST_DIALOG_CUSTOM_CONTROL_LOCATION_TOP] = ClearCustomControl(self.customControls[LIST_DIALOG_CUSTOM_CONTROL_LOCATION_TOP])
    self.customControls[LIST_DIALOG_CUSTOM_CONTROL_LOCATION_BOTTOM] = ClearCustomControl(self.customControls[LIST_DIALOG_CUSTOM_CONTROL_LOCATION_BOTTOM])
    self:ClearList()
end
----------------------------
-- ZO_MultiSelectListDialog
----------------------------
ZO_MultiSelectListDialog = ZO_ListDialog:Subclass()
function ZO_MultiSelectListDialog:Initialize(listTemplate, listItemHeight, listSetupFunction)
    ZO_ListDialog.Initialize(self, listTemplate, listItemHeight, listSetupFunction)
    self.selectedItems = {}
end
function ZO_MultiSelectListDialog:SetupList(listTemplate, listItemHeight, listSetupFunction)
    local function OnMouseUp(rowControl, button, upInside)
        if upInside then
            local data = ZO_ScrollList_GetData(rowControl)
            self:SelectItem(data)
        end
    end
    local function Setup(rowControl, ...)
        rowControl:SetHandler("OnMouseUp", OnMouseUp)
        listSetupFunction(rowControl, ...)
    end
    ZO_ScrollList_AddDataType(self.list, SCROLL_TYPE_ITEM, listTemplate, listItemHeight, Setup)
    self.listItemHeight = listItemHeight
    self.minVisibleItems = zo_max(zo_floor(220 / listItemHeight), 2)
    self.maxVisibleItems = zo_max(zo_floor(300 / listItemHeight), self.minVisibleItems)
end
function ZO_MultiSelectListDialog:SelectItem(data)
    local newSelectionStatus = not self:IsItemSelected(data)
    if newSelectionStatus then
        self:AddItemToSelected(data)
    else
        self:RemoveItemFromSelected(data)
    end
    if self.onSelectedCallback then
        self.onSelectedCallback(data)
    end
    -- Refresh the data that was just selected so the selection highlight properly shows/hides
end
function ZO_MultiSelectListDialog:AddItemToSelected(data)
    table.insert(self.selectedItems, data)
end
function ZO_MultiSelectListDialog:RemoveItemFromSelected(data)
end
function ZO_MultiSelectListDialog:IsItemSelected(data)
    return ZO_IsElementInNumericallyIndexedTable(self.selectedItems, data)
end
function ZO_MultiSelectListDialog:SelectAll()
    ZO_ClearNumericallyIndexedTable(self.selectedItems)
    local scrollData = ZO_ScrollList_GetDataList(self.list)
    for i, dataEntry in ipairs(scrollData) do
        local data = dataEntry.data
        self:AddItemToSelected(data)
    end
end
function ZO_MultiSelectListDialog:GetSelectedItems()
    return self.selectedItems
end
function ZO_MultiSelectListDialog:OnHide()
    ZO_ListDialog.OnHide(self)
    ZO_ClearNumericallyIndexedTable(self.selectedItems)
end
function ZO_ListDialog_OnHide(dialog)
    dialog.owner:OnHide()
end