Mercurial > wow > degaine
comparison Degaine.lua @ 5:66640a8f1cbd
Started reworking code ordering in files, comments and vars are in english
| author | contrebasse |
|---|---|
| date | Fri, 10 Dec 2010 16:04:55 +0100 |
| parents | 97422a3e11c9 |
| children | 120f29645e34 |
comparison
equal
deleted
inserted
replaced
| 4:388b12209e6c | 5:66640a8f1cbd |
|---|---|
| 1 local AddonName = ... | 1 local AddonName = ... |
| 2 -- Dégaine auto des armes | 2 -- Draw your weapons automagically |
| 3 | 3 |
| 4 -- BUGS connus : | 4 -- Known bugs : |
| 5 -- le camouflage du voleur ne rentre pas l'arme | |
| 6 -- si on se lève en sautant ça ne sort pas l'arme (impossible de savoir si on était assis avant) | 5 -- si on se lève en sautant ça ne sort pas l'arme (impossible de savoir si on était assis avant) |
| 7 | 6 |
| 8 -- DEBUG | 7 --@debug@ |
| 9 local debug = false; | 8 local debug = false; |
| 9 --@end-debug@ | |
| 10 | 10 |
| 11 -- Fonctions globales en local | 11 --------------------------------------------------------------- |
| 12 -- GLOBALS: ToggleAutoDegaine, isDegaineAuto, BINDING_HEADER_DEGAINE | 12 -- Globals to local |
| 13 local GossipFrame = GossipFrame; | 13 --------------------------------------------------------------- |
| 14 local DEFAULT_CHAT_FRAME = DEFAULT_CHAT_FRAME; | 14 -- These need to be globals |
| 15 local InCombatLockdown = InCombatLockdown; | 15 -- GLOBALS: ToggleAutoDegaine, Degaine_isAuto, BINDING_HEADER_DEGAINE |
| 16 local ToggleSheath = ToggleSheath; | |
| 17 | 16 |
| 17 -- It doesn't matter if they are globals | |
| 18 -- GLOBALS: DEFAULT_CHAT_FRAME, GossipFrame | |
| 19 | |
| 20 -- Lua functions | |
| 18 local pairs = pairs; | 21 local pairs = pairs; |
| 19 local type = type; | 22 local type = type; |
| 20 | 23 |
| 21 -- Pour les raccourcis | 24 -- Wow functions |
| 22 -- Cf Bindings.xml pour les raccourcis eux-mêmes | 25 local InCombatLockdown = InCombatLockdown |
| 23 BINDING_HEADER_DEGAINE = "Dégainer automatiquement"; | 26 local ToggleSheath = ToggleSheath |
| 24 | 27 |
| 25 -- Variables locales | |
| 26 local DegaineFrame = CreateFrame("Frame"); | |
| 27 local playername = UnitName("player"); | |
| 28 local done = true; | |
| 29 local t_restant = -1; | |
| 30 | 28 |
| 31 -- Configuration des events à regarder | 29 --------------------------------------------------------------- |
| 32 local events = { -- events sans arguments à voir | 30 -- Some stuff... |
| 31 --------------------------------------------------------------- | |
| 32 -- Bindings | |
| 33 BINDING_HEADER_DEGAINE = "Dégainer automatiquement" | |
| 34 | |
| 35 -- Print status | |
| 36 local printState = function() | |
| 37 DEFAULT_CHAT_FRAME:AddMessage(Degaine_isAuto and "Dégainage automatique |cFF00FF00activé|r" or "Dégainage automatique |cFFFF0000désactivé|r"); | |
| 38 end | |
| 39 | |
| 40 | |
| 41 --------------------------------------------------------------- | |
| 42 -- Local vars | |
| 43 --------------------------------------------------------------- | |
| 44 -- const | |
| 45 local DegaineFrame = CreateFrame("Frame") | |
| 46 local playername = UnitName("player") | |
| 47 | |
| 48 -- vars | |
| 49 local done = true | |
| 50 local t_left = -1 | |
| 51 | |
| 52 --------------------------------------------------------------- | |
| 53 -- Events config | |
| 54 --------------------------------------------------------------- | |
| 55 -- Events to watch | |
| 56 local events = { -- no args to watch | |
| 33 GOSSIP_SHOW = true, | 57 GOSSIP_SHOW = true, |
| 34 MERCHANT_SHOW = function() return GossipFrame:IsShown(); end, | 58 MERCHANT_SHOW = function() return GossipFrame:IsShown(); end, |
| 35 BANKFRAME_OPENED = true; --function() return GossipFrame:IsShown(); end, | 59 BANKFRAME_OPENED = true, --function() return GossipFrame:IsShown(); end, |
| 36 AUCTION_HOUSE_SHOW = true, | 60 AUCTION_HOUSE_SHOW = true, |
| 37 }; | 61 } |
| 38 local events1player = { -- events dont le 1er argument doit être le joueur | 62 local SpellBlacklist = { |
| 39 UNIT_SPELLCAST_FAILED = true; | 63 [1784] = true, -- Stealth (Rogue) |
| 40 UNIT_SPELLCAST_INTERRUPTED = true; | 64 [58984] = true, -- Shadowmeld (Nightelf stealth) |
| 41 UNIT_SPELLCAST_SUCCEEDED = true; | 65 } |
| 42 }; | 66 local events1player = { -- the first arg is "player" |
| 43 local events2playername = { -- events dont le 2e argument doit être le joueur | 67 UNIT_SPELLCAST_FAILED = true, |
| 44 CHAT_MSG_SAY = true; | 68 UNIT_SPELLCAST_INTERRUPTED = true, |
| 45 CHAT_MSG_YELL = true; | 69 UNIT_SPELLCAST_SUCCEEDED = true, |
| 46 CHAT_MSG_TEXT_EMOTE = true; | 70 } |
| 47 }; | 71 local events2playername = { -- the 2nd arg is playername |
| 72 CHAT_MSG_SAY = true, | |
| 73 CHAT_MSG_YELL = true, | |
| 74 CHAT_MSG_TEXT_EMOTE = true, | |
| 75 } | |
| 48 | 76 |
| 49 -- Fonctions d'activation/désactivation des events | |
| 50 local registerEvents = function() | |
| 51 --if debug then | |
| 52 -- DEBUG | |
| 53 -- DegaineFrame:RegisterAllEvents(); | |
| 54 --else | |
| 55 for k,v in pairs(events) do DegaineFrame:RegisterEvent(k); end | |
| 56 for k,v in pairs(events1player) do DegaineFrame:RegisterEvent(k); end | |
| 57 for k,v in pairs(events2playername) do DegaineFrame:RegisterEvent(k); end | |
| 58 --end | |
| 59 end -- function | |
| 60 local unregisterEvents = function() | |
| 61 if not debug then | |
| 62 for k,v in pairs(events) do DegaineFrame:UnregisterEvent(k); end | |
| 63 for k,v in pairs(events1player) do DegaineFrame:UnregisterEvent(k); end | |
| 64 for k,v in pairs(events2playername) do DegaineFrame:UnregisterEvent(k); end | |
| 65 end | |
| 66 end -- function | |
| 67 | 77 |
| 68 -- Fonction de timer | 78 --------------------------------------------------------------- |
| 69 -- On attend un petit délai avant de dégainer, pour voir l'animation et éviter que ça bug | 79 -- Events registering |
| 70 local OnUpdate = function(self,t_ecoule) | 80 --------------------------------------------------------------- |
| 81 | |
| 82 | |
| 83 --------------------------------------------------------------- | |
| 84 -- Timer | |
| 85 --------------------------------------------------------------- | |
| 86 -- Wait a few seconds before drawing, to be able to see the curren animation (and avoid bugs) | |
| 87 local Degaine_OnUpdate = function(self,t_elapsed) | |
| 71 if not done then | 88 if not done then |
| 72 t_restant = t_restant - t_ecoule; | 89 t_left = t_left - t_elapsed; |
| 73 if t_restant<=0 then | 90 if t_left<=0 then |
| 74 done = true; | 91 done = true; |
| 75 ToggleSheath(); | 92 ToggleSheath(); |
| 76 end | 93 end |
| 77 end | 94 end |
| 78 end | 95 end |
| 79 | 96 |
| 80 -- Fonctions d'activation/désactivation | 97 |
| 81 local activer = function() | 98 --------------------------------------------------------------- |
| 82 if (not isDegaineAuto) or InCombatLockdown() then return end | 99 -- Activation functions |
| 83 | 100 --------------------------------------------------------------- |
| 84 DegaineFrame:SetScript("OnUpdate", OnUpdate); | 101 local activate = function() |
| 85 registerEvents(); | 102 if (not Degaine_isAuto) or InCombatLockdown() then return end |
| 103 | |
| 104 DegaineFrame:SetScript("OnUpdate", Degaine_OnUpdate); | |
| 105 --@debug@ | |
| 106 if debug then | |
| 107 DegaineFrame:RegisterAllEvents(); | |
| 108 else | |
| 109 --@end-debug@ | |
| 110 for k,v in pairs(events) do DegaineFrame:RegisterEvent(k); end | |
| 111 for k,v in pairs(events1player) do DegaineFrame:RegisterEvent(k); end | |
| 112 for k,v in pairs(events2playername) do DegaineFrame:RegisterEvent(k); end | |
| 113 --@end-debug@ | |
| 114 end | |
| 115 --@end-debug@ | |
| 86 end | 116 end |
| 87 local desactiver = function() | 117 local desactivate = function() |
| 88 -- Désactivation | 118 for k,v in pairs(events) do DegaineFrame:UnregisterEvent(k); end |
| 89 unregisterEvents(); | 119 for k,v in pairs(events1player) do DegaineFrame:UnregisterEvent(k); end |
| 120 for k,v in pairs(events2playername) do DegaineFrame:UnregisterEvent(k); end | |
| 90 DegaineFrame:SetScript("OnUpdate", nil); | 121 DegaineFrame:SetScript("OnUpdate", nil); |
| 91 done = true; | 122 done = true; |
| 92 end | 123 end |
| 124 ToggleAutoDegaine = function() | |
| 125 if Degaine_isAuto then | |
| 126 Degaine_isAuto = false | |
| 127 desactivate() | |
| 128 else | |
| 129 Degaine_isAuto = true | |
| 130 activate() | |
| 131 end | |
| 132 printState() | |
| 133 end | |
| 93 | 134 |
| 94 -- Fonctions | 135 |
| 95 local printState = function() | 136 --------------------------------------------------------------- |
| 96 DEFAULT_CHAT_FRAME:AddMessage(isDegaineAuto and "Dégainage automatique |cFF00FF00activé|r" or "Dégainage automatique |cFFFF0000désactivé|r"); | 137 -- Event/hooks functions |
| 97 end | 138 --------------------------------------------------------------- |
| 98 local OnEvent = function(self,event, arg1, arg2, ...) | 139 local Degaine_OnEvent = function(self,event, arg1, arg2, ...) |
| 140 --@debug@ | |
| 99 if debug then | 141 if debug then |
| 100 DEFAULT_CHAT_FRAME:AddMessage(event); | 142 DEFAULT_CHAT_FRAME:AddMessage(event) |
| 101 if arg1 then DEFAULT_CHAT_FRAME:AddMessage("arg1 = "..arg1); end | 143 if arg1 then DEFAULT_CHAT_FRAME:AddMessage("arg1 = "..arg1); end |
| 102 if arg2 then DEFAULT_CHAT_FRAME:AddMessage("arg2 = "..arg2); end | 144 if arg2 then DEFAULT_CHAT_FRAME:AddMessage("arg2 = "..arg2); end |
| 103 end | 145 end |
| 104 | 146 --@end-debug@ |
| 147 | |
| 105 if ((events[event] and (type(events[event])~="function" or events[event]())) | 148 if ((events[event] and (type(events[event])~="function" or events[event]())) |
| 106 or (events1player[event] and arg1=="player") | 149 or (events1player[event] and arg1=="player") |
| 107 or (events2playername[event] and arg2==playername)) then | 150 or (events2playername[event] and arg2==playername)) then |
| 108 t_restant = 2; | 151 t_left = 2 |
| 109 done = false; | 152 done = false |
| 153 | |
| 110 elseif event == "PLAYER_REGEN_ENABLED" then | 154 elseif event == "PLAYER_REGEN_ENABLED" then |
| 111 desactiver(); | 155 desactivate() |
| 112 elseif event == "PLAYER_REGEN_DISABLED" then | 156 elseif event == "PLAYER_REGEN_DISABLED" then |
| 113 activer(); | 157 activate() |
| 158 | |
| 114 elseif event == "ADDON_LOADED" and arg1==AddonName then | 159 elseif event == "ADDON_LOADED" and arg1==AddonName then |
| 115 if isDegaineAuto==nil then | 160 if Degaine_isAuto==nil then |
| 116 isDegaineAuto = true; | 161 Degaine_isAuto = true |
| 117 end | 162 end |
| 118 if isDegaineAuto then | 163 if Degaine_isAuto then |
| 119 activer(); | 164 activate() |
| 120 else | 165 else |
| 121 desactiver(); | 166 desactivate() |
| 122 end | 167 end |
| 123 printState(); | 168 printState() |
| 169 | |
| 170 -- Not needed anymore | |
| 171 DegaineFrame:UnregisterEvent("ADDON_LOADED") | |
| 124 end | 172 end |
| 125 end | 173 end |
| 174 -- Hook when standing up | |
| 175 -- (Didn't work, see http://forums.wowace.com/showthread.php?p=310547#post310547) | |
| 176 hooksecurefunc("SitStandOrDescendStart",function() | |
| 177 if Degaine_isAuto and not InCombatLockdown() then | |
| 178 t_left = 2 | |
| 179 done = false | |
| 180 end | |
| 181 end) | |
| 126 | 182 |
| 127 -- Pour mettre le dégainage auto on/off TODO | |
| 128 ToggleAutoDegaine = function() | |
| 129 if isDegaineAuto then | |
| 130 isDegaineAuto = false; | |
| 131 desactiver(); | |
| 132 else | |
| 133 isDegaineAuto = true; | |
| 134 activer(); | |
| 135 end | |
| 136 printState(); | |
| 137 end | |
| 138 | 183 |
| 139 -- Enregistrement des events et activation | 184 |
| 140 DegaineFrame:RegisterEvent("PLAYER_REGEN_ENABLED"); | 185 --------------------------------------------------------------- |
| 141 DegaineFrame:RegisterEvent("PLAYER_REGEN_DISABLED"); | 186 -- Initialization |
| 142 DegaineFrame:RegisterEvent("ADDON_LOADED"); | 187 --------------------------------------------------------------- |
| 143 DegaineFrame:SetScript("OnEvent",OnEvent); | 188 DegaineFrame:SetScript("OnEvent",Degaine_OnEvent) |
| 144 -- ça ne marche pas... question posée sur http://forums.wowace.com/showthread.php?p=310547#post310547 | 189 |
| 145 hooksecurefunc("SitStandOrDescendStart",function() if isDegaineAuto and not InCombatLockdown() then t_restant = 2; done = false; end; end); | 190 -- Global events |
| 146 activer(); | 191 DegaineFrame:RegisterEvent("PLAYER_REGEN_ENABLED") |
| 192 DegaineFrame:RegisterEvent("PLAYER_REGEN_DISABLED") | |
| 193 DegaineFrame:RegisterEvent("ADDON_LOADED") | |
| 194 activate() |
