WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Using LUA but having problems with making a button... (https://www.wowinterface.com/forums/showthread.php?t=47740)

Lazare 08-30-13 10:11 PM

Using LUA but having problems with making a button...
 
that does not use UIPanelButtonTemplate for a background. Would love to have a button that is just the text itself or an icon or "|TInterface\\TargetingFrame\\UI-RaidTargetingIcon_6:14:14|t" as an example.
I just like clean interfaces and believe that having just the raid icon itself against the background of the frame would be great. I could probably do it in XML but Phanx pointed out everything should be done in Lua, so I shifted my whole project to that Lei ShenCoordinator 2.
The buttons that I currently have use the following code:
Lua Code:
  1. local function CreateBasicButton(parent, name, text, tooltipText)
  2.     local button = CF("Button", name, parent, "SecureActionButtonTemplate, UIPanelButtonTemplate")
  3.     button.text = _G[button:GetName().."Text"]
  4.     button.text:SetText(text)
  5.     button:SetWidth(30)
  6.     button:SetHeight(30)
  7.     button:SetScript("OnEnter", function(self)
  8.       GameTooltip:SetOwner(self, "ANCHOR_TOP")
  9.       GameTooltip:AddLine(tooltipText, 0, 1, 0.5, 1, 1, 1)
  10.       GameTooltip:Show()
  11.     end)
  12.     button:SetScript("OnLeave", function(self) GameTooltip:Hide() end)
  13.     return button
  14.   end
  15.  
  16.   --create world marker buttons
  17. local button = CreateBasicButton(LeiShenCoordinator, "Button", "|TInterface\\TargetingFrame\\UI-RaidTargetingIcon_6:14:14|t", "Raid Marker 1\nRight-click to clear")
  18.     button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
  19.     button:SetAttribute("type", "macro")
  20.     button:SetAttribute("macrotext", format("/wm 1"))
  21.     --if not previousButton then
  22.       button:SetPoint("TOPRIGHT", LeiShenCoordinator, -120, -80)
  23.       button:SetAttribute("type2", "macro")
  24.     button:SetAttribute("macrotext2", format("/cwm 1"))

Thanks in advance!

Phanx 08-30-13 11:25 PM

So just create the button without that template, and create icons and/or font strings as desired:

Code:

-- Create the button:
local button = CreateFrame("Button", name, parent, "SecureActionButtonTemplate")
button:SetSize(30, 30)

-- Add the icon:
local icon = button:CreateTexture(nil, "ARTWORK")
icon:SetAllPoints(true)
icon:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcon_6")
button.icon = icon

-- Set scripts etc.

Code:

-- Create the button:
local button = CreateFrame("Button", name, parent, "SecureActionButtonTemplate")
button:SetSize(15, 45)

-- Add a font string:
local text = button:CreateFontString(nil, "OVERLAY")
text:SetPoint("CENTER")
text:SetText("Blue Square")
button.text = text

-- Set scripts etc.

On a side note, there is no benefit to upvaluing CreateFrame, since if you are calling it enough to matter, you're doing something horribly wrong; and when you're upvaluing global functions, for the sake of readability you should keep the full name, eg:

Code:

-- Do this:
local CreateFrame = CreateFrame

-- NOT this:
local CF = CreateFrame

Addons aren't macros or text messages with limited character space. You don't need to condense everything to the fewest possible characters. Imagine that you take 6 months off from coding, and then come back and read your code -- if you'd be confused by your variable name, you shouldn't use that variable name in the first place. This is especially true for API functions, which already have names; giving them different names doesn't provide any benefit, but does create confusion.

SDPhantom 08-31-13 01:14 AM

Here's my take on creating better code. I kept the idea of using a function to generate the buttons dynamically, yet improved on its operation. First of all, the buttons share the same OnEnter and OnLeave scripts to minimize on excessive memory usage. The function arguments changed to incorporate native support for displaying a texture instead of using a FontString to display them. You can still put text on the button if you choose to.

Lua Code:
  1. local CreateButton; do-- Prototype for function
  2. --  Tooltip Handlers
  3.     local function OnEnter(self)
  4.         if self.Tooltip then
  5.             GameTooltip:SetOwner(self,"ANCHOR_TOP");
  6.             GameTooltip:AddLine(self.Tooltip,0,1,0.5,1,1,1);
  7.             GameTooltip:Show();
  8.         end
  9.     end
  10.     local function OnLeave(self) if GameTooltip:IsOwned(self) then GameTooltip:Hide(); end end
  11.  
  12. --  Button Generator (this will be assigned to the upvalue noted as a function prototype)
  13.     function CreateButton(parent,name,texture,text,tooltip)
  14.         tooltip=tooltip or text;--  If no tooltip, use button text
  15.  
  16. --      Create our button
  17.         local btn=CreateFrame("Button",name,parent,"SecureActionButtonTemplate");
  18.         btn:RegisterForClicks("AnyUp");--   Register all buttons
  19.         btn:SetSize(30,30);
  20.  
  21. --      Setup button text
  22.         btn:SetNormalFontObject("GameFontNormalSmall");
  23.         btn:SetHighlightFontObject("GameFontHighlightSmall");
  24.         btn:SetDisabledFontObject("GameFontDisableSmall");
  25.         btn:SetText(text);
  26.  
  27. --      Setup button's backgorund, you can use :SetNormalTexture() and other functions to set state-based textures
  28.         local tex=btn:CreateTexture(nil,"BACKGROUND");
  29.         tex:SetAllPoints(btn);
  30.         tex:SetTexture(texture);
  31.         btn.Texture=tex;
  32.  
  33. --      Register handlers
  34.         btn:SetScript("OnEnter",OnEnter);
  35.         btn:SetScript("OnLeave",OnLeave);
  36.         btn.Tooltip=tooltip;
  37.  
  38. --      Return our button
  39.         return btn;
  40.     end
  41. end
  42.  
  43. local button=CreateButton(LeiShenCoordinator,"LeiShenCoordinatorWorldMarkerButton1","Interface\\TargetingFrame\\UI-RaidTargetingIcon_6",nil,"Raid Marker 1\nRight-click to clear");
  44. button:SetPoint("TOPRIGHT",-120,-80);-- Anchors always default to an object's parent
  45. button:SetAttribute("type","macro");--  "type" without a number will handle all clicks unless a more specific attribute is found
  46. button:SetAttribute("macrotext1",SLASH_WORLD_MARKER1.." 1");--      Adds world marker 1 on left-click
  47. button:SetAttribute("macrotext2",SLASH_CLEAR_WORLD_MARKER1.." 1");--    Clears world marker 1 on right-click

Something Phanx didn't mention that should be covered. You should NEVER use a generic name such as "Button" for any UI object. This is the same rule that these names should not be used for any sort of global variable. Global collisions is one of the many reasons an addon can unexpectedly break. This is one reason why most addons use their name as part of any global object it creates. For example, I had your button named "LeiShenCoordinatorWorldMarkerButton1" to guarantee its uniqueness.

Lazare 08-31-13 06:57 PM

Thank you both for your help and comments. Will tryout your suggestions.

Clamsoda 08-31-13 07:06 PM

Just to save you the potential headache, line 3 in SDPhantom's code should read:

Lua Code:
  1. local function OnEnter(self)

instead of

Lua Code:
  1. local function OnEnter(delf)

Lazare 08-31-13 08:20 PM

I did catch that, thanks!

SDPhantom 09-03-13 10:19 PM

Quote:

Originally Posted by Clamsoda (Post 283707)
Just to save you the potential headache, line 3 in SDPhantom's code should read:

Lua Code:
  1. local function OnEnter(self)

instead of

Lua Code:
  1. local function OnEnter(delf)

D'oh! Fixed now. :rolleyes:


All times are GMT -6. The time now is 06:33 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI