View Single Post
04-14-14, 08:14 AM   #9
Niketa
A Wyrmkin Dreamwalker
 
Niketa's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2013
Posts: 54
As I said before, I haven't had time to rewrite any of this and try to reuse the code. The most I did was the test button from the previous post on a blank addon to try to test out the concept before I got back to this. I know there are a bunch of pointless tables for the gear to check (especially the one with just the gear slots and the one with gear slots and names is pretty much the same thing). Was lazy coding on my part to get it done quickly.

lua Code:
  1. local events = CreateFrame("Frame")
  2.  
  3. events:RegisterEvent("UNIT_INVENTORY_CHANGED")
  4.  
  5. -- Event Handler
  6. events:SetScript("OnEvent", function(self, event, ...)
  7.     return self[event] and self[event](self, event, ...)
  8. end)
  9.  
  10. -- Main frame to display information.
  11. local infoFrame = CreateFrame("Frame", nil, CharacterModelFrame)
  12.       infoFrame:SetPoint("TOPLEFT", CharacterModelFrame, "TOPLEFT", 0, 0)
  13.       infoFrame:SetPoint("BOTTOMRIGHT", CharacterModelFrame, "BOTTOMRIGHT", 0, 0)
  14.       infoFrame:SetFrameStrata("MEDIUM")
  15.       infoFrame:Hide()
  16.  
  17. local infoFrameBG = infoFrame:CreateTexture(nil, "BACKGROUND")
  18.       infoFrameBG:SetAllPoints(infoFrame)
  19.       infoFrameBG:SetTexture(0, 0, 0, 0.4)
  20.  
  21. -- Warning button.
  22. local warningBTN = CreateFrame("Button", nil, CharacterModelFrame)
  23.       warningBTN:SetWidth(16)
  24.       warningBTN:SetHeight(16)
  25.       warningBTN:SetPoint("TOPRIGHT", -10, -10)
  26.       warningBTN:SetFrameStrata("HIGH")
  27.       warningBTN:Hide()
  28.  
  29. local warningICO = warningBTN:CreateTexture(nil, "BACKGROUND")
  30.       warningICO:SetTexture("Interface\\Addons\\Unprepared\\Resources\\warningBTN.tga")
  31.       warningICO:SetAllPoints(warningBTN)
  32.  
  33. -- Optimized Button
  34. local optimizedBTN = CreateFrame("Button", nil, CharacterModelFrame)
  35.       optimizedBTN:SetWidth(16)
  36.       optimizedBTN:SetHeight(16)
  37.       optimizedBTN:SetPoint("TOPRIGHT", -10, -10)
  38.       optimizedBTN:SetFrameStrata("HIGH")
  39.       optimizedBTN:Hide()
  40.  
  41. local optimizedICO = optimizedBTN:CreateTexture(nil, "BACKGROUND")
  42.       optimizedICO:SetTexture("Interface\\Addons\\Unprepared\\Resources\\optimizedBTN.tga")
  43.       optimizedICO:SetAllPoints(optimizedBTN)
  44.  
  45. -- FontString Variables
  46. local strvars = {}
  47.  
  48. -- Enchant counter and name table.
  49. local eCounter = 0
  50. local eList = {}
  51.  
  52. -- Gem counter and name table.
  53. local gCounter = 0
  54. local gList = {}
  55.  
  56. -- Gear to check.
  57. local slots = {1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 15, 16, 17}
  58. local slotnames = {[1] = "Head", [2] = "Neck", [3] = "Shoulder", [5] = "Chest", [6] = "Waist", [7] = "Legs", [8] = "Feet", [9] ="Wrist", [10] = "Hands", [11] = "Finger0", [12] = "Finger1", [15] = "Back", [16] = "MainHand", [17] = "SecondaryHand"}
  59. local gemsonly = {[1] = true, [2] = true, [6] = true} -- Exclude checking for enchants.
  60. local prof1 = select(1, GetProfessions()) and select(7, GetProfessionInfo(select(1, GetProfessions())))
  61. local prof1 = select(2, GetProfessions()) and select(7, GetProfessionInfo(select(2, GetProfessions())))
  62.  
  63. local professions = {
  64.     164, -- blacksmith
  65.     333, --enchanting
  66.     202, --engineer
  67.     773, --inscription
  68.     755, --jewelcrafting
  69.     165, --leatherworking
  70.     197 --tailoring
  71. }
  72.  
  73. -- Check slots for missing enchants, gems, etc.
  74. local function CheckSlots()
  75.  
  76.     -- Hide old font strings.
  77.     for k, v in pairs(strvars) do
  78.         v:Hide()
  79.     end
  80.  
  81.     -- Empty strvars.
  82.     strvars = {}
  83.  
  84.     -- Enchant counter and name table.
  85.     eCounter = 0
  86.     eList = {}
  87.  
  88.     -- Gem counter and name table.
  89.     gCounter = 0
  90.     gList = {}
  91.  
  92.     for k, v in pairs(slots) do
  93.         local item = GetInventoryItemLink("player", v)
  94.  
  95.         if item then
  96.             local itemID, enchantID, gem1, gem2, gem3, gem4 = item:match("item:(%d+):(%d+):(%d+):(%d+):(%d+):(%d+)")
  97.             local iteminfo = {itemID, enchantID, gem1, gem2, gem3, gem4}
  98.  
  99.             -- Get number of sockets in item.
  100.             local itemstats = GetItemStats(item)
  101.             local sockets = 0
  102.  
  103.             if itemstats then
  104.                 for t, s in pairs(itemstats) do
  105.                     local search = t:find("EMPTY_SOCKET")
  106.                     if search then
  107.                         sockets = sockets + s
  108.                     end
  109.                 end
  110.  
  111.                 -- Check for missing gems.
  112.                 for i = 3, 2 + sockets do
  113.                     if iteminfo[i] == "0" then
  114.                         gCounter = gCounter + 1
  115.                         table.insert(gList, {item, v})
  116.                     end
  117.                 end
  118.             end
  119.  
  120.             -- Check for missing enchants.
  121.             if not gemsonly[v] and enchantID == "0" then
  122.                 if (v == 11 or v == 12) and prof1 ~= 333 and prof2 ~= 333 then
  123.                 else
  124.                     eCounter = eCounter + 1
  125.                     table.insert(eList, {item, v})
  126.                 end
  127.             end
  128.         end
  129.     end
  130.  
  131.     -- Display appropriate button.
  132.     if eCounter > 0 or gCounter > 0 then
  133.         warningBTN:Show()
  134.         optimizedBTN:Hide()
  135.     else
  136.         optimizedBTN:Show()
  137.         warningBTN:Hide()
  138.         infoFrame:Hide()
  139.     end
  140.  
  141.     -- Create font strings for Unprepared info frame.
  142.     strvars["title"] = infoFrame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  143.     strvars["title"]:SetPoint("TOPLEFT", 10, -10)
  144.     strvars["title"]:SetText("Unprepared")
  145.  
  146.     -- Enchants
  147.     if eCounter > 0 then
  148.         strvars["enchant_title"] = infoFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  149.         strvars["enchant_title"]:SetPoint("TOPLEFT", strvars["title"], "BOTTOMLEFT", 0, -20)
  150.         strvars["enchant_title"]:SetText("Missing Enchants (" .. eCounter .. "):")
  151.         strvars["enchant_title"]:SetJustifyH("LEFT")
  152.         strvars["enchant_title"]:CanWordWrap(true)
  153.         strvars["enchant_title"]:SetWidth(CharacterModelFrame:GetWidth() - 20)
  154.  
  155.         for key, value in pairs(eList) do
  156.             local name = GetItemInfo(value[1])
  157.             local texture = select(10, GetItemInfo(value[1]))
  158.  
  159.             -- Item icon.
  160.             strvars["eBTN" .. key] = CreateFrame("Button", nil, infoFrame)
  161.             strvars["eBTN" .. key]:SetWidth(16)
  162.             strvars["eBTN" .. key]:SetHeight(16)
  163.             if key == 1 then
  164.                 strvars["eBTN" .. key]:SetPoint("TOPLEFT", strvars["enchant_title"], "BOTTOMLEFT", 0, -5)
  165.             else
  166.                 strvars["eBTN" .. key]:SetPoint("TOPLEFT", strvars["eBTN" .. (key - 1)], "BOTTOMLEFT", 0, -10)
  167.             end
  168.  
  169.             strvars["eICO" .. key] = strvars["eBTN" .. key]:CreateTexture(nil, "BACKGROUND")
  170.             strvars["eICO" .. key]:SetTexture(texture)
  171.             strvars["eICO" .. key]:SetAllPoints(strvars["eBTN" .. key])
  172.  
  173.             -- Item name.
  174.             strvars["eStr" .. key] = infoFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  175.             strvars["eStr" .. key]:SetPoint("TOPLEFT", strvars["eBTN" .. key], "TOPRIGHT", 5, 0)
  176.             strvars["eStr" .. key]:SetText(name)
  177.             strvars["eStr" .. key]:SetJustifyH("LEFT")
  178.             strvars["eStr" .. key]:CanWordWrap(true)
  179.             strvars["eStr" .. key]:SetWidth(CharacterModelFrame:GetWidth() - 41)
  180.         end
  181.     end
  182.  
  183.         -- Gems
  184.     if gCounter > 0 then
  185.         strvars["gem_title"] = infoFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  186.         if eCounter > 0 then
  187.             strvars["gem_title"]:SetPoint("TOPLEFT", strvars["eICO" .. table.getn(eList)], "BOTTOMLEFT", 0, -20)
  188.         else
  189.             strvars["gem_title"]:SetPoint("TOPLEFT", strvars["title"], "BOTTOMLEFT", 0, -20)
  190.         end
  191.         strvars["gem_title"]:SetText("Missing Gems (" .. gCounter .. "):")
  192.         strvars["gem_title"]:SetJustifyH("LEFT")
  193.         strvars["gem_title"]:CanWordWrap(true)
  194.         strvars["gem_title"]:SetWidth(CharacterModelFrame:GetWidth() - 20)
  195.  
  196.         for key, value in pairs(gList) do
  197.             local name = GetItemInfo(value[1])
  198.             local texture = select(10, GetItemInfo(value[1]))
  199.  
  200.             -- Item icon.
  201.             strvars["gBTN" .. key] = CreateFrame("Button", nil, infoFrame)
  202.             strvars["gBTN" .. key]:SetWidth(16)
  203.             strvars["gBTN" .. key]:SetHeight(16)
  204.             if key == 1 then
  205.                 strvars["gBTN" .. key]:SetPoint("TOPLEFT", strvars["gem_title"], "BOTTOMLEFT", 0, -5)
  206.             else
  207.                 strvars["gBTN" .. key]:SetPoint("TOPLEFT", strvars["gBTN" .. (key - 1)], "BOTTOMLEFT", 0, -10)
  208.             end
  209.  
  210.             strvars["gICO" .. key] = strvars["gBTN" .. key]:CreateTexture(nil, "BACKGROUND")
  211.             strvars["gICO" .. key]:SetTexture(texture)
  212.             strvars["gICO" .. key]:SetAllPoints(strvars["gBTN" .. key])
  213.  
  214.             -- Item name.
  215.             strvars["gStr" .. key] = infoFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  216.             strvars["gStr" .. key]:SetPoint("TOPLEFT", strvars["gBTN" .. key], "TOPRIGHT", 5, 0)
  217.             strvars["gStr" .. key]:SetText(name)
  218.             strvars["gStr" .. key]:SetJustifyH("LEFT")
  219.             strvars["gStr" .. key]:CanWordWrap(true)
  220.             strvars["gStr" .. key]:SetWidth(CharacterModelFrame:GetWidth() - 41)
  221.         end
  222.     end
  223. end
  224.  
  225. local function HideHighlights()
  226.     if strvars then
  227.         for k, v in pairs(strvars) do
  228.             if k:find("Hlght") or k:find("TXT") then
  229.                 strvars[k]:Hide()
  230.             end
  231.         end
  232.     end
  233. end
  234.  
  235. local function HighlightGear()
  236.     if strvars then
  237.         HideHighlights()
  238.  
  239.         -- Highlight missing enchants.
  240.         for key, value in pairs(eList) do
  241.             -- Highlight item.
  242.             local name = GetItemInfo(value[1])
  243.             local slot = _G["Character" .. slotnames[value[2]] .. "Slot"]
  244.  
  245.             strvars[name .. "Hlght"] = slot:CreateTexture(nil, "OVERLAY")
  246.             strvars[name .. "Hlght"]:SetTexture("Interface\\Buttons\\UI-ActionButton-Border")
  247.             strvars[name .. "Hlght"]:SetBlendMode("ADD")
  248.             strvars[name .. "Hlght"]:SetHeight(68)
  249.             strvars[name .. "Hlght"]:SetWidth(68)
  250.             strvars[name .. "Hlght"]:SetVertexColor(1, 0, 0, 0.75)
  251.             strvars[name .. "Hlght"]:SetPoint("CENTER", slot, "CENTER", 0, 1)
  252.  
  253.             -- Create text overlay.
  254.             strvars[name .. "TXT"] = slot:CreateFontString("OVERLAY", nil, "ErrorFont")
  255.             strvars[name .. "TXT"]:SetPoint("CENTER", 0, 0)
  256.             strvars[name .. "TXT"]:SetSize(50, 50)
  257.             strvars[name .. "TXT"]:SetText("E")
  258.         end
  259.  
  260.         -- Highlight missing gems.
  261.         for key, value in pairs(gList) do
  262.             -- Highlight item.
  263.             local name = GetItemInfo(value[1])
  264.             local slot = _G["Character" .. slotnames[value[2]] .. "Slot"]
  265.  
  266.             if not strvars[name .. "Hlght"] then
  267.                 strvars[name .. "Hlght"] = slot:CreateTexture(nil, "OVERLAY")
  268.                 strvars[name .. "Hlght"]:SetTexture("Interface\\Buttons\\UI-ActionButton-Border")
  269.                 strvars[name .. "Hlght"]:SetBlendMode("ADD")
  270.                 strvars[name .. "Hlght"]:SetHeight(68)
  271.                 strvars[name .. "Hlght"]:SetWidth(68)
  272.                 strvars[name .. "Hlght"]:SetVertexColor(1, 0, 0, 0.75)
  273.                 strvars[name .. "Hlght"]:SetPoint("CENTER", slot, "CENTER", 0, 1)
  274.  
  275.                 -- Create text overlay.
  276.                 strvars[name .. "TXT"] = slot:CreateFontString("OVERLAY", nil, "ErrorFont")
  277.                 strvars[name .. "TXT"]:SetPoint("CENTER", 0, 0)
  278.                 strvars[name .. "TXT"]:SetSize(50, 50)
  279.                 strvars[name .. "TXT"]:SetText("G")
  280.             else
  281.                 strvars[name .. "TXT"]:SetText("E/G")
  282.             end
  283.         end
  284.     end
  285. end
  286.  
  287. CharacterModelFrame:SetScript("OnShow", function(self, ...)
  288.     CheckSlots()
  289.     if infoFrame:IsVisible() then
  290.         HighlightGear()
  291.     end
  292. end)
  293.  
  294. -- UNIT_INVENTORY_CHANGED
  295. function events:UNIT_INVENTORY_CHANGED(event, addon)
  296.     CheckSlots()
  297.     if infoFrame:IsVisible() then
  298.         HighlightGear()
  299.     end
  300. end
  301.  
  302. infoFrame:SetScript("OnShow", function(self)
  303.     CharacterModelFrame:EnableMouse(false)
  304.     if infoFrame:IsVisible() then
  305.         HighlightGear()
  306.     end
  307. end)
  308.  
  309. infoFrame:SetScript("OnHide", function(self)
  310.     CharacterModelFrame:EnableMouse(true)
  311.     HideHighlights()
  312. end)
  313.  
  314. warningBTN:SetScript("OnEnter", function(self)
  315.     GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
  316.     GameTooltip:SetText("Unprepared")
  317.     GameTooltip:SetPoint("TOPRIGHT", 1, 0)
  318.     GameTooltip:Show()
  319. end)
  320. warningBTN:SetScript("OnLeave", function(self)
  321.     GameTooltip:Hide()
  322. end)
  323. warningBTN:SetScript("OnClick", function(self)
  324.     if infoFrame:IsVisible() then
  325.         infoFrame:Hide()
  326.         HideHighlights()
  327.     else
  328.         infoFrame:Show()
  329.         HighlightGear()
  330.  
  331.     end
  332. end)
  Reply With Quote