Jump to content

Recommended Posts

I'm wrestling with a problem, and I can't find a solution, so I figured I would post it and see if anyone could help.

 

I'm attempting to write a function that centers a widget on the cursor. Unfortunately, the coordinates used to position widgets are dependent on the attributes of their parents, and I can't get the code to compensate correctly.

 

Here is my current code:

function Widget:GoToCursor()	local w,h = TheFrontEnd:GetResolution()	local pos = TheInput:GetScreenPosition() - Vector3(w/2,h/2,0)	print("Cursor's position (Relative to center):")	print(pos)	print("Calculating offsets")	local target = self	while target.parent do		print("Parent:")		print(target.parent)		local x,y = target.parent:GetPosition():Get()		print("Parent's expressed position:")		print(Vector3(x,y))		print("Calculating culminative scale")		local sx,sy = 1,1		local starget = self.parent		while starget.parent do			print("Parent:")			print(starget.parent)			local psx,psy = starget.parent:GetScale():Get()			print("Parent's expressed scale:")			print(Vector3(psx, psy))			sx = sx * psx			sy = sy * psy			print("Culminative scale:")			print(Vector3(sx,sy))			starget = starget.parent		end		print("Parent's scaled position:")		print(Vector3(x*sx,y*sy))		pos = pos - Vector3(x*sx,y*sy)		print("Offset position:")		print(pos)		target = target.parent	end	self:SetPosition(pos)end

(I made this under the assumption that at each level, a widget's position is expressed relative to the position of it's parent, and scaled according to the scales of all widgets higher in the hierarchy.)

 

'TheFrontEnd.GetResolution' is a utility function I wrote, added by this code:

AddGlobalClassPostConstruct("frontend", "FrontEnd", function(self)    self.GetResolution = function(inst)        local go = inst:GetGraphicsOptions()        local id = go:GetFullscreenDisplayID()        local mode_id = go:GetCurrentDisplayModeID(id)        return go:GetDisplayMode(id, mode_id)    endend)

Any ideas, anyone?

Link to comment
https://forums.kleientertainment.com/forums/topic/59402-widget-positioning/
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...