Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ADDON = ...
  2.  
  3. local slots = {
  4.     { name = "HeadSlot" },
  5.     { name = "NeckSlot" },
  6.     { name = "ShoulderSlot", canEnchant = true },
  7.     { name = "BackSlot", canEnchant = true },
  8.     { name = "ChestSlot", canEnchant = true },
  9.     { name = "WristSlot", canEnchant = true },
  10.     { name = "HandsSlot", canEnchant = true },
  11.     { name = "WaistSlot" },
  12.     { name = "LegsSlot", canEnchant = true },
  13.     { name = "FeetSlot", canEnchant = true },
  14.     { name = "Finger0Slot"  },
  15.     { name = "Finger1Slot"  },
  16.     { name = "Trinket0Slot" },
  17.     { name = "Trinket1Slot" },
  18.     { name = "MainHandSlot", canEnchant = true },
  19.     { name = "SecondaryHandSlot", canEnchant = true },
  20. }
  21.  
  22. for i = 1, #slots do
  23.     slots[i].id = GetInventorySlotInfo(slots[i].name)
  24. end
  25.  
  26. local LINE_HEIGHT, LINE_GAP = 20, 2
  27.  
  28. ------------------------------------------------------------------------
  29.  
  30. local Addon = CreateFrame("Frame", ADDON.."Frame", CharacterModelFrame)
  31. Addon:SetScript("OnEvent", function(self, event, ...) return self[event] and self[event](self, event, ...) end)
  32. Addon:RegisterEvent("PLAYER_REGEN_DISABLED")
  33. Addon:RegisterEvent("PLAYER_REGEN_ENABLED")
  34. Addon:Hide()
  35.  
  36. local Toggle = CreateFrame("Button", ADDON.."ToggleButton", CharacterModelFrame)
  37. Toggle:SetPoint("TOPRIGHT", 0, -4)
  38. Toggle:SetSize(24, 24)
  39. Toggle:SetNormalTexture("Interface\\BUTTONS\\WHITE8X8")
  40. Toggle:GetNormalTexture():SetVertexColor(0.25, 0.25, 0.25)
  41. Toggle:SetFontString(Toggle:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall"))
  42. Toggle:SetText("X")
  43. Addon.toggle = Toggle
  44.  
  45. Toggle:SetScript("OnClick", function()  Addon:SetShown(not Addon:IsShown()) end)
  46.  
  47. ------------------------------------------------------------------------
  48. --  Event logic
  49.  
  50. -- Player entered combat
  51. function Addon:PLAYER_REGEN_DISABLED()
  52.     self:UnregisterEvent("SKILL_LINES_CHANGED")
  53.     self:UnregisterEvent("UNIT_INVENTORY_CHANGED")
  54. end
  55.  
  56. -- Player left combat
  57. function Addon:PLAYER_REGEN_ENABLED()
  58.     self:RegisterEvent("SKILL_LINES_CHANGED")
  59.     self:RegisterUnitEvent("UNIT_INVENTORY_CHANGED", "player")
  60.     self:SKILL_LINES_CHANGED()
  61.     self:UNIT_INVENTORY_CHANGED()
  62. end
  63.  
  64. -- Player's professions changed
  65. do
  66.     local function CheckProfession(index, targetID, targetMinLevel)
  67.         if index then
  68.             local _, _, level, _, _, _, id = GetProfessionInfo(index)
  69.             return id == targetID and level >= targetMinLevel
  70.         end
  71.     end
  72.     function Addon:SKILL_LINES_CHANGED()
  73.         local profession1, profession2 = GetProfessions()
  74.  
  75.         local hasEnchanting = CheckProfession(profession1, 333, 360) or CheckProfession(profession2, 333, 360)
  76.         slots[11].canEnchant = hasEnchanting
  77.         slots[12].canEnchant = hasEnchanting
  78.  
  79.         local hasBlacksmithing = CheckProfession(profession1, 164, 400) or CheckProfession(profession2, 164, 400)
  80.         slots[6].canAddSocket = hasBlacksmithing
  81.         slots[7].canAddSocket = hasBlacksmithing
  82.     end
  83. end
  84.  
  85. local GetNumSockets
  86. do
  87.     local tooltip = CreateFrame("GameTooltip", ADDON.."ScanningTooltip", UIParent, "GameTooltipTemplate")
  88.     tooltip:SetOwner(Addon, "ANCHOR_NONE")
  89.  
  90.     local textures = {}
  91.     for i = 1, 10 do textures[i] = _G[ADDON.."ScanningTooltipTexture" .. i] end
  92.  
  93.     function GetNumSockets(slot, link)
  94.         local n = 0
  95.         if slot then
  96.             tooltip:SetInventoryItem("player", slot)
  97.         else
  98.             tooltip:SetHyperlink(link)
  99.         end
  100.         for i = 1, 10 do
  101.             if textures[i]:IsShown() then
  102.                 n = n + 1
  103.             end
  104.         end
  105.         return n
  106.     end
  107. end
  108.  
  109. -- Player's inventory changed
  110. function Addon:UNIT_INVENTORY_CHANGED()
  111.     local missing
  112.  
  113.     for i = 1, #slots do
  114.         local slot = slots[i]
  115.         slot.missingEnchant = nil
  116.         slot.missingGem = nil
  117.         slot.missingSocket = nil
  118.  
  119.         local itemLink = GetInventoryItemLink("player", slot.id)
  120.         slot.itemLink = itemLink
  121.         if itemLink then
  122.             local _, itemID, enchantID, gem1, gem2, gem3, gem4 = strsplit(":", itemLink)
  123.  
  124.             local numSockets = GetNumSockets(slot.id)
  125.             local numGems = (gem1 ~= "0" and 1 or 0) + (gem2 ~= "0" and 1 or 0) + (gem3 ~= "0" and 1 or 0) + (gem4 ~= "0" and 1 or 0)
  126.             if numGems < numSockets then
  127.                 slot.missingGem = true
  128.                 missing = true
  129.             end
  130.  
  131.             if slot.canEnchant and enchantID == "0" then
  132.                 slot.missingEnchant = true
  133.                 missing = true
  134.             end
  135.  
  136.             if slot.canAddSocket and numSockets == GetNumSockets(nil, itemLink) then
  137.                 slot.missingSocket = true
  138.                 missing = true
  139.             end
  140.         end
  141.     end
  142.  
  143.     local color = missing and RED_FONT_COLOR or GREEN_FONT_COLOR
  144.     self.toggle:GetNormalTexture():SetVertexColor(color.r * 0.5, color.g * 0.5, color.b * 0.5)
  145.  
  146.     if self:IsShown() then
  147.         self:UpdateDisplay()
  148.     end
  149. end
  150.  
  151. if not UnitAffectingCombat("player") then Addon:PLAYER_REGEN_ENABLED() end
  152.  
  153. ------------------------------------------------------------------------
  154. --  Display logic
  155.  
  156. Addon:SetScript("OnShow", function(self)
  157.     self.toggle:Hide()
  158.     self:UpdateDisplay()
  159. end)
  160.  
  161. Addon:SetScript("OnHide", function(self)
  162.     self.toggle:Show()
  163. end)
  164.  
  165. function Addon:UpdateDisplay()
  166.     if self.CreateDisplay then
  167.         self:CreateDisplay()
  168.     end
  169.  
  170.     local lineIndex = 0
  171.  
  172.     for i = 1, #slots do
  173.         local slot = slots[i]
  174.         if slot.itemLink and (slot.missingEnchant or slot.missingGem) then
  175.             lineIndex = lineIndex + 1
  176.             self.lines[lineIndex]:SetItem(slot)
  177.         end
  178.     end
  179.  
  180.     if lineIndex == 0 then
  181.         self.title:SetText("|cff7fff7fYou are prepared!")
  182.         self.scrollFrame:Hide()
  183.     else
  184.         self.title:SetText("|cffff7f7fYou are not prepared!")
  185.         self.scrollFrame.scrollChild:SetHeight((LINE_HEIGHT + LINE_GAP) * lineIndex - LINE_GAP)
  186.         self.scrollFrame:Show()
  187.     end
  188. end
  189.  
  190. ------------------------------------------------------------------------
  191. --  Display creation
  192.  
  193. function Addon:CreateDisplay()
  194.     self:SetPoint("BOTTOMLEFT", -3, 19)
  195.     self:SetPoint("TOPRIGHT", 3, 1)
  196.  
  197.     self:SetBackdrop({ bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", tile = true, tileSize = 16, edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16, insets = { left = 4, right = 4, top = 4, bottom = 4 } })
  198.     self:SetBackdropColor(0, 0, 0)
  199.     self:SetBackdropBorderColor(1, 1, 1)
  200.  
  201.     local close = CreateFrame("Button", "$parentCloseButton", self, "UIPanelCloseButton")
  202.     close:SetPoint("TOPRIGHT", 0, -2)
  203.     close:SetScript("OnClick", function(this) self:Hide() end)
  204.     self.close = close
  205.  
  206.     local title = self:CreateFontString(nil, "ARTWORK", "GameTooltipHeaderText")
  207.     title:SetPoint("TOPLEFT", 8, -8)
  208.     self.title = title
  209.  
  210.     local scrollFrame = CreateFrame("ScrollFrame", "$parentScrollFrame", self, "UIPanelScrollFrameTemplate")
  211.     scrollFrame:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -4)
  212.     scrollFrame:SetPoint("BOTTOMRIGHT", -28, 8)
  213.     self.scrollFrame = scrollFrame
  214.  
  215.     scrollFrame.ScrollBar:EnableMouseWheel(true)
  216.     scrollFrame.ScrollBar:SetScript("OnMouseWheel", function(self, direction)
  217.         ScrollFrameTemplate_OnMouseWheel(scrollFrame, direction)
  218.     end)
  219.  
  220.     local barBG = scrollFrame:CreateTexture(nil, "BACKGROUND", nil, -6)
  221.     barBG:SetPoint("TOP")
  222.     barBG:SetPoint("RIGHT", 25, 0)
  223.     barBG:SetPoint("BOTTOM")
  224.     barBG:SetWidth(26)
  225.     barBG:SetTexture("Interface\\PaperDollInfoFrame\\UI-Character-ScrollBar")
  226.     barBG:SetTexCoord(0, 0.45, 0.1640625, 1)
  227.     barBG:SetAlpha(0.5)
  228.  
  229.     local scrollChild = CreateFrame("Frame", nil, scrollFrame)
  230.     scrollChild:SetSize(scrollFrame:GetWidth(), 100)
  231.     scrollFrame:SetScrollChild(scrollChild)
  232.  
  233.     local function Line_OnEnter(this)
  234.         this.highlight:Show()
  235.         if this.slot then
  236.             GameTooltip:SetOwner(this, "ANCHOR_NONE")
  237.             GameTooltip:SetPoint("TOPLEFT", this, "TOPRIGHT", 0, -12)
  238.             GameTooltip:SetInventoryItem("player", this.slot.id)
  239.             if this.slot.missingEnchant then
  240.                 GameTooltip:AddLine("Missing enchant!")
  241.                 GameTooltip:AddTexture("Interface\\Icons\\Trade_Engraving")
  242.             end
  243.             if this.slot.missingGem then
  244.                 GameTooltip:AddLine("Missing gems!")
  245.                 GameTooltip:AddTexture("Interface\\Icons\\INV_Misc_Gem_01")
  246.             end
  247.             if this.slot.missingSocket then
  248.                 GameTooltip:AddLine("Missing extra socket!")
  249.                 GameTooltip:AddTexture("Interface\\ITEMSOCKETINGFRAME\\UI-EMPTYSOCKET-META")
  250.             end
  251.             GameTooltip:Show()
  252.         end
  253.     end
  254.  
  255.     local function Line_OnLeave(this)
  256.         this.highlight:Hide()
  257.         GameTooltip:Hide()
  258.     end
  259.  
  260.     local function Line_OnHide(this)
  261.         this.slot = nil
  262.         this.icon:SetTexture("")
  263.         this.text:SetText("")
  264.         this.enchant:SetAlpha(1)
  265.         this.gem:SetAlpha(1)
  266.         this.socket:SetAlpha(1)
  267.         this.socket:Hide()
  268.     end
  269.  
  270.     local function Line_SetItem(this, slot)
  271.         local name, _, quality, _, _, _, _, _, equipLoc, icon = GetItemInfo(slot.itemLink)
  272.         if equipLoc == "INVTYPE_WEAPON" then
  273.             equipLoc = slot.name == "MainHandSlot" and "INVTYPE_WEAPONMAINHAND" or "INVTYPE_WEAPONOFFHAND"
  274.         end
  275.         this.slot = slot
  276.         this.icon:SetTexture(icon)
  277.         this.enchant:SetAlpha(slot.missingEnchant and 1 or 0.25)
  278.         this.gem:SetAlpha(slot.missingGem and 1 or 0.25)
  279.         this.socket:SetShown(slot.canAddSocket)
  280.         this.socket:SetAlpha(slot.missingSocket and 1 or 0.25)
  281.         this.text:SetPoint("RIGHT", slot.canAddSocket and this.socket or this.enchant, "LEFT", -4, 0)
  282.         this.text:SetText(_G[equipLoc])
  283.         this:Show()
  284.     end
  285.  
  286.     self.lines = setmetatable({}, { __index = function(t, i)
  287.         local line = CreateFrame("Frame", nil, scrollChild)
  288.         line:SetWidth(scrollChild:GetWidth())
  289.         line:SetHeight(LINE_HEIGHT)
  290.  
  291.         if i > 1 then
  292.             line:SetPoint("TOPLEFT", t[i - 1], "BOTTOMLEFT", 0, -LINE_GAP)
  293.         else
  294.             line:SetPoint("TOPLEFT", scrollChild)
  295.         end
  296.  
  297.         line:EnableMouse(true)
  298.         line:SetScript("OnEnter", Line_OnEnter)
  299.         line:SetScript("OnLeave", Line_OnLeave)
  300.         line:SetScript("OnHide", Line_OnHide)
  301.  
  302.         line:EnableMouseWheel(true)
  303.         line:SetScript("OnMouseWheel", function(this, direction)
  304.             ScrollFrameTemplate_OnMouseWheel(scrollFrame, direction)
  305.         end)
  306.  
  307.         local icon = line:CreateTexture(nil, "ARTWORK")
  308.         icon:SetPoint("LEFT")
  309.         icon:SetSize(LINE_HEIGHT, LINE_HEIGHT)
  310.         icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
  311.         line.icon = icon
  312.  
  313.         local gem = line:CreateTexture(nil, "ARTWORK")
  314.         gem:SetPoint("RIGHT")
  315.         gem:SetSize(LINE_HEIGHT, LINE_HEIGHT)
  316.         gem:SetTexCoord(0.07, 0.93, 0.07, 0.93)
  317.         gem:SetTexture("Interface\\Icons\\INV_Misc_Gem_01")
  318.         line.gem = gem
  319.  
  320.         local enchant = line:CreateTexture(nil, "ARTWORK")
  321.         enchant:SetPoint("RIGHT", gem, "LEFT", -LINE_GAP, 0)
  322.         enchant:SetSize(LINE_HEIGHT, LINE_HEIGHT)
  323.         enchant:SetTexCoord(0.07, 0.93, 0.07, 0.93)
  324.         enchant:SetTexture("Interface\\Icons\\Trade_Engraving")
  325.         line.enchant = enchant
  326.  
  327.         local socket = line:CreateTexture(nil, "ARTWORK")
  328.         socket:SetPoint("RIGHT", enchant, "LEFT", -LINE_GAP, 0)
  329.         socket:SetSize(LINE_HEIGHT, LINE_HEIGHT)
  330.         socket:SetTexCoord(0.07, 0.93, 0.07, 0.93)
  331.         socket:SetTexture("Interface\\ITEMSOCKETINGFRAME\\UI-EMPTYSOCKET-META")
  332.         line.socket = socket
  333.  
  334.         local text = line:CreateFontString(nil, "ARTWORK", "GameTooltipText")
  335.         text:SetPoint("LEFT", icon, "RIGHT", 4, 0)
  336.         line.text = text
  337.  
  338.         local highlight = line:CreateTexture(nil, "BACKGROUND")
  339.         highlight:SetAllPoints(true)
  340.         highlight:SetBlendMode("ADD")
  341.         highlight:SetTexture([[Interface\QuestFrame\UI-QuestLogTitleHighlight]])
  342.         highlight:SetVertexColor(0.2, 0.4, 0.8)
  343.         highlight:Hide()
  344.         line.highlight = highlight
  345.  
  346.         line.id = i
  347.         line.SetItem = Line_SetItem
  348.  
  349.         t[i] = line
  350.         return line
  351.     end })
  352.  
  353.     self.CreateDisplay = nil
  354. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement