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:
--Tribute to Kip who's code I copied and altered... Consider this Kudos for your 'Bootstrap'
--define global vars (saved between sessions)
RyinnsCursorFinder_Enabled = true -- Choonster: I gave this global variable your Addon's name as a prefix so it's unique
--define local module vars and defaults
local AddonVersion = GetAddOnMetadata("RyinnsCursorFinder", "Version")
local function print(msg)
DEFAULT_CHAT_FRAME:AddMessage("RyinnsCursorFinder: " .. tostring(msg))
end
-- Choonster: I've moved the frame creation into the main body of the script. This is only executed once.
-- 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.
local frame = CreateFrame("Frame", "RyinnsCursorFinder_MainFrame", UIParent);
frame:SetFrameStrata("TOOLTIP");
frame:Hide() -- Choonster: Hide the frame initially
local texture = frame:CreateTexture();
texture:SetTexture([[Interface\Cooldown\ping4]]);
texture:SetBlendMode("ADD");
texture:SetAlpha(0.5);
-- 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?
local x = 0;
local y = 0;
local speed = 0;
-- Choonster: I've passed the function directly to :SetScript here instead of declaring it first and then passing it in.
-- If you're never going to call a function yourself (like most script handler functions), you don't have to give it a name.
frame:SetScript("OnUpdate", function(_, elapsed)
local dX = x;
local dY = y;
x, y = GetCursorPosition();
dX = x - dX;
dY = y - dY;
local weight = 2048 ^ -elapsed;
speed = math.min(weight * speed + (1 - weight) * math.sqrt(dX * dX + dY * dY) / elapsed, 768);
local size = speed / 6 - 16;
if (size > 0) then
local scale = UIParent:GetEffectiveScale();
texture:SetHeight(size);
texture:SetWidth(size);
texture:SetPoint("CENTER", UIParent, "BOTTOMLEFT", (x + 0.5 * dX) / scale, (y + 0.5 * dY) / scale);
texture:Show();
else
texture:Hide();
end
end);
-- Choonster: I've made this function local, it doesn't need to be global.
local function Ryinn_Command(Msg,Editbox)
if Msg then Msg=strlower(Msg); end
if Msg == "help" then
print("v"..AddonVersion)
print("Use /rcf + on/off to Enable/Disable")
print("Example: /rcf on")
elseif Msg == "on" then
frame:Show() -- Choonster: Showing the frame makes it visible and allows its OnUpdate handler to fire.
print("Ryinn's Flash Cursor is Now Enabled. Use /ryn off to disable")
elseif Msg == "off" then
frame:Hide() -- Choonster: Hiding the frame makes it invisible and stops its OnUpdate handler from firing.
--StaticPopup_Show("ReloadPop")
print("test")
else
Ryinn_Command("help")
end
end
--Create an anonymous blank frame
local Ryinn = CreateFrame("Frame")
Ryinn:RegisterEvent("PLAYER_REGEN_ENABLED")
Ryinn:RegisterEvent("PLAYER_REGEN_DISABLED")
Ryinn:SetScript("OnEvent", Ryinn_OnEvent)
--setup slash command feature
SlashCmdList["Ryinn"]=Ryinn_Command
SLASH_Ryinn1="/rcf",
Ryinn_Command("help")
--Setup our Pop Up Warning for Reload UI
--StaticPopupDialogs["ReloadPop"] = {
--text = "In order to remove the cursor texture, you need to reload your UI. Is that okay?",
--button1 = "Yes",
--button2 = "No",
--OnAccept = function() ReloadUI() end,
--timeout = 0,
--sound = "RaidWarning",
--whileDead = true,
--hideOnEscape = true,
--preferredIndex = 3, -- avoid some UI taint, see [url]http://www.wowace.com/announcements/how-to-avoid-some-ui-taint/[/url]
--}
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.