EePanels Scripts

From WowAce Wiki
Jump to: navigation, search

One of the features added is the ability to attach lua scripts to a panel. Your scripts have full access to everything, so be careful when using it. Even though the scripts are tied into the saved variables for a panel, they are free to modify or access other panels, or anything else you want to. The scripts are attached to the panels for two reasons:

  1. If you delete a panel, the script attached to it will be deleted as well. This helps when redistributing layouts
  2. If you use the word THIS (in all caps) in a script, it will be replaced with the name of the panel the script is attached to. This makes it easy to come up with code which you can share with others.

Unfortunately, I can't get non-ace2 event hooks to work. I think it's because the normal WoW API requires OnEvent scripts to be set when the addon is loaded,which I don't do. Thus, if you want to register events you'll need to do so through the ACE2 event handling system. This part of the code might change if I can divine a better way to handle scripts. If you need to modify multiple frames on the same event, I would suggest creating a new frame, setting it's alpha to 0, and using it to hold all your scripts.

Please feel free to share any scripts you come up with, as I'm sure others will appreciate it.

Helpful Links

WoWWiki Interface Customization portal
Programming in Lua 
For those of you who want a full grasp on Lua

The Scripts

Note: Any line beginning with "--" is commented out and not part of the code.

Show a panel In Combat

-- Hide the panel so it's not shown until we want it to
THIS:Hide()
-- Hide panel ooc
function eePanels2:PLAYER_REGEN_ENABLED()
	THIS:Hide()
end
-- Show panel ic
function eePanels2:PLAYER_REGEN_DISABLED()
	THIS:Show()
end
-- Ace2 event listeners; can't do this in more than one script
eePanels2:RegisterEvent("PLAYER_REGEN_DISABLED")
eePanels2:RegisterEvent("PLAYER_REGEN_ENABLED")

Show a panel Out of Combat

-- Hide the panel so it's not shown until we want it to
THIS:Hide()
-- Hide panel ic
function eePanels2:PLAYER_REGEN_DISABLED()
	THIS:Hide()
end
-- Show panel ic
function eePanels2:PLAYER_REGEN_ENABLED()
	THIS:Show()
end
-- Ace2 event listeners; can't do this in more than one script
eePanels2:RegisterEvent("PLAYER_REGEN_DISABLED")
eePanels2:RegisterEvent("PLAYER_REGEN_ENABLED")

Make an eePanel show when in a raid, and hide when not

-- Hide the panel so it's not shown until we want it to
THIS:Hide()
-- Change our panel's visibility when in/out of a raid
function eePanels2:RaidPanelVisibility(self, event, ...)
	-- If we're in a raid
	if GetNumPartyMembers() > 4 or GetNumRaidMembers() > 0 then
		-- Show the panel this script is attached to
		THIS:Show()
	-- If we're not in a raid
	else
        	-- Hide the panel this script is attached to
		THIS:Hide()
	end
end
-- Ace2 event listeners; can't do this in more than one script
eePanels2:RegisterEvent("PLAYER_ENTERING_WORLD", eePanels2.RaidPanelVisibility)
eePanels2:RegisterEvent("PARTY_MEMBERS_CHANGED", eePanels2.RaidPanelVisibility)
eePanels2:RegisterEvent("RAID_ROSTER_UPDATE", eePanels2.RaidPanelVisibility)

Make an eePanel resize when another frame is resized

ChatFrame1 is used in this example, though you may substitute it for any frame, or another eePanel. NOTE: This requires AceHook to be loaded, which isn't included with eePanels2! Either make sure an addon that uses AceHook is loaded before eePanels or add an addon that uses AceHook to the OptDeps line in the eePanels2 TOC.

-- Create a new ace2 addon for hooking
THIS_Hook = AceLibrary("AceAddon-2.0"):new("AceHook-2.1")
-- Create a function to resize the eePanel when the hooked frame is resized
function THIS_Hook:OnSizeChanged(frame, ...)
	THIS:SetWidth(frame:GetWidth())
	THIS:SetHeight(frame:GetHeight())
end
-- Hook to the frame we want to resize against (ChatFrame1 here)
THIS_Hook:HookScript(ChatFrame1, "OnSizeChanged")

If you have two eePanels (using one as a border behind the first), parent the border eePanel to the one above, adjust it's size, and use this script (where eePanelX is the panel used above):

-- Create a new ace2 addon for hooking
THIS_Hook = AceLibrary("AceAddon-2.0"):new("AceHook-2.1")
-- Create a function to resize the eePanel when the hooked frame is resized
function THIS_Hook:OnSizeChanged(frame, ...)
	THIS:SetHeight(frame:GetHeight()+10)
