View Single Post
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