Thread Tools Display Modes
04-11-13, 01:03 PM   #1
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Help Creating Textures, Frames, etc in Lua. And some questions!

Greetings,

Okay, so i'm trying to create a complete working AddOn using lua only! I know how to create a few using .XML to make the graphics and add event handlers. But i'm not sure in lua. As an example i'm going to use BagBuddy.

Allright, first i would like to ask if this is considered readable or if it can be written in an better way:

Lua Code:
  1. -------------------
  2. --BagBuddy Frames--
  3. -------------------
  4.  
  5.     -- The parent frame to add textures and fontstrings to
  6.     local frame = CreateFrame("Frame", "BagBuddy", UIParent)
  7.     frame:SetWidth(425)
  8.     frame:SetHeight(425)
  9.     frame:SetPoint("CENTER", UIParent, "CENTER")
  10.    
  11.     -- The body texture of BagBuddy
  12.     frame.skeleton = frame:CreateTexture("BagBuddy_Skeleton", "BORDER")
  13.     frame.skeleton:SetPoint("TOPLEFT")
  14.     frame.skeleton:SetTexture("Interface\\BankFrame\\UI-BankFrame")
  15.    
  16.     -- The portrait texture of BagBuddy
  17.     frame.icon = frame:CreateTexture("BagBuddy_Icon", "BACKGROUND")
  18.     frame.icon:SetWidth(60)
  19.     frame.icon:SetHeight(60)
  20.     frame.icon:SetPoint("TOPLEFT", 7, -6)
  21.     frame.icon:SetTexture("Interface\\Icons\\INV_Misc_EngGizmos_17")
  22.     SetPortraitToTexture(frame.icon, "Interface\\Icons\\INV_Misc_EngGizmos_17")
  23.    
  24.     -- The title fontstring of BagBuddy
  25.     frame.title = frame:CreateFontString("BagBuddy_Title", "OVERLAY", "GameFontNormal")
  26.     frame.title:SetPoint("TOP", 0, -18)
  27.     frame.title:SetText("BagBuddy")
  28.    
  29.     -- The button that allows BagBuddy to be hidden
  30.     frame.close = CreateFrame("Button", "BagBuddy_Close", frame, "UIPanelCloseButton")
  31.     frame.close:SetPoint("TOPRIGHT", -22, -8)
  32.    
  33. ----------------------
  34. --BagBuddy Functions--
  35. ----------------------
  36.  
  37.     function BagBuddy_OnLoad(self)
  38.  
  39.     end
  40.  
  41.     function BagBuddy_MakeMovable(self, motion)
  42.         self:EnableMouse(true)
  43.         self:SetMovable(true)
  44.         self:SetClampedToScreen(true)
  45.         self:RegisterForDrag("LeftButton")
  46.         self:SetScript("OnDragStart", self.StartMoving)
  47.         self:SetScript("OnDragStop", self.StopMovingOrSizing)
  48.     end
  49.  
  50.     BagBuddy_MakeMovable(frame)

the function BagBuddy_OnLoad(self) is empty because i'm not sure on how i would add 28 x buttons to the frame without using an XML template.

The template for the original BagBuddy looks like this:

