view Degaine.lua @ 8:47f9e596ef5a

Use a specific function for drawing
author contrebasse
date Fri, 10 Dec 2010 16:37:00 +0100
parents f8198806d261
children 571dcc76d801
line wrap: on
line source
local AddonName = ...
-- Draw your weapons automagically

-- Known bugs :
-- si on se lève en sautant ça ne sort pas l'arme (impossible de savoir si on était assis avant)

--@debug@
local debug = false;
--@end-debug@

---------------------------------------------------------------
-- Globals to local
---------------------------------------------------------------
-- These need to be globals
-- GLOBALS: Degaine_ToggleAuto, Degaine_isAuto, BINDING_HEADER_DEGAINE

-- It doesn't matter if they are globals
-- GLOBALS: DEFAULT_CHAT_FRAME, GossipFrame

-- Lua functions
local pairs = pairs;
local type = type;

-- Wow functions
local InCombatLockdown = InCombatLockdown
local ToggleSheath = ToggleSheath


---------------------------------------------------------------
-- Local vars
---------------------------------------------------------------
-- const
local DegaineFrame = CreateFrame("Frame")
local playername = UnitName("player")

-- vars
local done = true
local t_left = -1
local delay = 2 -- seconds

---------------------------------------------------------------
-- Some stuff...
---------------------------------------------------------------
-- Bindings
BINDING_HEADER_DEGAINE = "Dégainer automatiquement"

-- Print status
local printState = function()
	DEFAULT_CHAT_FRAME:AddMessage(Degaine_isAuto and "Dégainage automatique |cFF00FF00activé|r" or "Dégainage automatique |cFFFF0000désactivé|r");
end


---------------------------------------------------------------
-- Events config
---------------------------------------------------------------
-- Events to watch
local events = { -- no args to watch
	GOSSIP_SHOW = true,
	MERCHANT_SHOW = function() return GossipFrame:IsShown(); end,
	BANKFRAME_OPENED = true, --function() return GossipFrame:IsShown(); end,
	AUCTION_HOUSE_SHOW = true,
}
local SpellBlacklist = {
	[1784] = true,  -- Stealth (Rogue)
	[58984] = true, -- Shadowmeld (Nightelf stealth)
}
local events1player = { -- the first arg is "player"
	UNIT_SPELLCAST_FAILED = true,
	UNIT_SPELLCAST_INTERRUPTED = true,
	UNIT_SPELLCAST_SUCCEEDED = true,
}
local events2playername = { -- the 2nd arg is playername
	CHAT_MSG_SAY = true,
	CHAT_MSG_YELL = true,
	CHAT_MSG_TEXT_EMOTE = true,
}


---------------------------------------------------------------
-- Launch drawing weapons
---------------------------------------------------------------
-- Wait a few seconds before drawing, to be able to see the curren animation (and avoid bugs)
local Degaine_OnUpdate = function(self,t_elapsed)
	if not done then
		t_left = t_left - t_elapsed;
		if t_left<=0 then
			done = true;
			ToggleSheath();
			DegaineFrame:SetScript("OnUpdate", nil);
		end
	end
end
local start = function()
	if Degaine_isAuto and not InCombatLockdown() then
		t_left = delay
		done = false
		DegaineFrame:SetScript("OnUpdate", Degaine_OnUpdate);
	end
end

---------------------------------------------------------------
-- Activation functions
---------------------------------------------------------------
local activate = function()
	if (not Degaine_isAuto) or InCombatLockdown() then return end

	DegaineFrame:SetScript("OnUpdate", Degaine_OnUpdate);
	--@debug@
	if debug then
		DegaineFrame:RegisterAllEvents();
	else
	--@end-debug@
		for k,v in pairs(events) do DegaineFrame:RegisterEvent(k); end
		for k,v in pairs(events1player) do DegaineFrame:RegisterEvent(k); end
		for k,v in pairs(events2playername) do DegaineFrame:RegisterEvent(k); end
	--@end-debug@
	end
	--@end-debug@
end
local desactivate = function()
	for k,v in pairs(events) do DegaineFrame:UnregisterEvent(k); end
	for k,v in pairs(events1player) do DegaineFrame:UnregisterEvent(k); end
	for k,v in pairs(events2playername) do DegaineFrame:UnregisterEvent(k); end
	DegaineFrame:SetScript("OnUpdate", nil);
	done = true;
end
Degaine_ToggleAuto = function()
	if Degaine_isAuto then
		Degaine_isAuto = false
		desactivate()
	else
		Degaine_isAuto = true
		activate()
	end
	printState()
end


---------------------------------------------------------------
-- Event/hooks functions
---------------------------------------------------------------
local Degaine_OnEvent = function(self,event, arg1, arg2, ...)
	--@debug@
	if debug then
		DEFAULT_CHAT_FRAME:AddMessage(event)
		if arg1 then DEFAULT_CHAT_FRAME:AddMessage("arg1 = "..arg1); end
		if arg2 then DEFAULT_CHAT_FRAME:AddMessage("arg2 = "..arg2); end
	end
	--@end-debug@

	if ((events[event] and (type(events[event])~="function" or events[event]()))
			or (events1player[event] and arg1=="player")
			or (events2playername[event] and arg2==playername)) then
		start()

	elseif event == "PLAYER_REGEN_ENABLED" then
		desactivate()
	elseif event == "PLAYER_REGEN_DISABLED" then
		activate()

	elseif event == "ADDON_LOADED" and arg1==AddonName then
		if Degaine_isAuto==nil then
			Degaine_isAuto = true
		end
		if Degaine_isAuto then activate(); else desactivate(); end
		printState()

		-- Not needed anymore
		DegaineFrame:UnregisterEvent("ADDON_LOADED")
	end
end
-- Hook when standing up
-- (Didn't work, see http://forums.wowace.com/showthread.php?p=310547#post310547)
hooksecurefunc("SitStandOrDescendStart",start);


---------------------------------------------------------------
-- Commands
---------------------------------------------------------------
-- Bindings
BINDING_HEADER_DEGAINE = "Dégainer automatiquement"

-- Slash command
-- GLOBALS: SLASH_DEGAINE1
SLASH_DEGAINE1 = "/degaine"
SlashCmdList["DEGAINE"] = Degaine_ToggleAuto


---------------------------------------------------------------
-- Initialization
---------------------------------------------------------------
DegaineFrame:SetScript("OnEvent",Degaine_OnEvent)

-- Global events
DegaineFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
DegaineFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
DegaineFrame:RegisterEvent("ADDON_LOADED")
activate()