View Single Post
Unread 01-16-13, 10:09 PM   #2
A Pyroguard Emberseer
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,290
Don't use DIALOG or TOOLTIP strata for things that are not dialogs or tooltips unless you have a very good reason. For all other cases, you should just parent frames appropriately, and they will automatically appear on the correct strata. If you "need" strange parenting for some reason, put both x and y on the same strata and use y:SetFrameLevel(x:GetFrameLevel() + 1) to fix the layering.

For boolean values, you can just do "if x then y else z end" instead of "if x == true then y elseif x == false then z end".

You initially set the button to 20% alpha. When it's clicked, you set the alpha to 100% when you hide the frame, but when you show the frame again you don't set the button back to 20% alpha. I'm assuming this is why you added the OnEnter and OnLeave scripts, but you can get rid of those if you just do btn:SetAlpha(0.2) when you do f:Show() in the OnClick script.

If that function is intended to be in the global namespace -- eg. not local, and not a table member like MyAddon:collapse_container() -- you should give it a more descriptive name that clearly identifies which addon it belongs to, like MyAddon_CollapseContainer.

You should add a check to prevent the function from creating frames for the same object more than once.

Finally, your code will be a lot easier to read and maintain if you use consistent indentation, spacing between comma-separated items, line breaks, and consistent naming practices (variables are lowercase, function and object names are camelcase, etc.).

function MyAddon_CollapseContainer(name, attach, state, fwidth, fheight, bwidth, bheight, point, relpoint)
	if not attach.containerFrame then
		local f = CreateFrame("Frame", name.."_Frame", attach)
		f:SetPoint("CENTER", attach, "CENTER")
		f:SetSize(100, 100)
		attach.containerFrame = f

			bgFile = "Interface/Tooltips/UI-Tooltip-Background", 
			edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
			tile = true, tileSize = 16, edgeSize = 16, 
			insets = { left = 4, right = 4, top = 4, bottom = 4 }
		f:SetBackdropColor(0, 0, 0, 1)

		f:SetScript("OnDragStart", f.StartMoving)
		f:SetScript("OnDragStop", f.StopMovingOrSizing)

		local btn = CreateFrame("Button", name.."_Button", attach)
		btn:SetFrameLevel(f:GetFrameLevel() + 1)
		btn:SetPoint("TOPLEFT", f, -25, 0)
		btn:SetSize(28, 28)
		attach.containerButton = btn


		btn.state = state
		btn:SetScript("OnClick", function(self, mouseButtonClicked)
			if self.state then
				--show f, lock btn to 1 alpha
				self.state = false
				--hide f, keep showing btn
				self.state = true

	if state then
Also, I don't see anything in your code that's actually attaching the object to be contained to the container, so when you move/collapse the container, the object itself is unaffected. Have you actually tried to use this code, or are you just throwing out a drycoded idea?
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!

Last edited by Phanx : 01-16-13 at 10:14 PM.
Phanx is offline   Reply With Quote