Quantcast

* Delay hooking a bit.

Christopher Rosell [07-07-10 - 14:28]
* Delay hooking a bit.
* Fix bug caused by names with dashes.
Filename
Moniker.lua
Moniker.toc
diff --git a/Moniker.lua b/Moniker.lua
index d5b8a31..8a56db4 100644
--- a/Moniker.lua
+++ b/Moniker.lua
@@ -1,8 +1,8 @@
-local addon = CreateFrame'Frame'
+local addon = CreateFrame"Frame"
 local addonname = ...

+local loaddelay = 1.5
 local origs = {}
-local origname = ""

 UnitPopupButtons["BN_RENAME"] = { text = PET_RENAME, dist = 0 }
 table.insert(UnitPopupMenus["BN_FRIEND"], 7, "BN_RENAME")
@@ -34,7 +34,7 @@ local function ReplaceText(frame)
 		if not text then return end

 		for orig, nick in pairs(MonikerDB) do
-			if text:find(orig) then
+			if text:find(orig, nil, true) then
 				return frame:SetText(text:replace(orig, nick))
 			end
 		end
@@ -44,7 +44,7 @@ end
 local function AddMessage(self, text, ...)
 	if text:find"You are now in a conversation with" then
 		for orig, nick in pairs(MonikerDB) do
-			if text:find(orig) then
+			if text:find(orig, nil, true) then
 				text = text:replace(orig, nick)
 			end
 		end
@@ -60,46 +60,7 @@ local function HookChatFrame(cf)
 	end
 end

-StaticPopupDialogs["BN_RENAME_FRIEND"] = {
-	text = "Enter desired name of RealID friend or leave it empty if you want to use the original name",
-	button1 = ACCEPT,
-	button2 = CANCEL,
-	hasEditBox = 1,
-	hideOnEscape = 1,
-	timeout = 0,
-	exclusive = 1,
-	whileDead = 1,
-
-	OnAccept = function(self)
-		local name = self.editBox:GetText()
-		UpdateName(origname, name)
-	end,
-
-	EditBoxOnEnterPressed = function(self)
-		local parent = self:GetParent()
-		local name = parent.editBox:GetText()
-
-		UpdateName(origname, name)
-		parent:Hide()
-	end,
-
-	EditBoxOnEscapePressed = function(self)
-		self:GetParent():Hide()
-	end,
-
-	OnShow = function(self)
-		local name = MonikerDB[origname] or ""
-		self.editBox:SetText(name)
-		self.editBox:SetFocus()
-	end,
-}
-
-addon:RegisterEvent"ADDON_LOADED"
-addon:SetScript("OnEvent", function(self, e, addon)
-	if addon ~= addonname then return end
-
-	MonikerDB = MonikerDB or {}
-
+local function HookFunctions()
 	-- Hook tooltips
 	FriendsTooltip:HookScript("OnShow", function(self)
 		ReplaceText(FriendsTooltipHeader)
@@ -169,7 +130,7 @@ addon:SetScript("OnEvent", function(self, e, addon)
 	end)

 	for i = 1, NUM_CHAT_WINDOWS do
-		HookChatFrame(_G['ChatFrame'..i])
+		HookChatFrame(_G["ChatFrame"..i])
 	end

 	-- Handle right click menu
@@ -181,9 +142,60 @@ addon:SetScript("OnEvent", function(self, e, addon)
 			local pid, firstname, lastname = BNGetFriendInfoByID(dropdown.presenceID)
 			local name = firstname.." "..lastname

-			origname = name
+			StaticPopup_Show("BN_RENAME_FRIEND", name, nil, name)
+		end
+	end)
+
+	FriendsFrame_Update()
+end
+
+StaticPopupDialogs["BN_RENAME_FRIEND"] = {
+	text = "Set a nickname for %s",
+	button1 = ACCEPT,
+	button2 = CANCEL,
+	hasEditBox = 1,
+	hideOnEscape = 1,
+	timeout = 0,
+	exclusive = 1,
+	whileDead = 1,
+
+	OnAccept = function(self)
+		local name = self.editBox:GetText()
+		UpdateName(self.data, name)
+	end,
+
+	OnShow = function(self)
+		local name = MonikerDB[self.data] or ""
+		self.editBox:SetText(name)
+		self.editBox:SetFocus()
+	end,
+
+	EditBoxOnEnterPressed = function(self)
+		local parent = self:GetParent()
+		local name = parent.editBox:GetText()
+
+		UpdateName(parent.data, name)
+		parent:Hide()
+	end,
+
+	EditBoxOnEscapePressed = function(self)
+		self:GetParent():Hide()
+	end,
+}
+
+
+addon:RegisterEvent"ADDON_LOADED"
+addon:SetScript("OnEvent", function(self, ev, addon)
+	if addon ~= addonname then return end
+
+	MonikerDB = MonikerDB or {}

-			StaticPopup_Show"BN_RENAME_FRIEND"
+	local elapsed = 0
+	self:SetScript("OnUpdate", function(self, e)
+		elapsed = elapsed + e
+		if elapsed > loaddelay then
+			HookFunctions()
+			self:SetScript("OnUpdate", nil)
 		end
 	end)
 end)
diff --git a/Moniker.toc b/Moniker.toc
index ceebf93..c31b781 100644
--- a/Moniker.toc
+++ b/Moniker.toc
@@ -1,13 +1,13 @@
 ## Interface: 30300
 ## Title: Moniker
 ## Notes: Give your RealID friends nicknames
-## Version: 1.3
+## Version: 1.4
 ## Author: Christopher Rosell
 ## X-Email: chrippa@gmail.com
 ## X-Category: Social

 ## LoadManagers: AddonLoader
-## X-LoadOn-Always: true
+## X-LoadOn-Always: delayed

 ## SavedVariables: MonikerDB