Quantcast
WoWInterface - View Single Post - LUA CreateFrame, then RemoveFrame??
View Single Post
08-22-14, 09:10 AM   #6
Choonstertwo
A Chromatic Dragonspawn
 
Choonstertwo's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 194
I've cleaned up the code a little and added a few comments (all prefixed with my name) explaining the changes and questioning why certain things are the way they are.

Lua Code:
  1. --Tribute to Kip who's code I copied and altered... Consider this Kudos for your 'Bootstrap'
  2.  
  3. --define global vars (saved between sessions)
  4. RyinnsCursorFinder_Enabled = true -- Choonster: I gave this global variable your Addon's name as a prefix so it's unique
  5.  
  6. --define local module vars and defaults
  7. local AddonVersion = GetAddOnMetadata("RyinnsCursorFinder", "Version")
  8.  
  9. local function print(msg)
  10.     DEFAULT_CHAT_FRAME:AddMessage("RyinnsCursorFinder: " .. tostring(msg))
  11. end
  12.  
  13. -- Choonster: I've moved the frame creation into the main body of the script. This is only executed once.
  14. -- I've also given it a name. I'd recommend giving any visible frame a name with your AddOn's name in it so the user knows which AddOn adds it.
  15. local frame = CreateFrame("Frame", "RyinnsCursorFinder_MainFrame", UIParent);
  16. frame:SetFrameStrata("TOOLTIP");
  17. frame:Hide() -- Choonster: Hide the frame initially
  18.  
  19. local texture = frame:CreateTexture();
  20. texture:SetTexture([[Interface\Cooldown\ping4]]);
  21. texture:SetBlendMode("ADD");
  22. texture:SetAlpha(0.5);
  23.  
  24. -- Choonster: Do these variables need to be declared outside of the OnUpdate function? Are they ever used by another function? Do they need to be saved between each call of the function?
  25. local x = 0;
  26. local y = 0;
  27. local speed = 0;
  28.  
  29. -- Choonster: I've passed the function directly to :SetScript here instead of declaring it first and then passing it in.
  30. -- If you're never going to call a function yourself (like most script handler functions), you don't have to give it a name.
  31. frame:SetScript("OnUpdate", function(_, elapsed)
  32.   local dX = x;
  33.   local dY = y;
  34.   x, y = GetCursorPosition();
  35.   dX = x - dX;
  36.   dY = y - dY;
  37.   local weight = 2048 ^ -elapsed;
  38.   speed = math.min(weight * speed + (1 - weight) * math.sqrt(dX * dX + dY * dY) / elapsed, 768);
  39.   local size = speed / 6 - 16;
  40.   if (size > 0) then
  41.     local scale = UIParent:GetEffectiveScale();
  42.     texture:SetHeight(size);
  43.     texture:SetWidth(size);
  44.     texture:SetPoint("CENTER", UIParent, "BOTTOMLEFT", (x + 0.5 * dX) / scale, (y + 0.5 * dY) / scale);
  45.     texture:Show();
  46.   else
  47.     texture:Hide();
  48.   end
  49. end);
  50.  
  51. -- Choonster: I've made this function local, it doesn't need to be global.
  52. local function Ryinn_Command(Msg,Editbox)
  53.     if Msg then Msg=strlower(Msg); end
  54.     if Msg == "help" then
  55.         print("v"..AddonVersion)
  56.         print("Use /rcf + on/off to Enable/Disable")
  57.         print("Example: /rcf on")
  58.     elseif Msg == "on" then
  59.         frame:Show() -- Choonster: Showing the frame makes it visible and allows its OnUpdate handler to fire.
  60.         print("Ryinn's Flash Cursor is Now Enabled. Use /ryn off to disable")
  61.     elseif Msg == "off" then
  62.         frame:Hide() -- Choonster: Hiding the frame makes it invisible and stops its OnUpdate handler from firing.
  63.         --StaticPopup_Show("ReloadPop")
  64.         print("test")
  65.     else
  66.         Ryinn_Command("help")
  67.     end
  68. end
  69.  
  70. --Create an anonymous blank frame
  71. local Ryinn = CreateFrame("Frame")
  72. Ryinn:RegisterEvent("PLAYER_REGEN_ENABLED")
  73. Ryinn:RegisterEvent("PLAYER_REGEN_DISABLED")
  74. Ryinn:SetScript("OnEvent", Ryinn_OnEvent)
  75.  
  76. --setup slash command feature
  77. SlashCmdList["Ryinn"]=Ryinn_Command
  78. SLASH_Ryinn1="/rcf",
  79. Ryinn_Command("help")
  80.  
  81. --Setup our Pop Up  Warning for Reload UI
  82. --StaticPopupDialogs["ReloadPop"] = {
  83. --text = "In order to remove the cursor texture, you need to reload your UI.  Is that okay?",
  84. --button1 = "Yes",
  85. --button2 = "No",
  86. --OnAccept = function() ReloadUI() end,
  87. --timeout = 0,
  88. --sound = "RaidWarning",
  89. --whileDead = true,
  90. --hideOnEscape = true,
  91. --preferredIndex = 3,  -- avoid some UI taint, see [url]http://www.wowace.com/announcements/how-to-avoid-some-ui-taint/[/url]
  92. --}

It's not perfect and I can't guarantee it will work, but it probably will.

If you're going to be saving whether or not the AddOn is enabled, you should use the ADDON_LOADED event to set the default enabled state and then show/hide your frame based on the enabled state.

Last edited by Choonstertwo : 08-22-14 at 09:28 AM.
  Reply With Quote