View Single Post
07-03-12, 11:38 PM   #9
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Here - more fixups and streamlining. This separates the textures from the commands, removes duplicated code, and ensures that the param is of the same case (lower) as the table keys.

lua Code:
  1. -- Tribal Experience Bar
  2. -- By: Othgar
  3. -- Copyleft All Rights Reversed
  4.  
  5. -- Default state
  6. -- Only set a value if the variable is nil, else you're overwriting every time.
  7. TXPVisible = TXPVisible or true;
  8. TXPLocked = TXPLocked or true;
  9. TXPTexture = TXPTexture or "simple"
  10.  
  11. -- Constants
  12. local ThousandSeparator = ","
  13. local TipTitle = "Tribal XP"
  14. local _, class = UnitClass("player")
  15. local r,g,b = RAID_CLASS_COLORS[class].r, RAID_CLASS_COLORS[class].g, RAID_CLASS_COLORS[class].b
  16.  
  17.  
  18. local TEXTURES = {
  19.     curve = [[Interface\AddOns\TXP\media\curve]],
  20.     simple = [[Interface\AddOns\TXP\media\simple]],
  21.     split = [[Interface\AddOns\TXP\media\split]],
  22. }
  23.  
  24. local function FmtBigNum(n)
  25.         local left, num, right = string.match(n .. "", '^([^%d]*%d)(%d*)(.-)')
  26.         return left..(num:reverse():gsub('(%d%d%d)', '%1'.. ","):reverse())..right
  27. end
  28.  
  29. local TribalExperienceBar = CreateFrame("StatusBar","TribalExperienceBar",UIParent)
  30. TribalExperienceBar:SetSize(675, 150)
  31. TribalExperienceBar:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
  32. TribalExperienceBar:SetFrameStrata("MEDIUM");  
  33. TribalExperienceBar:SetClampedToScreen(1);
  34. TribalExperienceBar:SetBackdrop({bgFile="Interface\\Tooltips\\UI-Tooltip-Background", edgeFile="", tile=1, tileSize=10, edgeSize=10, insets={left=2, right=2, top=2, bottom=2}});
  35. TribalExperienceBar:SetBackdropColor(r,g,b,0)
  36. TribalExperienceBar:SetMovable(1);
  37. TribalExperienceBar:EnableMouse(1);
  38.  
  39. -- Use the variable, not a string representation of its name
  40. TribalExperienceBar:SetStatusBarTexture(TEXTURES[TXPTexture])
  41. TribalExperienceBar:SetStatusBarColor(r,g,b)
  42. TribalExperienceBar:SetAlpha(.75)
  43.  
  44.  
  45.  
  46. local TXPBarText = TribalExperienceBar:CreateFontString("TXPBarText","OVERLAY");
  47. TXPBarText:SetFontObject(GameFontNormal)
  48. TXPBarText:SetTextColor(r,g,b)
  49. TXPBarText:SetPoint("CENTER", TribalExperienceBar, "BOTTOM", 0, 0);
  50. TXPBarText:SetJustifyH("CENTER");
  51. TXPBarText:SetJustifyV("CENTER");
  52.  
  53. --Register Events
  54.  
  55. TribalExperienceBar:RegisterEvent("PLAYER_ENTERING_WORLD");
  56. TribalExperienceBar:RegisterEvent("PLAYER_XP_UPDATE");
  57. TribalExperienceBar:RegisterEvent("PLAYER_LOGIN");
  58. TribalExperienceBar:RegisterEvent("PLAYER_LEVEL_UP");  
  59.  
  60.  
  61. --Event handling
  62. TribalExperienceBar:SetScript("OnEvent", function(self, event, ...)
  63.     local curxp = UnitXP("player")
  64.     local levelmax = UnitXPMax("player")
  65.     local perc = string.format("%.1f%%", (100*(curxp/levelmax)))
  66.     local perbox = levelmax / 20
  67.  
  68.     local mystr = string.format("%.1fb - %.1fb tnl | %s",  curxp / perbox, (levelmax - curxp) / perbox, perc)
  69.    
  70.     TXPBarText:SetText(mystr)
  71.  
  72.    
  73.     TribalExperienceBar:SetMinMaxValues(0, levelmax)
  74.     TribalExperienceBar:SetValue(curxp)
  75.  
  76.     if TXPVisible then
  77.         TribalExperienceBar:Show()
  78.     else
  79.         TribalExperienceBar:Hide()
  80.     end
  81. end)
  82.  
  83. -- move me
  84.  
  85. TribalExperienceBar:SetScript("OnMouseDown", function()
  86.     if not TXPLocked then
  87.         TribalExperienceBar:StartMoving()
  88.     else
  89.         TribalExperienceBar:StopMovingOrSizing()
  90.     end
  91. end)
  92.  
  93. TribalExperienceBar:SetScript("OnMouseUp", function() TribalExperienceBar:StopMovingOrSizing() end)
  94.  
  95.  
  96. -- register slash commands
  97.  
  98. SLASH_TXP1 = '/TXP';
  99. local COMMAND_PARAMS = {
  100.     hide = function()
  101.         if TXPVisible then
  102.             TXPVisible=false
  103.             TribalExperienceBar:Hide()
  104.             print("Experience hidden, type /TXP show to show.")
  105.         end
  106.     end,
  107.     show = function()
  108.             if not TXPVisible then
  109.             TXPVisible=true
  110.             TribalExperienceBar:Show()
  111.             print("Experience displayed, type /TXP hide to hide.")
  112.         end
  113.     end,    
  114.     rested = function()
  115.         if not GetXPExhaustion() then
  116.            print("You have no rested EXP.")
  117.             else
  118.            print("You have "..GetXPExhaustion().." rested EXP remaining.")
  119.         end
  120.     end,
  121.     lock = function()
  122.         if TXPLocked then
  123.            TXPLocked = false
  124.            print("TXP UNlocked")
  125.         else
  126.            TXPLocked = true
  127.            print("TXP Locked")
  128.         end
  129.     end,
  130. }      
  131.  
  132. local function slashcmdhandler(param)
  133.     param = param:lower()
  134.  
  135.     if TEXTURES[param] then
  136.         if TXPTexture == param then
  137.             print(("Texture already set to %s"):format(param))
  138.             return
  139.         end
  140.         TXPTexture = param
  141.         TribalExperienceBar:SetStatusBarTexture(TEXTURES[param])
  142.         print(("Texture set to %s"):format(param))
  143.         return
  144.     end
  145.  
  146.     if COMMAND_PARAMS[param] then
  147.        COMMAND_PARAMS[param]()
  148.        return
  149.     end
  150.     print("Valid commands are:");
  151.     print("/TXP show (Show the experience bar.)");
  152.     print("/TXP hide (Hide the experience bar.)");
  153.     print("/TXP rested (Shows the amount of rested XP.)");
  154.     print("/TXP Lock (Locks the experience bar if it is unlocked, Unlocks it if it is locked.")
  155.     print("/TXP Curve (Sets the status bar texture to Curve.")
  156.     print("/TXP Simple (Sets the bar texture to Simple")
  157.     print("/TXP Split (Sets the Bar texture to Split")
  158. end
  159. SlashCmdList["TXP"] = slashcmdhandler;
  160.  
  161. --Tooltip
  162.  
  163. local function OnEnter(self)  
  164.     GameTooltip:Show()
  165. end
  166.  
  167. local function OnLeave(self)  
  168.     GameTooltip:Hide()
  169. end
  170.  
  171. --hide @ 85
  172. if UnitLevel("player") >= 85 then
  173.     TribalExperienceBar:Hide() print("Experience bar hidden, type /TXP show to show.")
  174.     TXPVisible = false
  175. end
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.

Last edited by Torhal : 07-03-12 at 11:41 PM.