annotate SelectorList.lua @ 52:7c7e80f63b51

First pass. Taking stuff from TreeGroup that I like/need/want in order to make a standalone selection list
author John@Yosemite-PC
date Sat, 24 Mar 2012 13:37:29 -0400
parents
children 708d8a423b4c
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@52 36
John@52 37 local function Layout(self)
John@52 38 self:SetHeight(self.numlines * 18 + 20)
John@52 39
John@52 40 end
John@52 41
John@52 42 local function CreateButton(widget)
John@52 43 local num = AceGUI:GetNextWidgetNum("TreeGroupButton")
John@52 44 local button = CreateFrame("Button", ("AceGUI30TreeButton%d"):format(num), widget.slframe, "OptionsListButtonTemplate")
John@52 45 button.obj = widget
John@52 46
John@52 47 local icon = button:CreateTexture(nil, "OVERLAY")
John@52 48 icon:SetWidth(14)
John@52 49 icon:SetHeight(14)
John@52 50 button.icon = icon
John@52 51
John@52 52 button:SetScript("OnClick",Button_OnClick)
John@52 53 button:SetScript("OnDoubleClick", Button_OnDoubleClick)
John@52 54 button:SetScript("OnEnter",Button_OnEnter)
John@52 55 button:SetScript("OnLeave",Button_OnLeave)
John@52 56
John@52 57 return button
John@52 58 end
John@52 59
John@52 60 local function UpdateButton(button, line, selected)
John@52 61 local self = button.obj
John@52 62 local frame = self.frame
John@52 63 local text = line.text or ""
John@52 64 local icon = line.icon
John@52 65 local iconCoords = line.iconCoords
John@52 66 local level = line.level or 1
John@52 67 local value = line.value
John@52 68 local uniquevalue = line.uniquevalue
John@52 69 local disabled = line.disabled
John@52 70
John@52 71 button.line = line
John@52 72 button.value = value
John@52 73 button.uniquevalue = uniquevalue
John@52 74 if selected then
John@52 75 button:LockHighlight()
John@52 76 button.selected = true
John@52 77 else
John@52 78 button:UnlockHighlight()
John@52 79 button.selected = false
John@52 80 end
John@52 81 local normalTexture = button:GetNormalTexture()
John@52 82 local line = button.line
John@52 83 button.level = level
John@52 84 if ( level == 1 ) then
John@52 85 button:SetNormalFontObject("GameFontNormal")
John@52 86 button:SetHighlightFontObject("GameFontHighlight")
John@52 87 button.text:SetPoint("LEFT", (icon and 16 or 0) + 8, 2)
John@52 88 else
John@52 89 button:SetNormalFontObject("GameFontHighlightSmall")
John@52 90 button:SetHighlightFontObject("GameFontHighlightSmall")
John@52 91 button.text:SetPoint("LEFT", (icon and 16 or 0) + 8 * level, 2)
John@52 92 end
John@52 93
John@52 94 if disabled then
John@52 95 button:EnableMouse(false)
John@52 96 button.text:SetText("|cff808080"..text..FONT_COLOR_CODE_CLOSE)
John@52 97 else
John@52 98 button.text:SetText(text)
John@52 99 button:EnableMouse(true)
John@52 100 end
John@52 101
John@52 102 if icon then
John@52 103 button.icon:SetTexture(icon)
John@52 104 button.icon:SetPoint("LEFT", 8 * level, (level == 1) and 0 or 1)
John@52 105 else
John@52 106 button.icon:SetTexture(nil)
John@52 107 end
John@52 108
John@52 109 if iconCoords then
John@52 110 button.icon:SetTexCoord(unpack(iconCoords))
John@52 111 else
John@52 112 button.icon:SetTexCoord(0, 1, 0, 1)
John@52 113 end
John@52 114 end
John@52 115
John@52 116
John@52 117 local methods = {
John@52 118
John@52 119 ["OnAcquire"] = function(self)
John@52 120 self:SetWidth(DEFAULT_SL_WIDTH)
John@52 121 self:SetNumLines()
John@52 122 --self:EnableButtonTooltips(true)
John@52 123 end,
John@52 124 --["SetWidth"] = function(self, width)
John@52 125 -- self.slframe:SetWidth(width)
John@52 126
John@52 127 -- local status = self.status or self.localstatus
John@52 128 -- status.width = width
John@52 129 -- status.treesizable = resizable
John@52 130
John@52 131 --end,
John@52 132 ["SetNumLines"] = function(self,value)
John@52 133 if not value or value < 4 then
John@52 134 value = 4
John@52 135 end
John@52 136 self.numlines = value
John@52 137 Layout(self)
John@52 138 end,
John@52 139 ["SetList"] = function(self, list)
John@52 140 for i,v in ipairs(list) do
John@52 141 self.lines[i] = v
John@52 142 end
John@52 143 self:Refresh()
John@52 144 end,
John@52 145 ["Refresh"] = function(self)
John@52 146 local f = self.slframe
John@52 147 local buttons = self.buttons
John@52 148 local lines = self.lines
John@52 149
John@52 150 local bnum = 1
John@52 151 for i=1,self.numlines do
John@52 152 local l = lines[i]
John@52 153 if not l then
John@52 154 break
John@52 155 end
John@52 156 local b = buttons[bnum]
John@52 157 if not b then
John@52 158 b = CreateButton(self)
John@52 159 buttons[bnum] = b
John@52 160 b:SetParent(f)
John@52 161 b:SetFrameLevel(f:GetFrameLevel()+1)
John@52 162 b:ClearAllPoints()
John@52 163 if bnum == 1 then
John@52 164 -- todo: if scroll ...
John@52 165 b:SetPoint("TOPLEFT",0,-10)
John@52 166 b:SetPoint("TOPRIGHT",0,-10)
John@52 167 else
John@52 168 b:SetPoint("TOPRIGHT", buttons[bnum-1], "BOTTOMRIGHT", 0, 0)
John@52 169 b:SetPoint("TOPLEFT", buttons[bnum-1], "BOTTOMLEFT", 0, 0)
John@52 170 end
John@52 171 end
John@52 172 UpdateButton(b, l)
John@52 173 b:Show()
John@52 174 bnum = bnum + 1
John@52 175 end
John@52 176 end,
John@52 177 }
John@52 178
John@52 179
John@52 180
John@52 181
John@52 182
John@52 183
John@52 184
John@52 185
John@52 186
John@52 187 local PaneBackdrop = {
John@52 188 bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
John@52 189 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
John@52 190 tile = true, tileSize = 16, edgeSize = 16,
John@52 191 insets = { left = 3, right = 3, top = 5, bottom = 3 }
John@52 192 }
John@52 193
John@52 194 local function Constructor()
John@52 195 local num = AceGUI:GetNextWidgetNum(Type)
John@52 196 local frame = CreateFrame("Frame", nil, UIParent)
John@52 197
John@52 198 local slframe = CreateFrame("Frame", nil, frame)
John@52 199 slframe:SetAllPoints()
John@52 200 --slframe:SetPoint("TOPLEFT")
John@52 201 --slframe:SetPoint("BOTTOMLEFT")
John@52 202 --slframe:SetWidth(DEFAULT_SL_WIDTH)
John@52 203 --slframe:SetHeight(DEFAULT_SL_WIDTH) -- todo
John@52 204 slframe:EnableMouseWheel(true)
John@52 205 slframe:SetBackdrop(PaneBackdrop)
John@52 206 slframe:SetBackdropColor(0.1, 0.1, 0.1, 0.5)
John@52 207 slframe:SetBackdropBorderColor(0.4, 0.4, 0.4)
John@52 208 --slframe:SetResizable(true)
John@52 209 --slframe:SetMinResize(100, 1)
John@52 210 --slframe:SetMaxResize(400, 1600)
John@52 211 --slframe:SetScript("OnUpdate", FirstFrameUpdate)
John@52 212 --slframe:SetScript("OnSizeChanged", Tree_OnSizeChanged)
John@52 213 --slframe:SetScript("OnMouseWheel", Tree_OnMouseWheel)
John@52 214
John@52 215 --local scrollbar = CreateFrame("Slider", ("AceConfigDialogTreeGroup%dScrollBar"):format(num), slframe, "UIPanelScrollBarTemplate")
John@52 216 --scrollbar:SetScript("OnValueChanged", nil)
John@52 217 --scrollbar:SetPoint("TOPRIGHT", -10, -26)
John@52 218 --scrollbar:SetPoint("BOTTOMRIGHT", -10, 26)
John@52 219 --scrollbar:SetMinMaxValues(0,0)
John@52 220 --scrollbar:SetValueStep(1)
John@52 221 --scrollbar:SetValue(0)
John@52 222 --scrollbar:SetWidth(16)
John@52 223 --scrollbar:SetScript("OnValueChanged", OnScrollValueChanged)
John@52 224
John@52 225 --local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND")
John@52 226 --scrollbg:SetAllPoints(scrollbar)
John@52 227 --scrollbg:SetTexture(0,0,0,0.4)
John@52 228
John@52 229 --local border = CreateFrame("Frame",nil,frame)
John@52 230 --border:SetPoint("TOPLEFT", slframe, "TOPRIGHT")
John@52 231 --border:SetPoint("BOTTOMRIGHT")
John@52 232 --border:SetBackdrop(PaneBackdrop)
John@52 233 --border:SetBackdropColor(0.1, 0.1, 0.1, 0.5)
John@52 234 --border:SetBackdropBorderColor(0.4, 0.4, 0.4)
John@52 235
John@52 236 ----Container Support
John@52 237 --local content = CreateFrame("Frame", nil, border)
John@52 238 --content:SetPoint("TOPLEFT", 10, -10)
John@52 239 --content:SetPoint("BOTTOMRIGHT", -10, 10)
John@52 240
John@52 241 local widget = {
John@52 242 frame = frame,
John@52 243 lines = {},
John@52 244 --levels = {},
John@52 245 buttons = {},
John@52 246 --hasChildren = {},
John@52 247 localstatus = {
John@52 248 --groups = {},
John@52 249 scrollvalue = 0 },
John@52 250 filter = false,
John@52 251 slframe = slframe,
John@52 252 --dragger = dragger,
John@52 253 --scrollbar = scrollbar,
John@52 254 --border = border,
John@52 255 --content = slframe, -- content is for containers
John@52 256 type = Type
John@52 257 }
John@52 258 if methods then
John@52 259 for method, func in pairs(methods) do
John@52 260 widget[method] = func
John@52 261 end
John@52 262 end
John@52 263 slframe.obj = widget
John@52 264 --dragger.obj = widget
John@52 265 --scrollbar.obj = widget
John@52 266
John@52 267 return AceGUI:RegisterAsWidget(widget)
John@52 268 end
John@52 269
John@52 270 AceGUI:RegisterWidgetType(Type, Constructor, Version)
John@52 271
John@52 272
John@52 273
John@52 274