Advertisement
Galvin

Untitled

Apr 3rd, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.72 KB | None | 0 0
  1. --
  2. -- ShardBar.lua
  3. --
  4. -- Displays the Warlock shard bar.
  5.  
  6. -------------------------------------------------------------------------------
  7. -- GUB   shared data table between all parts of the addon
  8. -------------------------------------------------------------------------------
  9. local MyAddon, GUB = ...
  10.  
  11. GUB.ShardBar = {}
  12. local Main = GUB.Main
  13. local Bar = GUB.Bar
  14.  
  15. -- shared from Main.lua
  16. local LSM = Main.LSM
  17. local PowerTypeToNumber = Main.PowerTypeToNumber
  18. local MouseOverDesc = Main.MouseOverDesc
  19.  
  20. -- localize some globals.
  21. local _
  22. local abs, mod, max, floor, ceil, mrad,     mcos,     msin =
  23.       abs, mod, max, floor, ceil, math.rad, math.cos, math.sin
  24. local strfind, strsub, strupper, strlower, format, strconcat, strmatch, gsub =
  25.       strfind, strsub, strupper, strlower, format, strconcat, strmatch, gsub
  26. local pcall, pairs, ipairs, type, table, select, next, print =
  27.       pcall, pairs, ipairs, type, table, select, next, print
  28. local GetTime, MouseIsOver, IsModifierKeyDown, GameTooltip =
  29.       GetTime, MouseIsOver, IsModifierKeyDown, GameTooltip
  30. local UnitHasVehicleUI, UnitIsDeadOrGhost, UnitAffectingCombat, UnitExists, HasPetUI =
  31.       UnitHasVehicleUI, UnitIsDeadOrGhost, UnitAffectingCombat, UnitExists, HasPetUI
  32. local UnitPowerType, UnitClass, UnitHealth, UnitHealthMax, UnitPower, UnitBuff, UnitPowerMax, UnitGetIncomingHeals =
  33.       UnitPowerType, UnitClass, UnitHealth, UnitHealthMax, UnitPower, UnitBuff, UnitPowerMax, UnitGetIncomingHeals
  34. local GetRuneCooldown, CooldownFrame_SetTimer, GetRuneType =
  35.       GetRuneCooldown, CooldownFrame_SetTimer, GetRuneType
  36. local GetComboPoints, GetShapeshiftFormID, GetPrimaryTalentTree, GetEclipseDirection, GetInventoryItemID =
  37.       GetComboPoints, GetShapeshiftFormID, GetPrimaryTalentTree, GetEclipseDirection, GetInventoryItemID
  38. local CreateFrame, UnitGUID, getmetatable, setmetatable =
  39.       CreateFrame, UnitGUID, getmetatable, setmetatable
  40.  
  41. -------------------------------------------------------------------------------
  42. -- Locals
  43.  
  44. -- UnitBarF = UnitBarsF[]
  45. --
  46. -- UnitBarF.UnitBar                  Reference to the unitbar data for the shard bar.
  47. -- UnitBarF.ShardBar                 Contains the shard bar displayed on screen.
  48. --
  49. -- SoulShardTexture                  Contains all the data for the soul shards texture.
  50. --   Texture                         Path name to the texture file.
  51. --   Width                           Width of the texture.
  52. --   Height                          Height of the texture.
  53. --   Left, Right, Top, Bottom        Coordinates inside the main texture for the texture we need.
  54. -- SoulShardDarkColor                Used to make the light colored soulshard texture dark.
  55. --
  56. -- LastSoulShards                    Keeps track of change in the soulshard bar.
  57. --
  58. -- NOTE: SoulShard bar has two modes.  In BoxMode the soulshard bar is broken into 3 statusbars.
  59. --       This works just like the combobar.  When not normal mode.  The bar uses textures instead.
  60. -------------------------------------------------------------------------------
  61. local MaxSoulShards = 3
  62.  
  63. -- Powertype constants
  64. local PowerShard = PowerTypeToNumber['SOUL_SHARDS']
  65.  
  66. -- Soulshard Texture constants
  67. local SoulShardBox = 1
  68. local SoulShardDark = 2
  69. local SoulShardLight = 3
  70.  
  71. local LastSoulShards = nil
  72.  
  73. local SoulShardTexture = {
  74.         Texture = [[Interface\PlayerFrame\UI-WarlockShard]],
  75.         Width = 17 + 15, Height = 16 + 15,
  76.         Left = 0.01562500, Right = 0.28125000, Top = 0.00781250, Bottom = 0.13281250
  77.       }
  78. local SoulShardDarkColor = {r = 0.25, g = 0.25, b = 0.25, a = 1}
  79.  
  80. --*****************************************************************************
  81. --
  82. -- Shardbar display
  83. --
  84. --*****************************************************************************
  85.  
  86. -------------------------------------------------------------------------------
  87. -- UpdateSoulShards
  88. --
  89. -- Lights or darkens the soul shards
  90. --
  91. -- Usage: UpdateSoulShards(ShardBarF, SoulShards, FinishFadeOut)
  92. --
  93. -- ShardBarF        SoulShard bar containing shards to update.
  94. -- SoulShards       Updates the soul shards based on the number to light up.
  95. -- FinishFadeOut    If true then any fadeout animation currently playing
  96. --                  will be stopped.
  97. --                  If nil or false then does nothing.
  98. -------------------------------------------------------------------------------
  99. local function UpdateSoulShards(ShardBarF, SoulShards, FinishFadeOut)
  100.   local ShardBar = ShardBarF.ShardBar
  101.   local Action = nil
  102.   if FinishFadeOut then
  103.     Action = 'finishfadeout'
  104.   end
  105.  
  106.   for ShardIndex = 1, MaxSoulShards do
  107.     if ShardIndex <= SoulShards then
  108.       ShardBar:ShowTexture(ShardIndex, SoulShardBox)
  109.       ShardBar:ShowTexture(ShardIndex, SoulShardLight)
  110.     else
  111.       ShardBar:HideTexture(ShardIndex, SoulShardBox, Action)
  112.       ShardBar:HideTexture(ShardIndex, SoulShardLight, Action)
  113.     end
  114.   end
  115. end
  116.  
  117. -------------------------------------------------------------------------------
  118. -- UpdateShardBar (Update) [UnitBar assigned function]
  119. --
  120. -- Update the number of shards of the player
  121. --
  122. -- usage: UpdateShardBar(Event)
  123. --
  124. -- Event         'change' then the bar will only get updated if there is a change.
  125. -------------------------------------------------------------------------------
  126. function GUB.ShardBar:UpdateShardBar(Event)
  127.   local SoulShards = UnitPower('player', PowerShard)
  128.  
  129.   -- Return if no change.
  130.   if Event == 'change' and SoulShards == LastSoulShards then
  131.     return
  132.   end
  133.  
  134.   LastSoulShards = SoulShards
  135.  
  136.   UpdateSoulShards(self, SoulShards)
  137.  
  138.     -- Set this IsActive flag
  139.   self.IsActive = SoulShards > 0
  140.  
  141.   -- Do a status check for active status.
  142.   self:StatusCheck()
  143. end
  144.  
  145. -------------------------------------------------------------------------------
  146. -- CancelAnimationShard (CancelAnimation) [UnitBar assigned function]
  147. --
  148. -- Usage: CancelAnimationShard()
  149. --
  150. -- Cancels all animation playing in the shard bar.
  151. -------------------------------------------------------------------------------
  152. function GUB.ShardBar:CancelAnimationShard()
  153.   UpdateSoulShards(self, 0, true)
  154. end
  155.  
  156. --*****************************************************************************
  157. --
  158. -- Shardbar creation/setting
  159. --
  160. --*****************************************************************************
  161.  
  162. -------------------------------------------------------------------------------
  163. -- EnableMouseClicksShard (EnableMouseClicks) [UnitBar assigned function]
  164. --
  165. -- This will enable or disbable mouse clicks for the shard bar.
  166. -------------------------------------------------------------------------------
  167. function GUB.ShardBar:EnableMouseClicksShard(Enable)
  168.   local ShardBar = self.ShardBar
  169.  
  170.   -- Enable/Disable normal mode.
  171.   ShardBar:SetEnableMouseClicks(nil, Enable)
  172.  
  173.   -- Enable/disable box mode.
  174.   ShardBar:SetEnableMouseClicks(0, Enable)
  175. end
  176.  
  177. -------------------------------------------------------------------------------
  178. -- FrameSetScriptShard (FrameSetScript) [UnitBar assigned function]
  179. --
  180. -- Set up script handlers for the Shardbar.
  181. -------------------------------------------------------------------------------
  182. function GUB.ShardBar:FrameSetScriptShard()
  183.   local ShardBar = self.ShardBar
  184.  
  185.   -- Enable normal mode. for the bar.
  186.   ShardBar:SetEnableMouse(nil)
  187.  
  188.   -- Enable box mode.
  189.   ShardBar:SetEnableMouse(0)
  190. end
  191.  
  192. -------------------------------------------------------------------------------
  193. -- EnableScreenClampShard (EnableScreenClamp) [UnitBar assigned function]
  194. --
  195. -- Enables or disble screen clamp for the shard bar.
  196. -------------------------------------------------------------------------------
  197. function GUB.ShardBar:EnableScreenClampShard(Enable)
  198.   self.ShardBar:SetClamp(Enable)
  199. end
  200.  
  201. -------------------------------------------------------------------------------
  202. -- SetAttrShard  (SetAttr) [UnitBar assigned function]
  203. --
  204. -- Sets different parts of the shardbar.
  205. --
  206. -- Usage: SetAttrShard(Object, Attr)
  207. --
  208. -- Object       Object being changed:
  209. --               'bg' for background (Border).
  210. --               'bar' for forground (StatusBar).
  211. --               'frame' for the frame.
  212. -- Attr         Type of attribute being applied to object:
  213. --               'color'     Color being set to the object.
  214. --               'backdrop'  Backdrop settings being set to the object.
  215. --               'scale'     Scale settings being set to the object.
  216. --               'size'    Size being set to the object.
  217. --               'padding' Amount of padding set to the object.
  218. --               'texture' One or more textures set to the object.
  219. --               'strata'    Frame strata for the object.
  220. --
  221. -- NOTE: To apply one attribute to all objects. Object must be nil.
  222. --       To apply all attributes to one object. Attr must be nil.
  223. --       To apply all attributes to all objects both must be nil.
  224. -------------------------------------------------------------------------------
  225. function GUB.ShardBar:SetAttrShard(Object, Attr)
  226.   local ShardBar = self.ShardBar
  227.  
  228.   -- Check scale and strata for 'frame'
  229.   Main:UnitBarSetAttr(self, Object, Attr)
  230.  
  231.   -- Get the unitbar data.
  232.   local UB = self.UnitBar
  233.   local Border = self.Border
  234.  
  235.   -- Check if we're in boxmode.
  236.   if UB.General.BoxMode then
  237.     local Bar = UB.Bar
  238.     local Background = UB.Background
  239.     local Padding = Bar.Padding
  240.     local BackdropSettings = Background.BackdropSettings
  241.  
  242.     for ShardIndex = 1, MaxSoulShards do
  243.  
  244.       -- Background (Border).
  245.       if Object == nil or Object == 'bg' then
  246.         local BgColor = nil
  247.  
  248.         -- Get all color if ColorAll is true.
  249.         if Background.ColorAll then
  250.           BgColor = Background.Color
  251.         else
  252.           BgColor = Background.Color[ShardIndex]
  253.         end
  254.  
  255.         if Attr == nil or Attr == 'backdrop' or Attr == 'color' then
  256.           ShardBar:SetBackdrop(ShardIndex, BackdropSettings, BgColor.r, BgColor.g, BgColor.b, BgColor.a)
  257.         end
  258.       end
  259.  
  260.       -- Forground (Statusbar).
  261.       if Object == nil or Object == 'bar' then
  262.         if Attr == nil or Attr == 'texture' then
  263.           ShardBar:SetTexture(ShardIndex, SoulShardBox, Bar.StatusBarTexture)
  264.           ShardBar:SetFillDirection(ShardIndex, SoulShardBox, Bar.FillDirection)
  265.           ShardBar:SetRotateTexture(ShardIndex, SoulShardBox, Bar.RotateTexture)
  266.         end
  267.         if Attr == nil or Attr == 'color' then
  268.           local BarColor = nil
  269.  
  270.           -- Get all color if ColorAll is true.
  271.           if Bar.ColorAll then
  272.             BarColor = Bar.Color
  273.           else
  274.             BarColor = Bar.Color[ShardIndex]
  275.           end
  276.           ShardBar:SetColor(ShardIndex, SoulShardBox, BarColor.r, BarColor.g, BarColor.b, BarColor.a)
  277.         end
  278.       end
  279.     end
  280.  
  281.     -- Forground (Statusbar).
  282.     if Object == nil or Object == 'bar' then
  283.       if Attr == nil or Attr == 'padding' then
  284.         ShardBar:SetTexturePadding(0, SoulShardBox, Padding.Left, Padding.Right, Padding.Top, Padding.Bottom)
  285.       end
  286.     end
  287.   else
  288.  
  289.     -- Else in normal bar mode.
  290.  
  291.     -- Background (Border).
  292.     if Object == nil or Object == 'bg' then
  293.       local Border = self.Border
  294.  
  295.       local BgColor = UB.Background.Color
  296.  
  297.       if Attr == nil or Attr == 'backdrop' or Attr == 'color' then
  298.         ShardBar:SetBackdrop(nil, UB.Background.BackdropSettings, BgColor.r, BgColor.g, BgColor.b, BgColor.a)
  299.       end
  300.     end
  301.   end
  302. end
  303.  
  304. -------------------------------------------------------------------------------
  305. -- SetLayoutShard (SetLayout) [UnitBar assigned function]
  306. --
  307. -- Set a shardbar to a new layout
  308. --
  309. -- Usage: SetLayoutShard()
  310. -------------------------------------------------------------------------------
  311. function GUB.ShardBar:SetLayoutShard()
  312.   local ShardBar = self.ShardBar
  313.  
  314.   -- Get the unitbar data.
  315.   local UB = self.UnitBar
  316.   local Gen = self.UnitBar.General
  317.  
  318.   -- Convert old shard size to a default of 1.
  319.   if Gen.ShardSize > 9 then
  320.     Gen.ShardSize = 1
  321.   end
  322.  
  323.   -- Set all attributes.
  324.   self:SetAttr(nil, nil)
  325.  
  326.   -- Set padding and rotation and fadeout
  327.   ShardBar:SetPadding(0, Gen.ShardPadding)
  328.   ShardBar:SetAngle(Gen.ShardAngle)
  329.   ShardBar:SetFadeOutTime(0, SoulShardBox, Gen.ShardFadeOutTime)
  330.   ShardBar:SetFadeOutTime(0, SoulShardLight, Gen.ShardFadeOutTime)
  331.  
  332.   -- Check for box mode.
  333.   if Gen.BoxMode then
  334.  
  335.     -- Set size
  336.     ShardBar:SetBoxSize(UB.Bar.BoxWidth, UB.Bar.BoxHeight)
  337.     ShardBar:SetBoxScale(1)
  338.     ShardBar:SetTextureScale(0, SoulShardDark, 1)
  339.     ShardBar:SetTextureScale(0, SoulShardLight, 1)
  340.  
  341.     -- Hide/show Box mode.
  342.     ShardBar:HideTextureFrame(0, SoulShardDark)
  343.     ShardBar:HideTextureFrame(0, SoulShardLight)
  344.     ShardBar:ShowTextureFrame(0, SoulShardBox)
  345.  
  346.     ShardBar:HideBorder(nil)
  347.     ShardBar:ShowBorder(0)
  348.   else
  349.  
  350.     -- Texture mode
  351.     local ShardScale = Gen.ShardScale
  352.  
  353.     -- Set Size
  354.     ShardBar:SetBoxSize(SoulShardTexture.Width, SoulShardTexture.Height)
  355.     ShardBar:SetBoxScale(Gen.ShardSize)
  356.     ShardBar:SetTextureScale(0, SoulShardDark, ShardScale)
  357.     ShardBar:SetTextureScale(0, SoulShardLight, ShardScale)
  358.  
  359.     -- Hide/show Texture mode.
  360.     ShardBar:ShowTextureFrame(0, SoulShardDark)
  361.     ShardBar:ShowTextureFrame(0, SoulShardLight)
  362.     ShardBar:HideTextureFrame(0, SoulShardBox)
  363.  
  364.    --ShardBar:HideBorder(0)
  365.     ShardBar:ShowBorder(nil)
  366.   end
  367.  
  368.   -- Display the shardbar.
  369.   self.Width, self.Height = ShardBar:Display()
  370. end
  371.  
  372. -------------------------------------------------------------------------------
  373. -- CreateBar
  374. --
  375. -- Usage: GUB.ShardBar:CreateBar(UnitBarF, UB, Anchor, ScaleFrame)
  376. --
  377. -- UnitBarF     The unitbar frame which will contain the shard bar.
  378. -- UB           Unitbar data.
  379. -- Anchor       The unitbars anchor.
  380. -- ScaleFrame   ScaleFrame which the unitbar must be a child of for scaling.
  381. -------------------------------------------------------------------------------
  382. function GUB.ShardBar:CreateBar(UnitBarF, UB, Anchor, ScaleFrame)
  383.   local ColorAllNames = {}
  384.  
  385.   -- Create the shardbar.
  386.   local ShardBar = Bar:CreateBar(ScaleFrame, Anchor, MaxSoulShards)
  387.  
  388.   for ShardIndex = 1, MaxSoulShards do
  389.  
  390.       -- Create the textures for box and runes.
  391.     ShardBar:CreateBoxTexture(ShardIndex, SoulShardBox, 'statusbar')
  392.     ShardBar:CreateBoxTexture(ShardIndex, SoulShardDark, 'texture', 'ARTWORK')
  393.     ShardBar:CreateBoxTexture(ShardIndex, SoulShardLight, 'texture', 'OVERLAY')
  394.  
  395.     -- Set the textures
  396.     ShardBar:SetTexture(ShardIndex, SoulShardDark, SoulShardTexture.Texture)
  397.     ShardBar:SetTexture(ShardIndex, SoulShardLight, SoulShardTexture.Texture)
  398.  
  399.     -- Set the soulshard dark texture
  400.     ShardBar:SetTexCoord(ShardIndex, SoulShardDark, SoulShardTexture.Left, SoulShardTexture.Right, SoulShardTexture.Top, SoulShardTexture.Bottom)
  401.  
  402.     ShardBar:SetTextureSize(ShardIndex, SoulShardDark, SoulShardTexture.Width, SoulShardTexture.Height)
  403.     ShardBar:SetDesaturated(ShardIndex, SoulShardDark, true)
  404.     ShardBar:SetColor(ShardIndex, SoulShardDark, SoulShardDarkColor.r, SoulShardDarkColor.g, SoulShardDarkColor.b, SoulShardDarkColor.a)
  405.  
  406.     -- Set the soulshard light texture
  407.     ShardBar:SetTexCoord(ShardIndex, SoulShardLight, SoulShardTexture.Left, SoulShardTexture.Right, SoulShardTexture.Top, SoulShardTexture.Bottom)
  408.     ShardBar:SetTextureSize(ShardIndex, SoulShardLight, SoulShardTexture.Width, SoulShardTexture.Height)
  409.  
  410.      -- Set and save the name for tooltips for box mode.
  411.     local Name = strconcat('Shard ', ShardIndex)
  412.  
  413.     ShardBar:SetTooltip(ShardIndex, Name, MouseOverDesc)
  414.  
  415.     ColorAllNames[ShardIndex] = Name
  416.   end
  417.  
  418.   -- Show the dark textures.
  419.   ShardBar:ShowTexture(0 , SoulShardDark)
  420.  
  421.   -- Save the name for tooltips for normal mode.
  422.   ShardBar:SetTooltip(nil, UB.Name, MouseOverDesc)
  423.  
  424.   -- Save the color all names.
  425.   UnitBarF.ColorAllNames = ColorAllNames
  426.  
  427.   -- Save the shardbar
  428.   UnitBarF.ShardBar = ShardBar
  429. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement