Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Autoscale GUI to center of the screen?

Asked by 3 years ago

So i wanna make it so that a frame for example thats somewhere on the screen to get positioned to the center of the screen i've been searching everywhere got some formulas to get the center position none of them worked but the plugin i found "Ripepperoni's Gui Tools" works soo now i got the source code from the plugin and i want to get the code only for positioning the gui to the center of the screen but i dont understand it exactly and idk how to do it this is the code:

local save = {obj = nil,position = nil}
if game.CoreGui:FindFirstChild"guiTools" then
    game.CoreGui['guiTools']:Destroy()
end
local off = 'rbxassetid://203550334'
local on = 'rbxassetid://203550339'
local gui = script.guiTools
local toolbar = plugin:CreateToolbar("Ripepperoni's Gui Tools")
local data = {
['TL'] = {
    "Top Left",
    "rbxassetid://338839412"
},
['TC'] = {
    'Top Center',
    "rbxassetid://338839545"
},
['TR'] = {
    'Top Right',
    "rbxassetid://338840545"
},
['ML'] = {
    "Middle Left",
    "rbxassetid://338840636"
},
['MC'] = {
    "Center",
    "rbxassetid://338840695"
},
['MR'] = {
    "Middle Right",
    "rbxassetid://338841755"
},
['BL'] = {
    "Botton Left",
    "rbxassetid://338838938"
},
['BC'] = {
    "Bottom Center",
    "rbxassetid://338838871"
},
['BR'] = {
    "Bottom Right",
    "rbxassetid://338838760"
},
}

--[[local revert = toolbar:CreateButton(
    "Revert",
    "Revert back one step. The history only saves last position.",
    ""--"rbxassetid://2606310"
)]]

local togglebutt = toolbar:CreateButton(
    "Toggle Interface",
    "This button opens up the UI which you do stuff with.",
    "rbxassetid://190014863"
)

