Guest User

casting.lua

a guest
Nov 29th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.95 KB | None | 0 0
  1. local BS = AceLibrary("Babble-Spell-2.2")
  2. local elapsed = 0
  3.  
  4. local roman = {
  5. 'I', 'II', 'III', 'IV', 'V',
  6. 'VI', 'VII', 'VIII', 'IX', 'X',
  7. 'XI', 'XII', 'XIII', 'XIV', 'XV',
  8. 'XVI', 'XVII', 'XVIII', 'XIX', 'XX'
  9. } -- 20 enough for vanilla ?
  10.  
  11. -- dIsInSeconds is passed by custom clients if they want to save on maths
  12. -- dontRegister is passed by custom clients if they need to call Stop/Failed/Delayed manually
  13. function oCB:SpellStart(s, d, dIsInSeconds, dontRegister)
  14. self:Debug(string.format("SpellStart - %s | %s (%s)%s", s, d, dIsInSeconds and "s" or "ms", dontRegister and " | Not Registering" or ""))
  15.  
  16. if s == "" then
  17. s = (getglobal("GameTooltipTextLeft1"):GetText() or "")
  18. end
  19.  
  20. if not dontRegister then
  21. self:RegisterEvent("SPELLCAST_STOP", "SpellStop")
  22. self:RegisterEvent("SPELLCAST_INTERRUPTED","SpellFailed")
  23. self:RegisterEvent("SPELLCAST_FAILED", "SpellFailed")
  24. self:RegisterEvent("SPELLCAST_DELAYED", "SpellDelayed")
  25. end
  26.  
  27. local c = self.db.profile.Colors.Casting
  28.  
  29. self.startTime = GetTime()
  30.  
  31. if not dIsInSeconds then
  32. d = d/1000
  33. end
  34. self.maxValue = self.startTime + d
  35.  
  36. self.frames.CastingBar.Bar:SetStatusBarColor(c.r, c.g, c.b)
  37. self.frames.CastingBar.Bar:SetMinMaxValues(1,100)
  38. self.frames.CastingBar.Bar:SetValue(1)
  39.  
  40. self.SpellIcon = BS:GetSpellIcon(s)
  41. self.ItemIcon = self:FindItemIcon(s)
  42.  
  43. if oCBRank and self.db.profile.CastingBar.spellShowRank then
  44. if oCB:IsSpell(s, oCBRank) then
  45. self:Debug("Rank Found: "..s.." "..oCBRank)
  46. if self.db.profile.CastingBar.spellRomanRank then
  47. local num = tonumber(oCBRank)
  48. if num and num > 0 then
  49. oCBRank = roman[num]
  50. end
  51. end
  52. if not self.db.profile.CastingBar.spellShortRank then
  53. if (GetLocale() == "frFR") then
  54. self.frames.CastingBar.Spell:SetText(s.." "..string.format(string.gsub(RANK_COLON, ":", "%%s"), oCBRank))
  55. else
  56. self.frames.CastingBar.Spell:SetText(s.." "..string.format(string.gsub(RANK_COLON, ":", " %%s"), oCBRank))
  57. end
  58. else
  59. self.frames.CastingBar.Spell:SetText(s.." "..oCBRank)
  60. end
  61. else
  62. self.frames.CastingBar.Spell:SetText(s)
  63. end
  64. else
  65. self.frames.CastingBar.Spell:SetText(s)
  66. end
  67. self.frames.CastingBar:SetAlpha(1)
  68. self.frames.CastingBar.Time:SetText("")
  69. self.frames.CastingBar.Delay:SetText("")
  70.  
  71. if oCBCastSent then
  72. local mylatency = math.floor((GetTime()-oCBCastSent)*1000)
  73. local w = math.floor(self.frames.CastingBar.Bar:GetWidth())
  74. mylatency = mylatency
  75. self.frames.CastingBar.Latency:SetText(mylatency.."ms")
  76. self.frames.CastingBar.LagBar:SetStatusBarColor(1, 0, 0, 0.5)
  77. self.frames.CastingBar.LagBar:SetMinMaxValues(0, 100)
  78. self.frames.CastingBar.LagBar:SetValue(100)
  79. local lagw = w - (w*(self.maxValue-self.startTime-(mylatency/1000))/(self.maxValue-self.startTime))
  80. if lagw >= w then lagw = w end
  81. self.frames.CastingBar.LagBar:SetWidth(lagw)
  82. else
  83. self.frames.CastingBar.Latency:SetText("")
  84. self.frames.CastingBar.LagBar:SetValue(0)
  85. end
  86.  
  87. if not self.SpellIcon and not self.ItemIcon then
  88. if (TradeSkillFrame and TradeSkillFrame:IsVisible()) then
  89. self:Debug("entering loop !")
  90. for i=1,GetNumTradeSkills() do
  91. if GetTradeSkillInfo(i) == s then
  92. self:Debug("found! "..GetTradeSkillInfo(i))
  93. self.SpellIcon = GetTradeSkillIcon(i);
  94. break
  95. end
  96. end
  97. if not self.SpellIcon then self:Debug("Craft icon not found :(") end
  98. elseif oCBTooltip then
  99. self.ItemIcon = self:FindItemIcon(oCBTooltip)
  100. end
  101. end
  102.  
  103. if oCBIcon == self.ItemIcon and oCBIcon ~= nil then
  104. self.frames.CastingBar.Texture:SetTexture(oCBIcon)
  105. self.frames.CastingBar.Icon:Show()
  106. elseif self.SpellIcon then
  107. self.frames.CastingBar.Texture:SetTexture(self.SpellIcon)
  108. self.frames.CastingBar.Icon:Show()
  109. elseif string.find(s, "^Recette") or string.find(s, "^Plans :") or string.find(s, "^Patron :") or string.find(s, "^Formule :") then --missing translation
  110. self.frames.CastingBar.Texture:SetTexture("Interface\\AddOns\\oCB\\Icons\\Spell_Arcane_MindMastery")
  111. self.frames.CastingBar.Icon:Show()
  112. self.frames.CastingBar.Latency:SetText("")
  113. self.frames.CastingBar.LagBar:SetWidth(0)
  114. elseif self.ItemIcon then
  115. self.frames.CastingBar.Texture:SetTexture(self.ItemIcon)
  116. self.frames.CastingBar.Icon:Show()
  117. self.frames.CastingBar.Latency:SetText("")
  118. self.frames.CastingBar.LagBar:SetValue(0)
  119. elseif s == "Invocation d'un char d'assaut qiraji jaune" then --clean that
  120. self.frames.CastingBar.Texture:SetTexture("Interface/Icons/INV_Misc_QirajiCrystal_01")
  121. self.frames.CastingBar.Icon:Show()
  122. self.frames.CastingBar.Latency:SetText("")
  123. self.frames.CastingBar.LagBar:SetValue(0)
  124. else
  125. self.frames.CastingBar.Texture:SetTexture("Interface\\AddOns\\oCB\\Icon")
  126. self.frames.CastingBar.Icon:Show()
  127. end
  128.  
  129. self.holdTime = 0
  130. self.delay = 0
  131. self.casting = 1
  132. self.fadeOut = nil
  133. self.SpellIcon = nil
  134. self.ItemIcon = nil
  135.  
  136. self.frames.CastingBar:Show()
  137. self.frames.CastingBar.Spark:Show()
  138. end
  139.  
  140. -- Arg is for custom clients
  141. function oCB:SpellStop(dontUnregister)
  142. self:Debug("SpellStop - Stopping cast")
  143. local c = self.db.profile.Colors.Complete
  144.  
  145. self.frames.CastingBar.Bar:SetValue(100)
  146.  
  147. self.frames.CastingBar.Latency:SetText("")
  148. self.frames.CastingBar.LagBar:SetValue(0)
  149.  
  150. if not self.channeling then
  151. self.frames.CastingBar.Bar:SetStatusBarColor(c.r, c.g, c.b)
  152. self.frames.CastingBar.Spark:Hide()
  153. end
  154.  
  155. self.delay = 0
  156. self.casting = nil
  157. self.fadeOut = 1
  158.  
  159. oCBCastSent = nil
  160.  
  161. if not dontUnregister then
  162. self:UnregisterEvent("SPELLCAST_STOP")
  163. self:UnregisterEvent("SPELLCAST_FAILED")
  164. self:UnregisterEvent("SPELLCAST_INTERRUPTED")
  165. self:UnregisterEvent("SPELLCAST_DELAYED")
  166. end
  167. end
  168.  
  169. function oCB:SpellFailed(dontUnregister)
  170. local c = self.db.profile.Colors.Failed
  171.  
  172. self.frames.CastingBar.Bar:SetValue(100)
  173. self.frames.CastingBar.Bar:SetStatusBarColor(c.r, c.g, c.b)
  174. self.frames.CastingBar.Spark:Hide()
  175.  
  176. self.frames.CastingBar.Latency:SetText("")
  177. self.frames.CastingBar.LagBar:SetValue(0)
  178.  
  179. if (event == "SPELLCAST_FAILED") then
  180. self.frames.CastingBar.Spell:SetText(FAILED)
  181. else
  182. self.frames.CastingBar.Spell:SetText(INTERRUPTED)
  183. end
  184.  
  185. self.casting = nil
  186. self.channeling = nil
  187. self.fadeOut = 1
  188. self.holdTime = GetTime() + 1
  189.  
  190. oCBCastSent = nil
  191.  
  192. if not dontUnregister then
  193. self:UnregisterEvent("SPELLCAST_STOP")
  194. self:UnregisterEvent("SPELLCAST_FAILED")
  195. self:UnregisterEvent("SPELLCAST_INTERRUPTED")
  196. self:UnregisterEvent("SPELLCAST_DELAYED")
  197. end
  198. end
  199.  
  200. function oCB:SpellDelayed(d)
  201. self:Debug(string.format("SpellDelayed - Spell delayed with %s", d/1000))
  202. d = d / 1000
  203.  
  204. if(self.frames.CastingBar:IsShown()) then
  205. if self.delay == nil then self.delay = 0 end
  206.  
  207. self.startTime = self.startTime + d
  208. self.maxValue = self.maxValue + d
  209. self.delay = self.delay + d
  210.  
  211. self.frames.CastingBar.Bar:SetMinMaxValues(1,100)
  212. end
  213. end
  214.  
  215. function oCB:SpellChannelStart(d)
  216. self:Debug("SpellChannelStart - Starting channel")
  217. self:Debug("ChannelInfo - "..oCBName or arg2.." - "..(oCBRank or "no rank").." - "..(oCBIcon or ""))
  218. d = d / 1000
  219. local c = self.db.profile.Colors.Channel
  220.  
  221. self.startTime = GetTime()
  222. self.endTime = self.startTime + d
  223. self.maxValue = self.endTime
  224.  
  225. self.frames.CastingBar.Bar:SetStatusBarColor(c.r, c.g, c.b)
  226. self.frames.CastingBar.Bar:SetMinMaxValues(1,100)
  227. self.frames.CastingBar.Bar:SetValue(1)
  228.  
  229. if oCBRank and self.db.profile.CastingBar.spellShowRank then
  230. if oCB:IsSpell(oCBName, oCBRank) then
  231. self:Debug("Found: "..oCBName.." (Rank: "..oCBRank..")")
  232. if self.db.profile.CastingBar.spellRomanRank then
  233. local num = tonumber(oCBRank)
  234. if num and num > 0 then
  235. oCBRank = roman[num]
  236. end
  237. end
  238. if not self.db.profile.CastingBar.spellShortRank then
  239. self.frames.CastingBar.Spell:SetText(oCBName.." "..string.format(string.gsub(RANK_COLON, ":", "%%s"), oCBRank))
  240. else
  241. self.frames.CastingBar.Spell:SetText(oCBName.." "..oCBRank)
  242. end
  243. else
  244. self.frames.CastingBar.Spell:SetText(oCBName or arg2)
  245. end
  246. else
  247. self.frames.CastingBar.Spell:SetText(oCBName or arg2)
  248. end
  249.  
  250. self.frames.CastingBar.Time:SetText("")
  251. self.frames.CastingBar.Delay:SetText("")
  252. self.frames.CastingBar:SetAlpha(1)
  253.  
  254. self.frames.CastingBar.Icon:Hide()
  255. self.frames.CastingBar.Latency:SetText("")
  256.  
  257. if oCBIcon then
  258. self.frames.CastingBar.Texture:SetTexture(oCBIcon)
  259. self.frames.CastingBar.Icon:Show()
  260. end
  261.  
  262. self.holdTime = 0
  263. self.casting = nil
  264. self.channeling = 1
  265. self.fadeOut = nil
  266.  
  267. self.frames.CastingBar:Show()
  268. self.frames.CastingBar.Spark:Show()
  269. end
  270.  
  271. function oCB:SpellChannelStop()
  272. self:Debug("SpellChannelStop - Stopping channel")
  273. if not self.channeling then return end
  274. local c = self.db.profile.Colors.Complete
  275.  
  276. self.frames.CastingBar.Bar:SetValue(100)
  277. self.frames.CastingBar.Bar:SetStatusBarColor(c.r, c.g, c.b)
  278. self.frames.CastingBar.Spark:Hide()
  279.  
  280. self.delay = 0
  281. self.casting = nil
  282. self.channeling = nil
  283. self.fadeOut = 1
  284.  
  285. oCBCastSent = nil
  286. end
  287.  
  288. function oCB:SpellChannelUpdate(d)
  289. self:Debug("SpellChannelUpdate - Updating channel")
  290. d = d / 1000
  291.  
  292. if(self.frames.CastingBar:IsShown()) then
  293. local origDuration = self.endTime - self.startTime
  294.  
  295. if self.delay == nil then self.delay = 0 end
  296.  
  297. self.delay = self.delay + d
  298. self.endTime = GetTime() + d
  299. self.maxValue = self.endTime
  300. self.startTime = self.endTime - origDuration
  301.  
  302. self.frames.CastingBar.Bar:SetMinMaxValues(1,100)
  303. end
  304. end
  305.  
  306. function oCB:OnCasting()
  307. elapsed= elapsed + arg1
  308. if(oCB.casting) then
  309. local delay, n, sp = false, GetTime(), 0
  310.  
  311. if (n >= oCB.maxValue) then n = oCB.maxValue end
  312.  
  313. oCB.frames.CastingBar.Time:SetText(string.format( "%.1f", n-oCB.startTime).." / "..string.format("%.1f", oCB.maxValue-oCB.startTime))
  314.  
  315. if (oCB.delay ~= 0) then delay = 1 end
  316. if (delay) then
  317. oCB.frames.CastingBar.Delay:SetText("+"..string.format("%.1f", oCB.delay or "" ))
  318. else
  319. oCB.frames.CastingBar.Delay:SetText("")
  320. end
  321.  
  322. if UnitOnTaxi("player") then
  323. oCB.frames.CastingBar.Texture:SetTexture("Interface/Icons/Ability_Hunter_EagleEye")
  324. oCB.frames.CastingBar.Icon:Show()
  325. oCB.frames.CastingBar.Latency:SetText("")
  326. oCB.frames.CastingBar.LagBar:SetValue(0)
  327. end
  328.  
  329. local np = ((n - oCB.startTime) / (oCB.maxValue - oCB.startTime)) * 100
  330. oCB.frames.CastingBar.Bar:SetValue(np)
  331.  
  332. local w = oCB.frames.CastingBar.Bar:GetWidth()
  333. sp = ((n - oCB.startTime) / (oCB.maxValue - oCB.startTime)) * w
  334. if( sp < 0 ) then sp = 0 end
  335.  
  336. oCB.frames.CastingBar.Spark:SetPoint("CENTER", oCB.frames.CastingBar.Bar, "LEFT", sp, 0)
  337. elseif (oCB.channeling) then
  338. local delay, n, sp = false, GetTime(), 0
  339.  
  340. if (n > oCB.endTime) then n = oCB.endTime end
  341. if (n == oCB.endTime) then
  342. oCB.channeling = nil
  343. oCB.fadeOut = 1
  344. return
  345. end
  346.  
  347. local b = oCB.startTime + (oCB.endTime - n)
  348.  
  349. oCB.frames.CastingBar.Time:SetText(string.format( "%.1f", math.max(oCB.maxValue - n, 0.0)))
  350.  
  351. if (oCB.delay and oCB.delay ~= 0) then delay = 1 end
  352. if (delay) then
  353. oCB.frames.CastingBar.Delay:SetText("-"..string.format("%.1f", oCB.delay ))
  354. else
  355. oCB.frames.CastingBar.Delay:SetText("")
  356. end
  357.  
  358. local bp = (b-oCB.startTime) / (oCB.endTime - oCB.startTime) * 100
  359. oCB.frames.CastingBar.Bar:SetValue(bp)
  360.  
  361. local w = oCB.frames.CastingBar.Bar:GetWidth()
  362. sp = ((b - oCB.startTime) / (oCB.endTime - oCB.startTime)) * w
  363.  
  364. oCB.frames.CastingBar.Spark:SetPoint("CENTER", oCB.frames.CastingBar.Bar, "LEFT", sp, 0)
  365. elseif(GetTime() < oCB.holdTime) then
  366. return
  367. elseif(oCB.fadeOut) then
  368. local a = this:GetAlpha() - .05
  369.  
  370. if (a > 0) then
  371. oCB.frames.CastingBar:SetAlpha(a)
  372. else
  373. oCB.fadeOut = nil
  374. oCB.frames.CastingBar:Hide()
  375. oCB.frames.CastingBar.Time:SetText("")
  376. oCB.frames.CastingBar.Delay:SetText("")
  377. oCB.frames.CastingBar:SetAlpha(1)
  378. end
  379. end
  380. end
Advertisement
Add Comment
Please, Sign In to add comment