Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.38 KB | None | 0 0
  1. --[[
  2.     All Credit for Datapanel.lua goes to Tuks.
  3.     Tukui = http://www.tukui.org/download.php.
  4.     Edited by Cokedriver.
  5. ]]
  6.  
  7. local Core = LibStub("AceAddon-3.0"):GetAddon("BasicUI")
  8. local Module = Core:NewModule("Datapanel", "AceEvent-3.0")
  9.  
  10. -- Constants (variables whose values are never altered):
  11. local PLAYER_CLASS = UnitClass("player")
  12. local PLAYER_NAME = UnitName("player")
  13. local PLAYER_REALM = GetRealmName()
  14. local SCREEN_WIDTH = tonumber(string.match(({GetScreenResolutions()})[GetCurrentResolution()], "(%d+)x+%d"))
  15. local TOC_VERSION = select(4, GetBuildInfo())
  16. local GAME_LOCALE = GetLocale()
  17.  
  18. -- Variables:
  19. local db, currentFightDPS, playerRole
  20. local hexa, hexb = "|cffffffff", "|r" -- not very descriptive names :(
  21.  
  22. -- Variables that point to frames or other objects:
  23. local MainPanel, LeftPanel, CenterPanel, RightPanel, BGPanel
  24.  
  25. ------------------------------------------------------------------------
  26. -- Local functions
  27.  
  28. local function RGBToHex(r, g, b)
  29.     if r > 1 then r = 1 elseif r < 0 then r = 0 end
  30.     if g > 1 then g = 1 elseif g < 0 then g = 0 end
  31.     if b > 1 then b = 1 elseif b < 0 then b = 0 end
  32.     return format("|cff%02x%02x%02x", r*255, g*255, b*255)
  33. end
  34.  
  35. local function HexToRGB(hex)
  36.     local rhex, ghex, bhex = strsub(hex, 1, 2), strsub(hex, 3, 4), strsub(hex, 5, 6)
  37.     return tonumber(rhex, 16), tonumber(ghex, 16), tonumber(bhex, 16)
  38. end
  39.  
  40. local function ShortValue(v)
  41.     if v >= 1e6 then
  42.         return format("%.1fm", v / 1e6):gsub("%.?0+([km])$", "%1")
  43.     elseif v >= 1e3 or v <= -1e3 then
  44.         return format("%.1fk", v / 1e3):gsub("%.?0+([km])$", "%1")
  45.     else
  46.         return v
  47.     end
  48. end
  49.  
  50. local function GetTooltipAnchor()
  51.     local _, y = GetCursorPosition()
  52.     if (y * 2) > UIParent:GetHeight() then
  53.         return "ANCHOR_BOTTOM", 1, 3
  54.     else
  55.         return "ANCHOR_TOP", 1, -3
  56.     end
  57. end
  58.  
  59. ------------------------------------------------------------------------
  60. -- Module functions
  61.  
  62. function Module:OnEnable()
  63.     -- You should fix your DB to use module namespaces properly:
  64.     local db = core.db.profile
  65.  
  66.     -- This line should not be needed if you're using modules correctly:
  67.     if not db.datapanel.enable then return end
  68.  
  69.     if db.datapanel.enablepanel then -- How is this different than "enable" ? If the panel is not enabled, what's the point of doing anything else?
  70.         self:CreatePanel() -- factor this giant blob out into its own function to keep things clean
  71.     end
  72.  
  73.     if db.misc.classcolor then
  74.         local _, class = UnitClass("player")
  75.         local color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLOR)[class]
  76.         hexa = color.colorStr -- no need to format it yourself, it's already done
  77.     else
  78.         hexa = RGBToHex(db.datapanel.customcolor.r, db.datapanel.customcolor.g, db.datapanel.customcolor.b) -- no need to duplicate the code you already wrote into the function
  79.     end
  80.  
  81.     -- Rather than hardcode all the possible plugins here just use a nice table that you can add/remove stuff to much more easily. Scroll to the bottom to see it.
  82.     self.plugins = {}
  83.     for name, constructor in pairs(self.pluginConstructors) do
  84.         local position = db.datapanel[name]
  85.         if position and position > 0 then
  86.             local plugin = constructor()
  87.             self.plugins[name] = plugin
  88.             self:PlacePlugin(position, plugin)
  89.         end
  90.     end
  91.  
  92.     -- no need to make a separate frame to handle events, your module object already does this!
  93.     self:UpdatePlayerRole()
  94.     self:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED", "UpdatePlayerRole")
  95. end
  96.  
  97. function Module:UpdatePlayerRole()
  98.     local spec = GetSpecialization()
  99.     local specRole = GetSpecializationRole(spec) -- no need for a giant table that must be maintained by hand
  100.     if specRole == "TANK" then
  101.         playerRole = "Tank"
  102.     elseif specRole == "HEALER" then
  103.         playerRole = "Caster"
  104.     elseif specRole == "DAMAGER" then
  105.         if UnitPowerType("player") == SPELL_POWER_MANA then
  106.             playerRole = "Caster"
  107.         else
  108.             playerRole = "Melee"
  109.         end
  110.     else
  111.         playerRole = nil -- no spec
  112.     end
  113. end
  114.  
  115. function Module:CreateFontString(parent, file, size, flags)
  116.     local fs = parent:CreateFontString(nil, "OVERLAY")
  117.     fs:SetFont(file, size, flags)
  118.     fs:SetJustifyH("LEFT")
  119.     fs:SetShadowColor(0, 0, 0)
  120.     fs:SetShadowOffset(1.25, -1.25)
  121.     return fs
  122. end
  123.  
  124. function Module:PlacePlugin(position, plugin)
  125.     local left = PanelLeft
  126.     local center = PanelCenter
  127.     local right = PanelRight
  128.  
  129.     -- Left Panel Data
  130.     if position == 1 then
  131.         plugin:SetParent(left)
  132.         plugin:SetHeight(left:GetHeight())
  133.         plugin:SetPoint('LEFT', left, 30, 0)
  134.         plugin:SetPoint('TOP', left)
  135.         plugin:SetPoint('BOTTOM', left)
  136.     elseif position == 2 then
  137.         plugin:SetParent(left)
  138.         plugin:SetHeight(left:GetHeight())
  139.         plugin:SetPoint('TOP', left)
  140.         plugin:SetPoint('BOTTOM', left)
  141.     elseif position == 3 then
  142.         plugin:SetParent(left)
  143.         plugin:SetHeight(left:GetHeight())
  144.         plugin:SetPoint('RIGHT', left, -30, 0)
  145.         plugin:SetPoint('TOP', left)
  146.         plugin:SetPoint('BOTTOM', left)
  147.  
  148.     -- Center Panel Data
  149.     elseif position == 4 then
  150.         plugin:SetParent(center)
  151.         plugin:SetHeight(center:GetHeight())
  152.         plugin:SetPoint('LEFT', center, 30, 0)
  153.         plugin:SetPoint('TOP', center)
  154.         plugin:SetPoint('BOTTOM', center)
  155.     elseif position == 5 then
  156.         plugin:SetParent(center)
  157.         plugin:SetHeight(center:GetHeight())
  158.         plugin:SetPoint('TOP', center)
  159.         plugin:SetPoint('BOTTOM', center)
  160.     elseif position == 6 then
  161.         plugin:SetParent(center)
  162.         plugin:SetHeight(center:GetHeight())
  163.         plugin:SetPoint('RIGHT', center, -30, 0)
  164.         plugin:SetPoint('TOP', center)
  165.         plugin:SetPoint('BOTTOM', center)
  166.  
  167.     -- Right Panel Data
  168.     elseif position == 7 then
  169.         plugin:SetParent(right)
  170.         plugin:SetHeight(right:GetHeight())
  171.         plugin:SetPoint('LEFT', right, 30, 0)
  172.         plugin:SetPoint('TOP', right)
  173.         plugin:SetPoint('BOTTOM', right)
  174.     elseif position == 8 then
  175.         plugin:SetParent(right)
  176.         plugin:SetHeight(right:GetHeight())
  177.         plugin:SetPoint('TOP', right)
  178.         plugin:SetPoint('BOTTOM', right)
  179.     elseif position == 9 then
  180.         plugin:SetParent(right)
  181.         plugin:SetHeight(right:GetHeight())
  182.         plugin:SetPoint('RIGHT', right, -30, 0)
  183.         plugin:SetPoint('TOP', right)
  184.         plugin:SetPoint('BOTTOM', right)
  185.     end
  186. end
  187.  
  188. function Module:CreatePanel()
  189.     if MainPanel then return end -- already done
  190.  
  191.     -- These are not "local" here because they're already "local" at the top of the file.
  192.     MainPanel = CreateFrame("Frame", "BasicUI_Datapanel", UIParent)
  193.     PanelLeft = CreateFrame("Frame", nil, MainPanel)
  194.     PanelCenter = CreateFrame("Frame", nil, MainPanel)
  195.     PanelRight = CreateFrame("Frame", nil, MainPanel)
  196.     BGPanel = CreateFrame("Frame", nil, MainPanel)
  197.  
  198.     -- These are common to all panel styles, so don't duplicate code.
  199.     MainPanel:SetHeight(35)
  200.     MainPanel:SetFrameStrata("LOW")
  201.     MainPanel:SetBackdrop({ bgFile = db.datapanel.background, edgeFile = db.datapanel.border, edgeSize = 25, insets = { left = 5, right = 5, top = 5, bottom = 5 } })
  202.     MainPanel:SetBackdropColor(0, 0, 0, 1)
  203.  
  204.     -- This section can almost certainly be cleaned up by a huge amount,
  205.     -- but I've already spend enough time on this.
  206.     if db.datapanel.panel == "top" then
  207.         MainPanel:SetPoint("TOP", UIParent, 0, 0)
  208.         MainPanel:SetWidth(SCREEN_WIDTH)
  209.         -- Left Panel
  210.         PanelLeft:SetPoint("LEFT", MainPanel, 5, 0)
  211.         PanelLeft:SetHeight(35)
  212.         PanelLeft:SetWidth(SCREEN_WIDTH / 3)
  213.         PanelLeft:SetFrameStrata("LOW")
  214.         PanelLeft:SetFrameLevel(1)
  215.         -- Center Panel
  216.         PanelCenter:SetPoint("CENTER", MainPanel, 0, 0)
  217.         PanelCenter:SetHeight(35)
  218.         PanelCenter:SetWidth(SCREEN_WIDTH / 3)
  219.         PanelCenter:SetFrameStrata("LOW")
  220.         PanelCenter:SetFrameLevel(1)
  221.         -- Right Panel
  222.         PanelRight:SetPoint("RIGHT", MainPanel, -5, 0)
  223.         PanelRight:SetHeight(35)
  224.         PanelRight:SetWidth(SCREEN_WIDTH / 3)
  225.         PanelRight:SetFrameStrata("LOW")
  226.         PanelRight:SetFrameLevel(1)
  227.         -- Battleground Panel
  228.         BGPanel:SetAllPoints(PanelLeft)
  229.         BGPanel:SetFrameStrata("LOW")
  230.         BGPanel:SetFrameLevel(1)
  231.     elseif db.datapanel.panel == "bottom" then
  232.         MainPanel:SetPoint("BOTTOM", UIParent, 0, 0)
  233.         MainPanel:SetWidth(1200)
  234.         -- Left Panel
  235.         PanelLeft:SetPoint("LEFT", MainPanel, 5, 0)
  236.         PanelLeft:SetHeight(35)
  237.         PanelLeft:SetWidth(1200 / 3)
  238.         PanelLeft:SetFrameStrata("LOW")
  239.         PanelLeft:SetFrameLevel(1)
  240.         -- Center Panel
  241.         PanelCenter:SetPoint("CENTER", MainPanel, 0, 0)
  242.         PanelCenter:SetHeight(35)
  243.         PanelCenter:SetWidth(1200 / 3)
  244.         PanelCenter:SetFrameStrata("LOW")
  245.         PanelCenter:SetFrameLevel(1)
  246.         -- Right panel
  247.         PanelRight:SetPoint("RIGHT", MainPanel, -5, 0)
  248.         PanelRight:SetHeight(35)
  249.         PanelRight:SetWidth(1200 / 3)
  250.         PanelRight:SetFrameStrata("LOW")
  251.         PanelRight:SetFrameLevel(1)
  252.         -- Battleground Panel
  253.         BGPanel:SetAllPoints(PanelLeft)
  254.         BGPanel:SetFrameStrata("LOW")
  255.         BGPanel:SetFrameLevel(1)
  256.     elseif db.datapanel.panel == "shortbar" then
  257.         MainPanel:SetPoint("BOTTOM", UIParent, 0, 0)
  258.         MainPanel:SetWidth(725)
  259.         -- Left Panel
  260.         PanelLeft:SetPoint("LEFT", MainPanel, 5, 0)
  261.         PanelLeft:SetHeight(35)
  262.         PanelLeft:SetWidth(725 / 2)
  263.         PanelLeft:SetFrameStrata("LOW")
  264.         PanelLeft:SetFrameLevel(1)
  265.         -- Right panel
  266.         PanelRight:SetPoint("RIGHT", MainPanel, -5, 0)
  267.         PanelRight:SetHeight(35)
  268.         PanelRight:SetWidth(725 / 2)
  269.         PanelRight:SetFrameStrata("LOW")
  270.         PanelRight:SetFrameLevel(1)
  271.         -- Battleground Panel
  272.         BGPanel:SetAllPoints(PanelLeft)
  273.         BGPanel:SetFrameStrata("LOW")
  274.         BGPanel:SetFrameLevel(1)
  275.     end
  276.    
  277.     -- I did not include any of your code for moving other frames,
  278.     -- but you would want to include it here, after fixing all of the
  279.     -- parts where you break secure frames by overriding their methods.
  280. end
  281.  
  282. ------------------------------------------------------------------------
  283. -- Panel creation
  284. -- This can be simplified hugely by using a table, instead of a bunch of if/end blocks.
  285.  
  286. Module.pluginConstructors = {
  287.     armor = function()
  288.         local plugin = CreateFrame("Frame", nil, MainPanel) -- proper parentage
  289.         plugin:EnableMouse(true)
  290.  
  291.         local text = plugin:CreateFontString(nil, "OVERLAY") -- proper parentage
  292.         text:SetFont(db.media.fontNormal, db.media.fontSize,"THINOUTLINE")
  293.         plugin.text = Text -- important!
  294.  
  295.         plugin:RegisterEvent("PLAYER_ENTERING_WORLD")
  296.         plugin:RegisterUnitEvent("UNIT_AURA", "player") -- limit UNIT_* events to the player when possible
  297.         plugin:RegisterUnitEvent("UNIT_INVENTORY_CHANGED", "player")
  298.         plugin:SetScript("OnEvent", function(self)
  299.             local _, effectiveArmor = UnitArmor("player")
  300.             self.text:SetFormattedText("%sArmor:|r %s", hexa, effectiveArmor)
  301.             self:SetWidth(self.text:GetStringWidth()) -- important!
  302.         end)
  303.  
  304.         plugin:SetScript("OnEnter", function(self)
  305.             if UnitAffectingCombat("player") then return end
  306.  
  307.             local anchor, x, y = GetTooltipAnchor(self)
  308.             GameTooltip:SetOwner(self, GameTooltip:SetOwner(self, anchor, x, y)
  309.             GameTooltip:AddLine("Mitigation by Level:")
  310.  
  311.             -- Your code for this section does not work properly, because you
  312.             -- only calculated the value of "mitigation" once for level 83, and
  313.             -- then showed that same value for all 4 level lines and the target line.
  314.             -- I didn't update it for you, or include the broken stuff here.
  315.  
  316.             GameTooltip:Show()
  317.         end)
  318.  
  319.         plugin:SetScript("OnClick", function(self, button)
  320.             ToggleCharacter("PaperDollFrame")
  321.         end)
  322.  
  323.         return plugin -- important!
  324.     end,
  325.     -- You can update the rest yourself if you want.
  326.     -- Also, it would be more maintainable to put each plugin in its own file, and add them to this table from there, eg:
  327.     --[[
  328.         local Core = LibStub("AceAddon-3.0"):GetAddon("BasicUI")
  329.         local Module = Core:GetModule("Datapanel")
  330.  
  331.         Module.pluginConstructors["armor"] = function()
  332.             -- create and return the armor plugin
  333.         end
  334.     ]]
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement