Quantcast

- XP : On Hide stop events to save cycles; on Show start plugin

urnati [01-19-24 - 01:01]
- XP : On Hide stop events to save cycles; on Show start plugin
Filename
TitanXP/TitanXP.lua
diff --git a/TitanXP/TitanXP.lua b/TitanXP/TitanXP.lua
index 2d41504..647e39a 100644
--- a/TitanXP/TitanXP.lua
+++ b/TitanXP/TitanXP.lua
@@ -76,12 +76,7 @@ function TitanPanelXPButton_OnLoad(self)
 			DisplayOnRightSide = false,
 		}
 	};
-
-	self:RegisterEvent("PLAYER_ENTERING_WORLD");
-	self:RegisterEvent("TIME_PLAYED_MSG");
-	self:RegisterEvent("PLAYER_XP_UPDATE");
-	self:RegisterEvent("PLAYER_LEVEL_UP");
-	self:RegisterEvent("CHAT_MSG_COMBAT_XP_GAIN");
+--	self:RegisterEvent("PLAYER_ENTERING_WORLD");
 end

 --[[
@@ -93,7 +88,22 @@ end
 --        added to the panel
 -- **************************************************************************
 --]]
-function TitanPanelXPButton_OnShow()
+function TitanPanelXPButton_OnShow(self)
+	if (not self.sessionTime) then -- initial login / PEW
+		self.sessionTime = time();
+	end
+	if (not self.initXP) then -- initial login / PEW
+		self.initXP = UnitXP("player");
+		self.accumXP = 0;
+		self.sessionXP = 0;
+		self.startSessionTime = time();
+		lastXP = self.initXP;
+	end
+	self:RegisterEvent("TIME_PLAYED_MSG");
+	self:RegisterEvent("PLAYER_XP_UPDATE");
+	self:RegisterEvent("PLAYER_LEVEL_UP");
+	self:RegisterEvent("CHAT_MSG_COMBAT_XP_GAIN");
+
 	TitanPanelXPButton_SetIcon();
 	found = nil;
 	if not TitanPanelXPButton_ButtonAdded then
@@ -103,7 +113,12 @@ function TitanPanelXPButton_OnShow()
 end


-function TitanPanelXPButton_OnHide()
+function TitanPanelXPButton_OnHide(self)
+	self:UnregisterEvent("TIME_PLAYED_MSG");
+	self:UnregisterEvent("PLAYER_XP_UPDATE");
+	self:UnregisterEvent("PLAYER_LEVEL_UP");
+	self:UnregisterEvent("CHAT_MSG_COMBAT_XP_GAIN");
+
 	if (TitanPanelSettings) then
 		for i = 1, table.getn(TitanPanelSettings.Buttons) do
 			if(TitanPanelSettings.Buttons[i] == TITAN_XP_ID) then
@@ -124,28 +139,22 @@ end
 -- **************************************************************************
 --]]
 function TitanPanelXPButton_OnEvent(self, event, a1, a2, ...)
+---[[
+print("XP _OnEvent"
+.." "..tostring(event)..""
+)
+--]]
 	if (event == "PLAYER_ENTERING_WORLD") then
-		if (not self.sessionTime) then
-			self.sessionTime = time();
-		end
-		if (not self.initXP) then
-			self.initXP = UnitXP("player");
-			self.accumXP = 0;
-			self.sessionXP = 0;
-			self.startSessionTime = time();
-			lastXP = self.initXP;
-		end
 	elseif (event == "TIME_PLAYED_MSG") then
 		-- Remember play time
 		self.totalTime = a1;
 		self.levelTime = a2;
 	elseif (event == "PLAYER_XP_UPDATE") then
-		if (not self.initXP) then
-			self.initXP = UnitXP("player");
-			self.accumXP = 0;
-			self.sessionXP = 0;
-			self.startSessionTime = time();
-		end
+		self.initXP = UnitXP("player");
+		self.accumXP = 0;
+		self.sessionXP = 0;
+		self.startSessionTime = time();
+
 		XPGain = UnitXP("player") - lastXP;
 		lastXP = UnitXP("player");
 		if XPGain < 0 then XPGain = 0 end
@@ -544,9 +553,12 @@ local function Create_Frames()
 --	TitanPanelButton_OnLoad(window); -- Titan XML template calls this...

 	window:SetScript("OnShow", function(self)
-		TitanPanelXPButton_OnShow()
+		TitanPanelXPButton_OnShow(self)
 		TitanPanelButton_OnShow(self)
 	end)
+	window:SetScript("OnHide", function(self)
+		TitanPanelXPButton_OnHide(self)
+	end)
 	window:SetScript("OnEvent", function(self, event, ...)
 		TitanPanelXPButton_OnEvent(self, event, ...)
 	end)