Quantcast
Looking for lua code example of how to get a button to highlight on push down - WoWInterface
Thread Tools Display Modes
08-26-21, 11:45 PM   #1
Zimzarina
A Murloc Raider
Join Date: Aug 2019
Posts: 6
Looking for lua code example of how to get a button to highlight on push down

For an addon I am working on I have created a icon button similar to other profession buttons like cooking and leatherworking. I am able to SetNormalTexture and SetHighlightTexture fine and it looks as expected in those cases. However, when I try to SetPushedTexture, "ADD" I don't get the nice appearance I expect I get basically a transparent button with a edge highlight. What I want is to find an example code of how to create a button that uses the same highlight and pushed button effects that regular ActionBars do. Thanks!

Example code below that shows what I am asking about.

local NewButtonEventFrame, NewButtonEvents, NewButtonFunctionName = CreateFrame("Frame"), {}

function NewButtonEvents:PLAYER_ENTERING_WORLD(isInitialLogin, isReloadingUi)
if not NewButton then
NewButton = CreateFrame("Button", "NewButton", UIParent)
NewButton:SetWidth(40)
NewButton:SetHeight(40)
NewButton:SetPoint("CENTER",0,0)
NewButton:SetNormalTexture("Interface\\Icons\\INV_Misc_ArmorKit_17")
NewButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square","ADD",0)
NewButton:SetPushedTexture("Interface\\Icons\\INV_Misc_ArmorKit_17")
NewButton:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress","ADD")
NewButton:SetScript("OnClick", function() NewButtonClick() end)
end
if NewButton then NewButton:Show() end
end

function NewButtonClick()
DEFAULT_CHAT_FRAME:AddMessage("Click")
end

NewButtonEventFrame:SetScript("OnEvent", function(self, event, ...)
NewButtonEvents[event](self, ...)
end)
for NewButtonEventName, NewButtonFunctionName in pairs(NewButtonEvents) do
NewButtonEventFrame:RegisterEvent(NewButtonEventName)
end

Last edited by Zimzarina : 08-27-21 at 02:41 PM.
  Reply With Quote
08-28-21, 05:58 AM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,050
A few notes:

I wouldn't consider NewButton a unique enough name to occupy the global namespace. It's common practice to prefix the name of your addon to any global (including frame names) you create to prevent collisions with other addons or even the DefaultUI.

In most cases, you can create your frames in the main part of your code. You don't need to wait for any special event to do so.

If both offsets for UIObject:SetPoint() are zero, they can be omitted. (If either are non-zero, you must provide both.)

Button:SetPushedTexture() replaces the normal texture when the button is in that state. To have a persistent normal texture, you'll have to create another texture object to show it.

The push texture for that path doesn't need alpha blending. Alpha blending converts gradients of white/black depending on the mode to transparent. If a texture already contains transparency, this causes the image to do unintentional things.

You don't need to call UIObject:Show() after creating an object. It is already shown by default providing it has a valid anchor and size. (Size can also be defined by setting more than one anchor if desired.)

You should use print() instead of DEFAULT_CHAT_FRAME:AddMessage(). This lets other addons redirect through their own print handler without causing taint issues.



With these in mind, here's an example.
Lua Code:
  1. local ExampleButton=CreateFrame("Button",nil,UIParent);
  2. ExampleButton:SetPoint("CENTER");
  3. ExampleButton:SetSize(40,40);
  4. ExampleButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square","ADD");
  5. ExampleButton:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress");
  6.  
  7. local ExampleButtonIcon=ExampleButton:CreateTexture(nil,"BACKGROUND");
  8. ExampleButtonIcon:SetAllPoints(ExampleButton);
  9. ExampleButtonIcon:SetTexture("Interface\\Icons\\INV_Misc_ArmorKit_17");
  10.  
  11. ExampleButton:SetScript("OnClick",function(self,button)
  12.     print("Button Clicked");
  13. end);
PS: ActionButtons do some funky thing with setting a custom size for their normal texture. This texture is the empty box you see if you have them shown on your multi-bars. If you look close enough, you might see a double-border on occupied ones. This is a side effect of that implementation. In the example above, this was removed for the sake of simplicity.
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 08-28-21 at 11:05 PM.
  Reply With Quote
08-28-21, 12:49 PM   #3
Zimzarina
A Murloc Raider
Join Date: Aug 2019
Posts: 6
Thanks for the great feedback along with all the ideas and suggestion for code improvement! This writeup was way more detailed and helpful then I expected to receive. I really appreciate all extra effort and guidance you went to. Thanks!
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Looking for lua code example of how to get a button to highlight on push down

Thread Tools
Display Modes

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