/
listbox.lua
254 lines (208 loc) · 6.75 KB
/
listbox.lua
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
local Type, ItemType, Version = "ListBox", 'ListBoxItem', 26
local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
local CreateFrame, UIParent = CreateFrame, UIParent
local create_listbox_item
BFWC_ListBoxs = {}
local methods = {
["OnAcquire"] = function(self)
self:superOnAcquire()
self:SetDisabled(false)
self:SetCallback('OnItemSelected',self.OnItemSelected)
local found = false
for _,o in ipairs(BFWC_ListBoxs) do
if o == self then
found = true
end
end
if not found then
table.insert(BFWC_ListBoxs,self)
end
end,
['OnRelease'] = function(self)
for i=#BFWC_ListBoxs,1,-1 do
if BFWC_ListBoxs[i] == self then
table.remove(BFWC_ListBoxs,i)
end
end
end,
['SetDisabled'] = function(self,dis)
end,
['SetValue'] = function(self,val)
end,
--之前的children已经被清掉
['SetList'] = function(self,list)
local v = self.scrollbar:GetValue() or 0
for _,it in ipairs(list) do
local item = AceGUI:Create('ListBoxItem')
item:SetItem(it.text,it,it.id==self.selitemid)
self:realAddChild(item)
end
--TODO:滚到恰当位置,保持被选中元素的显示位置
--value 0~1000
self:SetScroll(v)
end,
['SetLabel'] = function(self,label)
self.name = label
end,
['AddChild'] = function(self,child,beforeWidget)
end,
['OnItemSelected'] = function(self,event,data)
local itemid = data.id
if itemid and itemid == self.selitemid then
self.selitemid = nil
else
self.selitemid = itemid
end
for _,widget in pairs(self.children) do
widget:SetSelected(widget.itemdata.id==self.selitemid)
end
if not self.selitemid then
self:Fire('OnValueChanged',nil)
else
self:Fire('OnValueChanged',data)
end
end,
['LayoutFinished'] = function(self,width,height)
if not self.frame:GetParent() or not self.frame:GetParent():GetParent() then
return
end
local winh = math.floor(self.frame:GetParent():GetParent():GetHeight())
local oldh = math.floor(self.frame:GetHeight())
local newh = winh - 130
if oldh == newh then
return
end
self.frame:SetHeight(newh)
end
}
local function Constructor()
scroll = AceGUI:Create("ScrollFrame")
scroll:SetFullWidth(true)
--scroll:SetHeight(350)
scroll:SetAutoAdjustHeight(true)
scroll:SetLayout('fill')
local tx = scroll.frame:CreateTexture(nil, "BACKGROUND")
--Interface\\MINIMAP\\TooltipBackdrop
tx:SetTexture('Interface\\Tooltips\\CHATBUBBLE-BACKGROUND') -- Interface\\Tooltips\\UI-Tooltip-Background
tx:SetPoint("TOPLEFT", 0, 0)
tx:SetPoint("BOTTOMRIGHT", 0, 0)
scroll.superOnAcquire = scroll.OnAcquire
scroll.type = Type
scroll.scroll = scroll
scroll.realAddChild = scroll.AddChild
scroll:FixScroll()
for method,func in pairs(methods) do
scroll[method] = func
end
return scroll
end
AceGUI:RegisterWidgetType(Type, Constructor, Version)
local item_methods = {
["OnAcquire"] = function(self)
self:SetFullWidth(true)
self.resizing = true
self:SetHeight(36)
self.resizing = nil
end,
["SetText"] = function(self, text)
self.label:SetText(text)
end,
['SetSelected'] = function(self,sel)
if self.selected == sel then
return
end
self.selected = sel
if sel then
self.border:SetBackdropColor(0.3, 0.3, 0.3)
self.border:SetBackdropBorderColor(0.8, 0.8, 0.8)
else
self.border:SetBackdropColor(0.1, 0.1, 0.1)
self.border:SetBackdropBorderColor(0.4, 0.4, 0.4)
end
end,
['SetItem'] = function(self,text,data,hilight)
self.label:SetText(text)
self.msg = text
self.itemdata = data
self:SetSelected(hilight)
end
}
local function getAnchors(frame)
local x, y = frame:GetCenter()
if not x or not y then return "CENTER" end
local hhalf = (x > UIParent:GetWidth()*2/3) and "RIGHT" or (x < UIParent:GetWidth()/3) and "LEFT" or ""
local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM"
return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf
end
local function ShowTooltip(self,show,text)
if not self or not GameTooltip then
return
end
if not show then
GameTooltip:Hide()
return
end
GameTooltip:SetOwner(self.frame, "ANCHOR_NONE")
GameTooltip:SetPoint(getAnchors(self.frame))
GameTooltip:ClearLines()
GameTooltip:AddLine(text,1,1,1,1,true)
GameTooltip:Show()
end
local function onMouseEnter(btn)
if not btn then
return
end
local w = btn:GetWidth()
if not w then
return
end
local label = btn.obj.label
if not label then
return
end
if label:GetStringWidth()>w then
ShowTooltip(btn.obj,true,btn.obj.msg)
end
end
local function onMouseLeave(btn)
ShowTooltip(btn.obj,false)
end
local function OnItemClick(self,button)
self.obj.parent:Fire('OnItemSelected',self.obj.itemdata)
end
local function ConstructorItem()
local frame = CreateFrame("Button", nil, UIParent, BackdropTemplateMixin and "BackdropTemplate")
frame:Hide()
frame:SetScript('OnClick',OnItemClick)
frame:SetScript('OnEnter',onMouseEnter)
frame:SetScript('OnLeave',onMouseLeave)
local itemBackdrop = {
bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 3, right = 3, top = 5, bottom = 3 }
}
local border = CreateFrame("Frame", nil, frame, BackdropTemplateMixin and "BackdropTemplate")
border:SetPoint("TOPLEFT", 0, 0)
border:SetPoint("BOTTOMRIGHT", 0, 0)
border:SetBackdrop(itemBackdrop)
border:SetBackdropColor(0.1, 0.1, 0.1)
border:SetBackdropBorderColor(0.4, 0.4, 0.4)
local label = border:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
label:SetPoint('TOPLEFT',5,-5)
label:SetPoint('BOTTOMRIGHT',-5,5)
label:SetJustifyH("LEFT")
label:SetJustifyV("MIDDLE")
local item = {
label = label,
frame = frame,
border = border,
type = ItemType,
}
for method, func in pairs(item_methods) do
item[method] = func
end
return AceGUI:RegisterAsWidget(item)
end
AceGUI:RegisterWidgetType(ItemType, ConstructorItem, Version)