Scaling a position relative to a different frame

Hello!

I am attempting to create a somewhat custom grid system that has all of it’s components inside of a parent frame, however, I am trying to scale all of their positions relative to a different frame. I have searched almost the entire DevForum for an answer and so far nothing has worked.

Here is an image to represent what I am attempting:


image

Here is what is currently happening:

It is essential that this system has all of the components parented to the “ParentFrame”, as the components are buttons and need the position of the button relative to the parent frame when pressed.

Thank you for your help!

2 Likes

If you iterate through each one, and do some processing on their positions, like such:

local ScalerFrame = ...
local ScalerPos = ScalerFrame.Position
local ScalerSize = ScalerFrame.Size

local Component = ...
local pos = Component.Position
Component.Position = UDim2.fromScale(
    ScalerPos.X.Scale + ScalerSize.X.Scale * pos.X.Scale, 
    ScalerPos.Y.Scale + ScalerSize.Y.Scale * pos.Y.Scale
)

does it work how intended?

2 Likes

It didn’t work quite as it should have, however, with a little bit of editing I was able to get it working:

local ScalerFrame = ...
local ScalerPos = ScalerFrame.Position
local ScalerSize = ScalerFrame.Size

local Component = ...
local pos = Component.Position
Component.Position = UDim2.fromScale(
    (ScalerSize.X.Scale * pos.X.Scale) + ((1 - ScalerSize.X.Scale) / 2), 
    (ScalerSize.Y.Scale * pos.Y.Scale) + ((1 - ScalerSize.Y.Scale) / 2)
)

While this works, it doesn’t account for frames that are not positioned at (.5,0,.5,0) as I haven’t figured out how to implement this.

For example: If the “ScalerFrame” is positioned at (.3,0,.8,0) the above code still moves all of the components to the center of the screen, even though the “ScalerFrame” is not there.

EDIT: I was able to figure out how to make it work:

local ScalerFrame = ...
local ScalerPos = ScalerFrame.Position
local ScalerSize = ScalerFrame.Size

local Component = ...
local pos = Component.Position
Component.Position = UDim2.fromScale(
    (ScalerSize.X.Scale * pos.X.Scale) -- Multiply by scale factor (ScalerSize)
    + ((1 - ScalerSize.X.Scale) / 2) -- Account for size difference
    - (.5 - ScalerPos.X.Scale), -- Account for position difference

    (ScalerSize.Y.Scale * pos.Y.Scale) -- Multiply by scale factor (ScalerSize)
    + ((1 - ScalerSize.Y.Scale) / 2) -- Account for size difference
    - (.5 - ScalerPos.Y.Scale) -- Account for position difference
)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.