View Single Post
02-12-16, 02:02 PM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,336
Here's some points I need to address before moving on:

The "parent" of a frame refers to another frame that it shares its visibility, scale, and other attributes with. For example, you have one frame representing a dialog box, and another representing a button. Normally, the button would have its parent set to the dialog box so when you show and hide the box, the button would respond accordingly and show or hide itself as well.

ACFrame is defined at the bottom, which isn't useful at all. Lua and other programming languages execute code in top-down order. When you reference ACFrame, Lua doesn't know what it is yet and attempts to find a global with that name. At this time, it's simply nil. When Frame:SetPoint() gets this, it positions relative to the screen itself instead of the frame you're creating later.



To start, let's rearrange the code so we can get things to work:

This can stay where it is. The function only reacts to what's passed to it. Drag handlers registered to move parent. Button:SetNormalTexture() was used to show each icon.
Lua Code:
  1. local CreateButton; do-- Prototype for function
  2.     --  Drag Handlers
  3.     local function OnDragStart(self) self:GetParent():StartMoving(); end
  4.     local function OnDragStop(self) self:GetParent():StopMovingOrSizing(); end
  5.  
  6.     --  Tooltip Handlers
  7.     local function OnEnter(self)
  8.         if self.Tooltip then
  9.             GameTooltip:SetOwner(self,"ANCHOR_TOP");
  10.             GameTooltip:AddLine(self.Tooltip,0,1,0.5,1,1,1);
  11.             GameTooltip:Show();
  12.         end
  13.     end
  14.     local function OnLeave(self) if GameTooltip:IsOwned(self) then GameTooltip:Hide(); end end
  15.  
  16.     --  Button Generator (this will be assigned to the upvalue noted as a function prototype)
  17.     function CreateButton(parent,name,texture,text,tooltip)
  18.         tooltip=tooltip or text;--  If no tooltip, use button text
  19.  
  20.         --      Create our button
  21.         local btn=CreateFrame("Button",name,parent,"SecureActionButtonTemplate");
  22.         btn:SetSize(30,30);
  23.  
  24.         --      Setup button text
  25.         btn:SetNormalFontObject("GameFontNormalSmall");
  26.         btn:SetHighlightFontObject("GameFontHighlightSmall");
  27.         btn:SetDisabledFontObject("GameFontDisableSmall");
  28.         btn:SetText(text);
  29.  
  30.         --      Setup button's backgorund
  31.         btn:SetNormalTexture(texture);
  32.  
  33.         --      Register handlers
  34.         btn:RegisterForClicks("AnyUp");--   Register all buttons
  35.         btn:RegisterForDrag("LeftButton");--    Register for left drag
  36.         btn:SetScript("OnDragStart",OnDragStart);
  37.         btn:SetScript("OnDragStop",OnDragStop);
  38.         btn:SetScript("OnEnter",OnEnter);
  39.         btn:SetScript("OnLeave",OnLeave);
  40.         btn.Tooltip=tooltip;
  41.  
  42.         --      Return our button
  43.         return btn;
  44.     end
  45. end

The frame definition is moved here so the code below can use it. Proper registration for dragging has been implemented.
New frames are always shown and have no anchors, so Frame:Show() and Frame:ClearAllPoints() respectively are unnecessary. Frame:SetSize() replaces both Frame:SetWidth() and Frame:SetHeight(). Frame:SetScale() was removed as you were sending nil to it and Frame:SetMovable() was properly set.
Lua Code:
  1. local frame = CreateFrame("Frame","ACFrame",UIParent)
  2. frame:SetPoint("CENTER",UIParent)
  3. frame:SetSize(30+30+20,(7*30)+20)
  4. frame:SetBackdrop(StaticPopup1:GetBackdrop())
  5.  
  6. frame:SetMovable(true)
  7. frame:EnableMouse(true)--   Receive mouse events (Buttons automatically have this set)
  8. frame:RegisterForDrag("LeftButton")--   Register left button for dragging
  9. frame:SetScript("OnDragStart",frame.StartMoving)--  Set script for drag start
  10. frame:SetScript("OnDragStop",frame.StopMovingOrSizing)--    Set script for drag stop

