comparison AskMrRobotUi.lua @ 0:ec731d2fe6ba

Version 1.2.12.0
author Adam tegen <adam.tegen@gmail.com>
date Tue, 20 May 2014 21:43:23 -0500
parents
children ece9167c0d1c
comparison
equal deleted inserted replaced
-1:000000000000 0:ec731d2fe6ba
1 local _, AskMrRobot = ...
2
3 local showImportDetailsError = nil
4 local showImportErrorTab = nil
5
6 AskMrRobot.AmrUI = AskMrRobot.inheritsFrom(AskMrRobot.Frame)
7
8 function AskMrRobot.AmrUI:swapSimilarSlotsIfNeeded(slotName1, slotName2)
9 local slotId1 = GetInventorySlotInfo(slotName1)
10 local slotId2 = GetInventorySlotInfo(slotName2)
11 local slotNum1 = AskMrRobot.slotIdToSlotNum[slotId1]
12 local slotNum2 = AskMrRobot.slotIdToSlotNum[slotId2]
13
14 local itemLink1 = GetInventoryItemLink("player", slotId1)
15 local itemLink2 = GetInventoryItemLink("player", slotId2)
16
17 -- see how bad the items are in the original order
18 AskMrRobot.populateItemDiffs(self.importedItems[slotNum1], itemLink1, slotNum1)
19 AskMrRobot.populateItemDiffs(self.importedItems[slotNum2], itemLink2, slotNum2)
20
21 local badCountOriginalOrder = 0
22 if AskMrRobot.itemDiffs.items[slotNum1] then badCountOriginalOrder = badCountOriginalOrder + 1 end
23 if AskMrRobot.itemDiffs.items[slotNum2] then badCountOriginalOrder = badCountOriginalOrder + 1 end
24
25 -- try the order swapped
26 AskMrRobot.populateItemDiffs(self.importedItems[slotNum1], itemLink2, slotNum1)
27 AskMrRobot.populateItemDiffs(self.importedItems[slotNum2], itemLink1, slotNum2)
28
29 local badCountNewOrder = 0
30 if AskMrRobot.itemDiffs.items[slotNum1] then badCountNewOrder = badCountNewOrder + 1 end
31 if AskMrRobot.itemDiffs.items[slotNum2] then badCountNewOrder = badCountNewOrder + 1 end
32
33 -- if the items were less bad in the new order, swap the imported items
34 if badCountNewOrder < badCountOriginalOrder then
35 local tempItem = self.importedItems[slotNum1]
36 self.importedItems[slotNum1] = self.importedItems[slotNum2]
37 self.importedItems[slotNum2] = tempItem
38 end
39 end
40
41 function AskMrRobot.AmrUI:displayImportItems()
42 if not self.importedItems then
43 return false
44 end
45
46 --see if rings or trinkets are swapped, and alter self.importedItems accordingly
47 self:swapSimilarSlotsIfNeeded("Finger0Slot", "Finger1Slot")
48 self:swapSimilarSlotsIfNeeded("Trinket0Slot", "Trinket1Slot")
49
50 for slotNum = 1, #AskMrRobot.slotIds do
51 if AskMrRobot.OptimizationSlots[slotNum] then
52 local slotId = AskMrRobot.slotIds[slotNum]
53 local itemLink = GetInventoryItemLink("player", slotId)
54 AskMrRobot.populateItemDiffs(self.importedItems[slotNum], itemLink, slotNum)
55 end
56 end
57
58 self.summaryTab:showBadItems()
59
60 -- if there are incorrect items equiped, display errors on other tabs
61 if AskMrRobot.itemDiffs and AskMrRobot.itemDiffs.items then
62 for k,v in pairs(AskMrRobot.itemDiffs.items) do
63 if not v.needsUpgrade then
64 self.hasImportError = true
65 end
66 end
67 end
68
69 if self.hasImportError then
70 showImportDetailsError()
71 else
72 self.gemTab:Update()
73 self.enchantTab:showBadEnchants()
74 self.reforgeTab:Render()
75 self.shoppingTab:Update()
76 end
77 end
78
79 function AskMrRobot.AmrUI:showImportError(text, ...)
80 self.summaryTab:showImportError(text, ...)
81 if text then
82 self.hasImportError = true
83 showImportDetailsError()
84 else
85 self.hasImportError = false
86 end
87 end
88
89 function AskMrRobot.AmrUI:showImportWarning(text, ...)
90 self.summaryTab:showImportWarning(text, ...)
91 self.hasImportError = false
92 if text then
93 showImportDetailsError()
94 end
95 end
96
97 function AskMrRobot.AmrUI:validateInput(input)
98
99 self.importedItems = nil
100 self.mostlySuccess = false
101
102 local parsed = AskMrRobot.parseAmr(input)
103
104 if not parsed.realm then
105 self:showImportError("Oops, you didn't have proper import text", "Please go back to AskMrRobot.com and grab optimizations for this character")
106 elseif not AskMrRobot.validateCharacterName(parsed.name) then
107 self:showImportError("Oops, you've imported optimizations for " .. parsed.name, "Please go back to AskMrRobot.com and grab optimizations for this character, who is much better looking anyway!")
108 elseif not AskMrRobot.validateRace(parsed.race) then
109 self:showImportError("It looks like your race may have changed, which affects the optimizations.", "Right now, Mr. Robot thinks you are a " .. parsed.race)
110 elseif not AskMrRobot.validateFaction(parsed.faction) then
111 self:showImportError("It looks like your faction may have changed.", "Right now, Mr. Robot thinks you belong to the " .. parsed.faction)
112 elseif not AskMrRobot.validateProfessions(parsed.professions) then
113 self:showImportError("Your professions have changed, which affects the optimizations.", "You will need to make sure your in-game professions match the professions on AskMrRobot.com when importing.")
114 elseif not AskMrRobot.validateSpec(parsed.spec) then
115 if parsed.spec and parsed.spec ~= 'nil' then
116 local _, specName = GetSpecializationInfoByID(parsed.spec)
117 self:showImportError("WARNING! Please check your character before proceeding:", "Change your spec to " .. specName .. ".")
118 else
119 self:showImportError("WARNING! Please check your character before proceeding:", "AskMrRobot.com did not expect to see a specialization.")
120 end
121 self.mostlySuccess = true
122 self.summaryTab.badRealm = nil
123 self.summaryTab.badTalents = nil
124 self.summaryTab.badGlyphs = nil
125 AmrImportString = input
126 else
127 self.summaryTab.badRealm = not AskMrRobot.validateRealm(parsed.realm) and parsed.realm
128 self.summaryTab.badTalents = not AskMrRobot.validateTalents(parsed.talents)
129 self.summaryTab.badGlyphs = not AskMrRobot.validateGlyphs(parsed.glyphs)
130 self.mostlySuccess = true
131 self:showImportError(nil)
132 AmrImportString = input
133 self.importedItems = parsed.items
134 return self:displayImportItems()
135 end
136 return false
137 end
138
139 local function createImportDetailsErrorTab(reforgeFrame)
140 local tab = CreateFrame("Frame", nil, reforgeFrame)
141 tab:SetPoint("TOPLEFT")
142 tab:SetPoint("BOTTOMRIGHT")
143 tab:Hide()
144
145 local text = tab:CreateFontString("AmrImportDetailsText1", "ARTWORK", "GameFontNormalLarge")
146 text:SetPoint("TOPLEFT", 0, -5)
147 text:SetText("Help")
148
149 local errorText1 = tab:CreateFontString("AmrImportDetailsText2", "ARTWORK", "GameFontRed")
150 errorText1:SetPoint("TOPLEFT", "AmrImportDetailsText1", "BOTTOMLEFT", 0, -20)
151 errorText1:SetText('You have no optimizations imported. Click the "Import" tab to get started.')
152 errorText1:SetPoint("RIGHT", -10, 0)
153 errorText1:SetWidth(errorText1:GetWidth())
154 errorText1:SetJustifyH("LEFT")
155
156 showImportDetailsError = function()
157 errorText1:SetText("I can't optimize yet. Please go to the summary tab for more information.")
158 end
159
160 showImportErrorTab = function(tabName)
161 if not tabName then
162 tab:Hide()
163 else
164 text:SetText(tabName)
165 tab:Show()
166 end
167 end
168
169 return tab
170 end
171
172 function AskMrRobot.AmrUI:createTabButtons()
173 local importTabButton = CreateFrame("Button", "AmrImportTabButton", self, "OptionsListButtonTemplate")
174
175 local buttons = {}
176 self.hasImportError = true
177
178 local function onTabButtonClick(clickedButton, event, ...)
179 showImportErrorTab(nil)
180 for i = 1, #buttons do
181 local button = buttons[i]
182 if clickedButton == button then
183 button.highlight:SetVertexColor(1, 1, 0)
184 button:LockHighlight()
185 if self.hasImportError and button.isImportDetails then
186 showImportErrorTab(button:GetText())
187 elseif button.element then
188 button.element:Show()
189 end
190 else
191 button.highlight:SetVertexColor(.196, .388, .8)
192 button:UnlockHighlight()
193 if button.element then
194 button.element:Hide()
195 end
196 end
197 end
198 end
199
200 local function createButton(text, spacing, isImportDetails)
201 local lastButton = #buttons
202 local i = lastButton + 1
203 local tabButton = CreateFrame("Button", "AmrTabButton" .. i, self, "OptionsListButtonTemplate")
204 tabButton.isImportDetails = isImportDetails
205 tabButton:SetText(text)
206 tabText = tabButton:GetFontString()
207 tabText:SetPoint("LEFT", 6, 0)
208 if i == 1 then
209 tabButton:SetPoint("TOPLEFT", 2, spacing)
210 else
211 tabButton:SetPoint("TOPLEFT", "AmrTabButton" .. lastButton, "BOTTOMLEFT", 0, spacing)
212 end
213 tabButton:SetWidth(125)
214 tabButton:SetHeight(20)
215 tinsert(buttons, tabButton)
216 tabButton:SetScript("OnClick", onTabButtonClick)
217 end
218
219 createButton("Import", -35, false)
220 createButton("Summary", -20, false)
221 createButton("Gems", 0, true)
222 createButton("Enchants", 0, true)
223 createButton("Reforges", 0, true)
224 createButton("Shopping List", 0, true)
225 createButton("Best in Bags", -20, false)
226 createButton("Combat Log", 0, false)
227 createButton("Help", -20, false)
228
229 return buttons
230 end
231
232 function AskMrRobot.AmrUI:new()
233 local o = AskMrRobot.Frame:new("AskMrRobot_Dialog", nil, "BasicFrameTemplateWithInset")
234
235 -- use the AmrUI class
236 setmetatable(o, { __index = AskMrRobot.AmrUI })
237
238 o:RegisterForDrag("LeftButton");
239 o:SetWidth(600)
240 o:SetHeight(530)
241 o.InsetBg:SetPoint("TOPLEFT", 125, -24)
242
243 o:SetParent("UIParent")
244 o:SetPoint("CENTER")
245 o:Hide()
246 o:EnableMouse(true)
247 o:EnableKeyboard(true)
248 o.hideOnEscape = 1
249 o:SetMovable(true)
250 o:SetToplevel(true)
251
252 --o:SetScript("OnEscapePressed", function)
253 o:SetScript("OnDragStart", AskMrRobot.AmrUI.OnDragStart)
254 o:SetScript("OnDragStop", AskMrRobot.AmrUI.OnDragStop)
255 o:SetScript("OnHide", AskMrRobot.AmrUI.OnHide)
256 -- make the UI show the first tab when its opened
257 o:SetScript("OnShow", AskMrRobot.AmrUI.OnShow)
258
259 o:RegisterEvent("AUCTION_HOUSE_CLOSED")
260 o:RegisterEvent("AUCTION_HOUSE_SHOW")
261 o:RegisterEvent("FORGE_MASTER_OPENED")
262 o:RegisterEvent("FORGE_MASTER_CLOSED")
263 o:RegisterEvent("SOCKET_INFO_UPDATE")
264 o:RegisterEvent("SOCKET_INFO_CLOSE")
265
266 o:SetScript("OnEvent", function(...)
267 o:OnEvent(...)
268 end)
269
270 tinsert(UISpecialFrames, o:GetName())
271
272 -- title
273 o.TitleText:SetText("Ask Mr. Robot " .. GetAddOnMetadata(AskMrRobot.AddonName, "Version"))
274
275 -- create the tab buttons
276 o.buttons = o:createTabButtons()
277
278 local tabArea = AskMrRobot.Frame:new(nil, o)
279 tabArea:SetPoint("TOPLEFT", 140, -30)
280 tabArea:SetPoint("BOTTOMRIGHT")
281
282 createImportDetailsErrorTab(tabArea)
283
284 -- create the import tab and associated it with the import tab button
285 o.importTab = AskMrRobot.ImportTab:new(tabArea)
286 o.buttons[1].element = o.importTab
287 o.importTab.scrollFrame.EditBox:SetScript("OnEscapePressed", function()
288 o:Hide()
289 end)
290
291 o.importTab.button:SetScript("OnClick", function(...)
292 o.summaryTab.importDate = date()
293 AmrImportDate = o.summaryTab.importDate
294 o:OnUpdate()
295 if o.mostlySuccess then
296 -- save import between sessions
297 AmrImportString = o.importTab.scrollFrame.EditBox:GetText()
298 AmrImportDate = o.summaryTab.importDate
299 end
300 o:ShowTab("summary")
301 end)
302
303 o.summaryTab = AskMrRobot.SummaryTab:new(tabArea)
304 o.buttons[2].element = o.summaryTab
305
306 o.gemTab = AskMrRobot.GemTab:new(nil, tabArea)
307 o.buttons[3].element = o.gemTab
308
309 o.enchantTab = AskMrRobot.EnchantTab:new(tabArea)
310 o.buttons[4].element = o.enchantTab
311
312 o.reforgeTab = AskMrRobot.ReforgesTab:new(tabArea)
313 o.buttons[5].element = o.reforgeTab
314
315 o.shoppingTab = AskMrRobot.ShoppingListTab:new(tabArea)
316 o.buttons[6].element = o.shoppingTab
317
318 o.shoppingTab.sendTo:SetScript("OnEscapePressed", function()
319 o:Hide()
320 end)
321
322 o.exportTab = AskMrRobot.ExportTab:new(tabArea)
323 o.buttons[7].element = o.exportTab
324
325 o.combatLogTab = AskMrRobot.CombatLogTab:new(tabArea)
326 o.buttons[8].element = o.combatLogTab
327
328 o.helpTab = AskMrRobot.HelpTab:new(tabArea)
329 o.buttons[9].element = o.helpTab
330
331 o.isSocketWindowVisible = false
332 o.isReforgeVisible = false
333 o.isAuctionHouseVisible = false
334
335 --hide the UI
336 o:Hide()
337
338 o:ShowTab("import")
339
340 o.initialize = false
341
342 return o
343 end
344
345 function AskMrRobot.AmrUI:OnUpdate()
346 local input = self.importTab.scrollFrame.EditBox:GetText()
347 if input and input:len() > 0 then
348 self:validateInput(input)
349 end
350 end
351
352 function AskMrRobot.AmrUI:OnShow()
353
354 if not self.initialized then
355 -- remember the import settings between sessions
356 self.importTab.scrollFrame.EditBox:SetText(AmrImportString or "")
357 self.initialized = true
358 end
359
360 self:OnUpdate()
361 end
362
363 function AskMrRobot.AmrUI:OnDragStart()
364 if not self.isLocked then
365 self:StartMoving();
366 end
367 end
368
369 function AskMrRobot.AmrUI:OnDragStop()
370 self:StopMovingOrSizing()
371 end
372
373 function AskMrRobot.AmrUI:OnHide()
374 self.visible = false
375 self:StopMovingOrSizing()
376 end
377
378 function AskMrRobot.AmrUI:ShowReforgeFrame()
379 self.visible = true
380 self:Show()
381 end
382
383 function AskMrRobot.AmrUI:Toggle()
384 if self.visible then
385 self:Hide()
386 else
387 self:ShowReforgeFrame()
388 end
389 end
390
391 local nameToButtonNumber = {
392 import = 1,
393 summary = 2,
394 gems = 3,
395 enchants = 4,
396 reforges = 5,
397 shopping = 6,
398 export = 7,
399 combatLog = 8,
400 help = 9
401 }
402
403 function AskMrRobot.AmrUI:ShowTab(tabName)
404 local buttonNumber = nameToButtonNumber[tabName]
405 if buttonNumber then
406 self.buttons[buttonNumber]:Click()
407 end
408 end
409
410 function AskMrRobot.AmrUI:OnEvent(frame, event, ...)
411 local handler = self["On_" .. event]
412 if handler then
413 handler(self, ...)
414 end
415 end
416
417 function AskMrRobot.AmrUI:On_AUCTION_HOUSE_SHOW()
418 self.isAuctionHouseVisible = true
419 if self.mostlySuccess then
420 local showTab = self.visible
421 if not AmrOptions.manualShowShop and not self.visible then
422
423 -- show if there is anything to buy
424 if self.shoppingTab:HasStuffToBuy() then
425 self:Show()
426 showTab = true
427 end
428 end
429
430 if showTab then
431 self:ShowTab("shopping")
432 end
433 end
434 end
435
436 function AskMrRobot.AmrUI:On_AUCTION_HOUSE_CLOSED()
437 self.isAuctionHouseVisible = false
438 if self.isReforgeVisible then
439 self:ShowTab("reforges")
440 elseif self.isSocketWindowVisible then
441 self:ShowTab("gems")
442 end
443 end
444
445 function AskMrRobot.AmrUI:On_FORGE_MASTER_OPENED()
446 self.isReforgeVisible = true
447 if self.mostlySuccess then
448 local showTab = self.visible
449 if not AmrOptions.manualShowReforge and not self.visible then
450
451 -- see if there are any reforges to do
452 local reforgeCount = 0
453 for slotNum, badReforge in pairs(AskMrRobot.itemDiffs.reforges) do
454 reforgeCount = reforgeCount + 1
455 end
456
457 if reforgeCount > 0 then
458 self:Show()
459 showTab = true
460 end
461 end
462
463 if showTab then
464 self:ShowTab("reforges")
465 end
466 end
467 end
468
469 function AskMrRobot.AmrUI:On_FORGE_MASTER_CLOSED()
470 self.isReforgeVisible = false
471 if self.isAuctionHouseVisible then
472 self:ShowTab("shopping")
473 elseif self.isSocketWindowVisible then
474 self:ShowTab("gems")
475 end
476 end
477
478 function AskMrRobot.AmrUI:On_SOCKET_INFO_UPDATE()
479 self.isSocketWindowVisible = true
480 if self.mostlySuccess then
481 if not self.visible then
482 self:Show()
483 end
484 self:ShowTab("gems")
485 end
486 end
487
488 function AskMrRobot.AmrUI:On_SOCKET_INFO_CLOSE()
489 self.isSocketWindowVisible = false
490 if self.isAuctionHouseVisible then
491 self:ShowTab("shopping")
492 elseif self.isReforgeVisible then
493 self:ShowTab("reforges")
494 end
495 end