Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- All Credit for Datapanel.lua goes to Tuks.
- Tukui = http://www.tukui.org/download.php.
- Edited by Cokedriver.
- ]]
- local Core = LibStub("AceAddon-3.0"):GetAddon("BasicUI")
- local Module = Core:NewModule("Datapanel", "AceEvent-3.0")
- -- Constants (variables whose values are never altered):
- local PLAYER_CLASS = UnitClass("player")
- local PLAYER_NAME = UnitName("player")
- local PLAYER_REALM = GetRealmName()
- local SCREEN_WIDTH = tonumber(string.match(({GetScreenResolutions()})[GetCurrentResolution()], "(%d+)x+%d"))
- local TOC_VERSION = select(4, GetBuildInfo())
- local GAME_LOCALE = GetLocale()
- -- Variables:
- local db, currentFightDPS, playerRole
- local hexa, hexb = "|cffffffff", "|r" -- not very descriptive names :(
- -- Variables that point to frames or other objects:
- local MainPanel, LeftPanel, CenterPanel, RightPanel, BGPanel
- ------------------------------------------------------------------------
- -- Local functions
- local function RGBToHex(r, g, b)
- if r > 1 then r = 1 elseif r < 0 then r = 0 end
- if g > 1 then g = 1 elseif g < 0 then g = 0 end
- if b > 1 then b = 1 elseif b < 0 then b = 0 end
- return format("|cff%02x%02x%02x", r*255, g*255, b*255)
- end
- local function HexToRGB(hex)
- local rhex, ghex, bhex = strsub(hex, 1, 2), strsub(hex, 3, 4), strsub(hex, 5, 6)
- return tonumber(rhex, 16), tonumber(ghex, 16), tonumber(bhex, 16)
- end
- local function ShortValue(v)
- if v >= 1e6 then
- return format("%.1fm", v / 1e6):gsub("%.?0+([km])$", "%1")
- elseif v >= 1e3 or v <= -1e3 then
- return format("%.1fk", v / 1e3):gsub("%.?0+([km])$", "%1")
- else
- return v
- end
- end
- local function GetTooltipAnchor()
- local _, y = GetCursorPosition()
- if (y * 2) > UIParent:GetHeight() then
- return "ANCHOR_BOTTOM", 1, 3
- else
- return "ANCHOR_TOP", 1, -3
- end
- end
- ------------------------------------------------------------------------
- -- Module functions
- function Module:OnEnable()
- -- You should fix your DB to use module namespaces properly:
- local db = core.db.profile
- -- This line should not be needed if you're using modules correctly:
- if not db.datapanel.enable then return end
- 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?
- self:CreatePanel() -- factor this giant blob out into its own function to keep things clean
- end
- if db.misc.classcolor then
- local _, class = UnitClass("player")
- local color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLOR)[class]
- hexa = color.colorStr -- no need to format it yourself, it's already done
- else
- 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
- end
- -- 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.
- self.plugins = {}
- for name, constructor in pairs(self.pluginConstructors) do
- local position = db.datapanel[name]
- if position and position > 0 then
- local plugin = constructor()
- self.plugins[name] = plugin
- self:PlacePlugin(position, plugin)
- end
- end
- -- no need to make a separate frame to handle events, your module object already does this!
- self:UpdatePlayerRole()
- self:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED", "UpdatePlayerRole")
- end
- function Module:UpdatePlayerRole()
- local spec = GetSpecialization()
- local specRole = GetSpecializationRole(spec) -- no need for a giant table that must be maintained by hand
- if specRole == "TANK" then
- playerRole = "Tank"
- elseif specRole == "HEALER" then
- playerRole = "Caster"
- elseif specRole == "DAMAGER" then
- if UnitPowerType("player") == SPELL_POWER_MANA then
- playerRole = "Caster"
- else
- playerRole = "Melee"
- end
- else
- playerRole = nil -- no spec
- end
- end
- function Module:CreateFontString(parent, file, size, flags)
- local fs = parent:CreateFontString(nil, "OVERLAY")
- fs:SetFont(file, size, flags)
- fs:SetJustifyH("LEFT")
- fs:SetShadowColor(0, 0, 0)
- fs:SetShadowOffset(1.25, -1.25)
- return fs
- end
- function Module:PlacePlugin(position, plugin)
- local left = PanelLeft
- local center = PanelCenter
- local right = PanelRight
- -- Left Panel Data
- if position == 1 then
- plugin:SetParent(left)
- plugin:SetHeight(left:GetHeight())
- plugin:SetPoint('LEFT', left, 30, 0)
- plugin:SetPoint('TOP', left)
- plugin:SetPoint('BOTTOM', left)
- elseif position == 2 then
- plugin:SetParent(left)
- plugin:SetHeight(left:GetHeight())
- plugin:SetPoint('TOP', left)
- plugin:SetPoint('BOTTOM', left)
- elseif position == 3 then
- plugin:SetParent(left)
- plugin:SetHeight(left:GetHeight())
- plugin:SetPoint('RIGHT', left, -30, 0)
- plugin:SetPoint('TOP', left)
- plugin:SetPoint('BOTTOM', left)
- -- Center Panel Data
- elseif position == 4 then
- plugin:SetParent(center)
- plugin:SetHeight(center:GetHeight())
- plugin:SetPoint('LEFT', center, 30, 0)
- plugin:SetPoint('TOP', center)
- plugin:SetPoint('BOTTOM', center)
- elseif position == 5 then
- plugin:SetParent(center)
- plugin:SetHeight(center:GetHeight())
- plugin:SetPoint('TOP', center)
- plugin:SetPoint('BOTTOM', center)
- elseif position == 6 then
- plugin:SetParent(center)
- plugin:SetHeight(center:GetHeight())
- plugin:SetPoint('RIGHT', center, -30, 0)
- plugin:SetPoint('TOP', center)
- plugin:SetPoint('BOTTOM', center)
- -- Right Panel Data
- elseif position == 7 then
- plugin:SetParent(right)
- plugin:SetHeight(right:GetHeight())
- plugin:SetPoint('LEFT', right, 30, 0)
- plugin:SetPoint('TOP', right)
- plugin:SetPoint('BOTTOM', right)
- elseif position == 8 then
- plugin:SetParent(right)
- plugin:SetHeight(right:GetHeight())
- plugin:SetPoint('TOP', right)
- plugin:SetPoint('BOTTOM', right)
- elseif position == 9 then
- plugin:SetParent(right)
- plugin:SetHeight(right:GetHeight())
- plugin:SetPoint('RIGHT', right, -30, 0)
- plugin:SetPoint('TOP', right)
- plugin:SetPoint('BOTTOM', right)
- end
- end
- function Module:CreatePanel()
- if MainPanel then return end -- already done
- -- These are not "local" here because they're already "local" at the top of the file.
- MainPanel = CreateFrame("Frame", "BasicUI_Datapanel", UIParent)
- PanelLeft = CreateFrame("Frame", nil, MainPanel)
- PanelCenter = CreateFrame("Frame", nil, MainPanel)
- PanelRight = CreateFrame("Frame", nil, MainPanel)
- BGPanel = CreateFrame("Frame", nil, MainPanel)
- -- These are common to all panel styles, so don't duplicate code.
- MainPanel:SetHeight(35)
- MainPanel:SetFrameStrata("LOW")
- MainPanel:SetBackdrop({ bgFile = db.datapanel.background, edgeFile = db.datapanel.border, edgeSize = 25, insets = { left = 5, right = 5, top = 5, bottom = 5 } })
- MainPanel:SetBackdropColor(0, 0, 0, 1)
- -- This section can almost certainly be cleaned up by a huge amount,
- -- but I've already spend enough time on this.
- if db.datapanel.panel == "top" then
- MainPanel:SetPoint("TOP", UIParent, 0, 0)
- MainPanel:SetWidth(SCREEN_WIDTH)
- -- Left Panel
- PanelLeft:SetPoint("LEFT", MainPanel, 5, 0)
- PanelLeft:SetHeight(35)
- PanelLeft:SetWidth(SCREEN_WIDTH / 3)
- PanelLeft:SetFrameStrata("LOW")
- PanelLeft:SetFrameLevel(1)
- -- Center Panel
- PanelCenter:SetPoint("CENTER", MainPanel, 0, 0)
- PanelCenter:SetHeight(35)
- PanelCenter:SetWidth(SCREEN_WIDTH / 3)
- PanelCenter:SetFrameStrata("LOW")
- PanelCenter:SetFrameLevel(1)
- -- Right Panel
- PanelRight:SetPoint("RIGHT", MainPanel, -5, 0)
- PanelRight:SetHeight(35)
- PanelRight:SetWidth(SCREEN_WIDTH / 3)
- PanelRight:SetFrameStrata("LOW")
- PanelRight:SetFrameLevel(1)
- -- Battleground Panel
- BGPanel:SetAllPoints(PanelLeft)
- BGPanel:SetFrameStrata("LOW")
- BGPanel:SetFrameLevel(1)
- elseif db.datapanel.panel == "bottom" then
- MainPanel:SetPoint("BOTTOM", UIParent, 0, 0)
- MainPanel:SetWidth(1200)
- -- Left Panel
- PanelLeft:SetPoint("LEFT", MainPanel, 5, 0)
- PanelLeft:SetHeight(35)
- PanelLeft:SetWidth(1200 / 3)
- PanelLeft:SetFrameStrata("LOW")
- PanelLeft:SetFrameLevel(1)
- -- Center Panel
- PanelCenter:SetPoint("CENTER", MainPanel, 0, 0)
- PanelCenter:SetHeight(35)
- PanelCenter:SetWidth(1200 / 3)
- PanelCenter:SetFrameStrata("LOW")
- PanelCenter:SetFrameLevel(1)
- -- Right panel
- PanelRight:SetPoint("RIGHT", MainPanel, -5, 0)
- PanelRight:SetHeight(35)
- PanelRight:SetWidth(1200 / 3)
- PanelRight:SetFrameStrata("LOW")
- PanelRight:SetFrameLevel(1)
- -- Battleground Panel
- BGPanel:SetAllPoints(PanelLeft)
- BGPanel:SetFrameStrata("LOW")
- BGPanel:SetFrameLevel(1)
- elseif db.datapanel.panel == "shortbar" then
- MainPanel:SetPoint("BOTTOM", UIParent, 0, 0)
- MainPanel:SetWidth(725)
- -- Left Panel
- PanelLeft:SetPoint("LEFT", MainPanel, 5, 0)
- PanelLeft:SetHeight(35)
- PanelLeft:SetWidth(725 / 2)
- PanelLeft:SetFrameStrata("LOW")
- PanelLeft:SetFrameLevel(1)
- -- Right panel
- PanelRight:SetPoint("RIGHT", MainPanel, -5, 0)
- PanelRight:SetHeight(35)
- PanelRight:SetWidth(725 / 2)
- PanelRight:SetFrameStrata("LOW")
- PanelRight:SetFrameLevel(1)
- -- Battleground Panel
- BGPanel:SetAllPoints(PanelLeft)
- BGPanel:SetFrameStrata("LOW")
- BGPanel:SetFrameLevel(1)
- end
- -- I did not include any of your code for moving other frames,
- -- but you would want to include it here, after fixing all of the
- -- parts where you break secure frames by overriding their methods.
- end
- ------------------------------------------------------------------------
- -- Panel creation
- -- This can be simplified hugely by using a table, instead of a bunch of if/end blocks.
- Module.pluginConstructors = {
- armor = function()
- local plugin = CreateFrame("Frame", nil, MainPanel) -- proper parentage
- plugin:EnableMouse(true)
- local text = plugin:CreateFontString(nil, "OVERLAY") -- proper parentage
- text:SetFont(db.media.fontNormal, db.media.fontSize,"THINOUTLINE")
- plugin.text = Text -- important!
- plugin:RegisterEvent("PLAYER_ENTERING_WORLD")
- plugin:RegisterUnitEvent("UNIT_AURA", "player") -- limit UNIT_* events to the player when possible
- plugin:RegisterUnitEvent("UNIT_INVENTORY_CHANGED", "player")
- plugin:SetScript("OnEvent", function(self)
- local _, effectiveArmor = UnitArmor("player")
- self.text:SetFormattedText("%sArmor:|r %s", hexa, effectiveArmor)
- self:SetWidth(self.text:GetStringWidth()) -- important!
- end)
- plugin:SetScript("OnEnter", function(self)
- if UnitAffectingCombat("player") then return end
- local anchor, x, y = GetTooltipAnchor(self)
- GameTooltip:SetOwner(self, GameTooltip:SetOwner(self, anchor, x, y)
- GameTooltip:AddLine("Mitigation by Level:")
- -- Your code for this section does not work properly, because you
- -- only calculated the value of "mitigation" once for level 83, and
- -- then showed that same value for all 4 level lines and the target line.
- -- I didn't update it for you, or include the broken stuff here.
- GameTooltip:Show()
- end)
- plugin:SetScript("OnClick", function(self, button)
- ToggleCharacter("PaperDollFrame")
- end)
- return plugin -- important!
- end,
- -- You can update the rest yourself if you want.
- -- Also, it would be more maintainable to put each plugin in its own file, and add them to this table from there, eg:
- --[[
- local Core = LibStub("AceAddon-3.0"):GetAddon("BasicUI")
- local Module = Core:GetModule("Datapanel")
- Module.pluginConstructors["armor"] = function()
- -- create and return the armor plugin
- end
- ]]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement