Mercurial > wow > buffalo2
comparison ObjectiveTracker/Widgets.lua @ 28:c33c17dd97e7
file renames
| author | Nenue |
|---|---|
| date | Wed, 13 Apr 2016 20:19:37 -0400 |
| parents | |
| children | 64f2a9bbea79 |
comparison
equal
deleted
inserted
replaced
| 27:c3aa94bc6be2 | 28:c33c17dd97e7 |
|---|---|
| 1 local B = select(2,...).frame | |
| 2 local T = B:RegisterModule("ObjectiveTracker", _G.VeneerObjectiveWrapper, 'BuffFrame') | |
| 3 local print = B.print('WidgetFactory') | |
| 4 local _G, UIParent = _G, UIParent | |
| 5 local GetQuestLogSpecialItemInfo, IsQuestLogSpecialItemInRange, GetQuestLogSpecialItemCooldown = GetQuestLogSpecialItemInfo, IsQuestLogSpecialItemInRange, GetQuestLogSpecialItemCooldown | |
| 6 local CooldownFrame_SetTimer, SetItemButtonTextureVertexColor, CreateFrame, VeneerObjectiveScroll = CooldownFrame_SetTimer, SetItemButtonTextureVertexColor, CreateFrame, VeneerObjectiveScroll | |
| 7 local tremove, tContains, pairs, ipairs, setmetatable, floor = tremove, tContains, pairs, ipairs, setmetatable, floor | |
| 8 local SetItemButtonTexture, SetItemButtonCount = SetItemButtonTexture, SetItemButtonCount | |
| 9 local ToggleWorldMap, GetTrackedAchievements, GetTasksTable = ToggleWorldMap, GetTrackedAchievements, GetTasksTable | |
| 10 -- GLOBALS: Veneer_QuestObjectiveItem_UpdateCooldown, Veneer_QuestObjectiveItem_OnUpdate | |
| 11 --- frame refs | |
| 12 local Wrapper = _G.VeneerObjectiveWrapper | |
| 13 local Scroller = Wrapper.scrollArea | |
| 14 local CloseButton = Wrapper.CloseButton | |
| 15 local QuestMapButton = Wrapper.QuestMapButton | |
| 16 local Scroll = _G.VeneerObjectiveScroll | |
| 17 local usedButtons = T.Quest.itemButtons | |
| 18 local freeButtons = T.Quest.freeButtons | |
| 19 | |
| 20 T.buttons = { | |
| 21 CloseButton = { | |
| 22 closedSwatch = { | |
| 23 [[Interface\Buttons\UI-Panel-QuestHideButton]], | |
| 24 [[Interface\Buttons\UI-Panel-QuestHideButton]], | |
| 25 0, 0.5, 0.5, 1, | |
| 26 0.5, 1, 0.5, 1, | |
| 27 }, | |
| 28 openSwatch = { | |
| 29 [[Interface\Buttons\UI-Panel-QuestHideButton]], | |
| 30 [[Interface\Buttons\UI-Panel-QuestHideButton]], | |
| 31 0.5, 1, 0.5, 1, | |
| 32 0, 0.5, 0.5, 1, | |
| 33 }, | |
| 34 parent = 'VeneerObjectiveWrapper' | |
| 35 }, | |
| 36 QuestMapButton = { | |
| 37 closedSwatch = { | |
| 38 [[Interface\QUESTFRAME\UI-QUESTMAP_BUTTON]], | |
| 39 [[Interface\QUESTFRAME\UI-QUESTMAP_BUTTON]], | |
| 40 0, 1, 0.5, 1, | |
| 41 0, 1, 0, 0.5, | |
| 42 }, | |
| 43 openSwatch = { | |
| 44 [[Interface\QUESTFRAME\UI-QUESTMAP_BUTTON]], | |
| 45 [[Interface\QUESTFRAME\UI-QUESTMAP_BUTTON]], | |
| 46 0, 1, 0, 0.5, | |
| 47 0, 1, 0.5, 1, | |
| 48 } | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 local Scroller_OnShow = function() | |
| 53 Wrapper.watchMoneyReasons = 0; | |
| 54 --T:Update() | |
| 55 --T:OnInitialize() | |
| 56 for i, region in ipairs(Wrapper.headerComplex) do | |
| 57 region:Show() | |
| 58 end | |
| 59 end | |
| 60 | |
| 61 local Scroller_OnHide = function() | |
| 62 local self = Wrapper | |
| 63 Wrapper:UnregisterAllEvents() | |
| 64 Wrapper:SetScript('OnEvent', nil) | |
| 65 for i, region in ipairs(Wrapper.headerComplex) do | |
| 66 region:Hide() | |
| 67 end | |
| 68 end | |
| 69 | |
| 70 local Scroller_OnMouseWheel = function(self, delta) | |
| 71 local r = Scroll:GetHeight() - Scroller:GetHeight() | |
| 72 local s = B.Conf.ObjectiveScroll - delta * floor(r/5+.5) | |
| 73 local from = self:GetVerticalScroll() | |
| 74 print('|cFF00FF00OnMouseWheel', 'scroll =', s) | |
| 75 if s >= r then | |
| 76 s = r | |
| 77 elseif s < 1 then | |
| 78 s = 0 | |
| 79 end | |
| 80 self:SetVerticalScroll(s) | |
| 81 B.Conf.ObjectiveScroll = s | |
| 82 print('|cFF00FF00OnMouseWheel', 'from = ', from, 'scroll =', s, ' range =', r, 'current =', self:GetVerticalScroll()) | |
| 83 | |
| 84 T.UpdateActionButtons('SCROLLING') | |
| 85 end | |
| 86 | |
| 87 local UpdatePanelButton = function (self, state) | |
| 88 state = state or true | |
| 89 local swatch = (state == true) and self.openSwatch or self.closedSwatch | |
| 90 print((state ~= true) and 'closedSwatch' or 'openSwatch') | |
| 91 self:SetNormalTexture(swatch[1]) | |
| 92 self:SetPushedTexture(swatch[2]) | |
| 93 if #swatch >= 6 then | |
| 94 self:GetNormalTexture():SetTexCoord(swatch[3], swatch[4], swatch[5], swatch[6]) | |
| 95 end | |
| 96 if #swatch == 10 then | |
| 97 self:GetPushedTexture():SetTexCoord(swatch[7], swatch[8], swatch[9], swatch[10]) | |
| 98 end | |
| 99 | |
| 100 end | |
| 101 | |
| 102 local OnClick = {} | |
| 103 OnClick.CloseButton = function(self) | |
| 104 T:SetEnabled() | |
| 105 UpdatePanelButton(self, T.Conf.enabled) | |
| 106 end | |
| 107 | |
| 108 OnClick.QuestMapButton = function() | |
| 109 ToggleWorldMap() | |
| 110 end | |
| 111 | |
| 112 | |
| 113 T.InitializeWidgets = function() | |
| 114 local panelButtons = T.buttons | |
| 115 --- tracker scroll | |
| 116 Scroller:SetScript('OnMouseWheel', Scroller_OnMouseWheel) | |
| 117 Scroller:SetScript('OnShow', Scroller_OnShow) | |
| 118 Scroller:SetScript('OnHide', Scroller_OnHide) | |
| 119 for name, swatch in pairs(panelButtons) do | |
| 120 local source = swatch and swatch or panelButtons.CloseButton | |
| 121 local button = Wrapper[name] | |
| 122 button.parent = swatch.parent | |
| 123 button.openSwatch = source.openSwatch | |
| 124 button.closedSwatch = source.closedSwatch | |
| 125 if OnClick[name] then | |
| 126 button:SetScript('OnClick', OnClick[name]) | |
| 127 end | |
| 128 UpdatePanelButton(button, T.Conf.enabled) | |
| 129 end | |
| 130 end | |
| 131 | |
| 132 ---------------------------------------------------------------------------------------- | |
| 133 --- modified version of the itemButton initializer used by Barjack's 'QuestKing 2' addon, | |
| 134 --- url: http://mods.curse.com/addons/wow/questking | |
| 135 ---------------------------------------------------------------------------------------- | |
| 136 T.SetItemButton = function(block, info) | |
| 137 local itemInfo = info.specialItem | |
| 138 if not itemInfo then | |
| 139 return | |
| 140 end | |
| 141 | |
| 142 local itemButton | |
| 143 if not info.itemButton then | |
| 144 if #freeButtons >= 1 then | |
| 145 print(' |cFF00FFFFfound a free button') | |
| 146 itemButton = freeButtons[#freeButtons] | |
| 147 freeButtons[#freeButtons] = nil | |
| 148 if itemButton.block then | |
| 149 itemButton.block.itemButton = nil | |
| 150 itemButton.block = nil | |
| 151 end | |
| 152 else | |
| 153 local buttonIndex = T.Quest.numButtons + #freeButtons + 1 | |
| 154 itemButton = CreateFrame('Button', 'VeneerQuestItemButton' .. buttonIndex, UIParent, 'VeneerItemButtonTemplate') | |
| 155 itemButton.buttonIndex = buttonIndex | |
| 156 itemButton:SetSize(36, 36) | |
| 157 itemButton:GetNormalTexture():SetSize(36 * (5/3), 36 * (5/3)) | |
| 158 print(' |cFFFF4400starting new button', itemButton:GetName()) | |
| 159 end | |
| 160 T.Quest.numButtons = T.Quest.numButtons + 1 | |
| 161 else | |
| 162 itemButton = info.itemButton | |
| 163 print(' |cFF00FF00found assigned button', itemButton:GetName()) | |
| 164 | |
| 165 end | |
| 166 -- set values | |
| 167 | |
| 168 info.itemButton = itemButton | |
| 169 usedButtons[info.questID] = itemButton | |
| 170 print(' |cFF8800FFassigning|r', itemButton:GetName(), 'to quest|cFF00FF00', info.questID, '|rat|cFFFFFF00', block:GetName(),'|r') | |
| 171 | |
| 172 for k,v in pairs(usedButtons) do | |
| 173 print('|cFFFF44DD'..k..'|r', v:GetName()) | |
| 174 end | |
| 175 | |
| 176 itemButton:SetAttribute("type", "item") | |
| 177 itemButton:SetAttribute("item", itemInfo.link) | |
| 178 | |
| 179 itemButton.questID = info.questID | |
| 180 itemButton.questLogIndex = info.questLogIndex | |
| 181 itemButton.charges = itemInfo.charges | |
| 182 itemButton.rangeTimer = -1 | |
| 183 itemButton.block = block | |
| 184 | |
| 185 SetItemButtonTexture(itemButton, itemInfo.icon) | |
| 186 SetItemButtonCount(itemButton, itemInfo.charges) | |
| 187 Veneer_QuestObjectiveItem_UpdateCooldown(itemButton); | |
| 188 | |
| 189 return itemButton | |
| 190 end | |
| 191 --- Clear an itemButton from the given block | |
| 192 T.FreeItemButtons = function(block) | |
| 193 | |
| 194 if block.itemButton then | |
| 195 local itemButton = block.itemButton | |
| 196 if itemButton.questID ~= block.info.questID then | |
| 197 block.itemButton = nil | |
| 198 itemButton.block = T.Quest.InfoBlock[itemButton.questID] | |
| 199 else | |
| 200 itemButton.block = nil | |
| 201 itemButton:Hide() | |
| 202 | |
| 203 usedButtons[itemButton.questID] = nil | |
| 204 freeButtons[#freeButtons + 1] = itemButton | |
| 205 T.Quest.numButtons = T.Quest.numButtons - 1 | |
| 206 print('|cFFFF0088released', itemButton:GetName(),'and', block:GetName()) | |
| 207 end | |
| 208 end | |
| 209 end | |
| 210 | |
| 211 function Veneer_QuestObjectiveItem_OnUpdate (self, elapsed) | |
| 212 -- Handle range indicator | |
| 213 local rangeTimer = self.rangeTimer | |
| 214 if (rangeTimer) then | |
| 215 rangeTimer = rangeTimer - elapsed | |
| 216 if (rangeTimer <= 0) then | |
| 217 local link, item, charges, showItemWhenComplete = GetQuestLogSpecialItemInfo(self.questLogIndex) | |
| 218 if ((not charges) or (charges ~= self.charges)) then | |
| 219 T:Update() | |
| 220 return | |
| 221 end | |
| 222 | |
| 223 local count = self.HotKey | |
| 224 local valid = IsQuestLogSpecialItemInRange(self.questLogIndex) | |
| 225 if (valid == 0) then | |
| 226 count:Show() | |
| 227 count:SetVertexColor(1.0, 0.1, 0.1) | |
| 228 elseif (valid == 1) then | |
| 229 count:Show() | |
| 230 count:SetVertexColor(0.6, 0.6, 0.6) | |
| 231 else | |
| 232 count:Hide() | |
| 233 end | |
| 234 rangeTimer = TOOLTIP_UPDATE_TIME | |
| 235 end | |
| 236 | |
| 237 self.rangeTimer = rangeTimer | |
| 238 end | |
| 239 end | |
| 240 | |
| 241 function Veneer_QuestObjectiveItem_UpdateCooldown (itemButton) | |
| 242 local start, duration, enable = GetQuestLogSpecialItemCooldown(itemButton.questLogIndex) | |
| 243 if (start) then | |
| 244 CooldownFrame_SetTimer(itemButton.Cooldown, start, duration, enable) | |
| 245 if (duration > 0 and enable == 0) then | |
| 246 SetItemButtonTextureVertexColor(itemButton, 0.4, 0.4, 0.4) | |
| 247 else | |
| 248 SetItemButtonTextureVertexColor(itemButton, 1, 1, 1) | |
| 249 end | |
| 250 end | |
| 251 end | |
| 252 | |
| 253 ----------------------------------------- | |
| 254 -- Criteria frames | |
| 255 | |
| 256 --[[ | |
| 257 text = description, | |
| 258 type = type, | |
| 259 finished = completed, | |
| 260 quantity = quantity, | |
| 261 requiredQuantity = requiredQuantity, | |
| 262 characterName = characterName, | |
| 263 flags = flags, | |
| 264 assetID = assetID, | |
| 265 quantityString = quantityString, | |
| 266 criteriaID = criteriaID, | |
| 267 ]] | |
| 268 local newWidgetID = 0 | |
| 269 T.WidgetRegistry = {} | |
| 270 local wr = T.WidgetRegistry | |
| 271 | |
| 272 --- Get a usable widget for the given achievement criteria set. | |
| 273 -- Returns a frame object with dimensioning parameters needed to size the receiving tracker block | |
| 274 T.SetWidget = function(line, data, objectiveType, objectiveKey) | |
| 275 local print = B.print('ObjectiveWidgets') | |
| 276 local widgetType = objectiveType | |
| 277 local widget | |
| 278 if wr[widgetType] and wr[widgetType].used[objectiveKey] then | |
| 279 widget = wr[widgetType].used[objectiveKey] | |
| 280 print('|cFF00FF00Updating ('..objectiveKey..')', widget) | |
| 281 elseif not wr[widgetType] or #wr[widgetType].free == 0 then | |
| 282 widget = CreateFrame('Frame', 'VeneerObjective' .. widgetType .. (wr[widgetType] and (wr[widgetType].lastn+1) or (1)), VeneerObjectiveScroll, 'VeneerObjectiveCriteria' .. widgetType) | |
| 283 | |
| 284 print('|cFFFF0088Creating `'..widget:GetName()..'` id', wr[widgetType].lastn) | |
| 285 else | |
| 286 widget = tremove(wr[widgetType].free) | |
| 287 print('|cFFFFFF00Acquiring released widget', widget:GetName()) | |
| 288 end | |
| 289 | |
| 290 | |
| 291 wr[widgetType].used[objectiveKey] = widget | |
| 292 widget.line = line | |
| 293 widget.objective = data | |
| 294 widget.key = objectiveKey | |
| 295 T.InitializeWidget(widget) | |
| 296 return widget | |
| 297 end | |
| 298 | |
| 299 --- WidgetTemplate 'OnLoad' | |
| 300 T.RegisterWidget = function(frame) | |
| 301 local print = B.print('ObjectiveWidgets') | |
| 302 local widgetType = frame.widgetType | |
| 303 if not wr[frame.widgetType] then | |
| 304 print('|cFFFF4400[[WidgetTemplate]]|r', widgetType) | |
| 305 wr[widgetType] = { lastn = 1, free = {}, used = {}, usedIndex = {}, freeIndex = {} } | |
| 306 else | |
| 307 print('|cFF0088FF+ [[WidgetTemplate]]r', widgetType, wr[widgetType].lastn) | |
| 308 wr[widgetType].lastn = wr[widgetType].lastn + 1 | |
| 309 end | |
| 310 end | |
| 311 | |
| 312 --- WidgetTemplate 'OnShow' | |
| 313 T.InitializeWidget = setmetatable({}, { | |
| 314 __call = function(t, frame) | |
| 315 -- todo: config pull | |
| 316 | |
| 317 frame:SetWidth(T.Conf.Wrapper.Width - T.Conf.Style.Format.status.Indent * 2) | |
| 318 frame:SetScript('OnEvent', T.UpdateWidget[frame.widgetType]) | |
| 319 frame:RegisterEvent('TRACKED_ACHIEVEMENT_UPDATE') | |
| 320 frame:RegisterEvent('TRACKED_ACHIEVEMENT_LIST_CHANGED') | |
| 321 frame:RegisterEvent('CRITERIA_UPDATE') | |
| 322 frame:RegisterEvent('CRITERIA_COMPLETE') | |
| 323 frame:RegisterEvent('CRITERIA_EARNED') | |
| 324 t[frame.widgetType](frame) | |
| 325 T.UpdateWidget[frame.widgetType](frame) | |
| 326 end, | |
| 327 }) | |
| 328 | |
| 329 --- WidgetTemplate 'OnEvent' | |
| 330 T.UpdateWidget = setmetatable({}, { | |
| 331 __call = function(t, frame) | |
| 332 if not frame.widgetType then | |
| 333 error('Invalid widget template, needs .widgetType') | |
| 334 return | |
| 335 end | |
| 336 | |
| 337 return t[frame.widgetType](frame) | |
| 338 end | |
| 339 }) | |
| 340 | |
| 341 --- WidgetTemplate 'OnHide' | |
| 342 T.ReleaseWidget = function(frame) | |
| 343 --[[ | |
| 344 local print = B.print('ObjectiveWidgets') | |
| 345 local reg = wr[frame.widgetType] | |
| 346 if reg and reg.used[frame.key] then | |
| 347 reg.used[frame.key] = nil | |
| 348 frame.line = nil | |
| 349 frame.info = nil | |
| 350 frame:UnregisterAllEvents() | |
| 351 tinsert(reg.free, frame) | |
| 352 print('|cFFBBBBBBreleased from service', frame:GetName()) | |
| 353 end | |
| 354 ]] | |
| 355 end | |
| 356 | |
| 357 --- RemoveTrackedAchievement post-hook | |
| 358 T.CleanWidgets = function() | |
| 359 local print = B.print('ObjectiveWidgets') | |
| 360 local tracked = {GetTrackedAchievements() } | |
| 361 local tasks = GetTasksTable() | |
| 362 for type, reg in pairs(T.WidgetRegistry) do | |
| 363 print('collecting', type) | |
| 364 for key, frame in pairs(reg.used) do | |
| 365 if frame.objective.cheevID then | |
| 366 local id = frame.objective.cheevID | |
| 367 | |
| 368 if id and not tContains(tracked, id) then | |
| 369 | |
| 370 print(' untracked achievement', id, 'associated with', key, frame:GetName()) | |
| 371 frame:Hide() | |
| 372 end | |
| 373 elseif frame.objective.questID then | |
| 374 -- do something for quest task | |
| 375 end | |
| 376 end | |
| 377 end | |
| 378 end | |
| 379 | |
| 380 | |
| 381 | |
| 382 T.defaults.WidgetStyle = { | |
| 383 | |
| 384 } | |
| 385 | |
| 386 local progressHeight = 16 | |
| 387 local progressBorder = 1 | |
| 388 local progressIndent = 3 | |
| 389 local progressFont = _G.VeneerCriteriaFontNormal | |
| 390 | |
| 391 | |
| 392 T.InitializeWidget.ProgressBar = function(self) | |
| 393 local c = T.Conf.Wrapper | |
| 394 self.height = progressHeight + c.TextSpacing | |
| 395 self.width = c.Width - c.TextSpacing | |
| 396 self.indent = progressIndent | |
| 397 | |
| 398 self:SetHeight(progressHeight) | |
| 399 self.bg:SetHeight(progressHeight) | |
| 400 self.bg:SetWidth(self.width) | |
| 401 self.fg:ClearAllPoints() | |
| 402 self.fg:SetPoint('BOTTOMLEFT', self, 'BOTTOMLEFT', progressBorder, progressBorder) | |
| 403 self.fg:SetHeight(progressHeight - progressBorder * 2) | |
| 404 self.status:SetFontObject(progressFont) | |
| 405 self.status:SetText(self.objective.quantityString) | |
| 406 end | |
| 407 | |
| 408 T.UpdateWidget.ProgressBar = function (self) | |
| 409 local quantity, requiredQuantity = self.objective.value, self.objective.maxValue | |
| 410 print('update vals:') | |
| 411 for k,v in pairs(self.line) do | |
| 412 print(k, v) | |
| 413 end | |
| 414 | |
| 415 if self.line.format then | |
| 416 self.status:SetFormattedText(self.line.format, quantity, requiredQuantity) | |
| 417 end | |
| 418 | |
| 419 local progress = (quantity / requiredQuantity) | |
| 420 if progress >= 1 then | |
| 421 self.fg:Show() | |
| 422 self.fg:SetWidth(self.width - progressBorder * 2) | |
| 423 elseif progress > 0 then | |
| 424 self.fg:Show() | |
| 425 print('color:', 1-progress*2 , progress*2 - 1,0,1) | |
| 426 print('width:', (self.width -progressBorder * 2) * progress) | |
| 427 self.fg:SetTexture(1-progress*2 , progress*2,0,1) | |
| 428 self.fg:SetWidth((self.width -progressBorder * 2) * progress) | |
| 429 else | |
| 430 self.fg:Hide() | |
| 431 end | |
| 432 end | |
| 433 | |
| 434 | |
| 435 T.InitializeWidget.Hidden = function (self) | |
| 436 self.height = 0 | |
| 437 end | |
| 438 T.UpdateWidget.Hidden = function (self) | |
| 439 self.height= 0 | |
| 440 end |
