Thread Tools Display Modes
11-27-16, 02:44 AM   #1
Walkerbo
A Cobalt Mageweaver
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 233
Adding buttons to player dropdown menu

Hello all
I am looking for a bit of help understanding how to add buttons correctly to the right click dropdown menu of a targeted player.
Here is my current code to add the buttons;
Code:
local playerMenuList = {"PLAYER","RAID_PLAYER","PARTY","FRIEND"}

UnitPopupButtons["GREEN_BUTTON"] = {text = "GREEN"}
UnitPopupButtons["YELLOW_BUTTON"] = {text = "YELLOW"}
UnitPopupButtons["RED_BUTTON"] = {text = "RED"}
}

for k,v in pairs(playerMenuList) do	
	table.insert(UnitPopupMenus[v],"GREEN_BUTTON")
	table.insert(UnitPopupMenus[v],"YELLOW_BUTTON")
	table.insert(UnitPopupMenus[v],"RED_BUTTON")
end

local function testPlayerMenuButton(self)
	if self.value == "GREEN_BUTTON" then
		print("GREEN_BUTTON clicked")
	elseif self.value == "YELLOW_BUTTON" then
		print("YELLOW_BUTTON clicked")
	elseif self.value == "RED_BUTTON" then
		print("RED_BUTTON clicked")
	end
end

hooksecurefunc("UnitPopup_OnClick",testPlayerMenuButton)
I would like to only display one of the buttons based on a condition such as;
Code:
if xzy=="Red" then
RED_BUTTON:Hide()
YELLOW_BUTTON:Hide()
GREEN_BUTTON:Show()
elseif xyz=="Yellow" then
RED_BUTTON:Hide()
GREEN_BUTTON:Hide()
YELLOW_BUTTON:Show()
else
GREEN_BUTTON:Hide()
YELLOW_BUTTON:Hide()
RED_BUTTON:Show()
end
Any help would would be great.
  Reply With Quote
11-28-16, 02:59 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
When asking for help with code, please post your actual code or at least, if you really insist on writing new code to post as an example, code you've verified actually works. The code you posted (a) has a syntax error because of the extra } on line 6 and (b) doesn't actually do anything anyway.

That said, you should really avoid doing what you're doing, since you automatically taint everything already in the popup (thus preventing the use of menu entries to target the unit, set your focus, etc.) and most likely the entire dropdown menu system for the entire default UI (and all addons that use it). If you want suggestions on what you should do instead, you'll have to post more details about what you're actually trying to do (eg. what you want the menu entries to do).

If you don't care about breaking dropdown menus for the whole UI, and want to go ahead with what you're doing:

#1 -- Don't overwrite the UnitPopupMenus["PLAYER"] table, as other addons may also be adding stuff to it. Just add your extra entries to the one that already exists. If you want to add them somewhere other than at the end, pass the optional position arg to table.insert to insert them at specific positions in the list.

#2 -- Use hooksecurefunc on the UnitPopup_HideButtons function to control the visibility of your custom menu entries each time the menu is shown.

#3 -- While the default UI handles changing other properties of each menu entry (eg. different text, disabled but still visible) in UnitPopup_Show, you should not do this, or your changes will be made after the menu entries have been rendered into the UI, and won't actually affect anything until the next time the menu is shown. Do that stuff in your UnitPopup_HideButtons hook (#2 above).
__________________
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.
  Reply With Quote
11-29-16, 01:33 AM   #3
Walkerbo
A Cobalt Mageweaver
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 233
Hi Phanx

Thanks for the reply.

Too say that I am a noob when it comes to coding is a massive understatement; I am self-learning and am generally embarrassed that my code is guaranteed to be messy, nasty and terribly inefficient.

I want to create a button that will add my current target details to a list if it is not already recorded, and a button that will remove my current target if it is already listed.
I want the buttons to only display based on that condition.
I do want to add buttons to the menu in a way that does not taint the UI.

Here is my current code, (tested in game using the hack addon).
Code:
local TestDropdownMenuList = {"PLAYER","RAID_PLAYER","PARTY","TARGET","FRIEND",}

local function menuButtonFunction(self)	
	if self.value == "TEST_BUTTON_A" then
		-- add target to list function
		print("Target added to list")		
		elseif self.value == "TEST_BUTTON_B" then		
		-- remove tagerget from list 
		print("Target removed from list")
	end
end

UnitPopupButtons["TEST_BUTTON_A"] = {
	text = "TEST_BUTTON_A",
	dist = 0,
	icon = "Interface\\TargetingFrame\\UI-RaidTargetingIcon_5.png",
}

UnitPopupButtons["TEST_BUTTON_B"] = {
	text = "TEST_BUTTON_B",
	dist = 0,
	icon = "Interface\\TargetingFrame\\UI-RaidTargetingIcon_8.png",
}

for k,v in pairs(TestDropdownMenuList) do		
	table.insert(UnitPopupMenus[v],"TEST_BUTTON_A")
	table.insert(UnitPopupMenus[v],"TEST_BUTTON_B")
	--	table.insert(UnitPopupMenus[v], "SUBSECTION_SEPARATOR")
end

hooksecurefunc("UnitPopup_OnClick",menuButtonFunction)
Thanks for your help.
  Reply With Quote
11-29-16, 08:58 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Walkerbo View Post
I do want to add buttons to the menu in a way that does not taint the UI.
There is no such way. Adding items to unit popup menus taints the UI, period.

Originally Posted by Walkerbo View Post
I want to create a button that will add my current target details to a list if it is not already recorded, and a button that will remove my current target if it is already listed.
I want the buttons to only display based on that condition.
There are plenty of ways you could achieve that without tainting the popup menu:
  • Put the toggle button on your list frame instead
  • Add a button near the target frame
  • Add a button on your minimap / DataBroker bar
  • Use a key binding
  • Use a slash command
  • etc.
__________________
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.
  Reply With Quote
12-02-16, 04:57 PM   #5
Walkerbo
A Cobalt Mageweaver
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 233
Hi Phanx

Thanks for the reply.

Looks like I will have to rethink how the addon will work.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Adding buttons to player dropdown menu


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off