View Single Post
11-02-16, 12:30 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
That will work... but it will only trigger when you either (a) move from one zone map to another or (b) enter or leave an instance. To make it trigger only when you change from "leader of a raid" to "not leader of a raid" you'd need to (a) listen for a different event and (b) keep track of which state you were in (to avoid triggering it repeatedly if the state wasn't changing). Example:

Code:
f:RegisterEvent("GROUP_ROSTER_UPDATE")

local isLeader = false

f:SetScript("OnEvent", function()
	local isLeaderNow = IsInRaid() and UnitIsGroupLeader("player")
	if IsLeaderNow == isLeader then return end
	isLeader = isLeaderNow

	if isLeaderNow then
		f:Show()
		f.Text:SetText("check loot method!")
	else
		f:Hide()
	end
end)
Other stuff:

When you create a frame that's visible on screen, it must be parented to the UIParent, or to something else whose parentage traces back to the UIParent. Otherwise, it will (a) not be hidden with Alt-Z and (b) won't respect the user's UI scale setting.
Code:
local f = CreateFrame("Frame", nil, UIParent)
You can just set the frame's text once when you create it. Since you only ever show one message, there's no need to set and un-set it when you show and hide the frame. While the frame is hidden, you won't see the message either!

If you want to be able to click on the frame, you need to turn on mouse interactivity:
Code:
f:EnableMouse(true)
If you do that, I'd recommend also resizing the frame so it's exactly the size of the message text (after you set the text):
Code:
f:SetWidth(f.Text:GetStringWidth())
f:SetHeight(f.Text:GetStringHeight())
Instead of needing to click to hide the message, you could also make it hide automatically after a certain amount of time:
Code:
f:Show()
C_Timer.After(10, function() f:Hide() end)
Or you could make a more traditional dialog-type frame with buttons. Here's an example that mimics the static popup dialog. You could just inherit from the template, but then you'd have to hide things and remove scripts you don't need, so it's better just to copy the parts you need. I've written them out here in Lua in the same order they appear in the XML, so you can follow along and see how XML is translated into Lua. Green highlights things that are specific to your addon, and not from the template.
Code:
local f = CreateFrame("Frame", "LeaderLootReminder", UIParent)
f:SetToplevel(true)
f:EnableKeyboard(true)
f:EnableMouse(true)
f:SetFrameStrata("DIALOG")
f:Hide()

f:SetSize(320, 72)

f:SetBackdrop({
	bgFile = [[Interface\DialogFrame\UI-DialogBox-Background]],
	edgeFile = [[Interface\DialogFrame\UI-DialogBox-Border]],
	tile = "true",
	insets = { left="11", right="12", top="12", bottom="11" },
	tileSize = 32,
	edgeSize = 32
})

f.text = f:CreateFontString("$parentText", "ARTWORK", "GameFontHighlight")
f.text:SetWidth(290)
f.text:SetPoint("TOP", 0, -16)
f.text:SetText("You are now a raid leader!\nPick your loot method:")

f.close = CreateFrame("Button", "$parentCloseButton", f, "UIPanelCloseButton")
f.close:SetPoint("TOPRIGHT", -3, -3)

local function Button_OnClick(self)
	local me = UnitName("player")
	SetLootMethod(self.lootType, me)
end

f.button1 = CreateFrame("Button", "$parentButton1", f, "StaticPopupButtonTemplate")
f.button1:SetPoint("BOTTOMRIGHT", f, "BOTTOM", -72, 16)
f.button1:SetText("Group Loot")
f.button1.lootType = "group"

f.button2 = CreateFrame("Button", "$parentButton2", f, "StaticPopupButtonTemplate")
f.button2:SetPoint("LEFT", f.button1, "RIGHT", 13, 0) -- (source)
f.button2:SetText("Free For All")
f.button2.lootType = "freeforall"

f.button3 = CreateFrame("Button", "$parentButton3", f, "StaticPopupButtonTemplate")
f.button3:SetPoint("LEFT", f.button2, "RIGHT", 13, 0)
f.button3:SetText("Master Looter")
f.button3.lootType = "master"

f:SetScript("OnEscapePressed", function(self)
	self:Hide()
end)

function f:SetAnchor(self)
	for i = 1, STATICPOPUP_NUMDIALOGS do
		local popup = _G["StaticPopup"..i]
		if popup:IsShown() then
			return self:SetPoint("TOP", popup, "BOTTOM")
		end
	end
	self:SetPoint("TOP", UIParent, 0, -135)
end

f:SetScript("OnShow", f.SetAnchor)
hooksecurefunc("StaticPopup_Show", f.SetAnchor)

-- Handle events here
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 11-02-16 at 12:41 AM.
  Reply With Quote