XML Code:
  1. <Button name="BagBuddyItemTemplate" inherits="SecureActionButtonTemplate" virtual="true">
  2.     <Size>
  3.       <AbsDimension x="37" y="37"/>
  4.     </Size>
  5.     <Layers>
  6.       <Layer level="BORDER">
  7.         <Texture name="$parentIconTexture" parentKey="icon"/>
  8.         <FontString name="$parentCount" parentKey="count" inherits="NumberFontNormal" justifyH="RIGHT" hidden="true">
  9.           <Anchors>
  10.             <Anchor point="BOTTOMRIGHT">
  11.               <Offset>
  12.                 <AbsDimension x="-5" y="2"/>
  13.               </Offset>
  14.             </Anchor>
  15.           </Anchors>
  16.         </FontString>
  17.       </Layer>
  18.       <Layer level="OVERLAY">
  19.         <Texture name="$parentGlow" parentKey="glow" alphaMode="ADD" file="Interface\Buttons\UI-ActionButton-Border">
  20.           <Size x="70" y="70"/>
  21.           <Anchors>
  22.             <Anchor point="CENTER"/>
  23.           </Anchors>
  24.           <Color r="1.0" g="1.0" b="1.0" a="0.6"/>
  25.         </Texture>
  26.       </Layer>
  27.     </Layers>
  28.     <Attributes>
  29.       <Attribute name="type2" type="string" value="item"/>
  30.     </Attributes>
  31.     <Scripts>
  32.       <OnEnter function="BagBuddy_Button_OnEnter"/>
  33.       <OnLeave function="BagBuddy_Button_OnLeave"/>
  34.     </Scripts>
  35.     <NormalTexture name="$parentNormalTexture" file="Interface\Buttons\UI-Quickslot2">
  36.       <Size>
  37.         <AbsDimension x="64" y="64"/>
  38.       </Size>
  39.       <Anchors>
  40.         <Anchor point="CENTER">
  41.           <Offset>
  42.             <AbsDimension x="0" y="-1"/>
  43.           </Offset>
  44.         </Anchor>
  45.       </Anchors>
  46.     </NormalTexture>
  47.     <PushedTexture file="Interface\Buttons\UI-Quickslot-Depress"/>
  48.     <HighlightTexture file="Interface\Buttons\ButtonHilight-Square" alphaMode="ADD"/>
  49.   </Button>

Using the above template i would add each button to the frame like this:

Lua Code:
  1. function BagBuddy_OnLoad(self)
  2.     self.items = {}
  3.     for idx = 1, 28 do
  4.         local item = CreateFrame("Button", "BagBuddy_Item" .. idx, self, "BagBuddyItemTemplate")
  5.         self.items[idx] = item
  6.         if idx == 1 then
  7.             item:SetPoint("TOPLEFT", 40, -73)
  8.         elseif idx == 8 or idx == 15 or idx == 22 then
  9.             item:SetPoint("TOPLEFT", self.items[idx - 7], "BOTTOMLEFT", 0, -7)
  10.         else
  11.             item:SetPoint("TOPLEFT", self.items[idx - 1], "TOPRIGHT", 12, 0)
  12.         end
  13.     end
  14. end

but this is easy because i can inherit the item's from BagBuddyItemTemplate in my XML document. How can i create something similar using lua only?

Also how do i set alphaMode="ADD" in lua?

Hope you guys understand what i'm trying to accomplish, if not, please ask me and i'll try to explain
  Reply With Quote
04-11-13, 03:09 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
We already discussed "templates" in Lua here: http://www.wowinterface.com/forums/s...ad.php?t=46184
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
04-11-13, 03:40 PM   #3
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
First of all, your code looks good, but you're making your functions global. They should either be local or part of the frame's table, like you do with the frame's regions/children.

Your BagBuddy_MakeMovable function is not needed, since you're only calling it once (at least so far). Just use the function's body, replacing 'self' with 'frame', your local variable.

OnLoad functions don't actually work in lua, because after calling CreateFrame(), the frame's (non-existing) OnLoad handler will have run already. Just put the code directly in the source file.

As for the alpha mode, that would be SetBlendMode.

Last edited by Haleth : 04-11-13 at 03:45 PM.
  Reply With Quote
04-11-13, 08:25 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
You pretty much have the framework down for transferring your template into Lua, All you really need to do is put in the code for Lua to create the objects within the loop that the template would've done.

Lua Code:
  1. local item=CreateFrame("Button","BagBuddy_Item"..idx,frame,"SecureActionButtonTemplate");
  2. item:SetSize(37,37);
  3. item:SetAttribute("type2","item");
  4. item:SetScript("OnEnter",BagBuddy_Button_OnEnter);
  5. item:SetScript("OnLeave",BagBuddy_Button_OnLeave);
  6.  
  7. item:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2");
  8. item:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress");
  9. item:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square","ADD");
  10. do local tex=item:GetNormalTexture();-- Hack to modify the existing texture object
  11.     tex:ClearAllPoints();
  12.     tex:SetPoint("CENTER",0,-1);
  13.     tex:SetSize(64,64);
  14. end
  15.  
  16. item.icon=item:CreateTexture("$parentIconTexture","BORDER");
  17.  
  18. item.count=item:CreateFontString("$parentCount","BORDER","NumberFontNormal");
  19. item.count:SetPoint("BOTTOMRIGHT",-5,2);
  20. item.count:SetJustifyH("RIGHT");
  21. item.count:Hide();
  22.  
  23. item.glow=item:CreateTexture("$parentGlow","OVERLAY");
  24. item.glow:SetPoint("CENTER");
  25. item.glow:SetSize(70,70);
  26. item.glow:SetTexture("Interface\\Buttons\\UI-ActionButton-Border");
  27. item.glow:SetBlendMode("ADD");
  28. item.glow:SetAlpha(0.6);

