adam@0: local _, AskMrRobot = ... yellowfive@11: local L = AskMrRobot.L; adam@0: adam@0: local showImportDetailsError = nil adam@0: local showImportErrorTab = nil adam@0: adam@0: AskMrRobot.AmrUI = AskMrRobot.inheritsFrom(AskMrRobot.Frame) adam@0: adam@0: function AskMrRobot.AmrUI:swapSimilarSlotsIfNeeded(slotName1, slotName2) adam@0: local slotId1 = GetInventorySlotInfo(slotName1) adam@0: local slotId2 = GetInventorySlotInfo(slotName2) adam@0: local slotNum1 = AskMrRobot.slotIdToSlotNum[slotId1] adam@0: local slotNum2 = AskMrRobot.slotIdToSlotNum[slotId2] adam@0: adam@0: local itemLink1 = GetInventoryItemLink("player", slotId1) adam@0: local itemLink2 = GetInventoryItemLink("player", slotId2) adam@0: adam@0: -- see how bad the items are in the original order adam@0: AskMrRobot.populateItemDiffs(self.importedItems[slotNum1], itemLink1, slotNum1) adam@0: AskMrRobot.populateItemDiffs(self.importedItems[slotNum2], itemLink2, slotNum2) adam@0: adam@0: local badCountOriginalOrder = 0 adam@0: if AskMrRobot.itemDiffs.items[slotNum1] then badCountOriginalOrder = badCountOriginalOrder + 1 end adam@0: if AskMrRobot.itemDiffs.items[slotNum2] then badCountOriginalOrder = badCountOriginalOrder + 1 end adam@0: adam@0: -- try the order swapped adam@0: AskMrRobot.populateItemDiffs(self.importedItems[slotNum1], itemLink2, slotNum1) adam@0: AskMrRobot.populateItemDiffs(self.importedItems[slotNum2], itemLink1, slotNum2) adam@0: adam@0: local badCountNewOrder = 0 adam@0: if AskMrRobot.itemDiffs.items[slotNum1] then badCountNewOrder = badCountNewOrder + 1 end adam@0: if AskMrRobot.itemDiffs.items[slotNum2] then badCountNewOrder = badCountNewOrder + 1 end adam@0: adam@0: -- if the items were less bad in the new order, swap the imported items adam@0: if badCountNewOrder < badCountOriginalOrder then adam@0: local tempItem = self.importedItems[slotNum1] adam@0: self.importedItems[slotNum1] = self.importedItems[slotNum2] adam@0: self.importedItems[slotNum2] = tempItem adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:displayImportItems() adam@0: if not self.importedItems then adam@0: return false adam@0: end adam@0: adam@0: --see if rings or trinkets are swapped, and alter self.importedItems accordingly adam@0: self:swapSimilarSlotsIfNeeded("Finger0Slot", "Finger1Slot") adam@0: self:swapSimilarSlotsIfNeeded("Trinket0Slot", "Trinket1Slot") adam@0: adam@0: for slotNum = 1, #AskMrRobot.slotIds do adam@0: if AskMrRobot.OptimizationSlots[slotNum] then adam@0: local slotId = AskMrRobot.slotIds[slotNum] adam@0: local itemLink = GetInventoryItemLink("player", slotId) adam@0: AskMrRobot.populateItemDiffs(self.importedItems[slotNum], itemLink, slotNum) adam@0: end adam@0: end adam@0: adam@0: self.summaryTab:showBadItems() adam@0: adam@0: -- if there are incorrect items equiped, display errors on other tabs adam@0: if AskMrRobot.itemDiffs and AskMrRobot.itemDiffs.items then adam@0: for k,v in pairs(AskMrRobot.itemDiffs.items) do adam@0: if not v.needsUpgrade then adam@0: self.hasImportError = true adam@0: end adam@0: end adam@0: end adam@0: adam@0: if self.hasImportError then adam@0: showImportDetailsError() adam@0: else adam@0: self.gemTab:Update() adam@0: self.enchantTab:showBadEnchants() adam@0: self.reforgeTab:Render() adam@0: self.shoppingTab:Update() adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:showImportError(text, ...) adam@0: self.summaryTab:showImportError(text, ...) adam@0: if text then adam@0: self.hasImportError = true adam@0: showImportDetailsError() adam@0: else adam@0: self.hasImportError = false adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:showImportWarning(text, ...) adam@0: self.summaryTab:showImportWarning(text, ...) adam@0: self.hasImportError = false adam@0: if text then adam@0: showImportDetailsError() adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:validateInput(input) adam@0: adam@0: self.importedItems = nil adam@0: self.mostlySuccess = false adam@0: adam@0: local parsed = AskMrRobot.parseAmr(input) adam@0: adam@0: if not parsed.realm then yellowfive@11: self:showImportError(L.AMR_UI_IMPORT_ERROR_IMPROPER,L.AMR_UI_IMPORT_ERROR_IMPROPER_GOTO) adam@0: elseif not AskMrRobot.validateCharacterName(parsed.name) then yellowfive@11: self:showImportError(L.AMR_UI_IMPORT_ERROR_CHARACTER:format(parsed.name), L.AMR_UI_IMPORT_ERROR_CHARACTER_GOTO) adam@0: elseif not AskMrRobot.validateRace(parsed.race) then yellowfive@11: self:showImportError(L.AMR_UI_IMPORT_ERROR_RACE, L.AMR_UI_IMPORT_ERROR_RACE_CURRENT:format(parsed.race)) adam@0: elseif not AskMrRobot.validateFaction(parsed.faction) then yellowfive@11: self:showImportError(L.AMR_UI_IMPORT_ERROR_FACTION, L.AMR_UI_IMPORT_ERROR_FACTION_CURRENT:format(parsed.faction)) adam@0: elseif not AskMrRobot.validateProfessions(parsed.professions) then yellowfive@11: self:showImportError(L.AMR_UI_IMPORT_ERROR_PROFESSIONS, L.AMR_UI_IMPORT_ERROR_PROFESSIONS_GOTO) adam@0: elseif not AskMrRobot.validateSpec(parsed.spec) then adam@0: if parsed.spec and parsed.spec ~= 'nil' then adam@0: local _, specName = GetSpecializationInfoByID(parsed.spec) yellowfive@11: self:showImportError(L.AMR_UI_IMPORT_ERROR_SPEC, L.AMR_UI_IMPORT_ERROR_SPEC_CHANGE:format(specName)) adam@0: else yellowfive@11: self:showImportError(L.AMR_UI_IMPORT_ERROR_SPEC, L.AMR_UI_IMPORT_ERROR_SPEC_UNEXPECTED) adam@0: end adam@0: self.mostlySuccess = true adam@0: self.summaryTab.badRealm = nil adam@0: self.summaryTab.badTalents = nil adam@0: self.summaryTab.badGlyphs = nil adam@0: AmrImportString = input adam@0: else adam@0: self.summaryTab.badRealm = not AskMrRobot.validateRealm(parsed.realm) and parsed.realm adam@0: self.summaryTab.badTalents = not AskMrRobot.validateTalents(parsed.talents) adam@0: self.summaryTab.badGlyphs = not AskMrRobot.validateGlyphs(parsed.glyphs) adam@0: self.mostlySuccess = true adam@0: self:showImportError(nil) adam@0: AmrImportString = input adam@0: self.importedItems = parsed.items adam@0: return self:displayImportItems() adam@0: end adam@0: return false adam@0: end adam@0: adam@0: local function createImportDetailsErrorTab(reforgeFrame) adam@0: local tab = CreateFrame("Frame", nil, reforgeFrame) adam@0: tab:SetPoint("TOPLEFT") adam@0: tab:SetPoint("BOTTOMRIGHT") adam@0: tab:Hide() adam@0: adam@0: local text = tab:CreateFontString("AmrImportDetailsText1", "ARTWORK", "GameFontNormalLarge") adam@0: text:SetPoint("TOPLEFT", 0, -5) adam@0: text:SetText("Help") adam@0: adam@0: local errorText1 = tab:CreateFontString("AmrImportDetailsText2", "ARTWORK", "GameFontRed") adam@0: errorText1:SetPoint("TOPLEFT", "AmrImportDetailsText1", "BOTTOMLEFT", 0, -20) yellowfive@11: errorText1:SetText(L.AMR_UI_IMPORT_ERROR_NO_IMPORT) adam@0: errorText1:SetPoint("RIGHT", -10, 0) adam@0: errorText1:SetWidth(errorText1:GetWidth()) adam@0: errorText1:SetJustifyH("LEFT") adam@0: adam@0: showImportDetailsError = function() yellowfive@11: errorText1:SetText(L.AMR_UI_IMPORT_ERROR_CANT_OPTIMIZE) adam@0: end adam@0: adam@0: showImportErrorTab = function(tabName) adam@0: if not tabName then adam@0: tab:Hide() adam@0: else adam@0: text:SetText(tabName) adam@0: tab:Show() adam@0: end adam@0: end adam@0: adam@0: return tab adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:createTabButtons() adam@0: local importTabButton = CreateFrame("Button", "AmrImportTabButton", self, "OptionsListButtonTemplate") adam@0: adam@0: local buttons = {} adam@0: self.hasImportError = true adam@0: adam@0: local function onTabButtonClick(clickedButton, event, ...) adam@0: showImportErrorTab(nil) adam@0: for i = 1, #buttons do adam@0: local button = buttons[i] adam@0: if clickedButton == button then adam@0: button.highlight:SetVertexColor(1, 1, 0) adam@0: button:LockHighlight() adam@0: if self.hasImportError and button.isImportDetails then adam@0: showImportErrorTab(button:GetText()) adam@0: elseif button.element then adam@0: button.element:Show() adam@0: end adam@0: else adam@0: button.highlight:SetVertexColor(.196, .388, .8) adam@0: button:UnlockHighlight() adam@0: if button.element then adam@0: button.element:Hide() adam@0: end adam@0: end adam@0: end adam@0: end adam@0: adam@0: local function createButton(text, spacing, isImportDetails) adam@0: local lastButton = #buttons adam@0: local i = lastButton + 1 adam@0: local tabButton = CreateFrame("Button", "AmrTabButton" .. i, self, "OptionsListButtonTemplate") adam@0: tabButton.isImportDetails = isImportDetails adam@0: tabButton:SetText(text) adam@0: tabText = tabButton:GetFontString() adam@0: tabText:SetPoint("LEFT", 6, 0) adam@0: if i == 1 then adam@0: tabButton:SetPoint("TOPLEFT", 2, spacing) adam@0: else adam@0: tabButton:SetPoint("TOPLEFT", "AmrTabButton" .. lastButton, "BOTTOMLEFT", 0, spacing) adam@0: end adam@0: tabButton:SetWidth(125) adam@0: tabButton:SetHeight(20) adam@0: tinsert(buttons, tabButton) adam@0: tabButton:SetScript("OnClick", onTabButtonClick) adam@0: end adam@0: yellowfive@11: createButton(L.AMR_UI_BUTTON_IMPORT, -35, false) yellowfive@11: createButton(L.AMR_UI_BUTTON_SUMMARY, -20, false) yellowfive@11: createButton(L.AMR_UI_BUTTON_GEMS, 0, true) yellowfive@11: createButton(L.AMR_UI_BUTTON_ENCHANTS, 0, true) yellowfive@11: createButton(L.AMR_UI_BUTTON_REFORGES, 0, true) yellowfive@11: createButton(L.AMR_UI_BUTTON_SHOPPING_LIST, 0, true) yellowfive@11: createButton(L.AMR_UI_BUTTON_BEST_IN_BAGS, -20, false) yellowfive@11: createButton(L.AMR_UI_BUTTON_COMBAT_LOG, 0, false) yellowfive@11: createButton(L.AMR_UI_BUTTON_HELP, -20, false) adam@0: adam@0: return buttons adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:new() adam@0: local o = AskMrRobot.Frame:new("AskMrRobot_Dialog", nil, "BasicFrameTemplateWithInset") adam@0: adam@0: -- use the AmrUI class adam@0: setmetatable(o, { __index = AskMrRobot.AmrUI }) adam@0: adam@0: o:RegisterForDrag("LeftButton"); adam@0: o:SetWidth(600) adam@0: o:SetHeight(530) adam@0: o.InsetBg:SetPoint("TOPLEFT", 125, -24) adam@0: adam@0: o:SetParent("UIParent") adam@0: o:SetPoint("CENTER") adam@0: o:Hide() adam@0: o:EnableMouse(true) adam@0: o:EnableKeyboard(true) adam@0: o.hideOnEscape = 1 adam@0: o:SetMovable(true) adam@0: o:SetToplevel(true) adam@0: adam@0: --o:SetScript("OnEscapePressed", function) adam@0: o:SetScript("OnDragStart", AskMrRobot.AmrUI.OnDragStart) adam@0: o:SetScript("OnDragStop", AskMrRobot.AmrUI.OnDragStop) adam@0: o:SetScript("OnHide", AskMrRobot.AmrUI.OnHide) adam@0: -- make the UI show the first tab when its opened adam@0: o:SetScript("OnShow", AskMrRobot.AmrUI.OnShow) adam@0: adam@0: o:RegisterEvent("AUCTION_HOUSE_CLOSED") adam@0: o:RegisterEvent("AUCTION_HOUSE_SHOW") adam@0: o:RegisterEvent("FORGE_MASTER_OPENED") adam@0: o:RegisterEvent("FORGE_MASTER_CLOSED") adam@0: o:RegisterEvent("SOCKET_INFO_UPDATE") adam@0: o:RegisterEvent("SOCKET_INFO_CLOSE") adam@0: adam@0: o:SetScript("OnEvent", function(...) adam@0: o:OnEvent(...) adam@0: end) adam@0: adam@0: tinsert(UISpecialFrames, o:GetName()) adam@0: adam@0: -- title adam@0: o.TitleText:SetText("Ask Mr. Robot " .. GetAddOnMetadata(AskMrRobot.AddonName, "Version")) adam@0: adam@0: -- create the tab buttons adam@0: o.buttons = o:createTabButtons() adam@0: adam@0: local tabArea = AskMrRobot.Frame:new(nil, o) adam@0: tabArea:SetPoint("TOPLEFT", 140, -30) adam@0: tabArea:SetPoint("BOTTOMRIGHT") adam@0: adam@0: createImportDetailsErrorTab(tabArea) adam@0: adam@0: -- create the import tab and associated it with the import tab button adam@0: o.importTab = AskMrRobot.ImportTab:new(tabArea) adam@0: o.buttons[1].element = o.importTab adam@0: o.importTab.scrollFrame.EditBox:SetScript("OnEscapePressed", function() adam@0: o:Hide() adam@0: end) adam@0: adam@0: o.importTab.button:SetScript("OnClick", function(...) adam@0: o.summaryTab.importDate = date() adam@0: AmrImportDate = o.summaryTab.importDate adam@0: o:OnUpdate() adam@0: if o.mostlySuccess then adam@0: -- save import between sessions adam@0: AmrImportString = o.importTab.scrollFrame.EditBox:GetText() adam@0: AmrImportDate = o.summaryTab.importDate adam@0: end adam@0: o:ShowTab("summary") adam@0: end) adam@0: adam@0: o.summaryTab = AskMrRobot.SummaryTab:new(tabArea) adam@0: o.buttons[2].element = o.summaryTab adam@0: adam@0: o.gemTab = AskMrRobot.GemTab:new(nil, tabArea) adam@0: o.buttons[3].element = o.gemTab adam@0: adam@0: o.enchantTab = AskMrRobot.EnchantTab:new(tabArea) adam@0: o.buttons[4].element = o.enchantTab adam@0: adam@0: o.reforgeTab = AskMrRobot.ReforgesTab:new(tabArea) adam@0: o.buttons[5].element = o.reforgeTab adam@0: adam@0: o.shoppingTab = AskMrRobot.ShoppingListTab:new(tabArea) adam@0: o.buttons[6].element = o.shoppingTab adam@0: adam@0: o.shoppingTab.sendTo:SetScript("OnEscapePressed", function() adam@0: o:Hide() adam@0: end) adam@0: adam@0: o.exportTab = AskMrRobot.ExportTab:new(tabArea) adam@0: o.buttons[7].element = o.exportTab adam@0: adam@0: o.combatLogTab = AskMrRobot.CombatLogTab:new(tabArea) adam@0: o.buttons[8].element = o.combatLogTab adam@0: adam@0: o.helpTab = AskMrRobot.HelpTab:new(tabArea) adam@0: o.buttons[9].element = o.helpTab adam@0: adam@0: o.isSocketWindowVisible = false adam@0: o.isReforgeVisible = false adam@0: o.isAuctionHouseVisible = false adam@0: adam@0: --hide the UI adam@0: o:Hide() adam@0: adam@0: o:ShowTab("import") adam@0: adam@0: o.initialize = false adam@0: adam@0: return o adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:OnUpdate() adam@0: local input = self.importTab.scrollFrame.EditBox:GetText() adam@0: if input and input:len() > 0 then adam@0: self:validateInput(input) adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:OnShow() adam@0: adam@0: if not self.initialized then adam@0: -- remember the import settings between sessions adam@0: self.importTab.scrollFrame.EditBox:SetText(AmrImportString or "") adam@0: self.initialized = true adam@0: end adam@0: adam@0: self:OnUpdate() adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:OnDragStart() adam@0: if not self.isLocked then adam@0: self:StartMoving(); adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:OnDragStop() adam@0: self:StopMovingOrSizing() adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:OnHide() adam@0: self.visible = false adam@0: self:StopMovingOrSizing() adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:ShowReforgeFrame() adam@0: self.visible = true adam@0: self:Show() adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:Toggle() adam@0: if self.visible then adam@0: self:Hide() adam@0: else adam@0: self:ShowReforgeFrame() adam@0: end adam@0: end adam@0: adam@0: local nameToButtonNumber = { adam@0: import = 1, adam@0: summary = 2, adam@0: gems = 3, adam@0: enchants = 4, adam@0: reforges = 5, adam@0: shopping = 6, adam@0: export = 7, adam@0: combatLog = 8, adam@0: help = 9 adam@0: } adam@0: adam@0: function AskMrRobot.AmrUI:ShowTab(tabName) adam@0: local buttonNumber = nameToButtonNumber[tabName] adam@0: if buttonNumber then adam@0: self.buttons[buttonNumber]:Click() adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:OnEvent(frame, event, ...) adam@0: local handler = self["On_" .. event] adam@0: if handler then adam@0: handler(self, ...) adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:On_AUCTION_HOUSE_SHOW() adam@0: self.isAuctionHouseVisible = true adam@0: if self.mostlySuccess then adam@0: local showTab = self.visible adam@0: if not AmrOptions.manualShowShop and not self.visible then adam@0: adam@0: -- show if there is anything to buy adam@0: if self.shoppingTab:HasStuffToBuy() then adam@0: self:Show() adam@0: showTab = true adam@0: end adam@0: end adam@0: adam@0: if showTab then adam@0: self:ShowTab("shopping") adam@0: end adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:On_AUCTION_HOUSE_CLOSED() adam@0: self.isAuctionHouseVisible = false adam@0: if self.isReforgeVisible then adam@0: self:ShowTab("reforges") adam@0: elseif self.isSocketWindowVisible then adam@0: self:ShowTab("gems") adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:On_FORGE_MASTER_OPENED() adam@0: self.isReforgeVisible = true adam@0: if self.mostlySuccess then adam@0: local showTab = self.visible adam@0: if not AmrOptions.manualShowReforge and not self.visible then adam@0: adam@0: -- see if there are any reforges to do adam@0: local reforgeCount = 0 adam@0: for slotNum, badReforge in pairs(AskMrRobot.itemDiffs.reforges) do adam@0: reforgeCount = reforgeCount + 1 adam@0: end adam@0: adam@0: if reforgeCount > 0 then adam@0: self:Show() adam@0: showTab = true adam@0: end adam@0: end adam@0: adam@0: if showTab then adam@0: self:ShowTab("reforges") adam@0: end adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:On_FORGE_MASTER_CLOSED() adam@0: self.isReforgeVisible = false adam@0: if self.isAuctionHouseVisible then adam@0: self:ShowTab("shopping") adam@0: elseif self.isSocketWindowVisible then adam@0: self:ShowTab("gems") adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:On_SOCKET_INFO_UPDATE() adam@0: self.isSocketWindowVisible = true adam@0: if self.mostlySuccess then adam@0: if not self.visible then adam@0: self:Show() adam@0: end adam@0: self:ShowTab("gems") adam@0: end adam@0: end adam@0: adam@0: function AskMrRobot.AmrUI:On_SOCKET_INFO_CLOSE() adam@0: self.isSocketWindowVisible = false adam@0: if self.isAuctionHouseVisible then adam@0: self:ShowTab("shopping") adam@0: elseif self.isReforgeVisible then adam@0: self:ShowTab("reforges") adam@0: end adam@0: end