Jump to content

Widget Positioning


Arkathorn

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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...