If there aren't any errors I missed, that should create the exact frame your template is making. Just replace the line where you create the item button with this and as Haleth suggested, move the contents of the OnLoad function to the main chunk and replace all references of self with frame.
__________________
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 : 04-11-13 at 08:31 PM.
  Reply With Quote
04-12-13, 04:43 AM   #5
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Seerah View Post
We already discussed "templates" in Lua here: http://www.wowinterface.com/forums/s...ad.php?t=46184
I'm sorry for the double post, but i had more questions for my lua code than in that topic - i should have edited the post.

Originally Posted by Haleth View Post
First of all, your code looks good, but you're making your functions global. They should either be local or part of the frame's table, like you do with the frame's regions/children.

Your BagBuddy_MakeMovable function is not needed, since you're only calling it once (at least so far). Just use the function's body, replacing 'self' with 'frame', your local variable.

OnLoad functions don't actually work in lua, because after calling CreateFrame(), the frame's (non-existing) OnLoad handler will have run already. Just put the code directly in the source file.

As for the alpha mode, that would be SetBlendMode.
Thank you for the answer! I'll make my functions local instead. I decided to use the BagBuddy_MakeMovable function as i was not sure to wether i was going to develop the bagbuddy abit and maybe add some new feutures which would maybe require a new frame - if this is not the case i will ofc add the movable scripts directly to the frame created.

Originally Posted by SDPhantom View Post
You pretty much have the framework down for transferring your template into Lua, All you really need to do is put in the code for Lua to create the objects within the loop that the template would've done.

Lua Code:
  1. local item=CreateFrame("Button","BagBuddy_Item"..idx,frame,"SecureActionButtonTemplate");
  2. item:SetSize(37,37);
  3. item:SetAttribute("type2","item");
  4. item:SetScript("OnEnter",BagBuddy_Button_OnEnter);
  5. item:SetScript("OnLeave",BagBuddy_Button_OnLeave);
  6.  
  7. item:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2");
  8. item:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress");
  9. item:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square","ADD");
  10. do local tex=item:GetNormalTexture();-- Hack to modify the existing texture object
  11.     tex:ClearAllPoints();
  12.     tex:SetPoint("CENTER",0,-1);
  13.     tex:SetSize(64,64);
  14. end
  15.  
  16. item.icon=item:CreateTexture("$parentIconTexture","BORDER");
  17.  
  18. item.count=item:CreateFontString("$parentCount","BORDER","NumberFontNormal");
  19. item.count:SetPoint("BOTTOMRIGHT",-5,2);
  20. item.count:SetJustifyH("RIGHT");
  21. item.count:Hide();
  22.  
  23. item.glow=item:CreateTexture("$parentGlow","OVERLAY");
  24. item.glow:SetPoint("CENTER");
  25. item.glow:SetSize(70,70);
  26. item.glow:SetTexture("Interface\\Buttons\\UI-ActionButton-Border");
  27. item.glow:SetBlendMode("ADD");
  28. item.glow:SetAlpha(0.6);

If there aren't any errors I missed, that should create the exact frame your template is making. Just replace the line where you create the item button with this and as Haleth suggested, move the contents of the OnLoad function to the main chunk and replace all references of self with frame.
Thank you so much! Imma look this code through and see what youve done! Also thank you for showing how to give the ADD alphaMode in lua, i tried: SetHighlightTexture("Source", SetAlpha="ADD") haha now i get why that didn't work

imma update the full script soon, and tell you guys if i got any errors.

$parent works for lua aswell? or is that XML only?

Last edited by fRodzet : 04-12-13 at 04:46 AM.
  Reply With Quote
04-12-13, 05:46 AM   #6
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Allright here is my code, but it ain't working.. Doesn't create a button. Sorry - but as of now i'm still not good enough to figure out myself how to get it working, so some more help would be Lovely!

