Child Frame Movement

I have two frames, frameParent and frameChild. I made it so frameParent cannot be dragged, but frameChild can. My problem is I want frameChild to not be draggable outside of frameParent. Is there a way to do this or perhaps as the mouse moves to determine where frameChild is and if the top, right, bottom, or left edge is outside of frameParent disable movement past that point? I’m trying to make a slider of some type since I cannot get slider to work as I want so I thought it would be easier to make my own custom version I can then customize to my hearts desire. I think I just need some way to know when I am dragging frameChild to constantly call something such as OnMouseMove or OnDragging or something.

The easier question would probably be, how do you want your slider to work?

As very rough example of “two frame” slider:

local function IsDragging(self)
	if not self.IsDragging then return end
	local x, y = GetCursorPosition()
	local pcx = self:GetParent():GetCenter()
	local pcx = pcx * UIParent:GetEffectiveScale()
	if x > self.L + 3 and x < self.R - 3 then
		self:SetPoint("CENTER", (x-pcx) * self.W, 0)
	end
end

local f = CreateFrame("Frame", nil, UIParent)
f:SetSize(100, 20)
f:SetPoint("CENTER")
f.t = f:CreateTexture()
f.t:SetAllPoints()
f.t:SetColorTexture(0, 0 ,0)

local f1 = CreateFrame("Button", nil, f)
f1:SetFrameLevel(2)
f1:SetSize(5, 15)
f1:SetPoint("CENTER")
f1.t = f1:CreateTexture()
f1.t:SetAllPoints()
f1.t:SetColorTexture(1, 1, 1)
f1:RegisterForClicks("LeftButtonDown", "LeftButtonUp")
f1:SetScript("OnMouseDown", function(self)
	self.W = (self:GetWidth() /2) * UIParent:GetEffectiveScale()
	self.L = self:GetParent():GetLeft() * UIParent:GetEffectiveScale()
	self.R = self:GetParent():GetRight() * UIParent:GetEffectiveScale()
	self.IsDragging = true
	self:SetScript("OnUpdate", IsDragging)
end)
f1:SetScript("OnMouseUp", function(self)
	self.IsDragging = nil
	self:SetScript("OnUpdate", nil)
end)
1 Like

Thanks! The OnUpdate SetScript parameter was what I needed. I basically have 2 frames, a parent frame that is basically a line and a child frame that is a square (since I don’t know how to make it a circle). When the user tries to drag the square, I wanted to make sure it didn’t move away from the parent frame.

You can create sliders to look like anything eg. a line with a small circle for a thumb.

local f = CreateFrame("Slider", nil, IUParent)
f:SetSize(100, 10)
f:SetPoint("CENTER")
f:SetOrientation("HORIZONTAL")
f:SetThumbTexture("Interface\\QuestFrame\\UI-Quest-BulletPoint")
f:SetMinMaxValues(1, 100)
f:SetValue(50)

-- background and line
f.b = f:CreateTexture() -- Background
f.b:SetAllPoints()
f.b:SetColorTexture(0, 0 ,0)
f.b:SetDrawLayer("ARTWORK", -1)
f.l = f:CreateLine() -- Line
f.l:SetThickness(1)
f.l:SetDrawLayer("ARTWORK", 1)
f.l:SetColorTexture(1, 1, 1)
f.l:SetStartPoint("LEFT")
f.l:SetEndPoint("RIGHT")

The thumb texture could be any circle image you can create plus you get all the build-in scripts and math.

Edited: to make it a single slider frame.

2 Likes

Awesome! Thanks, I’ll play around with this to learn how sliders work.

https://warcraft.wiki.gg/wiki/UIOBJECT_Slider