The following is slightly modified to use the frame pointer stored in frame immediately above. Layout was corrected to fit nicely within frame.
Lua Code:
  1. local button=CreateButton(frame,"AshranCommanderButton1","Interface\\Icons\\achievement_pvp_a_h",nil,"Warspear Keep");
  2. button:SetPoint("TOPRIGHT",-10,-10);-- Anchors always default to an object's parent
  3. button:SetScript("OnClick", function()
  4.     SendChatMessage("Warspear Keep ","SAY")
  5.     DoEmote("follow")
  6. end )
  7. local button=CreateButton(frame,"AshranCommanderButton2","Interface\\Icons\\achievement_garrison_tier02_horde",nil,"Emberfall Tower");
  8. button:SetPoint("TOP",AshranCommanderButton1,"BOTTOM");-- Anchors always default to an object's parent
  9. button:SetScript("OnClick", function()
  10.     SendChatMessage("Emberfall Tower ","SAY")
  11.     DoEmote("follow" , UnitName("target"))
  12. end )
  13. local button=CreateButton(frame,"AshranCommanderButton3","Interface\\Icons\\achievement_garrison_tier01_horde",nil,"Volrath's Advance");
  14. button:SetPoint("TOP",AshranCommanderButton2,"BOTTOM");-- Anchors always default to an object's parent
  15. button:SetScript("OnClick", function()
  16.     SendChatMessage("Volrath's Advance ","SAY")
  17.     DoEmote("follow" , UnitName("target"))
  18. end )
  19. local button=CreateButton(frame,"AshranCommanderButton4","Interface\\Icons\\achievement_doublejeopardy",nil,"The Crossroads");
  20. button:SetPoint("TOP",AshranCommanderButton3,"BOTTOM");-- Anchors always default to an object's parent
  21. button:SetScript("OnClick", function()
  22.     SendChatMessage("The Crossroads ","SAY")
  23.     DoEmote("follow" , UnitName("target"))
  24. end )
  25. local button=CreateButton(frame,"AshranCommanderButton5","Interface\\Icons\\achievement_garrison_tier01_alliance",nil,"Tremblade's Vanguard");
  26. button:SetPoint("TOP",AshranCommanderButton4,"BOTTOM");-- Anchors always default to an object's parent
  27. button:SetScript("OnClick", function()
  28.     SendChatMessage("Tremblade's Vanguard ","SAY")
  29.     DoEmote("follow" , UnitName("target"))
  30. end )
  31. local button=CreateButton(frame,"AshranCommanderButton6","Interface\\Icons\\achievement_garrison_tier02_alliance",nil,"Archmage Overwatch");
  32. button:SetPoint("TOP",AshranCommanderButton5,"BOTTOM");-- Anchors always default to an object's parent
  33. button:SetScript("OnClick", function()
  34.     SendChatMessage("Archmage Overwatch ","SAY")
  35.     DoEmote("follow" , UnitName("target"))
  36. end )
  37. local button=CreateButton(frame,"AshranCommanderButton7","Interface\\Icons\\achievement_pvp_h_a",nil,"Stormshield Stronghold");
  38. button:SetPoint("TOP",AshranCommanderButton6,"BOTTOM");-- Anchors always default to an object's parent
  39. button:SetScript("OnClick", function()
  40.     SendChatMessage("Stormshield Stronghold ","SAY")
  41.     DoEmote("follow" , UnitName("target"))
  42. end )
  43. local button=CreateButton(frame,"AshranCommanderButton8","Interface\\Icons\\Ability_rogue_sprint",nil,"Amphitheater of Annihilation(AoA)");
  44. button:SetPoint("RIGHT",AshranCommanderButton1,"LEFT");-- Anchors always default to an object's parent
  45. button:SetScript("OnClick", function()
  46.     SendChatMessage("AoA - EVENT:STADIUM RACING - Block the entrance!","SAY")
  47.     DoEmote("follow" , UnitName("target"))
  48. end )
  49. local button=CreateButton(frame,"AshranCommanderButton9","Interface\\Icons\\spell_fire_fire",nil,"Brute's Rise(BR)");
  50. button:SetPoint("RIGHT",AshranCommanderButton2,"LEFT");-- Anchors always default to an object's parent
  51. button:SetScript("OnClick", function()
  52.     SendChatMessage("BR - EVENT:OGRE FIRES - Block the stairs! ","SAY")
  53.     DoEmote("follow" , UnitName("target"))
  54. end )
  55. local button=CreateButton(frame,"AshranCommanderButton10","Interface\\Icons\\achievement_boss_furyfurnace",nil,"Ring of Conquest(RoC)");
  56. button:SetPoint("RIGHT",AshranCommanderButton3,"LEFT");-- Anchors always default to an object's parent
  57. button:SetScript("OnClick", function()
  58.     SendChatMessage("Ring of Conquest GO RoC ","SAY")
  59.     DoEmote("follow" , UnitName("target"))
  60. end )
  61. local button=CreateButton(frame,"AshranCommanderButton11","Interface\\Icons\\Trade_archaeology_apexisstatue",nil,"Ashran Excavation(Mines)");
  62. button:SetPoint("RIGHT",AshranCommanderButton4,"LEFT");-- Anchors always default to an object's parent
  63. button:SetScript("OnClick", function()
  64.     SendChatMessage("Ashran Excavation - EVENT:APEXIS MARKS - Secure the center!  ","SAY")
  65.     DoEmote("follow" , UnitName("target"))
  66. end )
  67. local button=CreateButton(frame,"AshranCommanderButton12","Interface\\Icons\\achievement_reputation_ogre",nil,"Seat of Kor'lok");
  68. button:SetPoint("RIGHT",AshranCommanderButton5,"LEFT");-- Anchors always default to an object's parent
  69. button:SetScript("OnClick", function()
  70.     SendChatMessage("Seat of Kor'lok - Kor'lok - Kill the ogre!  ","SAY")
  71.     DoEmote("follow" , UnitName("target"))
  72. end )
  73. local button=CreateButton(frame,"AshranCommanderButton13","Interface\\Icons\\Spell_lifegivingspeed",nil,"Molten Quarry(MQ)");
  74. button:SetPoint("RIGHT",AshranCommanderButton6,"LEFT");-- Anchors always default to an object's parent
  75. button:SetScript("OnClick", function()
  76.     SendChatMessage("Molten Quarry - EVENT:Empowered Ore - Block the entrance!  ","SAY")
  77.     DoEmote("follow" , UnitName("target"))
  78. end )
  79. local button=CreateButton(frame,"AshranCommanderButton14","Interface\\Icons\\Achievement_halloween_ghost_01",nil,"Ashmaul Burial Grounds(ABG)");
  80. button:SetPoint("RIGHT",AshranCommanderButton7,"LEFT");-- Anchors always default to an object's parent
  81. button:SetScript("OnClick", function()
  82.     SendChatMessage("Ashmaul Burial Grounds - EVENT:RISEN SPIRITS - Clear the center and block the entrance!  ","SAY")
  83.     DoEmote("follow" , UnitName("target"))
  84. end )



There is more that can be done to optimize the code like creating a layout table and using that in a loop to create your buttons, but for now, this'll do.
__________________
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 : 02-12-16 at 02:49 PM.
  Reply With Quote