v1.1 Bug fix. Better handling of events

His Dad [03-21-15 - 01:17]
v1.1 Bug fix. Better handling of events
Filename
GRID.lua
ResearchTimer.lua
doc/changelog.txt
doc/description.txt
diff --git a/GRID.lua b/GRID.lua
index bf6a896..debdb7f 100644
--- a/GRID.lua
+++ b/GRID.lua
@@ -9,6 +9,7 @@ function RT.GRID_Remove_Expired()					--- Prevent display for completely expired
 				RT.SV.data.Craft[k][2].Simu_craft == 0))
 			then
 				RT.SV.data.Craft[k] = nil
+				if RT.debug then d("Expired char" .. k) end
 			end
 		end
 end
diff --git a/ResearchTimer.lua b/ResearchTimer.lua
index a8287e3..138d8de 100644
--- a/ResearchTimer.lua
+++ b/ResearchTimer.lua
@@ -5,11 +5,13 @@ RT = {
 	name = "ResearchTimer",
 	font = "ZoFontGame",
 	cmdsetup = "/rt",
-	version = "v1",
+	version = "v1.1",
 	playername = "",
 	Init_done = false,
 	UI = {},
-	SV={}
+	options = {},
+	SV={},
+	debug = false
 }


@@ -23,6 +25,7 @@ function RT.Collect_Data()
 	RT.Info_Research(CRAFTING_TYPE_BLACKSMITHING, 0)
 	RT.Info_Research(CRAFTING_TYPE_CLOTHIER, 1)
 	RT.Info_Research(CRAFTING_TYPE_WOODWORKING, 2)
+	if RT.debug then d("RT: Collected Data") end
 end


@@ -65,7 +68,7 @@ end

 -- for each character, find the research completing soonest and set RT.SV.data.Craft[Char].shortest
 function RT.Set_Shortest()
-	local char,craft,shortest, remaining,v
+	local shortest, remaining
 	for char,_ in pairs (RT.SV.data.Craft) do

 		shortest = 9999999999
@@ -80,8 +83,26 @@ function RT.Set_Shortest()
 		RT.SV.data.Craft[char].shortest = shortest
 	end
 end
+function RT.Update()
+	RT.GRID_Update(GetTimeStamp())
+	RT.GRID_Sort()
+end
+
+function RT.UpdateOnTimer()
+	if (RT.UI.GRID_TLW:IsHidden()) then return; end		-- Don't waste effort while its hidden.
+	RT.Update()
+end

-function RT.Research_started()  -- Triggered by Event
+function RT.Research_Changed(eventCode)  -- Triggered by Event, action is event code
+		if RT.debug then
+			local eventstr = "UNKNOWN EVENT"
+			if eventCode == EVENT_SMITHING_TRAIT_RESEARCH_STARTED
+			then eventstr = "EVENT_SMITHING_TRAIT_RESEARCH_STARTED"
+			elseif eventCode == EVENT_SMITHING_TRAIT_RESEARCH_COMPLETED
+			then eventstr = "EVENT_SMITHING_TRAIT_RESEARCH_COMPLETED"
+			end
+			d("RT: Research Change Event   " .. eventstr)
+		end
 		-- erase all data for this character
 		RT.SV.data.Craft[RT.playername] = {}
 		-- Collect new data
@@ -92,10 +113,46 @@ function RT.Research_started()  -- Triggered by Event
 		end
 		RT.Set_Shortest()
 		RT.UI.GRID_WD[RT.playername].label:SetText(string.upper(RT.playername)) -- remove highlighting
+		RT.Update()
+end
+
+
+
+function RT.Activated()
+	if RT.debug then d("RT: Player Activated")
+	end
+	RT.Collect_Data()		-- Collection of data is just NOT RELIABLE at load phase. Do it again
 end

+function RT.toggleGRID()
+	if (RT.UI.GRID_TLW:IsHidden()) then
+	RT.GRID_Sort()
+	RT.UI.GRID_TLW:SetHidden(false)
+	else
+	RT.UI.GRID_TLW:SetHidden(true)
+	end
+end