end
-- Hook to the frame we want to resize against (ChatFrame1 here)
--the eePanelX is the eepanel from above that you want to spy
THIS_Hook:HookScript(eePanelX, "OnSizeChanged")

Make an eePanel show only when in a raid or group, and not in a Battleground or Arena

-- Hide the panel so it's not shown until we want it to
THIS:Hide()
-- Change our panel's visibility when in/out of a raid
function eePanels2:RaidPanelVisibility(self, event, ...)
	-- If we're in a raid
	if GetNumPartyMembers() > 0 and not ((select(2, IsInInstance()) == "pvp" or select(2, IsInInstance()) == "arena") 
	or GetNumRaidMembers() > 0 and not ((select(2, IsInInstance()) == "pvp" or select(2, IsInInstance()) == "arena")

		-- Show the panel this script is attached to
		then THIS:Show()
		-- If we're not in a raid, hide the panel this script is attached to
		else THIS:Hide()
	end
end
-- Ace2 event listeners; can't do this in more than one script
eePanels2:RegisterEvent("PLAYER_ENTERING_WORLD", eePanels2.RaidPanelVisibility)
eePanels2:RegisterEvent("PARTY_MEMBERS_CHANGED", eePanels2.RaidPanelVisibility)
eePanels2:RegisterEvent("RAID_ROSTER_UPDATE", eePanels2.RaidPanelVisibility)

Change the color of a panel

Note: This is not a full script, only something you can use in one.

eePanels2.db.profile.panels[PANELNUMBERHERE].background.frame:SetGradientAlpha("ALIGNMENT", redValue, greenValue, blueValue, alphaValue, gradientRedValue, gradientGreenValue, gradientBlueValue, gradientAlphaValue)

Where ALIGNMENT is the direction of the gradient (VERTICAL or HORIZONTAL). All values are a range of 0-1. If you wish to have the background be a solid color, set the gradient color values the same as the background color values, and the gradient alpha to 1 (100%).

Change the color of a panel's border

Note: This is not a full script, only something you can use in one.

eePanelNameHere:SetBackdropBorderColor(r,g,b[,a])

or

THIS:SetBackdropBorderColor(r,g,b[,a])

Change color based on target's class

Panel:

function eePanels2:TargetFrameVisibility(self, event, ...)
	local _, class = UnitClass("target");
	if class == "WARRIOR" then
               eePanels2.db.profile.panels[PANELNUMBERHERE].background.frame:SetGradientAlpha("ALIGNMENT", redValue, greenValue, blueValue, alphaValue, gradientRedValue, gradientGreenValue, gradientBlueValue, gradientAlphaValue)
	elseif class == "PRIEST" then
               eePanels2.db.profile.panels[PANELNUMBERHERE].background.frame:SetGradientAlpha("ALIGNMENT", redValue, greenValue, blueValue, alphaValue, gradientRedValue, gradientGreenValue, gradientBlueValue, gradientAlphaValue)
	elseif ... then
		...
	end
end
eePanels2:RegisterEvent("PLAYER_ENTERING_WORLD", eePanels2.TargetFrameVisibility)
eePanels2:RegisterEvent("PLAYER_TARGET_CHANGED", eePanels2.TargetFrameVisibility)

Border:

function eePanels2:TargetFrameVisibility(self, event, ...)
	local _, class = UnitClass("target");
	if class == "WARRIOR" then
		THIS:SetBackdropBorderColor(someR,someG,someB,alpha)
	elseif class == "PRIEST" then
		THIS:SetBackdropBorderColor(someR,someG,someB,alpha)
	elseif ... then
		...
	end
end
eePanels2:RegisterEvent("PLAYER_ENTERING_WORLD", eePanels2.TargetFrameVisibility)
eePanels2:RegisterEvent("PLAYER_TARGET_CHANGED", eePanels2.TargetFrameVisibility)

Change the color of multiple panels' borders in one script

Note: You will get an error if you attempt to run this script in panel 1, as panel 2 hasn't been created yet. Also, this is not a full script, only something that can be used in one.

eePanels1:SetBackdropBorderColor(); eePanels2:SetBackdropBorderColor(); etc.

Check to see if an addon is loaded

Grid is used in this example. Note: This isn't a full script, only something to be used in one.

local gridExists = getglobal("NAME_OF_GRID_FRAME_HERE") -- this will be nil if it doesn't exist

Generate text in a panel

Note: This is not a full script, only something to be used in one.

local THIS_text = THIS:CreateFontString(nil, "OVERLAY ") -- set strata of text;
THIS_text:SetFontObject(GameFontHighlightSmall) -- Set the font type
THIS_text:SetPoint("CENTER", THIS, "CENTER", 0, 0) -- center the text
THIS_text:SetText("big badda boom") -- text to display

Show text based on Druid stances

This takes the above method of generating text and shows how to display what druid stance you are in. If you shapeshift spells are keybound, you could disable your stance bar and use an eePanel as an indicator instead.

local THIS_text = THIS:CreateFontString(nil, "OVERLAY ") -- set strata of text;
THIS_text:SetFontObject(GameFontHighlightSmall) -- Set the font type
THIS_text:SetPoint("CENTER", THIS, "CENTER", 0, 0) -- center the text

function eePanels2:StanceText (self, event,...)
	if GetShapeShiftForm() = 0 then
	   THIS_text:SetText ("Caster Form")	
	elseif GetShapeShiftForm() = 1 then
	   THIS_text:SetText ("Dire Bear Form")
	elseif GetShapeShiftForm() = 2 then
	   THIS_text:SetText ("Aquatic Form")
	elseif GetShapeShiftForm() = 3 then
	   THIS_text:SetText ("Cat Form")
	elseif GetShapeShiftForm() = 4 then
	   THIS_text:SetText ("Travel Form")
	end
end

eePanels2:RegisterEvent("PLAYER_ENTERING_WORLD", eePanels2.StanceText)
eePanels2:RegisterEvent("UPDATE_SHAPESHIFT_FORMS", eePanels2.StanceText)

Show if target is Elite

function eePanels2:PLAYER_TARGET_CHANGED()
local isElite = UnitIsPlusMob("target")
    if (isElite) then
        eePanel4:Show()
    else
        eePanel4:Hide()
    end
end

eePanels2:RegisterEvent("PLAYER_TARGET_CHANGED")

Parent a frame to Violation

Violation doesn't have a "real" frame name - the Vio name in this script is taken from Vio's code. Note: You need to add Violation to eePanel's Opt. Depts in the eePanels TOC file, to ensure that Violation loads (and therefore exists) before eePanels does. Otherwise, this will not work.

-- Set the parent
THIS:SetParent(Violation.windows[1].frame)
-- Set the anchor
THIS:SetPoint("CENTER", Violation.windows[1].frame, "CENTER")

The first "CENTER" is for the center of your panel, the second "CENTER" is for the center of the Vio window. You can exchange "CENTER" for anything listed in the eePanels anchor options (TOPRIGHT, LEFT, etc). You can also add offsets. A horizontal (x) offset of 5 and a vertical offset (y) of 10 would look like this:

THIS:SetPoint("CENTER", Violation.windows[1].frame, "CENTER", 5, 10)


Auto-resize a panel to AutoBar

While you're playing with this script to find out how big to make SIZE_PER_BUTTON, you'll need to do a /reload everytime you change it. Set the anchor to AutoBarFrame to anchor the panel to the red/green move button.

Note: This won't work with the newest version of AutoBar. It will work for revision 51227 and earlier.

 -- Get instance of our hook class
 THIS_Hook = AceLibrary("AceAddon-2.0"):new("AceHook-2.1")
 -- Create the hook function
 function THIS_Hook:AssignButtons(frame, size)
   local buttons = THIS_Hook.hooks[frame].AssignButtons(frame,size)
   -- Change SIZE_PER_BUTTON to be a number
   local newPanelSize = buttons * SIZE_PER_BUTTON
   -- If your autobar is horizontal, you'll want to change this to SetWidth
   THIS:SetHeight(newPanelSize)
   return buttons
 end
 -- Redirect function call
 THIS_Hook:Hook(AutoBar, "AssignButtons")

Change panel height if unit exists

This is used to alter the height of of a panel, depending on whether or not a unit exists. For example, we will use your pet.

This would enable you to do something like this:

http://www.wowace.com/mediawiki/images/8/8e/UFExample.jpg

Avoiding an aesthetic blemish like this:

http://www.wowace.com/mediawiki/images/6/68/UFExample2.jpg

 --Give our function a name
 function eePanels2:FrameResize()
   --Assign a variable that we can easily call later, that checks whether or not our pet exists
   local PetExists = UnitExists("pet")
     if PetExists then
       THIS:SetHeight(89) --Change the height of the panel to this when a pet is out
     else
       THIS:SetHeight(48) --Otherwise, be this height, change both as needed
     end
 end
 --And finally, register an event to look for that causes our FrameResize Function to fire
 eePanels2:RegisterEvent("UNIT_PET", eePanels2.FrameResize)