Advertisement
Guest User

ItemLevels.lua for Adibags

a guest
Oct 26th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.94 KB | None | 0 0
  1. --[[
  2. AdiBags - Adirelle's bag addon.
  3. Copyright 2013 Adirelle (adirelle@gmail.com)
  4. All rights reserved.
  5. --]]
  6.  
  7. local addonName, addon = ...
  8. local L = addon.L
  9.  
  10. --<GLOBALS
  11. local _G = _G
  12. local abs = _G.math.abs
  13. local GetItemInfo = _G.GetItemInfo
  14. local QuestDifficultyColors = _G.QuestDifficultyColors
  15. local UnitLevel = _G.UnitLevel
  16. local modf = _G.math.modf
  17. local max = _G.max
  18. local min = _G.min
  19. local pairs = _G.pairs
  20. local select = _G.select
  21. local unpack = _G.unpack
  22. --GLOBALS>
  23.  
  24. local mod = addon:NewModule('ItemLevel', 'AceEvent-3.0')
  25. mod.uiName = L['Item level']
  26. mod.uiDesc = L['Display the level of equippable item in the top left corner of the button.']
  27.  
  28. local colorSchemes = {
  29.     none = function() return 1, 1 ,1 end
  30. }
  31.  
  32. local texts = {}
  33. local ItemUpgradeInfo = LibStub('LibItemUpgradeInfo-1.0')
  34.  
  35. local SyLevel = _G.SyLevel
  36. local SyLevelBypass
  37. if SyLevel then
  38.     function SyLevelBypass() return mod:IsEnabled() and mod.db.profile.useSyLevel end
  39. else
  40.     function SyLevelBypass() return false end
  41. end
  42.  
  43. function mod:OnInitialize()
  44.     self.db = addon.db:RegisterNamespace(self.moduleName, {
  45.         profile = {
  46.             useSyLevel = false,
  47.             equippableOnly = true,
  48.             colorScheme = 'original',
  49.             minLevel = 1,
  50.             ignoreJunk = true,
  51.             ignoreHeirloom = true,
  52.         },
  53.     })
  54.     if self.db.profile.colored == true then
  55.         self.db.profile.colorScheme = 'original'
  56.         self.db.profile.colored = nil
  57.     elseif self.db.profile.colored == false then
  58.         self.db.profile.colorScheme = 'none'
  59.         self.db.profile.colored = nil
  60.     end
  61.     if SyLevel then
  62.         SyLevel:RegisterPipe(
  63.             'Adibags',
  64.             function() self.db.profile.useSyLevel = true end,
  65.             function() self.db.profile.useSyLevel = false end,
  66.             function() self:SendMessage('AdiBags_UpdateAllButtons') end,
  67.             'AdiBags'
  68.         )
  69.         SyLevel:RegisterFilterOnPipe('Adibags','Item level text') -- Activates the filter
  70.         SyLevelDB.EnabledFilters['Item level text']['Adibags'] = true -- Updates the DB to re-enable on login
  71.     end
  72. end
  73.  
  74. function mod:OnEnable()
  75.     self:RegisterMessage('AdiBags_UpdateButton', 'UpdateButton')
  76.     self:SendMessage('AdiBags_UpdateAllButtons')
  77. end
  78.  
  79. function mod:OnDisable()
  80.     for _, text in pairs(texts) do
  81.         text:Hide()
  82.     end
  83. end
  84.  
  85. local function CreateText(button)
  86.     local text = button:CreateFontString(nil, "OVERLAY", "NumberFontNormal")
  87.     text:SetPoint("TOPLEFT", button, 3, -1)
  88.     text:Hide()
  89.     texts[button] = text
  90.     return text
  91. end
  92.  
  93. function mod:UpdateButton(event, button)
  94.     local settings = self.db.profile
  95.     local link = button:GetItemLink()
  96.     local text = texts[button]
  97.  
  98.     if link then
  99.         local _, _, quality, _, reqLevel, _, _, _, loc = GetItemInfo(link)
  100.         local level = ItemUpgradeInfo:GetUpgradedItemLevel(link) or 0 -- Ugly workaround
  101.         if level >= settings.minLevel
  102.             and (quality ~= LE_ITEM_QUALITY_POOR or not settings.ignoreJunk)
  103.             and (loc ~= "" or not settings.equippableOnly)
  104.             and (quality ~= LE_ITEM_QUALITY_HEIRLOOM or not settings.ignoreHeirloom)
  105.         then
  106.             if SyLevel then
  107.                 if settings.useSyLevel then
  108.                     if not SyLevel:IsPipeEnabled('Adibags') then -- Enables the pipe should it be disabled on login.
  109.                         SyLevel:EnablePipe('Adibags')
  110.                     end
  111.                     if text then
  112.                         text:Hide()
  113.                     end
  114.                     SyLevel:CallFilters('Adibags', button, link)
  115.                     return
  116.                 else
  117.                     SyLevel:CallFilters('Adibags', button, nil)
  118.                 end
  119.             end
  120.             if not text then
  121.                 text = CreateText(button)
  122.             end
  123.             text:SetText(level)
  124.             text:SetTextColor(colorSchemes[settings.colorScheme](level, quality, reqLevel, (loc ~= "")))
  125.             return text:Show()
  126.         end
  127.     end
  128.     if SyLevel then
  129.         SyLevel:CallFilters('Adibags', button, nil)
  130.     end
  131.     if text then
  132.         text:Hide()
  133.     end
  134. end
  135.  
  136.  
  137. function mod:GetOptions()
  138.     return {
  139.         useSyLevel = SyLevel and {
  140.             name = L['Use SyLevel'],
  141.             desc = L['Let SyLevel handle the the display.'],
  142.             type = 'toggle',
  143.             order = 5,
  144.         } or nil,
  145.         equippableOnly = {
  146.             name = L['Only equippable items'],
  147.             desc = L['Do not show level of items that cannot be equipped.'],
  148.             type = 'toggle',
  149.             order = 10,
  150.         },
  151.         colorScheme = {
  152.             name = L['Color scheme'],
  153.             desc = L['Which color scheme should be used to display the item level ?'],
  154.             type = 'select',
  155.             hidden = SyLevelBypass,
  156.             values = {
  157.                 none     = L['None'],
  158.                 original = L['Same as InventoryItemLevels'],
  159.                 level    = L['Related to player level'],
  160.             },
  161.             order = 20,
  162.         },
  163.         minLevel = {
  164.             name = L['Mininum level'],
  165.             desc = L['Do not show levels under this threshold.'],
  166.             type = 'range',
  167.             min = 1,
  168.             max = 600,
  169.             step = 1,
  170.             bigStep = 5,
  171.             order = 30,
  172.         },
  173.         ignoreJunk = {
  174.             name = L['Ignore low quality items'],
  175.             desc = L['Do not show level of poor quality items.'],
  176.             type = 'toggle',
  177.             order = 40,
  178.         },
  179.         ignoreHeirloom = {
  180.             name = L['Ignore heirloom items'],
  181.             desc = L['Do not show level of heirloom items.'],
  182.             type = 'toggle',
  183.             order = 50,
  184.         },
  185.     }, addon:GetOptionHandler(self)
  186. end
  187.  
  188. -- Color scheme inspired from InventoryItemLevels
  189. do
  190.     local colors = {
  191.         -- { upper bound, r, g, b }
  192.         { 150, 0.55, 0.55, 0.55 }, -- gray
  193.         { 250, 1.00, 0.00, 0.00 }, -- red
  194.         { 300, 1.00, 0.70, 0.00 }, -- orange
  195.         { 350, 1.00, 1.00, 0.00 }, -- yellow
  196.         { 372, 0.00, 1.00, 0.00 }, -- green
  197.         { 385, 0.00, 1.00, 1.00 }, -- cyan
  198.         { 397, 0.00, 0.80, 1.00 }, -- blue
  199.         { 403, 1.00, 0.50, 1.00 }, -- purple,
  200.         { 410, 1.00, 0.75, 1.00 }, -- pink
  201.         { 999, 1.00, 1.00, 1.00 }, -- white
  202.     }
  203.  
  204.     colorSchemes.original = function(level)
  205.         for i, tuple in pairs(colors) do
  206.             if level < tuple[1] then
  207.                 return unpack(tuple, 2, 4)
  208.             end
  209.         end
  210.     end
  211. end
  212.  
  213. -- Color scheme based on player Level
  214. do
  215.     -- Color gradient function taken from my customized oUF
  216.     local colorGradient
  217.     do
  218.         local function GetY(r, g, b)
  219.             return 0.3 * r + 0.59 * g + 0.11 * b
  220.         end
  221.  
  222.         local function RGBToHCY(r, g, b)
  223.             local min, max = min(r, g, b), max(r, g, b)
  224.             local chroma = max - min
  225.             local hue
  226.             if chroma > 0 then
  227.                 if r == max then
  228.                     hue = ((g - b) / chroma) % 6
  229.                 elseif g == max then
  230.                     hue = (b - r) / chroma + 2
  231.                 elseif b == max then
  232.                     hue = (r - g) / chroma + 4
  233.                 end
  234.                 hue = hue / 6
  235.             end
  236.             return hue, chroma, GetY(r, g, b)
  237.         end
  238.  
  239.         local function HCYtoRGB(hue, chroma, luma)
  240.             local r, g, b = 0, 0, 0
  241.             if hue then
  242.                 local h2 = hue * 6
  243.                 local x = chroma * (1 - abs(h2 % 2 - 1))
  244.                 if h2 < 1 then
  245.                     r, g, b = chroma, x, 0
  246.                 elseif h2 < 2 then
  247.                     r, g, b = x, chroma, 0
  248.                 elseif h2 < 3 then
  249.                     r, g, b = 0, chroma, x
  250.                 elseif h2 < 4 then
  251.                     r, g, b = 0, x, chroma
  252.                 elseif h2 < 5 then
  253.                     r, g, b = x, 0, chroma
  254.                 else
  255.                     r, g, b = chroma, 0, x
  256.                 end
  257.             end
  258.             local m = luma - GetY(r, g, b)
  259.             return r + m, g + m, b + m
  260.         end
  261.  
  262.         colorGradient = function(a, b, ...)
  263.             local perc
  264.             if(b == 0) then
  265.                 perc = 0
  266.             else
  267.                 perc = a / b
  268.             end
  269.  
  270.             if perc >= 1 then
  271.                 local r, g, b = select(select('#', ...) - 2, ...)
  272.                 return r, g, b
  273.             elseif perc <= 0 then
  274.                 local r, g, b = ...
  275.                 return r, g, b
  276.             end
  277.  
  278.             local num = select('#', ...) / 3
  279.             local segment, relperc = modf(perc*(num-1))
  280.             local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)
  281.  
  282.             local h1, c1, y1 = RGBToHCY(r1, g1, b1)
  283.             local h2, c2, y2 = RGBToHCY(r2, g2, b2)
  284.             local c = c1 + (c2-c1) * relperc
  285.             local   y = y1 + (y2-y1) * relperc
  286.             if h1 and h2 then
  287.                 local dh = h2 - h1
  288.                 if dh < -0.5  then
  289.                     dh = dh + 1
  290.                 elseif dh > 0.5 then
  291.                     dh = dh - 1
  292.                 end
  293.                 return HCYtoRGB((h1 + dh * relperc) % 1, c, y)
  294.             else
  295.                 return HCYtoRGB(h1 or h2, c, y)
  296.             end
  297.  
  298.         end
  299.     end
  300.  
  301.     local maxLevelRanges = {
  302.         [60] = {  66,  92 },
  303.         [70] = { 100, 164 },
  304.         [80] = { 187, 284 },
  305.         [85] = { 333, 416 },
  306.         [90] = { 458, 580 }
  307.     }
  308.  
  309.     local maxLevelColors = {}
  310.     do
  311.         local t = maxLevelColors
  312.         t[1], t[2], t[3] = GetItemQualityColor(2)
  313.         t[4], t[5], t[6] = GetItemQualityColor(3)
  314.         t[7], t[8], t[9] = GetItemQualityColor(4)
  315.         t[10], t[11], t[12] = GetItemQualityColor(5)
  316.     end
  317.  
  318.     colorSchemes.level = function(level, quality, reqLevel, equipabble)
  319.         if not equipabble then return 1,1,1 end
  320.         local playerLevel = UnitLevel('player')
  321.         if playerLevel == _G.MAX_PLAYER_LEVEL then
  322.             -- Use the item level range for that level
  323.             local minLevel, maxLevel = unpack(maxLevelRanges[playerLevel])
  324.             if level < minLevel then
  325.                 return GetItemQualityColor(0)
  326.             else
  327.                 return colorGradient(level - minLevel, maxLevel - minLevel, unpack(maxLevelColors))
  328.             end
  329.         elseif reqLevel and reqLevel > 1 then
  330.             -- Use the
  331.             local delta, color = playerLevel - reqLevel
  332.             if delta < 0 then
  333.                 color = QuestDifficultyColors.trivial
  334.             elseif delta == 0 then
  335.                 color = QuestDifficultyColors.standard
  336.             elseif delta == 1 then
  337.                 color = QuestDifficultyColors.difficult
  338.             elseif delta == 2 then
  339.                 color = QuestDifficultyColors.verydifficult
  340.             else
  341.                 color = QuestDifficultyColors.impossible
  342.             end
  343.             return color.r, color.g, color.b
  344.         else
  345.             -- Would this happen ?
  346.             return 1, 1, 1
  347.         end
  348.     end
  349. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement