View Single Post
08-21-14, 08:55 PM   #1
RyinntheDK
A Murloc Raider
 
RyinntheDK's Avatar
Join Date: Aug 2014
Posts: 6
LUA CreateFrame, then RemoveFrame??

Hello all!

I'm about 3 hours in to my first attempt at creating (err, finding one and building on to it) an addon. I found an addon that I thought was a good start, but lacked a lot of what I desired, so why not learn LUA and write my own eh?

What I've had a tough time doing is creating a toggle effect when using slash commands + Msg.

As you can see in the code, if Msg == "on" then I CreateFrame. If Msg == "off", essentially I would like to just undo the CreateFrame that I just created. I can't figure out how to do this without just applying a ReloadUI (as you see commented out), which, while effective, rather annoying.

Any help would be much appreciated!

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. Enabled = true
  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. function Ryinn_Command(Msg,Editbox)
  14.     if Msg then Msg=strlower(Msg); end
  15.     if Msg == "help" then
  16.         print("v"..AddonVersion)
  17.         print("Use /rcf + on/off to Enable/Disable")
  18.         print("Example: /rcf on")
  19.     elseif Msg == "on" then
  20.                 local frame = CreateFrame("Frame", nil, UIParent);
  21.                 frame:SetFrameStrata("TOOLTIP");
  22.  
  23.                 local texture = frame:CreateTexture();
  24.                 texture:SetTexture([[Interface\Cooldown\ping4]]);
  25.                 texture:SetBlendMode("ADD");
  26.                 texture:SetAlpha(0.5);
  27.  
  28.                 local x = 0;
  29.                 local y = 0;
  30.                 local speed = 0;
  31.                 local function OnUpdate(_, 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.                 frame:SetScript("OnUpdate", OnUpdate);
  51.                 print("Ryinn's Flash Cursor is Now Enabled. Use /ryn off to disable")
  52.     elseif Msg == "off" then
  53.     Frame1:SetScript("OnShow",nil);
  54.     --StaticPopup_Show("ReloadPop")
  55.     print("test")
  56.     else
  57.         Ryinn_Command("help")
  58.     end
  59. end
  60.  
  61. --Create an anonymous blank frame
  62. local Ryinn = CreateFrame("Frame")
  63. Ryinn:RegisterEvent("PLAYER_REGEN_ENABLED")
  64. Ryinn:RegisterEvent("PLAYER_REGEN_DISABLED")
  65. Ryinn:SetScript("OnEvent", Ryinn_OnEvent)
  66.  
  67. --setup slash command feature
  68. SlashCmdList["Ryinn"]=Ryinn_Command
  69. SLASH_Ryinn1="/rcf",
  70. Ryinn_Command("help")
  71.  
  72. --Setup our Pop Up  Warning for Reload UI
  73. --StaticPopupDialogs["ReloadPop"] = {
  74. --text = "In order to remove the cursor texture, you need to reload your UI.  Is that okay?",
  75. --button1 = "Yes",
  76. --button2 = "No",
  77. --OnAccept = function() ReloadUI() end,
  78. --timeout = 0,
  79. --sound = "RaidWarning",
  80. --whileDead = true,
  81. --hideOnEscape = true,
  82. --preferredIndex = 3,  -- avoid some UI taint, see [url]http://www.wowace.com/announcements/how-to-avoid-some-ui-taint/[/url]
  83. --}
  Reply With Quote