Thread Tools Display Modes
08-15-17, 07:22 PM   #1
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
Moving Multiple Frames

I am writing an XP Bar addon. I have a frame for the background, rested XP, current XP and a frame to use for moving. The addon does what I want it to do for displaying XP (still needs more testing), but when I try to move the XP Bar, only the text (mouseFrame) moves and not the rest of the frames. How can I accomplish this or am I designing this addon wrong? I have looked at other XP addons and believe I am using a correct approach. Perhaps I am not handling the dragging correctly.

Code:
-- Yes Another XP Bar.
local JWBarHeight = 35
local JWBarWidth = 350
local JWBarAnchor = {"CENTER", UIPARENT, "CENTER", 0, -325}
local JWBarTexture = [[Interface\addons\JW00XPBar\fer2.tga]]
local JWframeType = "StatusBar"
local JWframeName = "JWxpBar"
local JWframeParent = {UIParent}
local JWframeTemplate = "TextStatusBar"

function comma_value(n) -- credit http://richard.warburton.it seems to work OK.
	local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
	return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

--Create Background and Border
local backdrop = CreateFrame("Frame", "JWBackdrop", UIParent)
backdrop:SetHeight(JWBarHeight)
backdrop:SetWidth(JWBarWidth)
backdrop:SetPoint(unpack(JWBarAnchor))
backdrop:SetBackdrop({
	bgFile = JWbarTexture, 
	edgeFile = JWbarTexture, 
	tile = false, tileSize = 0, edgeSize = 1, 
	insets = { left = -1, right = -1, top = -1, bottom = -1}
})
backdrop:SetBackdropColor(0, 0, 0)
backdrop:SetBackdropBorderColor(.2, .2, .2, 0)

--Rested XP Bar
local JWRestedxpBar = CreateFrame("StatusBar", "JWRestedxpBar", backdrop, "TextStatusBar")
JWRestedxpBar:SetHeight(JWBarHeight)
JWRestedxpBar:SetWidth(JWBarWidth)
JWRestedxpBar:SetPoint("TOP", backdrop,"TOP", 0, 0)
JWRestedxpBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
JWRestedxpBar:GetStatusBarTexture():SetHorizTile(false)
JWRestedxpBar:SetStatusBarColor(0,0,1,1)
JWRestedxpBar:Hide()

--XP Bar
local JWBar = CreateFrame("StatusBar", "JWBar", JWRestedxpBar, "TextStatusBar")
JWBar:SetWidth(JWBarWidth)
JWBar:SetHeight(JWBarHeight)
JWBar:SetPoint("TOP", backdrop,"TOP", 0, 0)
JWBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
JWBar:GetStatusBarTexture():SetHorizTile(false)
JWBar:SetStatusBarColor(0,1,0,1)

--Create frame used for mouseover, clicks, and text
local mouseFrame = CreateFrame("Frame", "JWmouseFrame", JWBar)
mouseFrame:SetAllPoints(backdrop)
mouseFrame:EnableMouse(true)
mouseFrame:SetMovable(true)
mouseFrame:SetClampedToScreen(true)

--Create XP Text
local Text = mouseFrame:CreateFontString("JWxpBarText", "OVERLAY")
Text:SetFont("Fonts\\FRIZQT__.TTF",14,"NONE")
Text:SetPoint("CENTER", mouseFrame, "CENTER",0,1)
Text:SetAlpha(1)

--Set Frame levels this seems to make the XP bar and Rested XP bar display properly.
backdrop:SetFrameLevel(0)
JWRestedxpBar:SetFrameLevel(1)
JWBar:SetFrameLevel(2)
mouseFrame:SetFrameLevel(3)

mouseFrame:SetScript("OnMouseDown", function(self, button)
  if button == "LeftButton" and (IsShiftKeyDown()) and not self.isMoving then
   self:StartMoving();
   self.isMoving = true;
  end
end)

mouseFrame:SetScript("OnMouseUp", function(self, button)
  if button == "LeftButton" and (IsShiftKeyDown()) and self.isMoving then
   self:StopMovingOrSizing();
   self.isMoving = false;
  end
end)
	
local function UpdateStatus()
	local JWBarCurrXP = UnitXP("player")
	local JWBarMaxXP = UnitXPMax("player")
	local JWBarRestXP = GetXPExhaustion() or 0
	local JWBarPercXP = floor(JWBarCurrXP/JWBarMaxXP*100)

	if UnitLevel("player") == MAX_PLAYER_LEVEL then
		backdrop:Hide()
		JWRestedxpBar:Hide()
		JWBar:Hide()
		mouseFrame:Hide()
	else
		JWBar:SetMinMaxValues(0,JWBarMaxXP)
		JWBar:SetValue(JWBarCurrXP)

		if JWBarRestXP then
			Text:SetText(format("%s/%s (%s%%|cffb3e1ff+%d%%|r)", comma_value(JWBarCurrXP), comma_value(JWBarMaxXP), JWBarPercXP, JWBarRestXP/JWBarMaxXP*100))
			JWRestedxpBar:Show()
			JWRestedxpBar:SetMinMaxValues(0,JWBarMaxXP)
			JWRestedxpBar:SetValue(JWBarCurrXP + JWBarRestXP)
		else
			JWRestedxpBar:Hide()
			Text:SetText(format("%s/%s (%s%%|cffb3e1ff+%d%%|r)", comma_value(JWBarCurrXP), comma_value(JWBarMaxXP), JWBarPercXP))
		end
	end
end

local frame = CreateFrame("Frame",nil,UIParent)
frame:RegisterEvent("PLAYER_LEVEL_UP")
frame:RegisterEvent("PLAYER_XP_UPDATE")
frame:RegisterEvent("UPDATE_EXHAUSTION")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
frame:SetScript("OnEvent", UpdateStatus)
__________________
Author of JWExpBar and JWRepBar.
  Reply With Quote
08-16-17, 12:08 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Okay, I see you doing SetPoint() but where are the SetParent() assignments? Without going into your code too deep, you said it yourself: you move your anchor frame, but the child frames are not moving except for the text. That is expected, as you don't use SetParent() for text.

Parent all the children to your anchor, then do SetPoint(). That ought to fix you up.

Last edited by myrroddin : 08-16-17 at 12:09 AM. Reason: grammar
  Reply With Quote
08-16-17, 09:27 AM   #3
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
I am slightly confused even after searching the API. Doesn't the way I am using SetPoint assign a parent?

JWRestedxpBar:SetPoint("TOP", backdrop,"TOP", 0, 0)

If I have to add SetParent then I would create the anchor frame first (mouseFrame) and then create the other frames for them to anchor properly?

I think I understand about the parenting has to be setup correctly for everything to drag.

I think the way I am creating the bars is a typical method unless there is some clever technique for only using one bar.
__________________
Author of JWExpBar and JWRepBar.
  Reply With Quote
08-16-17, 10:26 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Only mouseFrame (JWmouseFrame) is movable and the only frame that is attached to it is Text (JWxpBarText) hence those are the only two frames that move. You should SetPoint your other frames to mouseFrame so it can move them as well.

Edit: You could make Backdrop the movable frame as all the others seem to be attached to it in some way or another. Moving a frame that has others attached to it moves the "children". Moving a frame that is attached to another doesn't move the "parent".
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 08-16-17 at 12:32 PM.
  Reply With Quote
08-16-17, 06:08 PM   #5
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
Thanks to both of you, it is now working like I want it to. I am going to work on cleaning the code up next (variables, efficiency etc...)
__________________
Author of JWExpBar and JWRepBar.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Moving Multiple Frames


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