annotate MultiSelectWidget.lua @ 11:99c279ab0b75

Command to add people to end or random. you know.
author John@Yosemite-PC
date Wed, 07 Mar 2012 23:00:59 -0500
parents 21c58930f74e
children
rev   line source
John@1 1
John@1 2
John@1 3 --[[----------------------------------
John@1 4 -- MultiSelect widget for AceGUI-3.0
John@1 5 -- Written by Shirokuma
John@1 6 --]]----------------------------------
John@1 7
John@1 8
John@1 9 --[[-----------------
John@1 10 -- AceGUI
John@1 11 --]]-----------------
John@1 12 local AceGUI = LibStub("AceGUI-3.0")
John@1 13
John@1 14 --[[-----------------
John@1 15 -- Lua APIs
John@1 16 --]]-----------------
John@1 17 local format, pairs, tostring = string.format, pairs, tostring
John@1 18
John@1 19 --[[-----------------
John@1 20 -- WoW APIs
John@1 21 --]]-----------------
John@1 22 local CreateFrame, UIParent = CreateFrame, UIParent
John@1 23
John@1 24 --[[-----------------
John@1 25 -- Frame Elements
John@1 26 --]]-----------------
John@1 27 local FrameBackdrop = {
John@1 28 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
John@1 29 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
John@1 30 tile = true, tileSize = 16, edgeSize = 16,
John@1 31 insets = { left = 3, right = 3, top = 3, bottom = 3 }
John@1 32 }
John@1 33
John@1 34
John@1 35 --[[-----------------
John@1 36 -- Widget Info
John@1 37 --]]-----------------
John@1 38 local widgetType = "MultiSelect"
John@1 39 local widgetVersion = 1
John@1 40
John@1 41
John@1 42 --[[-----------------
John@1 43 -- Event Code
John@1 44 --]]-----------------
John@1 45 local function Label_OnEnter(label)
John@1 46 local self = label.obj
John@1 47 local value = label
John@1 48 self:Fire("OnLabelEnter", value)
John@1 49 end
John@1 50
John@1 51 local function Label_OnLeave(label)
John@1 52 local self = label.obj
John@1 53 local value = label
John@1 54 self:Fire("OnLabelEnter", value)
John@1 55 end
John@1 56
John@1 57 local function Label_OnClick(label)
John@1 58 local self = label.obj
John@1 59 local value = label
John@1 60 self:Fire("OnLabelClick", value)
John@1 61 AceGUI:ClearFocus()
John@1 62 end
John@1 63
John@1 64
John@1 65 --[[-----------------
John@1 66 -- MultiSelect Code
John@1 67 --]]-----------------
John@1 68 do
John@1 69 local function OnAcquire(self) -- set up the default size
John@1 70 self:SetWidth(200)
John@1 71 self:SetHeight(400)
John@1 72 end
John@1 73
John@1 74 local function SetWidth(self, w) -- override the SetWidth function to include the labelframe
John@1 75 self.frame:SetWidth(w)
John@1 76 self.labelframe:SetWidth(w-33)
John@1 77 end
John@1 78
John@1 79 local function SetLabel(self, text) -- sets the multiselect label text
John@1 80 self.label:SetText(text)
John@1 81 end
John@1 82
John@1 83 local function SetMultiSelect(self, value) -- set if multiple values can be selected simultaneously
John@1 84 self.multiselect = value
John@1 85 end
John@1 86
John@1 87 local function AddItem(self, str, color) -- add an item (create a new item label object)
John@1 88 local color = color
John@1 89 local label = CreateFrame("Button", nil, self.labelframe)
John@1 90 label.selected = false
John@1 91 label.obj = self
John@1 92 label:SetHeight(18)
John@1 93 label:SetPoint("TOPLEFT", self.labelframe, "TOPLEFT", 0, -(getn(self.labels) * 18))
John@1 94 label:SetPoint("TOPRIGHT", self.labelframe, "TOPRIGHT", 0, -(getn(self.labels) * 18))
John@1 95 self.labels[getn(self.labels) + 1] = label
John@1 96 self.labelframe:SetHeight(getn(self.labels) * 18)
John@1 97
John@1 98 local text = label:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
John@1 99 text:SetJustifyH("LEFT")
John@1 100 text:SetPoint("TOPLEFT",label,"TOPLEFT",5,0)
John@1 101 text:SetPoint("BOTTOMRIGHT",label,"BOTTOMRIGHT",-5,0)
John@1 102 if color ~= nil then
John@1 103 text:SetTextColor(color.r,color.g,color.b)
John@1 104 end
John@1 105 text:SetText(str)
John@1 106 label.text = text
John@1 107
John@1 108 local highlight = label:CreateTexture(nil, "OVERLAY")
John@1 109 highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
John@1 110 highlight:SetBlendMode("ADD")
John@1 111 highlight:SetHeight(14)
John@1 112 highlight:ClearAllPoints()
John@1 113 highlight:SetPoint("RIGHT",label,"RIGHT",0,0)
John@1 114 highlight:SetPoint("LEFT",label,"LEFT",0,0)
John@1 115 highlight:Hide()
John@1 116 label.highlight = highlight
John@1 117
John@1 118 label:SetScript("OnEnter", function(this)
John@1 119 this.highlight:Show()
John@1 120 Label_OnEnter(this)
John@1 121 end)
John@1 122 label:SetScript("OnLeave", function(this)
John@1 123 if not this.selected then
John@1 124 this.highlight:Hide()
John@1 125 end
John@1 126 end)
John@1 127 label:SetScript("OnClick", function(this)
John@1 128 if not this.selected then
John@1 129 this.selected = true
John@1 130 if not self.multiselect then
John@1 131 for index, items in pairs(self.labels) do
John@1 132 if self.labels[index] ~= this and self.labels[index].selected then
John@1 133 self.labels[index].selected = false
John@1 134 self.labels[index].highlight:Hide()
John@1 135 end
John@1 136 end
John@1 137 end
John@1 138 else
John@1 139 this.selected = false
John@1 140 end
John@1 141 Label_OnClick(this)
John@1 142 end)
John@1 143 end
John@1 144
John@1 145 local function GetItem(self, text) -- find an object based on the text parameter
John@1 146 for _, value in pairs(self.labels) do
John@1 147 if value.text:GetText() == text then
John@1 148 return value
John@1 149 end
John@1 150 end
John@1 151 return nil
John@1 152 end
John@1 153
John@1 154 local function GetText(self, value) -- get the text of a label object
John@1 155 for _,item in pairs(self.labels) do
John@1 156 if value == item then
John@1 157 return item.text:GetText()
John@1 158 end
John@1 159 end
John@1 160 return nil
John@1 161 end
John@1 162
John@1 163 local function SetText(self, value, text) -- set the text of a label object
John@1 164 for _, item in pairs(self.labels) do
John@1 165 if value == item then
John@1 166 value.text:SetText(text)
John@1 167 end
John@1 168 end
John@1 169 end
John@1 170
John@1 171 local function IsSelected(self, value) -- return if the label object is currently selected
John@1 172 for _, item in pairs(self.labels) do
John@1 173 if value == item then
John@1 174 return item.selected
John@1 175 end
John@1 176 end
John@1 177 return nil
John@1 178 end
John@1 179
John@1 180 local function GetSelected(self) -- return a table of the currently selected label objects
John@1 181 local selectedList = {}
John@1 182 for _, item in pairs(self.labels) do
John@1 183 if item.selected then
John@1 184 table.insert(selectedList, item)
John@1 185 end
John@1 186 end
John@1 187 return selectedList
John@1 188 end
John@1 189
John@1 190 local function SetItemList(self, list) -- create new labels from a list of strings
John@1 191 for _,item in pairs(self.labels) do
John@1 192 item:Hide()
John@1 193 item:ClearAllPoints()
John@1 194 end
John@1 195
John@1 196 self.labels = {}
John@1 197
John@1 198 if list then
John@1 199 for _,item in pairs(list) do
John@1 200 if type(item) == "string" then
John@1 201 self:AddItem(item)
John@1 202 elseif type(item) == "table" then
John@1 203 if item.string ~= nil and type(item.string) == "string" then
John@1 204 if item.color ~= nil then
John@1 205 if type(item.color) == "table" and item.color.r ~= nil and item.color.g ~= nil and item.color.b ~= nil then
John@1 206 self:AddItem(item.string, item.color)
John@1 207 else
John@1 208 assert(false and "setitemlist: item.color is set, but nonsense")
John@1 209 end
John@1 210 else
John@1 211 self:AddItem(item.string)
John@1 212 end
John@1 213 else
John@1 214 assert( false and "setitemlist: item is table without .string member")
John@1 215 end
John@1 216 else
John@1 217 assert(false and "SetItemList: nonsense list entry")
John@1 218 end
John@1 219 end
John@1 220 end
John@1 221 end
John@1 222
John@1 223 local function RemoveItem(self, item) -- delete an item
John@1 224 local function RedrawFrame()
John@1 225 for index,value in pairs(self.labels) do
John@1 226 value:SetPoint("TOPLEFT", self.labelframe, "TOPLEFT", 0, (-(index-1) * 18))
John@1 227 value:SetPoint("TOPRIGHT", self.labelframe, "TOPRIGHT", 0,(-(index-1) * 18))
John@1 228 end
John@1 229 end
John@1 230
John@1 231 for index, value in pairs(self.labels) do
John@1 232 if value == item then
John@1 233 table.remove(self.labels, index)
John@1 234 item:Hide()
John@1 235 item:ClearAllPoints()
John@1 236 RedrawFrame()
John@1 237 end
John@1 238 end
John@1 239 end
John@1 240
John@1 241 local function SetSelected(self, item, value)
John@1 242 if value then
John@1 243 if not self.multiselect then -- test
John@1 244 for _, value in pairs(self.labels) do
John@1 245 value.selected = false
John@1 246 value.highlight:Hide()
John@1 247 end
John@1 248 end
John@1 249 item.selected = true
John@1 250 item.highlight:Show()
John@1 251 else
John@1 252 item.selected = false
John@1 253 item.highlight:Hide()
John@1 254 end
John@1 255 end
John@1 256
John@1 257 local function Constructor() -- widget constructor
John@1 258 local frame = CreateFrame("Frame", nil, UIParent)
John@1 259 local backdrop = CreateFrame("Frame", nil, frame)
John@1 260 local self = {}
John@1 261 local labels = {}
John@1 262
John@1 263 self.type = widgetType
John@1 264 self.frame = frame
John@1 265 self.backdrop = backdrop
John@1 266 self.labels = {}
John@1 267 self.multiselect = true
John@1 268 frame.obj = self
John@1 269
John@1 270 local label = frame:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
John@1 271 label:SetJustifyH("LEFT")
John@1 272 label:SetPoint("TOPLEFT", 5, 0)
John@1 273 label:SetPoint("TOPRIGHT", -5, 0)
John@1 274 label:SetHeight(14)
John@1 275 label:SetText("MultiSelect")
John@1 276 self.label = label
John@1 277
John@1 278 backdrop:SetBackdrop(FrameBackdrop)
John@1 279 backdrop:SetBackdropColor(0, 0, 0)
John@1 280 backdrop:SetBackdropBorderColor(0.4, 0.4, 0.4)
John@1 281 backdrop:SetPoint("TOPLEFT", frame, "TOPLEFT", 5, -14)
John@1 282 backdrop:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -5, 0)
John@1 283
John@1 284 local scrollframe = CreateFrame("ScrollFrame", format("%s@%s@%s", widgetType, "ScrollFrame", tostring(self)), frame, "UIPanelScrollFrameTemplate")
John@1 285 scrollframe:SetPoint("TOPLEFT", backdrop, "TOPLEFT", 5, -6)
John@1 286 scrollframe:SetPoint("BOTTOMRIGHT", backdrop, "BOTTOMRIGHT", -28, 6)
John@1 287 scrollframe.obj = self
John@1 288 self.scrollframe = scrollframe
John@1 289
John@1 290 local labelframe = CreateFrame("Frame", nil, scrollframe)
John@1 291 labelframe:SetAllPoints()
John@1 292 labelframe.obj = self
John@1 293 scrollframe:SetScrollChild(labelframe)
John@1 294 self.labelframe = labelframe
John@1 295
John@1 296 -- method listing
John@1 297 self.OnAcquire = OnAcquire
John@1 298 self.SetLabel = SetLabel
John@1 299 self.AddItem = AddItem
John@1 300 self.SetWidth = SetWidth
John@1 301 self.SetMultiSelect = SetMultiSelect
John@1 302 self.SetItemList = SetItemList
John@1 303 self.GetItem = GetItem
John@1 304 self.RemoveItem = RemoveItem
John@1 305 self.GetText = GetText
John@1 306 self.SetText = SetText
John@1 307 self.IsSelected = IsSelected
John@1 308 self.GetSelected = GetSelected
John@1 309 self.SetSelected = SetSelected
John@1 310
John@1 311 AceGUI:RegisterAsWidget(self)
John@1 312 return self
John@1 313 end
John@1 314 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion)
John@1 315 end
John@1 316