annotate modules/Action.lua @ 234:0e20f65375d5

Reworked button creation to not use goofy event driven semantics.
author Flick
date Tue, 22 Mar 2011 17:05:51 -0700
parents c4b134512c50
children 704f4a05a1d7
rev   line source
flickerstreak@175 1 local addonName, addonTable = ...
flickerstreak@175 2 local ReAction = addonTable.ReAction
flickerstreak@24 3 local L = ReAction.L
flickerstreak@24 4 local _G = _G
flickerstreak@88 5 local format = string.format
flickerstreak@92 6 local wipe = wipe
flickerstreak@24 7
flickerstreak@116 8 local weak = { __mode="k" }
flickerstreak@116 9
flickerstreak@24 10 -- module declaration
flickerstreak@24 11 local moduleID = "Action"
flickerstreak@28 12 local module = ReAction:NewModule( moduleID )
flickerstreak@24 13
flickerstreak@90 14 -- Class declarations
flickerstreak@128 15 local Button = ReAction.Button.Action -- see /classes/ActionButton.lua
flickerstreak@90 16 local Handle = { }
flickerstreak@90 17 local PropHandler = { }
flickerstreak@87 18
flickerstreak@77 19 -- Event handlers
flickerstreak@24 20 function module:OnInitialize()
flickerstreak@90 21 self.handles = setmetatable({ }, weak)
flickerstreak@49 22
flickerstreak@63 23 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
flickerstreak@24 24 end
flickerstreak@24 25
flickerstreak@24 26 function module:OnEnable()
flickerstreak@90 27 ReAction:GetModule("State"):RegisterStateProperty("page", nil, PropHandler.GetOptions(), PropHandler)
flickerstreak@24 28 end
flickerstreak@24 29
flickerstreak@24 30 function module:OnDisable()
flickerstreak@90 31 ReAction:GetModule("State"):UnregisterStateProperty("page")
flickerstreak@90 32 end
flickerstreak@90 33
flickerstreak@24 34
flickerstreak@48 35
flickerstreak@90 36 ---- Interface ----
flickerstreak@90 37 function module:GetBarOptions(bar)
Flick@234 38 self.handles[bar] = self.handles[bar] or Handle:New(bar)
Flick@234 39 return self.handles[bar]:GetOptions()
flickerstreak@90 40 end
flickerstreak@90 41
flickerstreak@90 42
flickerstreak@90 43 ---- Bar Handle ----
flickerstreak@90 44
flickerstreak@87 45 do
flickerstreak@87 46 local options = {
flickerstreak@87 47 hideEmpty = {
flickerstreak@87 48 name = L["Hide Empty Buttons"],
flickerstreak@87 49 order = 1,
flickerstreak@87 50 type = "toggle",
flickerstreak@87 51 width = "double",
flickerstreak@87 52 get = "GetHideEmpty",
flickerstreak@87 53 set = "SetHideEmpty",
flickerstreak@87 54 },
flickerstreak@102 55 lockButtons = {
flickerstreak@102 56 name = L["Lock Buttons"],
flickerstreak@147 57 desc = L["Prevents picking up/dragging actions (use SHIFT to override this behavior)"],
flickerstreak@102 58 order = 2,
flickerstreak@102 59 type = "toggle",
flickerstreak@102 60 get = "GetLockButtons",
flickerstreak@102 61 set = "SetLockButtons",
flickerstreak@102 62 },
flickerstreak@102 63 lockOnlyCombat = {
flickerstreak@102 64 name = L["Only in Combat"],
flickerstreak@102 65 desc = L["Only lock the buttons when in combat"],
flickerstreak@102 66 order = 3,
flickerstreak@102 67 type = "toggle",
flickerstreak@102 68 disabled = "LockButtonsCombatDisabled",
flickerstreak@102 69 get = "GetLockButtonsCombat",
flickerstreak@102 70 set = "SetLockButtonsCombat",
flickerstreak@102 71 },
flickerstreak@87 72 pages = {
flickerstreak@87 73 name = L["# Pages"],
flickerstreak@87 74 desc = L["Use the Dynamic State tab to specify page transitions"],
flickerstreak@102 75 order = 4,
flickerstreak@87 76 type = "range",
flickerstreak@87 77 min = 1,
flickerstreak@87 78 max = 10,
flickerstreak@87 79 step = 1,
flickerstreak@87 80 get = "GetNumPages",
flickerstreak@87 81 set = "SetNumPages",
flickerstreak@87 82 },
flickerstreak@90 83 mindcontrol = {
flickerstreak@90 84 name = L["Mind Control Support"],
flickerstreak@90 85 desc = L["When possessing a target (e.g. via Mind Control), map the first 12 buttons of this bar to the possessed target's actions."],
flickerstreak@102 86 order = 5,
flickerstreak@90 87 type = "toggle",
flickerstreak@90 88 width = "double",
flickerstreak@90 89 set = "SetMindControl",
flickerstreak@90 90 get = "GetMindControl",
flickerstreak@90 91 },
flickerstreak@121 92 vehicle = {
flickerstreak@121 93 name = L["Vehicle Support"],
flickerstreak@121 94 desc = L["When on a vehicle, map the first 6 buttons of this bar to the vehicle actions. The vehicle-exit button is mapped to the 7th button. Pitch controls are not supported."],
flickerstreak@121 95 order = 6,
flickerstreak@121 96 type = "toggle",
flickerstreak@121 97 width = "double",
flickerstreak@121 98 get = "GetVehicle",
flickerstreak@121 99 set = "SetVehicle",
flickerstreak@121 100 },
flickerstreak@87 101 actions = {
flickerstreak@87 102 name = L["Edit Action IDs"],
flickerstreak@121 103 order = 7,
flickerstreak@87 104 type = "group",
flickerstreak@87 105 inline = true,
flickerstreak@87 106 args = {
flickerstreak@87 107 method = {
flickerstreak@87 108 name = L["Assign"],
flickerstreak@87 109 order = 1,
flickerstreak@87 110 type = "select",
flickerstreak@87 111 width = "double",
flickerstreak@87 112 values = { [0] = L["Choose Method..."],
flickerstreak@87 113 [1] = L["Individually"],
flickerstreak@87 114 [2] = L["All at Once"], },
flickerstreak@87 115 get = "GetActionEditMethod",
flickerstreak@87 116 set = "SetActionEditMethod",
flickerstreak@87 117 },
flickerstreak@87 118 rowSelect = {
flickerstreak@87 119 name = L["Row"],
flickerstreak@87 120 desc = L["Rows are numbered top to bottom"],
flickerstreak@87 121 order = 2,
flickerstreak@87 122 type = "select",
flickerstreak@87 123 width = "half",
flickerstreak@87 124 hidden = "IsButtonSelectHidden",
flickerstreak@87 125 values = "GetRowList",
flickerstreak@87 126 get = "GetSelectedRow",
flickerstreak@87 127 set = "SetSelectedRow",
flickerstreak@87 128 },
flickerstreak@87 129 colSelect = {
flickerstreak@87 130 name = L["Col"],
flickerstreak@87 131 desc = L["Columns are numbered left to right"],
flickerstreak@87 132 order = 3,
flickerstreak@87 133 type = "select",
flickerstreak@87 134 width = "half",
flickerstreak@87 135 hidden = "IsButtonSelectHidden",
flickerstreak@87 136 values = "GetColumnList",
flickerstreak@87 137 get = "GetSelectedColumn",
flickerstreak@87 138 set = "SetSelectedColumn",
flickerstreak@87 139 },
flickerstreak@87 140 pageSelect = {
flickerstreak@87 141 name = L["Page"],
flickerstreak@87 142 order = 4,
flickerstreak@87 143 type = "select",
flickerstreak@87 144 width = "half",
flickerstreak@87 145 hidden = "IsPageSelectHidden",
flickerstreak@87 146 values = "GetPageList",
flickerstreak@87 147 get = "GetSelectedPage",
flickerstreak@87 148 set = "SetSelectedPage",
flickerstreak@87 149 },
flickerstreak@87 150 single = {
flickerstreak@87 151 name = L["Action ID"],
flickerstreak@87 152 usage = L["Specify ID 1-120"],
flickerstreak@87 153 order = 5,
flickerstreak@87 154 type = "input",
flickerstreak@87 155 width = "half",
flickerstreak@87 156 hidden = "IsButtonSelectHidden",
flickerstreak@87 157 get = "GetActionID",
flickerstreak@87 158 set = "SetActionID",
flickerstreak@87 159 validate = "ValidateActionID",
flickerstreak@87 160 },
flickerstreak@87 161 multi = {
flickerstreak@87 162 name = L["ID List"],
flickerstreak@87 163 usage = L["Specify a comma-separated list of IDs for each button in the bar (in order). Separate multiple pages with semicolons (;)"],
flickerstreak@87 164 order = 6,
flickerstreak@87 165 type = "input",
flickerstreak@87 166 multiline = true,
flickerstreak@87 167 width = "double",
flickerstreak@87 168 hidden = "IsMultiIDHidden",
flickerstreak@87 169 get = "GetMultiID",
flickerstreak@87 170 set = "SetMultiID",
flickerstreak@87 171 validate = "ValidateMultiID",
flickerstreak@90 172 },
flickerstreak@90 173 },
flickerstreak@87 174 },
flickerstreak@87 175 }
flickerstreak@77 176
flickerstreak@90 177 local meta = { __index = Handle }
flickerstreak@90 178
flickerstreak@220 179 function Handle:New( bar )
Flick@234 180 return setmetatable(
flickerstreak@90 181 {
flickerstreak@90 182 bar = bar,
Flick@234 183 config = bar:GetConfig(),
flickerstreak@90 184 },
flickerstreak@90 185 meta)
flickerstreak@90 186 end
flickerstreak@90 187
flickerstreak@90 188 function Handle:Refresh()
Flick@234 189 Button:SetupBar(bar)
flickerstreak@90 190 end
flickerstreak@90 191
flickerstreak@102 192 function Handle:UpdateButtonLock()
flickerstreak@128 193 Button.SetButtonLock(self.bar, self.config.lockButtons, self.config.lockButtonsCombat)
flickerstreak@102 194 end
flickerstreak@102 195
flickerstreak@90 196 function Handle:GetLastButton()
Flick@234 197 return self.bar:GetButton(self.bar:GetNumButtons())
flickerstreak@90 198 end
flickerstreak@90 199
flickerstreak@90 200 -- options handlers
flickerstreak@90 201 function Handle:GetOptions()
flickerstreak@87 202 return {
flickerstreak@87 203 type = "group",
flickerstreak@87 204 name = L["Action Buttons"],
flickerstreak@90 205 handler = self,
flickerstreak@87 206 args = options
flickerstreak@87 207 }
flickerstreak@77 208 end
flickerstreak@77 209
flickerstreak@90 210 function Handle:SetHideEmpty(info, value)
flickerstreak@90 211 if value ~= self.config.hideEmpty then
flickerstreak@90 212 self.config.hideEmpty = value
Flick@234 213 for _, b in self.bar:IterateButtons() do
flickerstreak@128 214 b:ShowGrid(not value)
flickerstreak@128 215 end
flickerstreak@77 216 end
flickerstreak@77 217 end
flickerstreak@77 218
flickerstreak@90 219 function Handle:GetHideEmpty()
flickerstreak@90 220 return self.config.hideEmpty
flickerstreak@77 221 end
flickerstreak@87 222
flickerstreak@102 223 function Handle:GetLockButtons()
flickerstreak@128 224 return self.config.lockButtons
flickerstreak@102 225 end
flickerstreak@102 226
flickerstreak@102 227 function Handle:SetLockButtons(info, value)
flickerstreak@102 228 self.config.lockButtons = value
flickerstreak@102 229 self:UpdateButtonLock()
flickerstreak@102 230 end
flickerstreak@102 231
flickerstreak@102 232 function Handle:GetLockButtonsCombat()
flickerstreak@102 233 return self.config.lockButtonsCombat
flickerstreak@102 234 end
flickerstreak@102 235
flickerstreak@102 236 function Handle:SetLockButtonsCombat(info, value)
flickerstreak@102 237 self.config.lockButtonsCombat = value
flickerstreak@102 238 self:UpdateButtonLock()
flickerstreak@102 239 end
flickerstreak@102 240
flickerstreak@102 241 function Handle:LockButtonsCombatDisabled()
flickerstreak@128 242 return not self.config.lockButtons
flickerstreak@102 243 end
flickerstreak@102 244
flickerstreak@90 245 function Handle:GetNumPages()
flickerstreak@90 246 return self.config.nPages
flickerstreak@87 247 end
flickerstreak@87 248
flickerstreak@90 249 function Handle:SetNumPages(info, value)
flickerstreak@90 250 self.config.nPages = value
flickerstreak@90 251 self:Refresh()
flickerstreak@87 252 end
flickerstreak@87 253
flickerstreak@90 254 function Handle:GetMindControl()
flickerstreak@90 255 return self.config.mindcontrol
flickerstreak@90 256 end
flickerstreak@90 257
flickerstreak@90 258 function Handle:SetMindControl(info, value)
flickerstreak@90 259 self.config.mindcontrol = value
flickerstreak@90 260 self:Refresh()
flickerstreak@90 261 end
flickerstreak@90 262
flickerstreak@121 263 function Handle:GetVehicle()
flickerstreak@121 264 return self.config.vehicle
flickerstreak@121 265 end
flickerstreak@121 266
flickerstreak@121 267 function Handle:SetVehicle(info, value)
flickerstreak@121 268 self.config.vehicle = value
flickerstreak@121 269 self:Refresh()
flickerstreak@121 270 end
flickerstreak@121 271
flickerstreak@90 272 function Handle:GetActionEditMethod()
flickerstreak@87 273 return self.editMethod or 0
flickerstreak@87 274 end
flickerstreak@87 275
flickerstreak@90 276 function Handle:SetActionEditMethod(info, value)
flickerstreak@87 277 self.editMethod = value
flickerstreak@87 278 end
flickerstreak@87 279
flickerstreak@90 280 function Handle:IsButtonSelectHidden()
flickerstreak@87 281 return self.editMethod ~= 1
flickerstreak@87 282 end
flickerstreak@87 283
flickerstreak@90 284 function Handle:GetRowList()
flickerstreak@87 285 local r,c = self.bar:GetButtonGrid()
flickerstreak@87 286 if self.rowList == nil or #self.rowList ~= r then
flickerstreak@87 287 local list = { }
flickerstreak@87 288 for i = 1, r do
flickerstreak@87 289 table.insert(list,i)
flickerstreak@87 290 end
flickerstreak@87 291 self.rowList = list
flickerstreak@87 292 end
flickerstreak@87 293 return self.rowList
flickerstreak@87 294 end
flickerstreak@87 295
flickerstreak@90 296 function Handle:GetSelectedRow()
flickerstreak@87 297 local r, c = self.bar:GetButtonGrid()
flickerstreak@87 298 local row = self.selectedRow or 1
flickerstreak@87 299 if row > r then
flickerstreak@87 300 row = 1
flickerstreak@87 301 end
flickerstreak@87 302 self.selectedRow = row
flickerstreak@87 303 return row
flickerstreak@87 304 end
flickerstreak@87 305
flickerstreak@90 306 function Handle:SetSelectedRow(info, value)
flickerstreak@87 307 self.selectedRow = value
flickerstreak@87 308 end
flickerstreak@87 309
flickerstreak@90 310 function Handle:GetColumnList()
flickerstreak@87 311 local r,c = self.bar:GetButtonGrid()
flickerstreak@87 312 if self.columnList == nil or #self.columnList ~= c then
flickerstreak@87 313 local list = { }
flickerstreak@87 314 for i = 1, c do
flickerstreak@87 315 table.insert(list,i)
flickerstreak@87 316 end
flickerstreak@87 317 self.columnList = list
flickerstreak@87 318 end
flickerstreak@87 319 return self.columnList
flickerstreak@87 320 end
flickerstreak@87 321
flickerstreak@90 322 function Handle:GetSelectedColumn()
flickerstreak@87 323 local r, c = self.bar:GetButtonGrid()
flickerstreak@87 324 local col = self.selectedColumn or 1
flickerstreak@87 325 if col > c then
flickerstreak@87 326 col = 1
flickerstreak@87 327 end
flickerstreak@87 328 self.selectedColumn = col
flickerstreak@87 329 return col
flickerstreak@87 330 end
flickerstreak@87 331
flickerstreak@90 332 function Handle:SetSelectedColumn(info, value)
flickerstreak@87 333 self.selectedColumn = value
flickerstreak@87 334 end
flickerstreak@87 335
flickerstreak@90 336 function Handle:IsPageSelectHidden()
flickerstreak@90 337 return self.editMethod ~= 1 or (self.config.nPages or 1) < 2
flickerstreak@87 338 end
flickerstreak@87 339
flickerstreak@90 340 function Handle:GetPageList()
flickerstreak@90 341 local n = self.config.nPages or 1
flickerstreak@87 342 if self.pageList == nil or #self.pageList ~= n then
flickerstreak@87 343 local p = { }
flickerstreak@87 344 for i = 1, n do
flickerstreak@87 345 table.insert(p,i)
flickerstreak@87 346 end
flickerstreak@87 347 self.pageList = p
flickerstreak@87 348 end
flickerstreak@87 349 return self.pageList
flickerstreak@87 350 end
flickerstreak@87 351
flickerstreak@90 352 function Handle:GetSelectedPage()
flickerstreak@87 353 local p = self.selectedPage or 1
flickerstreak@90 354 if p > (self.config.nPages or 1) then
flickerstreak@87 355 p = 1
flickerstreak@87 356 end
flickerstreak@87 357 self.selectedPage = p
flickerstreak@87 358 return p
flickerstreak@87 359 end
flickerstreak@87 360
flickerstreak@90 361 function Handle:SetSelectedPage(info, value)
flickerstreak@87 362 self.selectedPage = value
flickerstreak@87 363 end
flickerstreak@87 364
flickerstreak@90 365 function Handle:GetActionID()
flickerstreak@87 366 local row = self.selectedRow or 1
flickerstreak@87 367 local col = self.selectedColumn or 1
flickerstreak@87 368 local r, c = self.bar:GetButtonGrid()
flickerstreak@87 369 local n = (row-1) * c + col
Flick@234 370 local btn = self.bar:GetButton(n)
flickerstreak@87 371 if btn then
flickerstreak@87 372 return tostring(btn:GetActionID(self.selectedPage or 1))
flickerstreak@87 373 end
flickerstreak@87 374 end
flickerstreak@87 375
flickerstreak@90 376 function Handle:SetActionID(info, value)
flickerstreak@87 377 local row = self.selectedRow or 1
flickerstreak@87 378 local col = self.selectedColumn or 1
flickerstreak@87 379 local r, c = self.bar:GetButtonGrid()
flickerstreak@87 380 local n = (row-1) * c + col
Flick@234 381 local btn = self.bar:GetButton(n)
flickerstreak@87 382 if btn then
flickerstreak@87 383 btn:SetActionID(tonumber(value), self.selectedPage or 1)
flickerstreak@87 384 end
flickerstreak@87 385 end
flickerstreak@87 386
flickerstreak@90 387 function Handle:ValidateActionID(info, value)
flickerstreak@87 388 value = tonumber(value)
flickerstreak@87 389 if value == nil or value < 1 or value > 120 then
flickerstreak@87 390 return L["Specify ID 1-120"]
flickerstreak@87 391 end
flickerstreak@87 392 return true
flickerstreak@87 393 end
flickerstreak@87 394
flickerstreak@90 395 function Handle:IsMultiIDHidden()
flickerstreak@87 396 return self.editMethod ~= 2
flickerstreak@87 397 end
flickerstreak@87 398
flickerstreak@90 399 function Handle:GetMultiID()
flickerstreak@87 400 local p = { }
flickerstreak@90 401 for i = 1, self.config.nPages or 1 do
flickerstreak@87 402 local b = { }
Flick@234 403 for _, btn in self.bar:IterateButtons() do
flickerstreak@87 404 table.insert(b, btn:GetActionID(i))
flickerstreak@87 405 end
flickerstreak@87 406 table.insert(p, table.concat(b,","))
flickerstreak@87 407 end
flickerstreak@87 408 return table.concat(p,";\n")
flickerstreak@87 409 end
flickerstreak@87 410
flickerstreak@87 411
flickerstreak@87 412 local function ParseMultiID(nBtns, nPages, s)
flickerstreak@87 413 if s:match("[^%d%s,;]") then
flickerstreak@87 414 return nil
flickerstreak@87 415 end
flickerstreak@87 416 local p = { }
flickerstreak@87 417 for list in s:gmatch("[^;]+") do
flickerstreak@87 418 local pattern = ("^%s?$"):format(("%s*(%d+)%s*,"):rep(nBtns))
flickerstreak@87 419 local ids = { list:match(pattern) }
flickerstreak@87 420 if #ids ~= nBtns then
flickerstreak@87 421 return nil
flickerstreak@87 422 end
flickerstreak@87 423 table.insert(p,ids)
flickerstreak@87 424 end
flickerstreak@87 425 if #p ~= nPages then
flickerstreak@87 426 return nil
flickerstreak@87 427 end
flickerstreak@87 428 return p
flickerstreak@87 429 end
flickerstreak@87 430
flickerstreak@90 431 function Handle:SetMultiID(info, value)
Flick@234 432 local p = ParseMultiID(self.bar:GetNumButtons(), self.config.nPages or 1, value)
flickerstreak@87 433 for page, b in ipairs(p) do
flickerstreak@87 434 for button, id in ipairs(b) do
Flick@234 435 self.bar:GetButton(button):SetActionID(id, page)
flickerstreak@87 436 end
flickerstreak@87 437 end
flickerstreak@87 438 end
flickerstreak@87 439
flickerstreak@90 440 function Handle:ValidateMultiID(info, value)
flickerstreak@87 441 local bad = L["Invalid action ID list string"]
Flick@234 442 if value == nil or ParseMultiID(self.bar:GetNumButtons(), self.config.nPages or 1, value) == nil then
flickerstreak@87 443 return bad
flickerstreak@87 444 end
flickerstreak@87 445 return true
flickerstreak@87 446 end
flickerstreak@77 447 end
flickerstreak@77 448
flickerstreak@77 449
flickerstreak@87 450 ------ State property options ------
flickerstreak@87 451 do
flickerstreak@87 452 local pageOptions = {
flickerstreak@87 453 page = {
flickerstreak@92 454 name = L["Show Page #"],
flickerstreak@92 455 order = 11,
flickerstreak@92 456 type = "select",
flickerstreak@92 457 width = "half",
flickerstreak@87 458 disabled = "IsPageDisabled",
flickerstreak@87 459 hidden = "IsPageHidden",
flickerstreak@87 460 values = "GetPageValues",
flickerstreak@87 461 set = "SetProp",
flickerstreak@87 462 get = "GetPage",
flickerstreak@87 463 },
flickerstreak@87 464 }
flickerstreak@50 465
flickerstreak@90 466 function PropHandler.GetOptions()
flickerstreak@90 467 return pageOptions
flickerstreak@87 468 end
flickerstreak@87 469
flickerstreak@90 470 function PropHandler:IsPageDisabled()
flickerstreak@220 471 local c = self.bar:GetConfig()
flickerstreak@90 472 local n = c and c.nPages or 1
flickerstreak@90 473 return not (n > 1)
flickerstreak@87 474 end
flickerstreak@87 475
flickerstreak@90 476 function PropHandler:IsPageHidden()
flickerstreak@220 477 return not self.bar:GetConfig()
flickerstreak@87 478 end
flickerstreak@87 479
flickerstreak@90 480 function PropHandler:GetPageValues()
flickerstreak@92 481 if not self._pagevalues then
flickerstreak@92 482 self._pagevalues = { }
flickerstreak@92 483 end
flickerstreak@220 484 local c = self.bar:GetConfig()
flickerstreak@87 485 if c then
flickerstreak@87 486 local n = c.nPages
flickerstreak@92 487 -- cache the results
flickerstreak@87 488 if self._npages ~= n then
flickerstreak@87 489 self._npages = n
flickerstreak@92 490 wipe(self._pagevalues)
flickerstreak@87 491 for i = 1, n do
flickerstreak@90 492 self._pagevalues["page"..i] = i
flickerstreak@87 493 end
flickerstreak@87 494 end
flickerstreak@87 495 end
flickerstreak@92 496 return self._pagevalues
flickerstreak@87 497 end
flickerstreak@87 498
flickerstreak@90 499 function PropHandler:GetPage(info)
flickerstreak@87 500 return self:GetProp(info) or 1
flickerstreak@87 501 end
flickerstreak@90 502
flickerstreak@87 503 end
flickerstreak@87 504