-function RT.Init(eventCode, addOnName)
+function RT.CommandText_setup(option)
+    option = string.lower(option)
+    RT.options = { string.match(option,"^(%S*)%s*(.-)$") }
+
+	if option == "" then
+		RT.toggleGRID()
+	elseif RT.options.read ~= nil then		-- cause a reread of the data without sorting
+	    d("RT: Running Collect_Data()")
+		RT.Collect_Data()
+	elseif RT.options.debug ~= nil then
+			if RT.debug
+			then	d("debug off")
+			else	d("debug on")
+			end
+			RT.debug = not RT.debug
+	end     -- #RT.options
+
+end
+
+function RT.Init(_, addOnName)

 	if(addOnName == RT.name) then
 	  RT.playername = GetUnitName("player")
@@ -107,50 +164,31 @@ function RT.Init(eventCode, addOnName)
 			 RT.SV.data.Craft[RT.playername]= {}
 		else RT.GRID_Remove_Expired()
 	  end
-		RT.InitialiseLanguage()
+	  RT.InitialiseLanguage()

 	    -- Slash commands
 	  SLASH_COMMANDS[RT.cmdsetup] = RT.CommandText_setup

-
+
 	    -- Create Keybinds
 	  ZO_CreateStringId("SI_BINDING_NAME_RT_toggleGRID", "Toggle Display")

-		local timestamp = GetTimeStamp()

 		RT.SV.data.Craft[RT.playername] = {}

 		RT.Collect_Data()
 		RT.GRID_Create()
-		RT.GRID_Update(timestamp)	--Manual Update
-		RT.GRID_Sort()
+		RT.Update()	--Manual Update
+
 		RT.Init_done = true

 		-- Only after initialised do we hook any other events.
-		EVENT_MANAGER:RegisterForEvent(RT.name, EVENT_SMITHING_TRAIT_RESEARCH_STARTED, RT.Research_started)
-		EVENT_MANAGER:RegisterForUpdate(RT.name, 1000, RT.Update)
+		EVENT_MANAGER:RegisterForEvent(RT.name, EVENT_SMITHING_TRAIT_RESEARCH_STARTED, RT.Research_Changed)
+		EVENT_MANAGER:RegisterForEvent(RT.name, EVENT_SMITHING_TRAIT_RESEARCH_COMPLETED, RT.Research_Changed)
+		EVENT_MANAGER:RegisterForEvent(RT.name, EVENT_PLAYER_ACTIVATED, RT.Activated)
+		EVENT_MANAGER:RegisterForUpdate(RT.name, 1000, RT.UpdateOnTimer)
 	end
 end
-function RT.Update()
-	if (RT.UI.GRID_TLW:IsHidden()) then return; end		-- Don't waste effort while its hidden.
-	local timestamp = GetTimeStamp()
-	RT.GRID_Update(timestamp)
-	RT.GRID_Sort()
-end
-
-
-function RT.toggleGRID()
-	if (RT.UI.GRID_TLW:IsHidden()) then
-	RT.GRID_Sort()
-	RT.UI.GRID_TLW:SetHidden(false)
-	else
-	RT.UI.GRID_TLW:SetHidden(true)
-	end
-end
-
-function RT.CommandText_setup()
-		RT.toggleGRID()
-end



diff --git a/doc/changelog.txt b/doc/changelog.txt
index f47a3a9..927403f 100644
--- a/doc/changelog.txt
+++ b/doc/changelog.txt
@@ -1,2 +1,5 @@
-2015-03-16 v1
+2015-03-16 v1.0
 	Basic functionality complete
+2015-03-16 v1.1
+
+
diff --git a/doc/description.txt b/doc/description.txt
index 2118e92..edc7480 100644
--- a/doc/description.txt
+++ b/doc/description.txt
@@ -1,7 +1,7 @@
 This addon keeps track of when research items will complete for all your characters.
 It is similar to Ato's "Craft Research Timer", however it does a lot less.
 The dialog isn't configurable and no, you can't move it.
-No, it doesn't pop up alerts.  (Wykkeds' taskbar can help with this)
+No, it doesn't pop up alerts.  (Wykkyds' taskbar can help with this)

 You pop it up for a look in game, plan and put it away.