Lua Code:
  1. -------------------
  2. --BagBuddy Frames--
  3. -------------------
  4.  
  5.     -- The parent frame of BagBuddy
  6.     local frame = CreateFrame("Frame", "BagBuddy", UIParent)
  7.     frame:SetSize(425, 425)
  8.     frame:SetPoint("CENTER", UIParent, "CENTER")
  9.  
  10.     -- The Body frame of BagBuddy (the border itself)
  11.     frame.body = frame:CreateTexture("BagBuddy_Body", "BORDER")
  12.     frame.body:SetPoint("TOPLEFT")
  13.     frame.body:SetTexture("Interface\\BankFrame\\UI-BankFrame")
  14.    
  15.     -- Making BagBuddy movable
  16.     frame:EnableMouse(true)
  17.     frame:SetMovable(true)
  18.     frame:SetClampedToScreen(true)
  19.     frame:RegisterForDrag("LeftButton")
  20.     frame:SetScript("OnDragStart", frame.StartMoving)
  21.     frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
  22.  
  23.     -- The icon(portrait) texture of BagBuddy
  24.     frame.icon = frame:CreateTexture("BagBuddy_Icon", "BACKGROUND")
  25.     frame.icon:SetSize(60, 60)
  26.     frame.icon:SetPoint("TOPLEFT", 7, -6)
  27.     frame.icon:SetTexture("Interface\\Icons\\INV_Misc_EngGizmos_17")
  28.     SetPortraitToTexture(frame.icon, "Interface\\Icons\\INV_Misc_EngGizmos_17")
  29.  
  30.     -- The title fontstring of BagBuddy.
  31.     frame.title = frame:CreateFontString("BagBuddy_Title", "OVERLAY", "GameFontNormal")
  32.     frame.title:SetPoint("TOP", 0, -18)
  33.     frame.title:SetText("BagBuddy")
  34.  
  35.     -- The close button for BagBuddy.
  36.     frame.close = CreateFrame("Button", "BagBuddy_Close", frame, "UIPanelCloseButton")
  37.     frame.close:SetPoint("TOPRIGHT", -22, -8)
  38.  
  39. -----------------------
  40. --BagBuddy Loop Items--
  41. -----------------------
  42.  
  43. frame.items = {}
  44. for idx = 1, 28 do
  45.     local item = CreateFrame("Button", "BagBuddy_Item" .. idx, frame, "SecureActionButtonTemplate")
  46.     item:SetSize(37, 37)
  47.    
  48.     item:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
  49.     item:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress")
  50.     item:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square", "ADD")
  51.     do local tex = item:GetNormalTexture()
  52.         tex:ClearAllPoints()
  53.         tex:SetPoint("CENTER", 0, -1)
  54.         tex:SetSize(64, 64)
  55.     end
  56.    
  57.     item.icon = item:CreateTexture("$parentIconTexture", "BORDER")
  58.    
  59.     item.count = item:CreateFontString("$parentCount", "BORDER", "NumberFontNormal")
  60.     item.coun:SetPoint("BOTTOMRIGHT", -5, 2)
  61.     item.count:SetJustifyH("RIGHT")
  62.     item.count:Hide()
  63.    
  64.     item.glow = item:CreateTexture("$parentGlow", "OVERLAY")
  65.     item.glow:SetPoint("CENTER")
  66.     item.glow:SetSize(70, 70)
  67.     item.glow:SetTexture("Interface\\Buttons\\UI-ActionButton-Border")
  68.     item.glow:SetBlendMode("ADD")
  69.     item.glow:SetAlpha(0.6)
  70.    
  71.     frame.items[idx] = item
  72.    
  73.     if idx == 1 then
  74.         item:SetPoint("TOPLEFT", 40, -73)
  75.     elseif idx == 8 or idx == 15 or idx == 22 then
  76.         item:SetPoint("TOPLEFT", frame.items[idx - 7], "BOTTOMLEFT", 0, -7)
  77.     else
  78.         item:SetPoint("TOPLEFT", frame.items[idx - 1], "TOPRIGHT", 12, 0)
  79.     end
  80. end
  81.  
  82.  
  83. ----------------------
  84. --BagBuddy Functions--
  85. ----------------------

I looked it through and doesn't seem like a typing mistake.

Last edited by fRodzet : 04-12-13 at 05:55 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help Creating Textures, Frames, etc in Lua. And some questions!


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