Advertisement
advancedev

chat

Jan 21st, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. --[[ Menu Help:
  2.  
  3. Use Menu.Structure to create the structure of the menu
  4.  
  5. indexing syntax:
  6. "Order$FIndex$Text"
  7. Order : A number that indicates the order of which the menu items will appear
  8. FIndex : Used for indexing a function in Menu.Handling (also sets the Name property)
  9. Text : Sets the text to be displayed on the menu item
  10.  
  11. Note: "0$Menu$" is the same as "0$Menu$Menu"
  12.  
  13.  
  14. Use Menu.Config to configure the display of the menus.
  15. Setting the size and position will affect only the top menu.
  16.  
  17.  
  18. Use Menu.Alignment for positioning submenus in
  19. relation to their parent menu.
  20.  
  21. Down=1; Right=1; Menus will expand to the right and downward
  22. Down=-1; Right=1; Menus will expand to the right and upward
  23. Down=1; Right=-1; Menus will expand to the left and downward
  24. Down=-1; Right=-1; Menus will expand to the left and upward
  25.  
  26.  
  27. Use Menu.Handle to hold functions used when a menu item is clicked
  28.  
  29. When you click on a menu item, using the FIndex
  30. you specifed, it will look up the index in the
  31. Menu.Handle table. If it finds a match, it will call
  32. the function, passing the player and the menu
  33. item as arguments.
  34. ]]
  35.  
  36. Style = { -- basic color scheme
  37. Index = {
  38. ["Background"] = Color3.new(0/255,32/255,96/255),
  39. ["Border"] = Color3.new(33/255,84/255,185/255),
  40. ["Selection"] = Color3.new(245/255,205/255,48/255),
  41. ["Text"] = Color3.new(171/255,214/255,255/255),
  42. ["Transparency"] = 0.5,
  43. },
  44. SetBGColor = function(object,color) object.BackgroundColor3 = Style.Index[color] end,
  45. }
  46.  
  47.  
  48. Menu = {
  49. Structure = { -- the menu structure
  50. ["0$Menu$"] = {
  51. ["0$Reset$"] = {},
  52. ["1$Example$Menu Item 2"] = {
  53. ["0$Example$Menu Item 1"] = {},
  54. ["1$Example$Menu Item 2"] = {},
  55. },
  56. ["2$Example$Menu Item 3"] = {
  57. ["0$Example$Menu Item 1"] = {},
  58. ["1$Example$Menu Item 2"] = {
  59. ["0$Example$Menu Item 1"] = {},
  60. ["1$Example$Menu Item 2"] = {},
  61. },
  62. ["2$Example$Menu Item 3"] = {},
  63. },
  64. ["3$Example$Menu Item 4"] = {
  65. ["0$Example$Menu Item 1"] = {},
  66. },
  67. },
  68. },
  69. Config = { -- configures the top menu
  70. BackgroundColor3 = Style.Index["Background"],
  71. BorderColor3 = Style.Index["Border"],
  72. TextColor3 = Style.Index["Text"],
  73. BackgroundTransparency = Style.Index["Transparency"],
  74. Position = UDim2.new(0,500,0,-20),
  75. Size = UDim2.new(0,100,0,20),
  76. },
  77. Alignment = { -- aligns the menus in relation to the top menu
  78. Down = 1, -- -1 for Up
  79. Right = 1, -- -1 for Left
  80. },
  81. Handle = { -- handles menu functions
  82. ["Reset"] = function(player)
  83. player.Character:BreakJoints()
  84. end,
  85. },
  86. }
  87.  
  88. local events = { -- used for handling gui events
  89. MouseButton1Click = {},
  90. MouseButton1Down = {},
  91. MouseButton1Up = {},
  92. MouseButton2Click = {},
  93. MouseButton2Down = {},
  94. MouseButton2Up = {},
  95. MouseEnter = {},
  96. MouseLeave = {},
  97. MouseMoved = {},
  98. }
  99. for index,event in pairs(events) do
  100. setmetatable(event,{
  101. __call = function(t,object,func,arg)
  102. if object[index] then
  103. event[object] = {}
  104. local eventitem = event[object]
  105. eventitem["Enabled"] = true
  106. eventitem["Connection"] = object[index]:connect(function(...)
  107. if eventitem["Enabled"] then
  108. func(object,arg,...)
  109. end
  110. end)
  111. else
  112. error("object not validated by function",0)
  113. end
  114. end
  115. })
  116. end
  117.  
  118. Setup = { -- menu setup functions
  119. hideMenu = function(player,menu) -- hides a menu item and its submenus
  120. if menu:IsA"GuiButton" then
  121. menu.Visible = false
  122. Style.SetBGColor(menu,"Background")
  123. for _,submenu in pairs(menu:GetChildren()) do
  124. Setup.hideMenu(player,submenu)
  125. end
  126. end
  127. end,
  128. ToggleClick = function(player,value)
  129. -- Visible and Active don't seem to work properly for textbuttons
  130. if value then
  131. player.MenuClick.Value.Parent = player.PlayerGui.Screen
  132. else
  133. player.MenuClick.Value.Parent = nil
  134. end
  135. end,
  136. MenuHover = function(object,player) -- hides the submenus of the menu item's siblings and shows the menu item's submenus
  137. Setup.ToggleClick(player,false)
  138. local menu = player.PlayerGui.Screen.Menu
  139. for _,submenu in pairs(object.Parent:GetChildren()) do
  140. Style.SetBGColor(submenu,"Background")
  141. for _,submenu in pairs(submenu:GetChildren()) do
  142. Setup.hideMenu(player,submenu)
  143. end
  144. end
  145. Style.SetBGColor(object,"Selection")
  146. for _,submenu in pairs(object:GetChildren()) do
  147. if submenu:IsA"GuiButton" then
  148. submenu.Visible = true
  149. end
  150. end
  151. end,
  152. MenuClick = function(object,player) -- handles functions when menu items are clicked
  153. local run = Menu.Handle[object.Name]
  154. if run then
  155. coroutine.resume(coroutine.create(run),player,object)
  156. end
  157. local menu = player.PlayerGui.Screen.Menu
  158. Style.SetBGColor(menu,"Background")
  159. for _,submenu in pairs(menu:GetChildren()) do
  160. Setup.hideMenu(player,submenu)
  161. end
  162. Setup.ToggleClick(player,false)
  163. end,
  164. setupMenu = function(menu,player) -- sets up events for menus
  165. if menu:IsA"GuiButton" then
  166. events.MouseEnter(menu,Setup.MenuHover,player)
  167. events.MouseLeave(menu,function()
  168. Setup.ToggleClick(player,true)
  169. end)
  170. events.MouseButton1Click(menu,Setup.MenuClick,player)
  171. for _,submenu in pairs(menu:GetChildren()) do
  172. Setup.setupMenu(submenu,player)
  173. end
  174. end
  175. end,
  176. }
  177.  
  178. function CreateMenu(structure,config,align,first,parent) -- Generates menus from the structure and config
  179. for info,next in pairs(structure) do
  180. -- parse the index
  181. local parsed = {}
  182. info:gsub("(%d+)$(.+)$(.*)",function(p,n,n2)
  183. parsed[1] = tonumber(p) or p
  184. parsed[2] = n or ""
  185. parsed[3] = n2 ~= "" and n2 or n
  186. end)
  187. -- make a new button
  188. local button = Instance.new("TextButton")
  189. button.AutoButtonColor = false
  190. button.ZIndex = 2
  191. button.Name = parsed[2]
  192. button.Text = parsed[3]
  193. -- set properties in config
  194. for p,v in pairs(config) do button[p] = v end
  195. if not first then
  196. -- sub menus only
  197. local space = 4 -- spacing between menu items
  198. button.Position = UDim2.new(align.Right,space*align.Right,parsed[1]*align.Down,parsed[1]*space*align.Down)
  199. button.Size = UDim2.new(1,0,1,0)
  200. button.Visible = false
  201. end
  202. button.Parent = parent
  203. -- go to next
  204. CreateMenu(next,config,align,false,button)
  205. end
  206.  
  207. end
  208.  
  209. -- create a screen
  210. local screen = Instance.new("ScreenGui")
  211. screen.Name = "Screen"
  212. screen.Parent = game.StarterGui
  213. -- create the menuclick
  214. local mc = Instance.new("ImageButton")
  215. mc.Name = "MenuClick"
  216. mc.AutoButtonColor = false
  217. mc.BackgroundTransparency = 1
  218. mc.Position = UDim2.new(0,0,0,-21)
  219. mc.Size = UDim2.new(1,0,1,20)
  220. mc.Parent = screen
  221.  
  222. CreateMenu(Menu.Structure,Menu.Config,Menu.Alignment,true,screen)
  223.  
  224. function CharacterAdded(player)
  225. local screen = player.PlayerGui.Screen
  226. -- setup the events for the menu
  227. Setup.setupMenu(screen.Menu,player)
  228. -- make an event to close the menu when clicking outside of it
  229. events.MouseButton1Click(screen.MenuClick,Setup.MenuClick,player)
  230. player.MenuClick.Value = screen.MenuClick
  231. -- menu isn't active yet, so turn off
  232. Setup.ToggleClick(player,false)
  233. end
  234.  
  235. function onPlayerEntered(player)
  236. local s = Instance.new("ObjectValue") -- holds the menuclick
  237. s.Name = "MenuClick"
  238. s.Parent = player
  239. if player.Character then CharacterAdded(player) end
  240. player.CharacterAdded:connect(function() CharacterAdded(player) end)
  241. end
  242.  
  243.  
  244.  
  245. game.Players.PlayerAdded:connect(onPlayerEntered)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement