Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
4,931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 36.64 KB | None | 0 0
  1. -- Tidy Plates - SMILE! :-D
  2.  
  3. TidyPlatesDebug = false
  4. DebugCount = 1
  5. ---------------------------------------------------------------------------------------------------------------------
  6. -- Variables and References
  7. ---------------------------------------------------------------------------------------------------------------------
  8. TidyPlates = {}
  9.  
  10. -- Local References
  11. local _
  12. local select, pairs, tostring  = select, pairs, tostring                -- Local function copy
  13. local CreateTidyPlatesStatusbar = CreateTidyPlatesStatusbar             -- Local function copy
  14. local WorldFrame, UIParent = WorldFrame, UIParent
  15.  
  16. -- Internal Data
  17. local Plates, PlatesVisible, PlatesFading, GUID = {}, {}, {}, {}                    -- Plate Lists
  18. local nameplate, extended, bars, regions, visual, carrier                           -- Temp/Local References
  19. local unit, unitcache, style, stylename, unitchanged                                -- Temp/Local References
  20. local numChildren = -1                                                              -- Cache the current number of plates
  21. local activetheme = {}                                                              -- Table Placeholder
  22. local InCombat, HasTarget = false, false                                            -- Player State Data
  23. local EnableFadeIn = true                                                           -- Enables Alpha Effects
  24. local EMPTY_TEXTURE = "Interface\\Addons\\TidyPlates\\Media\\Empty"
  25. local ResetPlates, UpdateAll = false, false
  26. local CompatibilityMode = false
  27.  
  28. ---------------------------------------------------------------------------------------------------------------------
  29. -- Core Function Declaration
  30. ---------------------------------------------------------------------------------------------------------------------
  31. -- Helpers
  32. local function ClearIndices(t) if t then for i,v in pairs(t) do t[i] = nil end return t end end
  33. local function IsPlateShown(plate) return plate and plate:IsShown() end
  34.  
  35. -- Queueing
  36. local function SetUpdateMe(plate) plate.UpdateMe = true end
  37. local function SetUpdateAll() UpdateAll = true end
  38. local function SetUpdateHealth(source) source.parentPlate.UpdateHealth = true end
  39.  
  40. -- Style
  41. local UpdateStyle
  42.  
  43. -- Indicators
  44. local UpdateIndicator_CustomScaleText, UpdateIndicator_Standard, UpdateIndicator_CustomAlpha
  45. local UpdateIndicator_Level, UpdateIndicator_ThreatGlow, UpdateIndicator_RaidIcon
  46. local UpdateIndicator_EliteIcon, UpdateIndicator_UnitColor, UpdateIndicator_Name
  47. local UpdateIndicator_HealthBar, UpdateIndicator_Target
  48. local OnUpdateCastbar, OnShowCastbar, OnHideCastbar
  49.  
  50. -- Event Functions
  51. local OnShowNameplate, OnHideNameplate, OnUpdateNameplate, OnResetNameplate
  52. local OnHealthUpdate, UpdateUnitCondition
  53. local UpdateUnitContext, OnRequestWidgetUpdate, OnRequestDelegateUpdate
  54. local UpdateUnitIdentity
  55. local OnNewNameplate
  56.  
  57. -- Main Loop
  58. local OnUpdate
  59. local OnNewNameplate
  60. local ForEachPlate
  61.  
  62. -- Context
  63. local function isHighlighted(plate)
  64.     --local r,g,b = plate.extended.regions.name:GetTextColor()
  65.     return (plate.extended.regions.highlight:IsShown())     -- 5.x Mouseover; Not valid in 6.0
  66.     --return ((b < .1) and (g > .8)) --or (plate.extended.unit.guid == UnitGUID("mouseover"))
  67.     -- ceil(select(3, plate.extended.regions.name:GetTextColor())) == 0 -- 6.0 Mouseover; Check name color
  68.     --[[
  69.         if you're in combat with something, the name goes red.
  70.         if you've got it highlighted, the name goes yellow, but not if it's red.
  71.     --]]
  72. end
  73.  
  74. -- UpdateReferences
  75. local function UpdateReferences(plate)
  76.     nameplate = plate
  77.     extended = plate.extended
  78.  
  79.     carrier = plate.carrier
  80.     bars = extended.bars
  81.     regions = extended.regions
  82.     unit = extended.unit
  83.     unitcache = extended.unitcache
  84.     visual = extended.visual
  85.     style = extended.style
  86.     threatborder = visual.threatborder
  87. end
  88.  
  89. ---------------------------------------------------------------------------------------------------------------------
  90. -- Nameplate Detection & Update Loop
  91. ---------------------------------------------------------------------------------------------------------------------
  92. do
  93.     -- Local References
  94.     local WorldGetNumChildren, WorldGetChildren = WorldFrame.GetNumChildren, WorldFrame.GetChildren
  95.  
  96.     -- IsNameplate
  97.     local function IsNameplate(frame)
  98.         if frame:GetName() and strfind(frame:GetName(), '^NamePlate%d') then
  99.             local textureChild, nameTextChild =frame:GetChildren()
  100.             --local nameTextChild = select(2, frame:GetChildren())
  101.             if nameTextChild then
  102.                 local nameTextRegion, otherRegion, anotherRegion = nameTextChild:GetRegions()
  103.                 --print(nameTextRegion:GetObjectType(), otherRegion, anotherRegion)
  104.                 return (nameTextRegion and nameTextRegion:GetObjectType() == 'FontString')
  105.             end
  106.         end
  107.     end
  108.  
  109.     -- ForEachPlate
  110.     function ForEachPlate(functionToRun, ...)
  111.         for plate in pairs(PlatesVisible) do
  112.             if plate.extended.Active then
  113.                 functionToRun(plate, ...)
  114.             end
  115.         end
  116.     end
  117.  
  118.         -- OnUpdate; This function is run frequently, on every clock cycle
  119.     function OnUpdate(self, e)
  120.         -- Poll Loop
  121.         local plate, curChildren
  122.  
  123.  
  124.             DebugCount = DebugCount + 1
  125.             if DebugCount > 200 then DebugCount = 1 end
  126.  
  127.  
  128.         for plate in pairs(PlatesVisible) do
  129.             local UpdateMe = UpdateAll or plate.UpdateMe
  130.             local UpdateHealth = plate.UpdateHealth
  131.             local carrier = plate.carrier
  132.             local extended = plate.extended
  133.  
  134.             -- Hide Carrier; FPS Improvement while changing position, etc.
  135.             carrier:Hide()
  136.  
  137.             -- Get Parent Alpha (Target Detection)
  138.             if HasTarget then
  139.                 plate.alpha = plate:GetAlpha()
  140.             end
  141.  
  142.             -- Positioning
  143.             if CompatibilityMode then
  144.                 plate:SetAlpha(1)
  145.             else
  146.                 --local _,_,_,x,y = extended.bars.group:GetPoint()
  147.                 --carrier:SetPoint("CENTER", WorldFrame, "BOTTOMLEFT", floor(x), floor(y+16))
  148.                 local x,y = plate:GetCenter()
  149.                 carrier:SetPoint("CENTER", WorldFrame, "BOTTOMLEFT", floor(x), floor(y))
  150.             end
  151.  
  152.             -- Get Highlight (Mouseover Detection)
  153.             if isHighlighted(plate) then
  154.                 if not plate.isMouseover then
  155.                     plate.isMouseover = true
  156.                     UpdateMe = true
  157.                 end
  158.  
  159.             elseif plate.isMouseover then
  160.                 plate.isMouseover = false
  161.                 UpdateMe = true
  162.             else
  163.  
  164.             end
  165.  
  166.  
  167.             -- Check for an Update Request
  168.             if UpdateMe or UpdateHealth then
  169.                 if not UpdateMe then
  170.                     OnHealthUpdate(plate)
  171.                 else
  172.                     OnUpdateNameplate(plate)
  173.                 end
  174.                 plate.UpdateMe = false
  175.                 plate.UpdateHealth = false
  176.             end
  177.  
  178.             -- Scaling Animation
  179.  
  180.             -- Alpha Animation
  181.             --EnableFadeIn
  182.             local increment = e * 7
  183.             if extended.visibleAlpha ~= extended.requestedAlpha then
  184.  
  185.                 if EnableFadeIn and extended.requestedAlpha > extended.visibleAlpha + increment then
  186.                     extended.visibleAlpha = extended.visibleAlpha + increment
  187.                 elseif EnableFadeIn and extended.requestedAlpha < extended.visibleAlpha - (increment * 1.5) then
  188.                     extended.visibleAlpha = extended.visibleAlpha - (increment * 1.5)
  189.                 else
  190.                     extended.visibleAlpha = extended.requestedAlpha
  191.                 end
  192.  
  193.                 extended:SetAlpha(extended.visibleAlpha)
  194.             end
  195.  
  196.             -- Restore Carrier
  197.             carrier:Show()
  198.         end
  199.  
  200.         -- Reset Mass-Update Flag
  201.         UpdateAll = false
  202.  
  203.         -- Detect New Nameplates
  204.         curChildren = select('#', WorldFrame:GetChildren())
  205.         if numChildren ~= curChildren then
  206.             local i, f
  207.             for i = 1, curChildren do
  208.                 f = select(i, WorldFrame:GetChildren())
  209.  
  210.                    if not Plates[f] and IsNameplate(f) then
  211.                          OnNewNameplate(f)
  212.                    end
  213.  
  214.             end
  215.             numChildren = curChildren
  216.         end
  217.  
  218.     end
  219. end
  220.  
  221. ---------------------------------------------------------------------------------------------------------------------
  222. --  Nameplate Extension: Applies scripts, hooks, and adds additional frame variables and regions
  223. ---------------------------------------------------------------------------------------------------------------------
  224. do
  225.     -- ApplyPlateExtesion
  226.     function OnNewNameplate(plate)
  227.         Plates[plate] = true
  228.  
  229.         -- Blizzard References
  230.         --------------------------------
  231.         local bars, regions = {}, {}
  232.         local bargroup, namegroup = plate:GetChildren()
  233.         local health, _, cast = bargroup:GetChildren()
  234.  
  235.         bars.health = health
  236.         bars.cast = cast
  237.         bars.group = bargroup
  238.  
  239.         health.parentPlate = plate      -- Needed for OnHealthUpdate Hook
  240.         cast.parentPlate = plate        -- Needed for UpdateCastBar Hook
  241.  
  242.         -- Region References
  243.         regions.threatglow,
  244.         regions.healthborder,
  245.         regions.highlight,
  246.         regions.level,
  247.         regions.skullicon,
  248.         regions.raidicon,
  249.         regions.eliteicon
  250.             = bargroup:GetRegions()
  251.  
  252.         regions.name
  253.             = namegroup:GetRegions()
  254.  
  255.         regions.castborder,
  256.         regions.castnostop,
  257.         regions.spellicon,
  258.         regions.spelltext,
  259.         regions.spellshadow
  260.             = select(2, cast:GetRegions())
  261.  
  262.         -- Make Blizzard Plate invisible
  263.         -- [[
  264.         health:SetStatusBarTexture(EMPTY_TEXTURE)
  265.         cast:SetStatusBarTexture(EMPTY_TEXTURE)
  266.  
  267.         health:Hide()
  268.         namegroup:Hide()
  269.  
  270.         regions.threatglow:SetTexture(nil)
  271.         regions.healthborder:Hide()
  272.         regions.highlight:SetTexture(nil)
  273.         regions.level:SetWidth( 000.1 )
  274.         regions.level:Hide()
  275.         regions.skullicon:SetTexture(nil)
  276.         --regions.skullicon:SetAlpha(0)
  277.         regions.raidicon:SetAlpha( 0 )
  278.         --regions.eliteicon:SetTexture(nil)
  279.         regions.eliteicon:SetAlpha(0)
  280.  
  281.         regions.name:Hide()
  282.  
  283.         regions.castborder:SetTexture(nil)
  284.         regions.castnostop:SetTexture(nil)
  285.         regions.spellicon:SetTexCoord( 0, 0, 0, 0 )
  286.         regions.spellicon:SetWidth(.001)
  287.         regions.spellshadow:SetTexture(nil)
  288.         regions.spellshadow:Hide()
  289.         regions.spelltext:Hide()
  290.         --]]
  291.  
  292.         -- Tidy Plates Frame
  293.         --------------------------------
  294.         local carrier
  295.         local frameName = "TidyPlatesCarrier"..numChildren
  296.  
  297.         if CompatibilityMode then carrier = CreateFrame("Frame", frameName, plate)
  298.             else carrier = CreateFrame("Frame", frameName, WorldFrame) end
  299.  
  300.         local extended = CreateFrame("Frame", nil, carrier)
  301.  
  302.         plate.carrier = carrier
  303.         plate.extended = extended
  304.  
  305.         -- Add Graphical Elements
  306.         local visual = {}
  307.         -- Status Bars
  308.         local healthbar = CreateTidyPlatesStatusbar(extended)
  309.         local castbar = CreateTidyPlatesStatusbar(extended)
  310.         healthbar:SetFrameStrata("BACKGROUND")
  311.         castbar:SetFrameStrata("BACKGROUND")
  312.         visual.healthbar = healthbar
  313.         visual.castbar = castbar
  314.         bars.healthbar = healthbar      -- For Threat Plates Compatibility
  315.         bars.castbar = castbar          -- For Threat Plates Compatibility
  316.         -- Parented to Health Bar - Lower Frame
  317.         visual.healthborder = healthbar:CreateTexture(nil, "ARTWORK")
  318.         visual.threatborder = healthbar:CreateTexture(nil, "ARTWORK")
  319.         visual.highlight = healthbar:CreateTexture(nil, "OVERLAY")
  320.         -- Parented to Extended - Middle Frame
  321.         visual.raidicon = extended:CreateTexture(nil, "ARTWORK")
  322.         visual.eliteicon = extended:CreateTexture(nil, "OVERLAY")
  323.         visual.skullicon = extended:CreateTexture(nil, "OVERLAY")
  324.         visual.target = extended:CreateTexture(nil, "BACKGROUND")
  325.         visual.customtext = extended:CreateFontString(nil, "OVERLAY")
  326.         visual.name  = extended:CreateFontString(nil, "OVERLAY")
  327.         visual.level = extended:CreateFontString(nil, "OVERLAY")
  328.         -- Cast Bar Frame - Highest Frame
  329.         visual.castborder = castbar:CreateTexture(nil, "ARTWORK")
  330.         visual.castnostop = castbar:CreateTexture(nil, "ARTWORK")
  331.         visual.spellicon = castbar:CreateTexture(nil, "OVERLAY")
  332.         visual.spelltext = castbar:CreateFontString(nil, "OVERLAY")
  333.         -- Set Base Properties
  334.         visual.raidicon:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
  335.         visual.highlight:SetAllPoints(visual.healthborder)
  336.         visual.highlight:SetBlendMode("ADD")
  337.         extended:SetFrameStrata("BACKGROUND")
  338.         healthbar:SetFrameLevel(0)
  339.         extended:SetFrameLevel(0)
  340.         castbar:SetFrameLevel(0)
  341.         castbar:Hide()
  342.         castbar:SetStatusBarColor(1,.8,0)
  343.         carrier:SetSize(16, 16)
  344.         -- Default Fonts
  345.         visual.customtext:SetFont(STANDARD_TEXT_FONT, 12, "NONE")
  346.         visual.name:SetFont(STANDARD_TEXT_FONT, 12, "NONE")
  347.         visual.level:SetFont(STANDARD_TEXT_FONT, 12, "NONE")
  348.         visual.spelltext:SetFont(STANDARD_TEXT_FONT, 12, "NONE")
  349.  
  350.         -- Tidy Plates Frame References
  351.         extended.regions = regions
  352.         extended.bars = bars
  353.         extended.visual = visual
  354.  
  355.         -- Allocate Tables
  356.         extended.style,
  357.         extended.unit,
  358.         extended.unitcache,
  359.         extended.stylecache,
  360.         extended.widgets
  361.             = {}, {}, {}, {}, {}
  362.  
  363.         extended.stylename = ""
  364.  
  365.         -- Hook for Updates
  366.         plate:HookScript("OnShow", OnShowNameplate )
  367.         plate:HookScript("OnHide", OnHideNameplate )
  368.  
  369.         health:HookScript("OnValueChanged", SetUpdateHealth)
  370.         health:HookScript("OnMinMaxChanged", SetUpdateHealth)
  371.  
  372.         cast:HookScript("OnShow", OnShowCastbar)
  373.         cast:HookScript("OnHide", OnHideCastbar)
  374.         cast:HookScript("OnValueChanged", OnUpdateCastbar)
  375.  
  376.         -- In 6.9.9, the 'health' object was hooked for OnShow and OnHide
  377.         -- In 6.10, this has been changed to plate, to allow us to hide 'health' to gain some FPS
  378.         if CompatibilityMode then
  379.             carrier:SetPoint("CENTER", bargroup, "CENTER")
  380.         end
  381.  
  382.         OnShowNameplate(plate)
  383.     end
  384.  
  385. end
  386.  
  387. ---------------------------------------------------------------------------------------------------------------------
  388. -- Nameplate Script Handlers
  389. ---------------------------------------------------------------------------------------------------------------------
  390. do
  391.  
  392.     -- UpdateUnitCache
  393.     local function UpdateUnitCache() for key, value in pairs(unit) do unitcache[key] = value end end
  394.  
  395.     -- CheckNameplateStyle
  396.     local function CheckNameplateStyle()
  397.         if activetheme.SetStyle then
  398.             stylename = activetheme.SetStyle(unit); extended.style = activetheme[stylename]
  399.         else extended.style = activetheme; stylename = tostring(activetheme) end
  400.  
  401.         style = extended.style
  402.  
  403.         if extended.stylename ~= stylename then
  404.             UpdateStyle()
  405.             extended.stylename = stylename
  406.             unit.style = stylename
  407.         end
  408.  
  409.     end
  410.  
  411.     -- ProcessUnitChanges
  412.     local function ProcessUnitChanges()
  413.             -- Unit Cache
  414.             unitchanged = false
  415.             for key, value in pairs(unit) do
  416.                 if unitcache[key] ~= value then
  417.                     unitchanged = true
  418.                 end
  419.             end
  420.  
  421.             -- Update Style/Indicators
  422.             if unitchanged or (not style)then
  423.                 CheckNameplateStyle()
  424.                 UpdateIndicator_Standard()
  425.                 UpdateIndicator_HealthBar()
  426.                 UpdateIndicator_Target()
  427.             end
  428.  
  429.             -- Update Widgets
  430.             if activetheme.OnUpdate then activetheme.OnUpdate(extended, unit) end
  431.  
  432.             -- Update Delegates
  433.             UpdateIndicator_ThreatGlow()
  434.             UpdateIndicator_CustomAlpha()
  435.             UpdateIndicator_CustomScaleText()
  436.  
  437.             -- Cache the old unit information
  438.             UpdateUnitCache()
  439.     end
  440.  
  441.     ---------------------------------------------------------------------------------------------------------------------
  442.     -- Create / Hide / Show Event Handlers
  443.     ---------------------------------------------------------------------------------------------------------------------
  444.  
  445.     -- OnShowNameplate
  446.     function OnShowNameplate(plate)
  447.         UpdateReferences(plate)
  448.  
  449.         carrier:Show()
  450.  
  451.         PlatesVisible[plate] = true
  452.         wipe(extended.unit)
  453.         wipe(extended.unitcache)
  454.  
  455.         unit.frame = extended
  456.         unit.alpha = 0
  457.         unit.isTarget = false
  458.         unit.isMouseover = false
  459.         extended.unitcache = ClearIndices(extended.unitcache)
  460.         extended.stylename = ""
  461.         extended.Active = true
  462.  
  463.         -- For Fading In
  464.         PlatesFading[plate] = EnableFadeIn
  465.         extended.requestedAlpha = 0
  466.         extended.visibleAlpha = 0
  467.         extended:Hide()     -- Yes, it seems counterintuitive, but...
  468.         extended:SetAlpha(0)
  469.  
  470.         -- Graphics
  471.         unit.isCasting = false
  472.         visual.castbar:Hide()
  473.         visual.highlight:Hide()
  474.         regions.highlight:Hide()
  475.  
  476.         -- Widgets/Extensions
  477.         if activetheme.OnInitialize then activetheme.OnInitialize(extended, activetheme) end
  478.  
  479.         -- Initial Data Gather
  480.         -- 6.12.Beta3: Disabled initial Data Gather because certain units are showing up with Target Alpha on the first cycle.
  481.         --UpdateUnitIdentity()
  482.         --UpdateUnitContext(plate)
  483.         --ProcessUnitChanges()
  484.  
  485.         plate.UpdateMe = true
  486.     end
  487.  
  488.  
  489.     -- OnHideNameplate
  490.     function OnHideNameplate(plate)
  491.         plate.extended:Hide()
  492.         plate.carrier:Hide()
  493.  
  494.         UpdateReferences(plate)
  495.  
  496.         extended.Active = false
  497.  
  498.         PlatesVisible[plate] = nil
  499.         if unit.guid then GUID[unit.guid] = nil end
  500.  
  501.         visual.castbar:Hide()
  502.         visual.castbar:SetScript("OnUpdate", nil)
  503.         unit.isCasting = false
  504.  
  505.         -- Remove anything from the function queue
  506.         plate.UpdateMe = false
  507.  
  508.         for widgetname, widget in pairs(extended.widgets) do widget:Hide() end
  509.     end
  510.  
  511.     -- OnUpdateNameplate
  512.     function OnUpdateNameplate(plate)
  513.         -- Gather Information
  514.         UpdateReferences(plate)
  515.         UpdateUnitIdentity()
  516.         UpdateUnitContext(plate)
  517.  
  518.         ProcessUnitChanges()
  519.  
  520.         -- Keep Regions Hidden
  521.         regions.level:Hide()
  522.     end
  523.  
  524.     -- OnHealthUpdate
  525.     function OnHealthUpdate(plate)
  526.         UpdateUnitCondition(plate)
  527.         ProcessUnitChanges()
  528.         UpdateIndicator_HealthBar()     -- Just to be on the safe side
  529.     end
  530.  
  531.      -- OnResetNameplate
  532.     function OnResetNameplate(plate)
  533.         local extended = plate.extended
  534.         plate.UpdateMe = true
  535.         extended.unitcache = ClearIndices(extended.unitcache)
  536.         extended.stylename = ""
  537.         OnShowNameplate(plate)
  538.     end
  539.  
  540. end
  541.  
  542.  
  543. ---------------------------------------------------------------------------------------------------------------------
  544. --  Unit Updates: Updates Unit Data, Requests indicator updates
  545. ---------------------------------------------------------------------------------------------------------------------
  546. do
  547.     -- Raid Icon Lookup table
  548.     local RaidIconCoordinate = { --from GetTexCoord. input is ULx and ULy (first 2 values).
  549.         [0]     = { [0]     = "STAR", [0.25]    = "MOON", },
  550.         [0.25]  = { [0]     = "CIRCLE", [0.25]  = "SQUARE", },
  551.         [0.5]   = { [0]     = "DIAMOND", [0.25] = "CROSS", },
  552.         [0.75]  = { [0]     = "TRIANGLE", [0.25]    = "SKULL", }, }
  553.  
  554.     -- ColorToString: Converts a color to a string with a C- prefix (For Color Lookup)
  555.     local function ColorToString(r,g,b) return "C"..math.floor((100*r) + 0.5)..math.floor((100*g) + 0.5)..math.floor((100*b) + 0.5) end
  556.  
  557.  
  558.     -- Class Color Lookup Table
  559.     local ClassReference = {}
  560.     do
  561.         for classname, color in pairs(RAID_CLASS_COLORS) do
  562.             local r, g, b = color.r, color.g, color.b
  563.             local colorstring = ColorToString(r, g, b)
  564.             ClassReference[colorstring] = classname
  565.         end
  566.         ClassReference["C010060"] = "MONK"
  567.     end
  568.  
  569.     -- GetUnitCombatStatus: Determines if a unit is in combat by checking the name text color
  570.     local function GetUnitCombatStatus(r, g, b) return (r > .5 and g < .5) end
  571.  
  572.     -- GetUnitAggroStatus: Determines if a unit is attacking, by looking at aggro glow region
  573.     local function GetUnitAggroStatus( threatRegion )
  574.         if not  threatRegion:IsShown() then return "LOW", 0 end
  575.  
  576.  
  577.  
  578.         local red, green, blue, alpha = threatRegion:GetVertexColor()
  579.         local opacity = threatRegion:GetVertexColor()
  580.  
  581.  
  582.         if threatRegion:IsShown() and (alpha < .9 or opacity < .9) then
  583.         --if threatRegion:IsShown() and alpha > .9 then
  584.             print(unit.name, alpha, opacity)
  585.  
  586.         end
  587.  
  588.  
  589.         if red > 0 then
  590.             if green > 0 then
  591.                 if blue > 0 then return "MEDIUM", 1 end
  592.                 return "MEDIUM", 2
  593.             end
  594.             return "HIGH", 3
  595.         end
  596.     end
  597.  
  598.     -- GetUnitReaction: Determines the reaction, and type of unit from the health bar color
  599.     local function GetUnitReaction(red, green, blue)
  600.         if red < .01 then   -- Friendly
  601.             if blue < .01 and green > .99 then return "FRIENDLY", "NPC"
  602.             elseif blue > .99 and green < .01 then return "FRIENDLY", "PLAYER"
  603.             end
  604.         elseif red > .99 then
  605.             if blue < .01 and green > .99 then return "NEUTRAL", "NPC"
  606.             elseif blue < .01 and green < .01 then return "HOSTILE", "NPC"
  607.             end
  608.         elseif red > .5 and red < .6 then
  609.             if green > .5 and green < .6 and blue > .5 and blue < .6 then return "TAPPED", "NPC" end    -- .533, .533, .99  -- Tapped Mob
  610.         end
  611.         return "HOSTILE", "PLAYER"
  612.     end
  613.  
  614.     -- UpdateUnitIdentity: Updates Low-volatility Unit Data
  615.     function UpdateUnitIdentity()
  616.         unit.name = regions.name:GetText()
  617.         unit.rawName = gsub(unit.name, " %(%*%)", "")
  618.  
  619.         unit.isBoss = regions.skullicon:IsShown()
  620.         unit.isDangerous = unit.isBoss
  621.  
  622.         unit.isElite = regions.eliteicon:IsShown()                      -- 6.0
  623.  
  624.         if bars.group:GetScale() > .9 then
  625.             unit.platetype = 1
  626.             unit.isTrivial = false; unit.isMini = false
  627.  
  628.         else
  629.             unit.platetype = 2
  630.             unit.isTrivial = true; unit.isMini = true
  631.  
  632.         end
  633.  
  634.         if unit.isBoss then
  635.             unit.level = "??"
  636.             unit.levelcolorRed, unit.levelcolorGreen, unit.levelcolorBlue = 1, 0, 0
  637.         else
  638.             unit.level = regions.level:GetText()
  639.             unit.levelcolorRed, unit.levelcolorGreen, unit.levelcolorBlue = regions.level:GetTextColor()
  640.         end
  641.     end
  642.  
  643.  
  644.         -- UpdateUnitContext: Updates Target/Mouseover
  645.     function UpdateUnitContext(plate)
  646.         local guid, healthmax
  647.  
  648.         UpdateReferences(plate)
  649.  
  650.         unit.isMouseover = plate.isMouseover
  651.  
  652.         if HasTarget then
  653.             unit.alpha = plate.alpha
  654.         else
  655.             unit.alpha = 1
  656.         end
  657.  
  658.         unit.isMouseover = isHighlighted(plate)
  659.  
  660.         unit.isTarget = HasTarget and unit.alpha == 1
  661.  
  662.         if unit.isMouseover then
  663.             visual.highlight:Show()
  664.             guid = UnitGUID("mouseover")
  665.             healthmax = UnitHealthMax("mouseover")
  666.         else visual.highlight:Hide() end
  667.  
  668.         if unit.isTarget then
  669.             guid = UnitGUID("target")
  670.             healthmax = UnitHealthMax("target")
  671.         end
  672.  
  673.         -- Update and verify guid
  674.         if unit.guid then
  675.             if guid and (unit.guid ~= guid) then
  676.                 GUID[unit.guid] = nil       -- Clear out old GUID
  677.                 GUID[guid] = plate          -- Update new GUID
  678.                 unit.guid = guid
  679.             end
  680.         else
  681.             if guid then
  682.                 unit.guid = guid
  683.                 GUID[guid] = plate
  684.             end
  685.         end
  686.  
  687.         if healthmax then
  688.             unit.healthmax = healthmax
  689.         end
  690.        
  691.         UpdateUnitCondition(plate)  -- This updates a bunch of properties
  692.         if activetheme.OnContextUpdate then activetheme.OnContextUpdate(extended, unit) end
  693.         if activetheme.OnUpdate then activetheme.OnUpdate(extended, unit) end
  694.     end
  695.  
  696.     -- UpdateUnitCondition: High volatility data
  697.     function UpdateUnitCondition(plate)
  698.         UpdateReferences(plate)
  699.  
  700.         if InCombat then unit.threatSituation, unit.threatValue = GetUnitAggroStatus(regions.threatglow)
  701.         else unit.threatSituation = "LOW"; unit.threatValue = 0 end
  702.  
  703.         unit.red, unit.green, unit.blue = bars.health:GetStatusBarColor()
  704.  
  705.         unit.reaction, unit.type = GetUnitReaction(unit.red, unit.green, unit.blue)
  706.         unit.isInCombat = GetUnitCombatStatus(regions.name:GetTextColor())
  707.         unit.class = ClassReference[ColorToString(unit.red, unit.green, unit.blue)] or "UNKNOWN"
  708.  
  709.         local healthpct = tonumber(bars.health:GetValue()) or 0
  710.         unit.healthmax = unit.healthmax or 100
  711.         unit.health = ceil(healthpct * unit.healthmax)
  712.        
  713.         unit.isMarked = regions.raidicon:IsShown() or false
  714.  
  715.         if unit.isMarked then
  716.             local ux, uy = regions.raidicon:GetTexCoord()
  717.             unit.raidIcon = RaidIconCoordinate[ux][uy]
  718.         else unit.raidIcon = nil end
  719.  
  720.     end
  721.  
  722.     -- OnRequestWidgetUpdate: Calls Update on just the Widgets
  723.     function OnRequestWidgetUpdate(plate)
  724.         if not IsPlateShown(plate) then return end
  725.         UpdateReferences(plate)
  726.         if activetheme.OnContextUpdate then activetheme.OnContextUpdate(extended, unit) end
  727.         if activetheme.OnUpdate then activetheme.OnUpdate(extended, unit) end
  728.     end
  729.  
  730.     -- OnRequestDelegateUpdate: Updates just the delegate function indicators
  731.     function OnRequestDelegateUpdate(plate)
  732.             if not IsPlateShown(plate) then return end
  733.             UpdateReferences(plate)
  734.             UpdateIndicator_ThreatGlow()
  735.             UpdateIndicator_CustomAlpha()
  736.             UpdateIndicator_CustomScaleText()
  737.     end
  738.  
  739. end     -- End of Nameplate/Unit Events
  740.  
  741.  
  742. ---------------------------------------------------------------------------------------------------------------------
  743. -- Indicators: These functions update the color, texture, strings, and frames within a style.
  744. ---------------------------------------------------------------------------------------------------------------------
  745. do
  746.     local color = {}
  747.     local threatborder, alpha, forcealpha, scale
  748.  
  749.  
  750.     -- UpdateIndicator_HealthBar: Updates the value on the health bar
  751.     function UpdateIndicator_HealthBar()
  752.         visual.healthbar:SetMinMaxValues(bars.health:GetMinMaxValues())
  753.         visual.healthbar:SetValue(bars.health:GetValue())
  754.     end
  755.  
  756.  
  757.     -- UpdateIndicator_Name:
  758.     function UpdateIndicator_Name()
  759.         visual.name:SetText(unit.name)
  760.  
  761.         -- Name Color
  762.         if activetheme.SetNameColor then
  763.             visual.name:SetTextColor(activetheme.SetNameColor(unit))
  764.         else visual.name:SetTextColor(1,1,1,1) end
  765.         --if activetheme.SetStatusbarWidthMatching then MatchTextWidth() end
  766.     end
  767.  
  768.  
  769.     -- UpdateIndicator_Level:
  770.     function UpdateIndicator_Level()
  771.         visual.level:SetText(unit.level)
  772.         local tr, tg, tb = regions.level:GetTextColor()
  773.         visual.level:SetTextColor(tr, tg, tb)
  774.     end
  775.  
  776.  
  777.     -- UpdateIndicator_ThreatGlow: Updates the aggro glow
  778.     function UpdateIndicator_ThreatGlow()
  779.         if not style.threatborder.show then return end
  780.         threatborder = visual.threatborder
  781.         if activetheme.SetThreatColor then
  782.  
  783.             threatborder:SetVertexColor(activetheme.SetThreatColor(unit) )
  784.         else
  785.             if InCombat and unit.reaction ~= "FRIENDLY" and unit.type == "NPC" then
  786.                 local color = style.threatcolor[unit.threatSituation]
  787.                 threatborder:Show()
  788.                 threatborder:SetVertexColor(color.r, color.g, color.b, (color.a or 1))
  789.             else threatborder:Hide() end
  790.         end
  791.     end
  792.  
  793.  
  794.     -- UpdateIndicator_Target
  795.     function UpdateIndicator_Target()
  796.         if unit.isTarget and style.target.show then visual.target:Show() else visual.target:Hide() end
  797.     end
  798.  
  799.  
  800.     -- UpdateIndicator_RaidIcon
  801.     function UpdateIndicator_RaidIcon()
  802.         if unit.isMarked and style.raidicon.show then
  803.             visual.raidicon:Show()
  804.             visual.raidicon:SetTexCoord(regions.raidicon:GetTexCoord())
  805.         else visual.raidicon:Hide() end
  806.     end
  807.  
  808.  
  809.     -- UpdateIndicator_EliteIcon: Updates the border overlay art and threat glow to Elite or Non-Elite art
  810.     function UpdateIndicator_EliteIcon()
  811.         threatborder = visual.threatborder
  812.         if unit.isElite and style.eliteicon.show then visual.eliteicon:Show() else visual.eliteicon:Hide() end
  813.     end
  814.  
  815.  
  816.     -- UpdateIndicator_UnitColor: Update the health bar coloring, if needed
  817.     function UpdateIndicator_UnitColor()
  818.         -- Set Health Bar
  819.         if activetheme.SetHealthbarColor then
  820.             visual.healthbar:SetAllColors(activetheme.SetHealthbarColor(unit))
  821.         else visual.healthbar:SetStatusBarColor(bars.health:GetStatusBarColor()) end
  822.         -- Name Color
  823.         if activetheme.SetNameColor then
  824.             visual.name:SetTextColor(activetheme.SetNameColor(unit))
  825.         else visual.name:SetTextColor(1,1,1,1) end
  826.     end
  827.  
  828.  
  829.     -- UpdateIndicator_Standard: Updates Non-Delegate Indicators
  830.     function UpdateIndicator_Standard()
  831.         if IsPlateShown(nameplate) then
  832.             if unitcache.name ~= unit.name then UpdateIndicator_Name() end
  833.             if unitcache.level ~= unit.level then UpdateIndicator_Level() end
  834.             UpdateIndicator_RaidIcon()
  835.             if unitcache.isElite ~= unit.isElite then UpdateIndicator_EliteIcon() end
  836.         end
  837.     end
  838.  
  839.  
  840.     -- UpdateIndicator_CustomAlpha: Calls the alpha delegate to get the requested alpha
  841.     function UpdateIndicator_CustomAlpha(event)
  842.         if activetheme.SetAlpha then
  843.             local previousAlpha = extended.requestedAlpha
  844.             extended.requestedAlpha = activetheme.SetAlpha(unit) or previousAlpha or unit.alpha or 1
  845.         else
  846.             extended.requestedAlpha = unit.alpha or 1
  847.         end
  848.  
  849.         if extended.requestedAlpha > 0 then
  850.             if nameplate:IsShown() then extended:Show() end
  851.         else
  852.             extended:Hide()        -- FRAME HIDE TEST
  853.         end
  854.  
  855.         -- Better Layering
  856.         if unit.isTarget then
  857.             extended:SetFrameLevel(3)
  858.         elseif unit.isMouseover then
  859.             extended:SetFrameLevel(2)
  860.         else
  861.             extended:SetFrameLevel(0)
  862.         end
  863.     end
  864.  
  865.  
  866.     -- UpdateIndicator_CustomScaleText: Updates indicators for custom text and scale
  867.     function UpdateIndicator_CustomScaleText()
  868.         threatborder = visual.threatborder
  869.  
  870.         if unit.health and (extended.requestedAlpha > 0) then
  871.             -- Scale
  872.             if activetheme.SetScale then
  873.                 scale = activetheme.SetScale(unit)
  874.                 if scale then extended:SetScale( scale )end
  875.             end
  876.  
  877.             -- Set Special-Case Regions
  878.             if style.customtext.show then
  879.                 if activetheme.SetCustomText then
  880.                     local text, r, g, b, a = activetheme.SetCustomText(unit)
  881.                     visual.customtext:SetText( text or "")
  882.                     visual.customtext:SetTextColor(r or 1, g or 1, b or 1, a or 1)
  883.                 else visual.customtext:SetText("") end
  884.             end
  885.  
  886.             UpdateIndicator_UnitColor()
  887.         end
  888.     end
  889.  
  890.  
  891.     -- OnUpdateCastbar
  892.     function OnUpdateCastbar(bar)
  893.         UpdateReferences(bar.parentPlate)
  894.         local castbar = visual.castbar
  895.         local r, g, b, a = 1, .8, 0, 1
  896.  
  897.         unit.isCasting = true
  898.         if regions.spelltext then unit.spellName = regions.spelltext:GetText() end
  899.         unit.spellIsShielded = regions.castnostop:IsShown()
  900.         unit.spellInterruptible = not unit.spellIsShielded
  901.  
  902.         if activetheme.SetCastbarColor then
  903.             r, g, b, a = activetheme.SetCastbarColor(unit)
  904.             if not (r and g and b and a) then return end
  905.         end
  906.  
  907.         castbar:SetValue( bar:GetValue())
  908.         castbar:SetMinMaxValues(bar:GetMinMaxValues())
  909.         castbar:SetStatusBarColor( r, g, b)
  910.         castbar:SetAlpha(a or 1)
  911.  
  912.         visual.spelltext:SetText(unit.spellName)
  913.  
  914.         visual.spellicon:SetTexture(regions.spellicon:GetTexture())
  915.  
  916.         if unit.spellIsShielded then
  917.                visual.castnostop:Show(); visual.castborder:Hide()
  918.         else visual.castnostop:Hide(); visual.castborder:Show() end
  919.     end
  920.  
  921.  
  922.     -- OnShowCastbar
  923.     function OnShowCastbar(bar)
  924.         UpdateReferences(bar.parentPlate)
  925.         local castbar = extended.visual.castbar
  926.  
  927.         if not unit.health then return end
  928.  
  929.         OnUpdateCastbar(bar)
  930.         castbar:Show()
  931.         regions.spellshadow:Hide()
  932.         UpdateIndicator_CustomScaleText()
  933.         UpdateIndicator_CustomAlpha()
  934.     end
  935.  
  936.  
  937.     -- OnHideCastbar
  938.     function OnHideCastbar(bar)
  939.         UpdateReferences(bar.parentPlate)
  940.  
  941.         if not unit.health then return end
  942.  
  943.         local castbar = extended.visual.castbar
  944.  
  945.         castbar:Hide()
  946.         unit.isCasting = false
  947.         UpdateIndicator_CustomScaleText()
  948.         UpdateIndicator_CustomAlpha()
  949.     end
  950.  
  951. end -- End Indicator section
  952.  
  953.  
  954. --------------------------------------------------------------------------------------------------------------
  955. -- WoW Event Handlers: sends event-driven changes to the appropriate gather/update handler.
  956. --------------------------------------------------------------------------------------------------------------
  957. do
  958.     local events = {}
  959.     local function EventHandler(self, event, ...)
  960.         events[event](event, ...)
  961.     end
  962.  
  963.     local TidyPlatesCore = CreateFrame("Frame", nil, WorldFrame)
  964.     TidyPlatesCore:SetFrameStrata("TOOLTIP")    -- When parented to WorldFrame, causes OnUpdate handler to run close to last
  965.     TidyPlatesCore:SetScript("OnEvent", EventHandler)
  966.  
  967.     -- Events
  968.     function events:PLAYER_ENTERING_WORLD() TidyPlatesCore:SetScript("OnUpdate", OnUpdate); end
  969.     function events:PLAYER_REGEN_ENABLED() InCombat = false; SetUpdateAll() end
  970.     function events:PLAYER_REGEN_DISABLED() InCombat = true; SetUpdateAll() end
  971.  
  972.     -- 6.0
  973.     function events:PLAYER_TARGET_CHANGED() HasTarget = UnitExists("target") == true;   SetUpdateAll()  end
  974.  
  975.     function events:RAID_TARGET_UPDATE() SetUpdateAll() end
  976.     function events:UNIT_THREAT_SITUATION_UPDATE() SetUpdateAll() end  -- Only fired when a target changes
  977.     function events:UNIT_LEVEL() SetUpdateAll() end
  978.     function events:PLAYER_CONTROL_LOST() SetUpdateAll() end
  979.     events.PLAYER_CONTROL_GAINED = events.PLAYER_CONTROL_LOST
  980.     events.UNIT_FACTION = events.PLAYER_CONTROL_LOST
  981.  
  982.     -- Registration of Blizzard Events
  983.     for eventname in pairs(events) do TidyPlatesCore:RegisterEvent(eventname) end
  984.     --TidyPlates.TidyPlatesCore = TidyPlatesCore
  985. end
  986.  
  987.  
  988. ---------------------------------------------------------------------------------------------------------------------
  989. --  Nameplate Styler: These functions parses the definition table for a nameplate's requested style.
  990. ---------------------------------------------------------------------------------------------------------------------
  991. do
  992.     -- Helper Functions
  993.     local function SetObjectShape(object, width, height) object:SetWidth(width); object:SetHeight(height) end
  994.     local function SetObjectFont(object,  font, size, flags) if not object:SetFont(font, size, flags) then object:SetFont("FONTS\\ARIALN.TTF", size or 12, flags) end end
  995.     local function SetObjectJustify(object, horz, vert) object:SetJustifyH(horz); object:SetJustifyV(vert) end
  996.     local function SetObjectAnchor(object, anchor, anchorTo, x, y) object:ClearAllPoints();object:SetPoint(anchor, anchorTo, anchor, x, y) end
  997.     local function SetObjectTexture(object, texture) object:SetTexture(texture) end
  998.     local function SetObjectBartexture(obj, tex, ori, crop) obj:SetStatusBarTexture(tex); obj:SetOrientation(ori); end
  999.  
  1000.     -- SetObjectShadow:
  1001.     local function SetObjectShadow(object, shadow)
  1002.         if shadow then
  1003.             object:SetShadowColor(0,0,0, tonumber(shadow) or 1)
  1004.             object:SetShadowOffset(.5, -.5)
  1005.         else object:SetShadowColor(0,0,0,0) end
  1006.     end
  1007.  
  1008.     -- SetFontGroupObject
  1009.     local function SetFontGroupObject(object, objectstyle)
  1010.         if objectstyle then
  1011.             SetObjectFont(object, objectstyle.typeface or "FONTS\\ARIALN.TTF",  objectstyle.size or 12, objectstyle.flags or "NONE")
  1012.             SetObjectJustify(object, objectstyle.align or "CENTER", objectstyle.vertical or "BOTTOM")
  1013.             SetObjectShadow(object, objectstyle.shadow or 1)
  1014.         end
  1015.     end
  1016.  
  1017.     -- SetAnchorGroupObject
  1018.     local function SetAnchorGroupObject(object, objectstyle, anchorTo)
  1019.         if objectstyle and anchorTo then
  1020.             SetObjectShape(object, objectstyle.width or 128, objectstyle.height or 16) --end
  1021.             SetObjectAnchor(object, objectstyle.anchor or "CENTER", anchorTo, objectstyle.x or 0, objectstyle.y or 0)
  1022.         end
  1023.     end
  1024.  
  1025.     -- SetTextureGroupObject
  1026.     local function SetTextureGroupObject(object, objectstyle)
  1027.         if objectstyle then
  1028.             if objectstyle.texture then SetObjectTexture(object, objectstyle.texture or EMPTY_TEXTURE) end
  1029.             object:SetTexCoord(objectstyle.left or 0, objectstyle.right or 1, objectstyle.top or 0, objectstyle.bottom or 1)
  1030.         end
  1031.     end
  1032.  
  1033.  
  1034.     -- SetBarGroupObject
  1035.     local function SetBarGroupObject(object, objectstyle, anchorTo)
  1036.         if objectstyle then
  1037.             SetAnchorGroupObject(object, objectstyle, anchorTo)
  1038.             SetObjectBartexture(object, objectstyle.texture or EMPTY_TEXTURE, objectstyle.orientation or "HORIZONTAL")
  1039.             if objectstyle.backdrop then
  1040.                 object:SetBackdropTexture(objectstyle.backdrop)
  1041.             end
  1042.             object:SetTexCoord(objectstyle.left, objectstyle.right, objectstyle.top, objectstyle.bottom)
  1043.         end
  1044.     end
  1045.  
  1046.     -- Style Groups
  1047.     local fontgroup = {"name", "level", "spelltext", "customtext"}
  1048.  
  1049.     local anchorgroup = {"healthborder", "threatborder", "castborder", "castnostop",
  1050.                         "name",  "spelltext", "customtext", "level",
  1051.                         "spellicon", "raidicon", "skullicon", "eliteicon", "target"}
  1052.  
  1053.     local bargroup = {"castbar", "healthbar"}
  1054.  
  1055.     local texturegroup = { "castborder", "castnostop", "healthborder", "threatborder", "eliteicon",
  1056.                         "skullicon", "highlight", "target", "spellicon", }
  1057.  
  1058.  
  1059.     -- UpdateStyle:
  1060.     function UpdateStyle()
  1061.         local index
  1062.         local objectstyle, objectname, objectregion, objectenable
  1063.  
  1064.         -- Frame
  1065.         SetAnchorGroupObject(extended, style.frame, carrier)
  1066.  
  1067.         -- Anchorgroup
  1068.         for index = 1, #anchorgroup do
  1069.             objectname = anchorgroup[index]; SetAnchorGroupObject(visual[objectname], style[objectname], extended)
  1070.             objectenable = style[objectname].show
  1071.             if objectenable then visual[objectname]:Show() else visual[objectname]:Hide() end
  1072.         end
  1073.         -- Bars
  1074.         for index = 1, #bargroup do objectname = bargroup[index]; SetBarGroupObject(visual[objectname], style[objectname], extended) end
  1075.         -- Texture
  1076.         for index = 1, #texturegroup do objectname = texturegroup[index]; SetTextureGroupObject(visual[objectname], style[objectname]) end
  1077.         -- Raid Icon Texture
  1078.         visual.raidicon:SetTexture(style.raidicon.texture)
  1079.         -- Font Group
  1080.         for index = 1, #fontgroup do objectname = fontgroup[index];SetFontGroupObject(visual[objectname], style[objectname]) end
  1081.         -- Hide Stuff
  1082.         if unit.isElite then visual.eliteicon:Hide() else visual.eliteicon:Hide() end
  1083.         if unit.isBoss then visual.level:Hide() else visual.skullicon:Hide() end
  1084.         if not unit.isTarget then visual.target:Hide() end
  1085.         if not unit.isMarked then visual.raidicon:Hide() end
  1086.  
  1087.     end
  1088.  
  1089.  
  1090. end
  1091.  
  1092. --------------------------------------------------------------------------------------------------------------
  1093. -- External Commands: Allows widgets and themes to request updates to the plates.
  1094. -- Useful to make a theme respond to externally-captured data (such as the combat log)
  1095. --------------------------------------------------------------------------------------------------------------
  1096. function TidyPlates:ForceUpdate() ForEachPlate(OnResetNameplate) end
  1097. function TidyPlates:Update() SetUpdateAll() end
  1098. function TidyPlates:RequestWidgetUpdate(plate) if plate then SetUpdateMe(plate) else SetUpdateAll() end end
  1099. function TidyPlates:RequestDelegateUpdate(plate) if plate then SetUpdateMe(plate) else SetUpdateAll() end end
  1100. function TidyPlates:ActivateTheme(theme) if theme and type(theme) == 'table' then TidyPlates.ActiveThemeTable, activetheme = theme, theme; ResetPlates = true; end end
  1101. function TidyPlates:EnableFadeIn() EnableFadeIn = true; end
  1102. function TidyPlates:DisableFadeIn() EnableFadeIn = nil; end
  1103. function TidyPlates:EnableCompatibilityMode() CompatibilityMode = true; end
  1104. TidyPlates.NameplatesByGUID, TidyPlates.NameplatesAll, TidyPlates.NameplatesByVisible = GUID, Plates, PlatesVisible
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement