Advertisement
TheAHMguy

Untitled

Mar 19th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. local THUMBSTICK_DEADZONE = .4
  2. local RADIUS = .5
  3. local NUM_OPTIONS = 6
  4. local ANGLE_OFFSET = 90
  5.  
  6. -- Services
  7. local ContextActionService = game:GetService("ContextActionService")
  8. local GuiService = game:GetService("GuiService")
  9.  
  10. -- The menu uses left bumper which the backpack also uses. Disabling backpack UI to prevent conflict
  11. game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
  12.  
  13. local player = game.Players.LocalPlayer
  14. local menuItems = {}
  15.  
  16. -- Define position and size for the menu frame in both opened and closed states
  17. local menuOpenPosition = UDim2.new(0.25, 0, 0.25, 0)
  18. local menuOpenSize = UDim2.new(0.5, 0, 0.5, 0)
  19. local menuClosedPosition = UDim2.new(0.5, 0, 0.5, 0)
  20. local menuClosedSize = UDim2.new(0.005, 0, 0.005, 0)
  21.  
  22. -- Define the element for the menu options
  23. local itemTemplate = Instance.new("TextButton")
  24. itemTemplate.Size = UDim2.new(0.3, 0, 0.2, 0)
  25. itemTemplate.AutoButtonColor = false
  26. itemTemplate.TextScaled = true
  27.  
  28. -- Create a screen and frame for the menu
  29. local menuScreen = Instance.new("ScreenGui", player:WaitForChild("PlayerGui"))
  30. menuScreen.Name = "MenuScreen"
  31. local menuFrame = Instance.new("Frame", menuScreen)
  32. menuFrame.Size = menuClosedSize
  33. menuFrame.Position = menuClosedPosition
  34. menuFrame.Visible = false
  35. menuFrame.BackgroundTransparency = 1
  36. menuFrame.BorderSizePixel = 0
  37. menuFrame.Name = "MenuFrame"
  38.  
  39. -- Storage for AutoSelectGuiEnabled and GuiNavigationEnabled
  40. local wasAutoSelectGuiEnabled = GuiService.AutoSelectGuiEnabled
  41. local wasGuiNavigationEnabled = GuiService.GuiNavigationEnabled
  42.  
  43. local function newMenuItem(name, angle, range)
  44. local newItem = {}
  45. local button = itemTemplate:Clone()
  46. button.Text = name
  47. button.Name = name
  48. local angleRadians = math.rad(ANGLE_OFFSET + angle)
  49. button.Position = UDim2.new(.5 + RADIUS * math.cos(angleRadians) - button.Size.X.Scale / 2, 0,
  50. .5 - RADIUS * math.sin(angleRadians) - button.Size.Y.Scale / 2, 0)
  51. button.Parent = menuFrame
  52. newItem.Label = button
  53. newItem.Vector = Vector2.new(math.cos(angleRadians), math.sin(angleRadians))
  54. newItem.Range = range
  55. table.insert(menuItems, newItem)
  56. end
  57.  
  58. for i = 1, NUM_OPTIONS do
  59. local angle = (360 / NUM_OPTIONS) * (i - 1)
  60. local name = "Option" .. i
  61. newMenuItem(name, angle, 360 / NUM_OPTIONS)
  62. end
  63.  
  64. -- Goes through the menuItems table and finds the angle between each item's vector and
  65. -- the passed in vector. If the angle is less than half the item's range, then that item
  66. -- is selected.
  67. local function getButtonFromVector(vector)
  68. for i = 1, #menuItems do
  69. local item = menuItems[i]
  70. local dotProduct = vector.X * item.Vector.X + vector.Y * item.Vector.Y
  71. local angle = math.acos(dotProduct / vector.magnitude)
  72. if angle <= math.rad(item.Range) / 2 then
  73. return item.Button
  74. end
  75. end
  76. return nil
  77. end
  78.  
  79. -- Function bound to left thumbstick movement.
  80. local function onThumbstickMoved(actionName, inputState, inputObject)
  81. -- Make sure the thumbstick was moved past the deadzone
  82. if inputObject.Position.magnitude >= THUMBSTICK_DEADZONE then
  83. -- Calculate the angle based on the position of the thumbstick
  84. local selectedButton = getButtonFromVector(inputObject.Position)
  85. GuiService.SelectedObject = selectedButton
  86. else
  87. -- Thumbstick inside deadzone, clear selection
  88. GuiService.SelectedObject = nil
  89. end
  90. end
  91.  
  92. -- Function to call when item is selected. This is where you should put your custom code
  93. -- to implement your menu
  94. local function onMenuSelect(option)
  95. print(option, "selected")
  96. end
  97.  
  98. local function openMenu()
  99. -- Store GuiNavigationEnabled and AutoSelectGuiEnabled and then set both to false
  100. wasGuiNavigationEnabled = GuiService.GuiNavigationEnabled
  101. wasAutoSelectGuiEnabled = GuiService.AutoSelectGuiEnabled
  102. GuiService.GuiNavigationEnabled = false
  103. GuiService.AutoSelectGuiEnabled = false
  104.  
  105. -- Bind onThumbstickMoved function
  106. ContextActionService:BindAction("RadialMenu", onThumbstickMoved, false, Enum.KeyCode.Thumbstick1)
  107.  
  108. -- Make sure frame is visible and play opening animation
  109. menuFrame.Visible = true
  110. menuFrame:TweenSizeAndPosition(menuOpenSize, menuOpenPosition, Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .5, true)
  111. menuOpen = true
  112. end
  113.  
  114. local function closeMenu()
  115. -- Restore GuiNavigationEnabled and AutoSelectGuiEnabled
  116. GuiService.GuiNavigationEnabled = wasGuiNavigationEnabled
  117. GuiService.AutoSelectGuiEnabled = wasAutoSelectGuiEnabled
  118.  
  119. -- Unbind onThumbstickMoved function
  120. ContextActionService:UnbindAction("RadialMenu")
  121.  
  122. if GuiService.SelectedObject then
  123. -- If there is a selection when the menu closed, this is the option the user wanted
  124. onMenuSelect(GuiService.SelectedObject)
  125. end
  126.  
  127. -- Clear selected object and play closing animation
  128. GuiService.SelectedObject = nil
  129. menuFrame:TweenSizeAndPosition(menuClosedSize, menuClosedPosition, Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .4, true,
  130. function()
  131. -- Callback function at end of animation. If the user hasn't reopened the menu hide it
  132. if not menuOpen then
  133. menuFrame.Visible = false
  134. end
  135. end)
  136. menuOpen = false
  137. end
  138.  
  139. local function toggleMenu(actionName, inputState)
  140. if inputState == Enum.UserInputState.Begin then
  141. openMenu()
  142. elseif inputState == Enum.UserInputState.End then
  143. closeMenu()
  144. end
  145. end
  146.  
  147. ContextActionService:BindAction("ToggleMenu", toggleMenu, false, Enum.KeyCode.ButtonL1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement