Skip to content

Instantly share code, notes, and snippets.

@GigsD4X
Created September 28, 2013 04:07
Show Gist options
  • Save GigsD4X/6738271 to your computer and use it in GitHub Desktop.
Save GigsD4X/6738271 to your computer and use it in GitHub Desktop.
Returns a table with a given number of frames positioned on a scale from 0 to 1 along a given dimension in a way where they all fit with equal spacing in between them, aligned to a given end of the dimension.
function getInterequidistantlyScaledFrames( count, padding, dimension, alignment )
-- Returns a table containing `count` Frame objects with `padding`
-- percent of the frame size (on a scale from 0 to 1) of spacing in
-- between each frame in the "x" or "y" `dimension`, aligned to the
-- "left", "center", or "right" (or, along the y-axis, "top",
-- "center", or "bottom")
local frames = {};
-- Calculate the size of each frame considering the padding in
-- between each one (so that all the frames fit)
local frame_size = 1 / ( count + ( padding * count ) );
for frame_number = 1, count do
-- Calculate the position of each frame
local frame_position = 1 / count * ( frame_number - 1 );
local last_frame_position = 1 / count * ( frame_number - 2 );
if alignment == "center" then
frame_position = frame_position + ( ( frame_position - ( last_frame_position + frame_size ) ) / 2 );
elseif alignment == "right" or alignment == "bottom" then
frame_position = frame_position + ( frame_position - ( last_frame_position + frame_size ) );
end;
-- Create frame `frame_number`
local Frame = Instance.new( "Frame" );
Frame.Size = UDim2.new( dimension == "x" and frame_size or 0, 0, dimension == "y" and frame_size or 0, 0 );
Frame.Position = UDim2.new( dimension == "x" and frame_position or 0, 0, dimension == "y" and frame_position or 0 );
-- Place frame `frame_number` in its appropriate position in the table
frames[frame_number] = Frame;
end;
-- Return the table of frames
return frames;
end;
@GigsD4X
Copy link
Author

GigsD4X commented Sep 28, 2013

A few examples of the kind of stuff that this is useful for:

local frames = getInterequidistantlyScaledFrames( 6, 0.5, "x", "center" );

local DemoGUI = Instance.new( "ScreenGui", Game:GetService( "StarterGui" ) );
for frame_number, Frame in pairs( frames ) do
    Frame.Position = Frame.Position + UDim2.new( 0, 0, 0.4, 0 );
    Frame.Size = Frame.Size + UDim2.new( 0, 0, 0.2, 0 );
    Frame.Parent = DemoGUI;
end;

getInterequidistantlyScaledFrames( 6, 0.5, "x", "center" )


local frames = getInterequidistantlyScaledFrames( 6, 1, "x", "center" );

local DemoGUI = Instance.new( "ScreenGui", Game:GetService( "StarterGui" ) );
for frame_number, Frame in pairs( frames ) do
    Frame.Position = Frame.Position + UDim2.new( 0, 0, 0.4, 0 );
    Frame.Size = Frame.Size + UDim2.new( 0, 0, 0.2, 0 );
    Frame.Parent = DemoGUI;
end;

getInterequidistantlyScaledFrames( 6, 1, "x", "center" );


local frames = getInterequidistantlyScaledFrames( 6, 1, "y", "center" );

local DemoGUI = Instance.new( "ScreenGui", Game:GetService( "StarterGui" ) );
for frame_number, Frame in pairs( frames ) do
    Frame.Position = Frame.Position + UDim2.new( 0.4, 0, 0, 0 );
    Frame.Size = Frame.Size + UDim2.new( 0.2, 0, 0, 0 );
    Frame.Parent = DemoGUI;
end;

getInterequidistantlyScaledFrames( 6, 1, "y", "center" );


local frames = getInterequidistantlyScaledFrames( 6, 2, "x", "right" );

local DemoGUI = Instance.new( "ScreenGui", Game:GetService( "StarterGui" ) );
for frame_number, Frame in pairs( frames ) do
    Frame.Position = Frame.Position + UDim2.new( 0, 0, 0.4, 0 );
    Frame.Size = Frame.Size + UDim2.new( 0, 0, 0.2, 0 );
    Frame.Parent = DemoGUI;
end;

getInterequidistantlyScaledFrames( 6, 2, "x", "right" );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment