annotate SelectorList.lua @ 53:708d8a423b4c

Scroller functional
author John@Yosemite-PC
date Sat, 24 Mar 2012 14:22:01 -0400
parents 7c7e80f63b51
children c8c17286af95
rev   line source
John@52 1 -- no bsk namespace here - we're hooking into AceGUI
John@52 2
John@52 3 local Type, Version = "SelectorList", 34
John@52 4 local AceGUI = LibStub and LibStub("AceGUI-3.0",true)
John@52 5 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
John@52 6
John@52 7 local next, pairs, ipairs, assert, type = next, pairs, ipairs, assert, type
John@52 8 local math_min, math_max, floor = math.min, math.max, floor
John@52 9 local select, tremove, unpack, tconcat = select, table.remove, unpack, table.concat
John@52 10
John@52 11 local CreateFrame, UIParent = CreateFrame, UIParent
John@52 12
John@52 13 local new, del
John@52 14 do
John@52 15 local pool = setmetatable({},{__mode='k'})
John@52 16 function new()
John@52 17 local t = next(pool)
John@52 18 if t then
John@52 19 pool[t] = nil
John@52 20 return t
John@52 21 else
John@52 22 return {}
John@52 23 end
John@52 24 end
John@52 25 function del(t)
John@52 26 for k in pairs(t) do
John@52 27 t[k] = nil
John@52 28 end
John@52 29 pool[t] = true
John@52 30 end
John@52 31 end
John@52 32
John@52 33 local DEFAULT_SL_WIDTH = 175
John@52 34 --local DEFAULT_TREE_SIZABLE = true
John@52 35
John@53 36 local function SL_OnMouseWheel(frame, delta)
John@53 37 local self = frame.obj
John@53 38 if self.showscroll then
John@53 39 local scrollbar = self.scrollbar
John@53 40 local min, max = scrollbar:GetMinMaxValues()
John@53 41 local value = scrollbar:GetValue()
John@53 42 local newvalue = math_min(max,math_max(min,value - delta))
John@53 43 if value ~= newvalue then
John@53 44 scrollbar:SetValue(newvalue)
John@53 45 end
John@53 46 end
John@53 47 end
John@53 48
John@53 49 local function OnScrollValueChanged(frame, value)
John@53 50 if frame.obj.noupdate then return end
John@53 51 local self = frame.obj
John@53 52 local status = self.status or self.localstatus
John@53 53 status.scrollvalue = value
John@53 54 self:Refresh()
John@53 55 AceGUI:ClearFocus()
John@53 56 end
John@53 57
John@53 58
John@52 59
John@52 60 local function Layout(self)
John@52 61 self:SetHeight(self.numlines * 18 + 20)
John@52 62
John@52 63 end
John@52 64
John@52 65 local function CreateButton(widget)
John@52 66 local num = AceGUI:GetNextWidgetNum("TreeGroupButton")
John@52 67 local button = CreateFrame("Button", ("AceGUI30TreeButton%d"):format(num), widget.slframe, "OptionsListButtonTemplate")
John@52 68 button.obj = widget
John@52 69
John@52 70 local icon = button:CreateTexture(nil, "OVERLAY")
John@52 71 icon:SetWidth(14)
John@52 72 icon:SetHeight(14)
John@52 73 button.icon = icon
John@52 74
John@52 75 button:SetScript("OnClick",Button_OnClick)
John@52 76 button:SetScript("OnDoubleClick", Button_OnDoubleClick)
John@52 77 button:SetScript("OnEnter",Button_OnEnter)
John@52 78 button:SetScript("OnLeave",Button_OnLeave)
John@52 79
John@52 80 return button
John@52 81 end
John@52 82
John@52 83 local function UpdateButton(button, line, selected)
John@52 84 local self = button.obj
John@52 85 local frame = self.frame
John@52 86 local text = line.text or ""
John@52 87 local icon = line.icon
John@52 88 local iconCoords = line.iconCoords
John@52 89 local level = line.level or 1
John@52 90 local value = line.value
John@52 91 local uniquevalue = line.uniquevalue
John@52 92 local disabled = line.disabled
John@52 93
John@52 94 button.line = line
John@52 95 button.value = value
John@52 96 button.uniquevalue = uniquevalue
John@52 97 if selected then
John@52 98 button:LockHighlight()
John@52 99 button.selected = true
John@52 100 else
John@52 101 button:UnlockHighlight()
John@52 102 button.selected = false
John@52 103 end
John@52 104 local normalTexture = button:GetNormalTexture()
John@52 105 local line = button.line
John@52 106 button.level = level
John@52 107 if ( level == 1 ) then
John@52 108 button:SetNormalFontObject("GameFontNormal")
John@52 109 button:SetHighlightFontObject("GameFontHighlight")
John@52 110 button.text:SetPoint("LEFT", (icon and 16 or 0) + 8, 2)
John@52 111 else
John@52 112 button:SetNormalFontObject("GameFontHighlightSmall")
John@52 113 button:SetHighlightFontObject("GameFontHighlightSmall")
John@52 114 button.text:SetPoint("LEFT", (icon and 16 or 0) + 8 * level, 2)
John@52 115 end
John@52 116
John@52 117 if disabled then
John@52 118 button:EnableMouse(false)
John@52 119 button.text:SetText("|cff808080"..text..FONT_COLOR_CODE_CLOSE)
John@52 120 else
John@52 121 button.text:SetText(text)
John@52 122 button:EnableMouse(true)
John@52 123 end
John@52 124
John@52 125 if icon then
John@52 126 button.icon:SetTexture(icon)
John@52 127 button.icon:SetPoint("LEFT", 8 * level, (level == 1) and 0 or 1)
John@52 128 else
John@52 129 button.icon:SetTexture(nil)
John@52 130 end
John@52 131
John@52 132 if iconCoords then
John@52 133 button.icon:SetTexCoord(unpack(iconCoords))
John@52 134 else
John@52 135 button.icon:SetTexCoord(0, 1, 0, 1)
John@52 136 end
John@52 137 end
John@52 138
John@52 139
John@53 140
John@52 141 local methods = {
John@52 142
John@52 143 ["OnAcquire"] = function(self)
John@52 144 self:SetWidth(DEFAULT_SL_WIDTH)
John@52 145 self:SetNumLines()
John@52 146 --self:EnableButtonTooltips(true)
John@52 147 end,
John@52 148 --["SetWidth"] = function(self, width)
John@52 149 -- self.slframe:SetWidth(width)
John@52 150
John@52 151 -- local status = self.status or self.localstatus
John@52 152 -- status.width = width
John@52 153 -- status.treesizable = resizable
John@52 154
John@52 155 --end,
John@52 156 ["SetNumLines"] = function(self,value)
John@52 157 if not value or value < 4 then
John@52 158 value = 4
John@52 159 end
John@52 160 self.numlines = value
John@52 161 Layout(self)
John@52 162 end,
John@52 163 ["SetList"] = function(self, list)
John@53 164 self.lines = {}
John@52 165 for i,v in ipairs(list) do
John@52 166 self.lines[i] = v
John@52 167 end
John@52 168 self:Refresh()
John@52 169 end,
John@53 170
John@53 171 ["ShowScroll"] = function(self, show)
John@53 172 self.showscroll = show
John@53 173 if show then
John@53 174 self.scrollbar:Show()
John@53 175 if self.buttons[1] then
John@53 176 self.buttons[1]:SetPoint("TOPRIGHT", self.slframe,"TOPRIGHT",-22,-10)
John@53 177 end
John@53 178 else
John@53 179 self.scrollbar:Hide()
John@53 180 if self.buttons[1] then
John@53 181 self.buttons[1]:SetPoint("TOPRIGHT", self.slframe,"TOPRIGHT",0,-10)
John@53 182 end
John@53 183 end
John@53 184 end,
John@53 185
John@52 186 ["Refresh"] = function(self)
John@52 187 local f = self.slframe
John@52 188 local buttons = self.buttons
John@52 189 local lines = self.lines
John@53 190 local status = self.status or self.localstatus
John@53 191
John@53 192 for i, v in ipairs(buttons) do
John@53 193 v:Hide()
John@53 194 end
John@53 195 local numlines = #lines
John@53 196 local maxlines = self.numlines
John@53 197
John@53 198 if numlines <= maxlines then
John@53 199 status.scrollvalue = 0
John@53 200 self:ShowScroll(false)
John@53 201 first, last = 1, numlines
John@53 202 else
John@53 203 self:ShowScroll(true)
John@53 204 self.noupdate = true
John@53 205 self.scrollbar:SetMinMaxValues(0, numlines - maxlines)
John@53 206 if numlines - status.scrollvalue < maxlines then
John@53 207 status.scrollvalue = numlines - maxlines
John@53 208 end
John@53 209 self.noupdate = nil
John@53 210 first, last = status.scrollvalue+1, status.scrollvalue + maxlines
John@53 211
John@53 212 if self.scrollbar:GetValue() ~= status.scrollvalue then
John@53 213 self.scrollbar:SetValue(status.scrollvalue)
John@53 214 end
John@53 215 end
John@52 216
John@52 217 local bnum = 1
John@53 218 for i= first, last do
John@52 219 local l = lines[i]
John@52 220 local b = buttons[bnum]
John@52 221 if not b then
John@52 222 b = CreateButton(self)
John@52 223 buttons[bnum] = b
John@52 224 b:SetParent(f)
John@52 225 b:SetFrameLevel(f:GetFrameLevel()+1)
John@52 226 b:ClearAllPoints()
John@52 227 if bnum == 1 then
John@53 228 if self.showscroll then
John@53 229 b:SetPoint("TOPRIGHT",-22,-10)
John@53 230 else
John@53 231 b:SetPoint("TOPRIGHT",0,-10)
John@53 232 end
John@52 233 b:SetPoint("TOPLEFT",0,-10)
John@52 234 else
John@52 235 b:SetPoint("TOPRIGHT", buttons[bnum-1], "BOTTOMRIGHT", 0, 0)
John@52 236 b:SetPoint("TOPLEFT", buttons[bnum-1], "BOTTOMLEFT", 0, 0)
John@52 237 end
John@52 238 end
John@52 239 UpdateButton(b, l)
John@52 240 b:Show()
John@52 241 bnum = bnum + 1
John@52 242 end
John@52 243 end,
John@52 244 }
John@52 245
John@52 246
John@52 247
John@52 248
John@52 249
John@52 250
John@52 251
John@52 252
John@52 253
John@52 254 local PaneBackdrop = {
John@52 255 bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
John@52 256 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
John@52 257 tile = true, tileSize = 16, edgeSize = 16,
John@52 258 insets = { left = 3, right = 3, top = 5, bottom = 3 }
John@52 259 }
John@52 260
John@52 261 local function Constructor()
John@52 262 local num = AceGUI:GetNextWidgetNum(Type)
John@52 263 local frame = CreateFrame("Frame", nil, UIParent)
John@52 264
John@52 265 local slframe = CreateFrame("Frame", nil, frame)
John@52 266 slframe:SetAllPoints()
John@52 267 slframe:EnableMouseWheel(true)
John@52 268 slframe:SetBackdrop(PaneBackdrop)
John@52 269 slframe:SetBackdropColor(0.1, 0.1, 0.1, 0.5)
John@52 270 slframe:SetBackdropBorderColor(0.4, 0.4, 0.4)
John@52 271 --slframe:SetResizable(true)
John@52 272 --slframe:SetMinResize(100, 1)
John@52 273 --slframe:SetMaxResize(400, 1600)
John@52 274 --slframe:SetScript("OnUpdate", FirstFrameUpdate)
John@52 275 --slframe:SetScript("OnSizeChanged", Tree_OnSizeChanged)
John@53 276 slframe:SetScript("OnMouseWheel", SL_OnMouseWheel)
John@52 277
John@53 278 local scrollbar = CreateFrame("Slider", ("SelectorList%dScrollBar"):format(num), slframe, "UIPanelScrollBarTemplate")
John@53 279 scrollbar:SetScript("OnValueChanged", nil)
John@53 280 scrollbar:SetPoint("TOPRIGHT", -10, -26)
John@53 281 scrollbar:SetPoint("BOTTOMRIGHT", -10, 26)
John@53 282 scrollbar:SetMinMaxValues(0,0)
John@53 283 scrollbar:SetValueStep(1)
John@53 284 scrollbar:SetValue(0)
John@53 285 scrollbar:SetWidth(16)
John@53 286 scrollbar:SetScript("OnValueChanged", OnScrollValueChanged)
John@52 287
John@53 288 local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND")
John@53 289 scrollbg:SetAllPoints(scrollbar)
John@53 290 scrollbg:SetTexture(0,0,0,0.4)
John@52 291
John@52 292 local widget = {
John@52 293 frame = frame,
John@52 294 lines = {},
John@52 295 --levels = {},
John@52 296 buttons = {},
John@52 297 localstatus = {
John@52 298 --groups = {},
John@52 299 scrollvalue = 0 },
John@52 300 filter = false,
John@52 301 slframe = slframe,
John@53 302 scrollbar = scrollbar,
John@52 303 type = Type
John@52 304 }
John@52 305 if methods then
John@52 306 for method, func in pairs(methods) do
John@52 307 widget[method] = func
John@52 308 end
John@52 309 end
John@52 310 slframe.obj = widget
John@53 311 scrollbar.obj = widget
John@52 312
John@52 313 return AceGUI:RegisterAsWidget(widget)
John@52 314 end
John@52 315
John@52 316 AceGUI:RegisterWidgetType(Type, Constructor, Version)
John@52 317
John@52 318
John@52 319
John@52 320