Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local THUMBSTICK_DEADZONE = .4
- local RADIUS = .5
- local NUM_OPTIONS = 6
- local ANGLE_OFFSET = 90
- -- Services
- local ContextActionService = game:GetService("ContextActionService")
- local GuiService = game:GetService("GuiService")
- -- The menu uses left bumper which the backpack also uses. Disabling backpack UI to prevent conflict
- game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
- local player = game.Players.LocalPlayer
- local menuItems = {}
- -- Define position and size for the menu frame in both opened and closed states
- local menuOpenPosition = UDim2.new(0.25, 0, 0.25, 0)
- local menuOpenSize = UDim2.new(0.5, 0, 0.5, 0)
- local menuClosedPosition = UDim2.new(0.5, 0, 0.5, 0)
- local menuClosedSize = UDim2.new(0.005, 0, 0.005, 0)
- -- Define the element for the menu options
- local itemTemplate = Instance.new("TextButton")
- itemTemplate.Size = UDim2.new(0.3, 0, 0.2, 0)
- itemTemplate.AutoButtonColor = false
- itemTemplate.TextScaled = true
- -- Create a screen and frame for the menu
- local menuScreen = Instance.new("ScreenGui", player:WaitForChild("PlayerGui"))
- menuScreen.Name = "MenuScreen"
- local menuFrame = Instance.new("Frame", menuScreen)
- menuFrame.Size = menuClosedSize
- menuFrame.Position = menuClosedPosition
- menuFrame.Visible = false
- menuFrame.BackgroundTransparency = 1
- menuFrame.BorderSizePixel = 0
- menuFrame.Name = "MenuFrame"
- -- Storage for AutoSelectGuiEnabled and GuiNavigationEnabled
- local wasAutoSelectGuiEnabled = GuiService.AutoSelectGuiEnabled
- local wasGuiNavigationEnabled = GuiService.GuiNavigationEnabled
- local function newMenuItem(name, angle, range)
- local newItem = {}
- local button = itemTemplate:Clone()
- button.Text = name
- button.Name = name
- local angleRadians = math.rad(ANGLE_OFFSET + angle)
- button.Position = UDim2.new(.5 + RADIUS * math.cos(angleRadians) - button.Size.X.Scale / 2, 0,
- .5 - RADIUS * math.sin(angleRadians) - button.Size.Y.Scale / 2, 0)
- button.Parent = menuFrame
- newItem.Label = button
- newItem.Vector = Vector2.new(math.cos(angleRadians), math.sin(angleRadians))
- newItem.Range = range
- table.insert(menuItems, newItem)
- end
- for i = 1, NUM_OPTIONS do
- local angle = (360 / NUM_OPTIONS) * (i - 1)
- local name = "Option" .. i
- newMenuItem(name, angle, 360 / NUM_OPTIONS)
- end
- -- Goes through the menuItems table and finds the angle between each item's vector and
- -- the passed in vector. If the angle is less than half the item's range, then that item
- -- is selected.
- local function getButtonFromVector(vector)
- for i = 1, #menuItems do
- local item = menuItems[i]
- local dotProduct = vector.X * item.Vector.X + vector.Y * item.Vector.Y
- local angle = math.acos(dotProduct / vector.magnitude)
- if angle <= math.rad(item.Range) / 2 then
- return item.Button
- end
- end
- return nil
- end
- -- Function bound to left thumbstick movement.
- local function onThumbstickMoved(actionName, inputState, inputObject)
- -- Make sure the thumbstick was moved past the deadzone
- if inputObject.Position.magnitude >= THUMBSTICK_DEADZONE then
- -- Calculate the angle based on the position of the thumbstick
- local selectedButton = getButtonFromVector(inputObject.Position)
- GuiService.SelectedObject = selectedButton
- else
- -- Thumbstick inside deadzone, clear selection
- GuiService.SelectedObject = nil
- end
- end
- -- Function to call when item is selected. This is where you should put your custom code
- -- to implement your menu
- local function onMenuSelect(option)
- print(option, "selected")
- end
- local function openMenu()
- -- Store GuiNavigationEnabled and AutoSelectGuiEnabled and then set both to false
- wasGuiNavigationEnabled = GuiService.GuiNavigationEnabled
- wasAutoSelectGuiEnabled = GuiService.AutoSelectGuiEnabled
- GuiService.GuiNavigationEnabled = false
- GuiService.AutoSelectGuiEnabled = false
- -- Bind onThumbstickMoved function
- ContextActionService:BindAction("RadialMenu", onThumbstickMoved, false, Enum.KeyCode.Thumbstick1)
- -- Make sure frame is visible and play opening animation
- menuFrame.Visible = true
- menuFrame:TweenSizeAndPosition(menuOpenSize, menuOpenPosition, Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .5, true)
- menuOpen = true
- end
- local function closeMenu()
- -- Restore GuiNavigationEnabled and AutoSelectGuiEnabled
- GuiService.GuiNavigationEnabled = wasGuiNavigationEnabled
- GuiService.AutoSelectGuiEnabled = wasAutoSelectGuiEnabled
- -- Unbind onThumbstickMoved function
- ContextActionService:UnbindAction("RadialMenu")
- if GuiService.SelectedObject then
- -- If there is a selection when the menu closed, this is the option the user wanted
- onMenuSelect(GuiService.SelectedObject)
- end
- -- Clear selected object and play closing animation
- GuiService.SelectedObject = nil
- menuFrame:TweenSizeAndPosition(menuClosedSize, menuClosedPosition, Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .4, true,
- function()
- -- Callback function at end of animation. If the user hasn't reopened the menu hide it
- if not menuOpen then
- menuFrame.Visible = false
- end
- end)
- menuOpen = false
- end
- local function toggleMenu(actionName, inputState)
- if inputState == Enum.UserInputState.Begin then
- openMenu()
- elseif inputState == Enum.UserInputState.End then
- closeMenu()
- end
- end
- ContextActionService:BindAction("ToggleMenu", toggleMenu, false, Enum.KeyCode.ButtonL1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement