annotate Core.lua @ 0:47fac96968e1

First commit
author John@Yosemite-PC
date Fri, 02 Mar 2012 00:15:09 -0500
parents
children 21c58930f74e
rev   line source
John@0 1 -- ideas: last attended data and/or remove people who haven't attended in X days
John@0 2 -- and/or just a "remove from all lists" option
John@0 3
John@0 4
John@0 5 -- order of implementation
John@0 6 -- ( ) lists fully functional (/script interface)
John@0 7 -- ( ) lists single-user functional via command line interface
John@0 8 -- ( ) single user + admin gui (manual suicides)
John@0 9 -- ( ) single user + admin gui (master loot)
John@0 10 -- ( ) communication and list trimming
John@0 11 -- ( ) admins
John@0 12 -- ( ) players gui
John@0 13 -- ( ) crypto / protection against tampering
John@0 14 -- ( ) alt tracking
John@0 15 -- ( ) reserves
John@0 16
John@0 17 -- important things to remember:
John@0 18 -- 1) ipairs iterates from 1 until the first missing int index -> no gaps
John@0 19 -- 2) a.x === a["x"]
John@0 20 -- 3) a["1"] =/= a[1]
John@0 21
John@0 22 bsk = LibStub("AceAddon-3.0"):NewAddon("bsk","AceConsole-3.0", "AceHook-3.0", "AceComm-3.0", "AceSerializer-3.0")
John@0 23 local L = LibStub("AceLocale-3.0"):GetLocale("bsk", false)
John@0 24
John@0 25 local AceGUI = LibStub("AceGUI-3.0")
John@0 26
John@0 27 function bsk:OnInitialize()
John@0 28
John@0 29 self.db = LibStub("AceDB-3.0"):New("BskDB", self.defaults, "Default")
John@0 30
John@0 31 self.options.args.profile = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
John@0 32 LibStub("AceConfig-3.0"):RegisterOptionsTable("bsk", self.options)
John@0 33 self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("bsk", "bsk")
John@0 34
John@0 35 self:RegisterChatCommand("bsk", "HandleCommand")
John@0 36 bsk:CreateWorkingStateFromChanges()
John@0 37 end
John@0 38
John@0 39 function bsk:OnEnable()
John@0 40 --self:HandleCommand()
John@0 41 end
John@0 42
John@0 43 function bsk:HandleCommand(paramIn)
John@0 44 local param = { strsplit(" ", paramIn) }
John@0 45 local FixPlayerName = function(p)
John@0 46 p = p:lower()
John@0 47 -- next two lines from sylvanaar
John@0 48 local MULTIBYTE_FIRST_CHAR = "^([\192-\255]?%a?[\128-\191]*)"
John@0 49 return string.gsub(p, MULTIBYTE_FIRST_CHAR, string.upper, 1)
John@0 50 end
John@0 51
John@0 52 if param[1] == nil or param[1] == "" then
John@0 53 bsk:Print("need args")
John@0 54 return
John@0 55 end
John@0 56 if param[1] == "players" then
John@0 57 bsk:PrintPlayers()
John@0 58 elseif param[1] == "add" then
John@0 59 if param[2] == nil or param[2] == "" then
John@0 60 bsk:PrintTable(param)
John@0 61 return
John@0 62 end
John@0 63 if param[3] == nil or param[3] == "" then
John@0 64 bsk:PrintTable(param)
John@0 65 return
John@0 66 end
John@0 67 if param[2] == "player" then
John@0 68 --if param[3] == "all" then
John@0 69 -- bsk:Pop
John@0 70 --else
John@0 71 local player = FixPlayerName(param[3])
John@0 72 bsk:AddPlayer(player)
John@0 73 --end
John@0 74 elseif param[2] == "list" then
John@0 75 bsk:CreateList(param[3])
John@0 76 end
John@0 77 elseif param[1] == "suicide" then
John@0 78 if param[2] == nil or param[2] == "" or param[3] == nil or param[3] == "" then
John@0 79 bsk:PrintTable(param)
John@0 80 return
John@0 81 end
John@0 82 local player = FixPlayerName(param[2])
John@0 83 bsk:Print(string.format("Fixed player name %s to %s",param[2],player))
John@0 84 bsk:SuicidePlayer(player,param[3])
John@0 85 elseif param[1] == "show" then
John@0 86 if param[2] == nil or param[2] == "" then
John@0 87 bsk:PrintTable(param)
John@0 88 return
John@0 89 end
John@0 90 bsk:PrintLists(param[2])
John@0 91 end
John@0 92
John@0 93 -- TODO: add options
John@0 94 --
John@0 95 -- for now: launch GUI
John@0 96 --
John@0 97
John@0 98 --if self.frame == nil then
John@0 99 --self:CreateGUI()
John@0 100 --self:ShowGUI()
John@0 101 --else
John@0 102 --self:ShowGUI()
John@0 103 --end
John@0 104
John@0 105 end
John@0 106 --MULTIBYTE_FIRST_CHAR = "^([\192-\255]?%a?[\128-\191]*)"
John@0 107 --function GetNamePattern(name)
John@0 108 -- local u = name:match(MULTIBYTE_FIRST_CHAR):upper()
John@0 109
John@0 110 -- if not u or u:len() == 0 then Prat.Print("GetNamePattern: name error", name) return end
John@0 111 -- local l = u:lower()
John@0 112 -- local namepat
John@0 113 -- if u == l then
John@0 114 -- namepat = name:lower()
John@0 115 -- elseif u:len() == 1 then
John@0 116 -- namepat = "["..u..l.."]"..name:sub(2):lower()
John@0 117 -- elseif u:len() > 1 then
John@0 118 -- namepat = ""
John@0 119 -- for i=1,u:len() do
John@0 120 -- namepat = namepat .. "[" .. u:sub(i,i)..l:sub(i,i).."]"
John@0 121 -- end
John@0 122 -- namepat = namepat .. name:sub(u:len()+1)
John@0 123 -- end
John@0 124 -- return "%f[%a\192-\255]"..namepat.."%f[^%a\128-\255]"
John@0 125 --end
John@0 126 --AnyNamePattern = "%f[%a\192-\255]([%a\192-\255]+)%f[^%a\128-\255]"
John@0 127
John@0 128
John@0 129 function bsk:CreateGUI()
John@0 130
John@0 131 -- Create a container frame
John@0 132 self.frame = AceGUI:Create("Frame")
John@0 133 self.frame:SetCallback("OnClose",function(widget) AceGUI:Release(widget) end)
John@0 134 self.frame:SetTitle("BSK")
John@0 135 self.frame:SetLayout("Flow")
John@0 136 self.frame:SetHeight(700)
John@0 137 self.frame:SetWidth(350)
John@0 138 local tab = AceGUI:Create("TabGroup")
John@0 139 tab:SetLayout("Flow")
John@0 140 tab:SetTabs(
John@0 141 {
John@0 142 {
John@0 143 text="Tab 1",
John@0 144 value="tab1"
John@0 145 },
John@0 146
John@0 147 {
John@0 148 text="Tab 2",
John@0 149 value="tab2"
John@0 150 },
John@0 151 {
John@0 152 text="Tab 3",
John@0 153 value="tab3"
John@0 154 },
John@0 155 {
John@0 156 text="Tab 4",
John@0 157 value="tab4"
John@0 158 }
John@0 159 }
John@0 160 )
John@0 161 tab.width = "fill"
John@0 162 tab.height = "fill"
John@0 163
John@0 164 tab:SetCallback("OnGroupSelected",ChangeTab)
John@0 165 tab:SelectTab("tab1")
John@0 166 self.frame:AddChild(tab)
John@0 167
John@0 168 -- Create a button
John@0 169 --local btn = AceGUI:Create("Button")
John@0 170 --btn:SetWidth(170)
John@0 171 --btn:SetText("Button !")
John@0 172 --btn:SetCallback("OnClick", function() print("Click!") end)
John@0 173 -- Add the button to the container
John@0 174 --self.frame:AddChild(btn)
John@0 175 end
John@0 176
John@0 177 bsk.defaults = {
John@0 178 profile = {
John@0 179 players = {},
John@0 180 changes = {},
John@0 181 listBase = {}
John@0 182 }
John@0 183 }
John@0 184
John@0 185 bsk.options = {
John@0 186 name= 'bsk',
John@0 187 type = 'group',
John@0 188 args =
John@0 189 {
John@0 190 version =
John@0 191 {
John@0 192 type = "execute",
John@0 193 name = "Version query",
John@0 194 desc = "Check others' versions",
John@0 195 func = function(self) self:Print("TODO") end
John@0 196 }
John@0 197 }
John@0 198 }
John@0 199 function ChangeTab(container, event, group)
John@0 200 container:ReleaseChildren()
John@0 201 if group == "tab2" then
John@0 202 local desc = AceGUI:Create("Label")
John@0 203 desc:SetText("This is Tab 1")
John@0 204 desc:SetFullWidth(true)
John@0 205 container:AddChild(desc)
John@0 206
John@0 207 local button = AceGUI:Create("Button")
John@0 208 button:SetText("Tab 1 Button")
John@0 209 button:SetWidth(200)
John@0 210 container:AddChild(button)
John@0 211 elseif group == "tab1" then
John@0 212 local item2 = {string="item2!", color = {r=1,g=0,b=0.5} }
John@0 213 local itemList = {"Item1", item2, "Item3", "Item4"}
John@0 214
John@0 215 local myMultiSelect = AceGUI:Create("MultiSelect")
John@0 216 myMultiSelect:SetLabel("My Multi Select")
John@0 217 myMultiSelect:SetWidth(200)
John@0 218 myMultiSelect:SetHeight(400)
John@0 219 myMultiSelect:SetItemList(itemList)
John@0 220 myMultiSelect:SetMultiSelect(false)
John@0 221 container:AddChild(myMultiSelect)
John@0 222 end
John@0 223 end
John@0 224
John@0 225
John@0 226
John@0 227
John@0 228
John@0 229
John@0 230
John@0 231
John@0 232
John@0 233
John@0 234
John@0 235 --[[----------------------------------
John@0 236 -- MultiSelect widget for AceGUI-3.0
John@0 237 -- Written by Shirokuma
John@0 238 --]]----------------------------------
John@0 239
John@0 240
John@0 241 --[[-----------------
John@0 242 -- AceGUI
John@0 243 --]]-----------------
John@0 244 --local AceGUI = LibStub("AceGUI-3.0")
John@0 245
John@0 246 --[[-----------------
John@0 247 -- Lua APIs
John@0 248 --]]-----------------
John@0 249 local format, pairs, tostring = string.format, pairs, tostring
John@0 250
John@0 251 --[[-----------------
John@0 252 -- WoW APIs
John@0 253 --]]-----------------
John@0 254 local CreateFrame, UIParent = CreateFrame, UIParent
John@0 255
John@0 256 --[[-----------------
John@0 257 -- Frame Elements
John@0 258 --]]-----------------
John@0 259 local FrameBackdrop = {
John@0 260 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
John@0 261 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
John@0 262 tile = true, tileSize = 16, edgeSize = 16,
John@0 263 insets = { left = 3, right = 3, top = 3, bottom = 3 }
John@0 264 }
John@0 265
John@0 266
John@0 267 --[[-----------------
John@0 268 -- Widget Info
John@0 269 --]]-----------------
John@0 270 local widgetType = "MultiSelect"
John@0 271 local widgetVersion = 1
John@0 272
John@0 273
John@0 274 --[[-----------------
John@0 275 -- Event Code
John@0 276 --]]-----------------
John@0 277 local function Label_OnEnter(label)
John@0 278 local self = label.obj
John@0 279 local value = label
John@0 280 self:Fire("OnLabelEnter", value)
John@0 281 end
John@0 282
John@0 283 local function Label_OnLeave(label)
John@0 284 local self = label.obj
John@0 285 local value = label
John@0 286 self:Fire("OnLabelEnter", value)
John@0 287 end
John@0 288
John@0 289 local function Label_OnClick(label)
John@0 290 local self = label.obj
John@0 291 local value = label
John@0 292 self:Fire("OnLabelClick", value)
John@0 293 AceGUI:ClearFocus()
John@0 294 end
John@0 295
John@0 296
John@0 297 --[[-----------------
John@0 298 -- MultiSelect Code
John@0 299 --]]-----------------
John@0 300 do
John@0 301 local function OnAcquire(self) -- set up the default size
John@0 302 self:SetWidth(200)
John@0 303 self:SetHeight(400)
John@0 304 end
John@0 305
John@0 306 local function SetWidth(self, w) -- override the SetWidth function to include the labelframe
John@0 307 self.frame:SetWidth(w)
John@0 308 self.labelframe:SetWidth(w-33)
John@0 309 end
John@0 310
John@0 311 local function SetLabel(self, text) -- sets the multiselect label text
John@0 312 self.label:SetText(text)
John@0 313 end
John@0 314
John@0 315 local function SetMultiSelect(self, value) -- set if multiple values can be selected simultaneously
John@0 316 self.multiselect = value
John@0 317 end
John@0 318
John@0 319 local function AddItem(self, str, color) -- add an item (create a new item label object)
John@0 320 local color = color
John@0 321 local label = CreateFrame("Button", nil, self.labelframe)
John@0 322 label.selected = false
John@0 323 label.obj = self
John@0 324 label:SetHeight(18)
John@0 325 label:SetPoint("TOPLEFT", self.labelframe, "TOPLEFT", 0, -(getn(self.labels) * 18))
John@0 326 label:SetPoint("TOPRIGHT", self.labelframe, "TOPRIGHT", 0, -(getn(self.labels) * 18))
John@0 327 self.labels[getn(self.labels) + 1] = label
John@0 328 self.labelframe:SetHeight(getn(self.labels) * 18)
John@0 329
John@0 330 local text = label:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
John@0 331 text:SetJustifyH("LEFT")
John@0 332 text:SetPoint("TOPLEFT",label,"TOPLEFT",5,0)
John@0 333 text:SetPoint("BOTTOMRIGHT",label,"BOTTOMRIGHT",-5,0)
John@0 334 if color ~= nil then
John@0 335 text:SetTextColor(color.r,color.g,color.b)
John@0 336 end
John@0 337 text:SetText(str)
John@0 338 label.text = text
John@0 339
John@0 340 local highlight = label:CreateTexture(nil, "OVERLAY")
John@0 341 highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
John@0 342 highlight:SetBlendMode("ADD")
John@0 343 highlight:SetHeight(14)
John@0 344 highlight:ClearAllPoints()
John@0 345 highlight:SetPoint("RIGHT",label,"RIGHT",0,0)
John@0 346 highlight:SetPoint("LEFT",label,"LEFT",0,0)
John@0 347 highlight:Hide()
John@0 348 label.highlight = highlight
John@0 349
John@0 350 label:SetScript("OnEnter", function(this)
John@0 351 this.highlight:Show()
John@0 352 Label_OnEnter(this)
John@0 353 end)
John@0 354 label:SetScript("OnLeave", function(this)
John@0 355 if not this.selected then
John@0 356 this.highlight:Hide()
John@0 357 end
John@0 358 end)
John@0 359 label:SetScript("OnClick", function(this)
John@0 360 if not this.selected then
John@0 361 this.selected = true
John@0 362 if not self.multiselect then
John@0 363 for index, items in pairs(self.labels) do
John@0 364 if self.labels[index] ~= this and self.labels[index].selected then
John@0 365 self.labels[index].selected = false
John@0 366 self.labels[index].highlight:Hide()
John@0 367 end
John@0 368 end
John@0 369 end
John@0 370 else
John@0 371 this.selected = false
John@0 372 end
John@0 373 Label_OnClick(this)
John@0 374 end)
John@0 375 end
John@0 376
John@0 377 local function GetItem(self, text) -- find an object based on the text parameter
John@0 378 for _, value in pairs(self.labels) do
John@0 379 if value.text:GetText() == text then
John@0 380 return value
John@0 381 end
John@0 382 end
John@0 383 return nil
John@0 384 end
John@0 385
John@0 386 local function GetText(self, value) -- get the text of a label object
John@0 387 for _,item in pairs(self.labels) do
John@0 388 if value == item then
John@0 389 return item.text:GetText()
John@0 390 end
John@0 391 end
John@0 392 return nil
John@0 393 end
John@0 394
John@0 395 local function SetText(self, value, text) -- set the text of a label object
John@0 396 for _, item in pairs(self.labels) do
John@0 397 if value == item then
John@0 398 value.text:SetText(text)
John@0 399 end
John@0 400 end
John@0 401 end
John@0 402
John@0 403 local function IsSelected(self, value) -- return if the label object is currently selected
John@0 404 for _, item in pairs(self.labels) do
John@0 405 if value == item then
John@0 406 return item.selected
John@0 407 end
John@0 408 end
John@0 409 return nil
John@0 410 end
John@0 411
John@0 412 local function GetSelected(self) -- return a table of the currently selected label objects
John@0 413 local selectedList = {}
John@0 414 for _, item in pairs(self.labels) do
John@0 415 if item.selected then
John@0 416 table.insert(selectedList, item)
John@0 417 end
John@0 418 end
John@0 419 return selectedList
John@0 420 end
John@0 421
John@0 422 local function SetItemList(self, list) -- create new labels from a list of strings
John@0 423 for _,item in pairs(self.labels) do
John@0 424 item:Hide()
John@0 425 item:ClearAllPoints()
John@0 426 end
John@0 427
John@0 428 self.labels = {}
John@0 429
John@0 430 if list then
John@0 431 for _,item in pairs(list) do
John@0 432 if type(item) == "string" then
John@0 433 self:AddItem(item)
John@0 434 elseif type(item) == "table" then
John@0 435 if item.string ~= nil and type(item.string) == "string" then
John@0 436 if item.color ~= nil then
John@0 437 if type(item.color) == "table" and item.color.r ~= nil and item.color.g ~= nil and item.color.b ~= nil then
John@0 438 self:AddItem(item.string, item.color)
John@0 439 else
John@0 440 assert(false and "setitemlist: item.color is set, but nonsense")
John@0 441 end
John@0 442 else
John@0 443 self:AddItem(item.string)
John@0 444 end
John@0 445 else
John@0 446 assert( false and "setitemlist: item is table without .string member")
John@0 447 end
John@0 448 else
John@0 449 assert(false and "SetItemList: nonsense list entry")
John@0 450 end
John@0 451 end
John@0 452 end
John@0 453 end
John@0 454
John@0 455 local function RemoveItem(self, item) -- delete an item
John@0 456 local function RedrawFrame()
John@0 457 for index,value in pairs(self.labels) do
John@0 458 value:SetPoint("TOPLEFT", self.labelframe, "TOPLEFT", 0, (-(index-1) * 18))
John@0 459 value:SetPoint("TOPRIGHT", self.labelframe, "TOPRIGHT", 0,(-(index-1) * 18))
John@0 460 end
John@0 461 end
John@0 462
John@0 463 for index, value in pairs(self.labels) do
John@0 464 if value == item then
John@0 465 table.remove(self.labels, index)
John@0 466 item:Hide()
John@0 467 item:ClearAllPoints()
John@0 468 RedrawFrame()
John@0 469 end
John@0 470 end
John@0 471 end
John@0 472
John@0 473 local function SetSelected(self, item, value)
John@0 474 if value then
John@0 475 if not self.multiselect then -- test
John@0 476 for _, value in pairs(self.labels) do
John@0 477 value.selected = false
John@0 478 value.highlight:Hide()
John@0 479 end
John@0 480 end
John@0 481 item.selected = true
John@0 482 item.highlight:Show()
John@0 483 else
John@0 484 item.selected = false
John@0 485 item.highlight:Hide()
John@0 486 end
John@0 487 end
John@0 488
John@0 489 local function Constructor() -- widget constructor
John@0 490 local frame = CreateFrame("Frame", nil, UIParent)
John@0 491 local backdrop = CreateFrame("Frame", nil, frame)
John@0 492 local self = {}
John@0 493 local labels = {}
John@0 494
John@0 495 self.type = widgetType
John@0 496 self.frame = frame
John@0 497 self.backdrop = backdrop
John@0 498 self.labels = {}
John@0 499 self.multiselect = true
John@0 500 frame.obj = self
John@0 501
John@0 502 local label = frame:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
John@0 503 label:SetJustifyH("LEFT")
John@0 504 label:SetPoint("TOPLEFT", 5, 0)
John@0 505 label:SetPoint("TOPRIGHT", -5, 0)
John@0 506 label:SetHeight(14)
John@0 507 label:SetText("MultiSelect")
John@0 508 self.label = label
John@0 509
John@0 510 backdrop:SetBackdrop(FrameBackdrop)
John@0 511 backdrop:SetBackdropColor(0, 0, 0)
John@0 512 backdrop:SetBackdropBorderColor(0.4, 0.4, 0.4)
John@0 513 backdrop:SetPoint("TOPLEFT", frame, "TOPLEFT", 5, -14)
John@0 514 backdrop:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -5, 0)
John@0 515
John@0 516 local scrollframe = CreateFrame("ScrollFrame", format("%s@%s@%s", widgetType, "ScrollFrame", tostring(self)), frame, "UIPanelScrollFrameTemplate")
John@0 517 scrollframe:SetPoint("TOPLEFT", backdrop, "TOPLEFT", 5, -6)
John@0 518 scrollframe:SetPoint("BOTTOMRIGHT", backdrop, "BOTTOMRIGHT", -28, 6)
John@0 519 scrollframe.obj = self
John@0 520 self.scrollframe = scrollframe
John@0 521
John@0 522 local labelframe = CreateFrame("Frame", nil, scrollframe)
John@0 523 labelframe:SetAllPoints()
John@0 524 labelframe.obj = self
John@0 525 scrollframe:SetScrollChild(labelframe)
John@0 526 self.labelframe = labelframe
John@0 527
John@0 528 -- method listing
John@0 529 self.OnAcquire = OnAcquire
John@0 530 self.SetLabel = SetLabel
John@0 531 self.AddItem = AddItem
John@0 532 self.SetWidth = SetWidth
John@0 533 self.SetMultiSelect = SetMultiSelect
John@0 534 self.SetItemList = SetItemList
John@0 535 self.GetItem = GetItem
John@0 536 self.RemoveItem = RemoveItem
John@0 537 self.GetText = GetText
John@0 538 self.SetText = SetText
John@0 539 self.IsSelected = IsSelected
John@0 540 self.GetSelected = GetSelected
John@0 541 self.SetSelected = SetSelected
John@0 542
John@0 543 AceGUI:RegisterAsWidget(self)
John@0 544 return self
John@0 545 end
John@0 546 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion)
John@0 547 end
John@0 548