-- SET UP GUI
local buttons = {}
local xbuff,ybuff
for i,v in next, gui.Frame:GetChildren() do
    if v:IsA'ImageButton' then
        buttons[#buttons+1] = v
        v.Icon.Image = data[v.Name][2]
    elseif v:IsA'ImageLabel' then
        v.Button.MouseButton1Down:connect(function()
            if v.Image == on then
                v.Image = off
            else
                v.Image = on
            end
        end)
    elseif v:IsA'TextBox' then
        if v.Name == "xBuffer" then
            xbuff = v
        elseif v.Name == 'yBuffer' then
            ybuff = v
        end
    end
end
local lastState = plugin:GetSetting("interfaceVisible")
gui.Parent = game.CoreGui
if lastState ~= nil then
    gui.Frame.Visible = lastState
else
    gui.Frame.Visible = true
end
-- TOGGLE GUI
local function toggle()
    gui.Frame.Visible = not gui.Frame.Visible
    plugin:SetSetting("interfaceVisible",gui.Frame.Visible)
end
togglebutt.Click:connect(toggle)
gui.Frame.Head.Exit.MouseButton1Down:connect(toggle)
-- FUNCTIONS 
local function getScale(px)
    return
end
local function getPx(scale)
    return
end

local funcs = {
["TL"] = function(size,x,y,scale,offset)
    return UDim2.new(0,x,0,y)
end,
["TC"] = function(size,x,y,scale,offset)
    return UDim2.new(scale and .5 - (size.X.Scale/2) or .5,offset and -(size.X.Offset/2) or 0,0,y)
end,
["TR"] = function(size,x,y,scale,offset)
    return UDim2.new(scale and 1 - size.X.Scale or 1,offset and -size.X.Offset-x or -x,0,y)
end,
["ML"] = function(size,x,y,scale,offset)
    return UDim2.new(0,x,scale and .5 - (size.Y.Scale/2) or .5,offset and -(size.Y.Offset/2) or 0)
end,
["MC"] = function(size,x,y,scale,offset)
    return UDim2.new(scale and .5 - (size.X.Scale/2) or .5,offset and -(size.X.Offset/2) or 0,scale and .5 - (size.Y.Scale/2) or .5, offset and -(size.Y.Offset/2) or 0)
end,
["MR"] = function(size,x,y,scale,offset)
    return UDim2.new(scale and 1 - size.X.Scale or 1,offset and -size.X.Offset-x or -x,scale and .5 - (size.Y.Scale/2) or .5, offset and -(size.Y.Offset/2) or 0)
end ,
["BL"] = function(size,x,y,scale,offset)
    return UDim2.new(0,x,scale and 1 - size.Y.Scale or 1,offset and -size.Y.Offset-y or -y)
end ,
["BC"] = function(size,x,y,scale,offset)
    return UDim2.new(scale and .5 - (size.X.Scale/2) or .5,offset and -(size.X.Offset/2) or 0,scale and 1 - size.Y.Scale or 1,offset and -size.Y.Offset-y or -y)
end ,
["BR"] = function(size,x,y,scale,offset)
    return UDim2.new(scale and 1 - size.X.Scale or 1,offset and -size.X.Offset-x or -x,scale and 1 - size.Y.Scale or 1,offset and -size.Y.Offset-y or -y)
end
}

-- CONNECT FUNCTIONS
for i,button in next, buttons do
    button.MouseButton1Down:connect(function()
        local items = game.Selection:Get()
        if #items > 0 then
            for _,selection in next, items do
                if selection and selection:IsA'GuiObject' and tonumber(ybuff.Text) and tonumber(xbuff.Text)  then
                    local pos = funcs[button.Name](selection.Size,math.floor(tonumber(xbuff.Text)),math.floor(tonumber(ybuff.Text)),gui.Frame.Scale.Image == on, gui.Frame.Offset.Image == on)
                    save.obj = selection
                    save.position = selection.Position
                    selection.Position = pos
                elseif not (tonumber(ybuff.Text) and tonumber(xbuff.Text)) then
                    gui.Fail:Play()
                    game:GetService"TestService":Error("[GUI TOOLS]: X buffer and Y buffer need to be numbers!")
                else
                    gui.Fail:Play()
                    game:GetService"TestService":Error('[GUI TOOLS]: "'..selection.Name..'" is not a GuiObject!')
                end
            end
        else
            gui.Fail:Play()
            game:GetService"TestService":Error("[GUI TOOLS]: You need to select a GuObject!")
        end
    end)
end

--[[revert.Click:connect(function()
    if save.obj and save.position then
        save.obj.Position = save.position
    end
end)]]

I've figured this is what i need

["MC"] = function(size,x,y,scale,offset)
    return UDim2.new(scale and .5 - (size.X.Scale/2) or .5,offset and -(size.X.Offset/2) or 0,scale and .5 - (size.Y.Scale/2) or .5, offset and -(size.Y.Offset/2) or 0)
end,

---
for i,button in next, buttons do
    button.MouseButton1Down:connect(function()
        local items = game.Selection:Get()
        if #items > 0 then
            for _,selection in next, items do
                if selection and selection:IsA'GuiObject' and tonumber(ybuff.Text) and tonumber(xbuff.Text)  then
                    local pos = funcs[button.Name](selection.Size,math.floor(tonumber(xbuff.Text)),math.floor(tonumber(ybuff.Text)),gui.Frame.Scale.Image == on, gui.Frame.Offset.Image == on)
                    save.obj = selection
                    save.position = selection.Position
                    selection.Position = pos
                elseif not (tonumber(ybuff.Text) and tonumber(xbuff.Text)) then
                    gui.Fail:Play()
                    game:GetService"TestService":Error("[GUI TOOLS]: X buffer and Y buffer need to be numbers!")
                else
                    gui.Fail:Play()
                    game:GetService"TestService":Error('[GUI TOOLS]: "'..selection.Name..'" is not a GuiObject!')
                end
            end
        else
            gui.Fail:Play()
            game:GetService"TestService":Error("[GUI TOOLS]: You need to select a GuObject!")
        end
    end)
end

Now idk what values exactly i need to get from the Frame

1 answer

Log in to vote
0
Answered by 3 years ago

I set the anchor point to 0.5,0.5 and the position to 0.5,0.5 and made it centered!

1
Perfect solution, some times it the simple stuff ;) AlexTheCreator 461 — 3y
Ad

Answer this question