Advertisement
Guest User

efsrfdsgdfg

a guest
Sep 1st, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.19 KB | None | 0 0
  1. //Clientside file only
  2. local fileLimit = CreateClientConVar("SoundMenu_fileLimit", "1000") //Limit of files in a folder before we start cutting it off
  3. local SM
  4. local binds = {}
  5. local soundPlaying = false
  6. local formats = {".wav", ".ogg", ".mp3"}
  7.  
  8. local function SoundMenu()
  9.  
  10.     local keyDown = false
  11.    
  12.     hook.Add("Think", "SM_PressedSpace", function()
  13.         if input.IsKeyDown(KEY_SPACE) and !keyDown then
  14.             SM.Btn1.DoClick()
  15.             keyDown = true
  16.             timer.Simple(0.1, function() keyDown = false end)
  17.         end
  18.     end)
  19.    
  20.     if SM then SM.Frame:SetVisible(true) return end
  21.    
  22.     SM = {}
  23.    
  24.     SM.Frame = vgui.Create("DFrame")
  25.     SM.Frame:SetTitle("Sound Browser")
  26.     SM.Frame:SetSize(ScrW()/3, 2/3 * ScrH() + 30)
  27.     SM.Frame:SetPos(ScrW()/2 - SM.Frame:GetWide()/2, ScrH()/2 - SM.Frame:GetTall()/2)
  28.     SM.Frame:SetDraggable(true)
  29.     SM.Frame:ShowCloseButton(true)
  30.     SM.Frame:SetDeleteOnClose(false)
  31.     SM.Frame:MakePopup()
  32.    
  33.     SM.Tree = vgui.Create("DTree", SM.Frame)
  34.     SM.Tree:SetPos(15, 40)
  35.     SM.Tree:SetSize(SM.Frame:GetWide() - 30, SM.Frame:GetTall() - 131)
  36.     SM.Tree:SetPadding(5)
  37.    
  38.     SM.Btn1 = vgui.Create("DButton", SM.Frame)
  39.     SM.Btn1:SetPos(15, SM.Frame:GetTall() - 73)
  40.     SM.Btn1:SetSize(SM.Tree:GetWide()/3 - 2, 28)
  41.     SM.Btn1:SetText("Play Sound")
  42.     SM.Btn1:SetTooltip("Use the spacebar as a hotkey!")
  43.    
  44.     SM.Btn2 = vgui.Create("DButton", SM.Frame)
  45.     SM.Btn2:SetPos(SM.Tree:GetWide()/3 + 17, SM.Frame:GetTall() - 73)
  46.     SM.Btn2:SetSize(SM.Tree:GetWide()/3 - 2, 28)
  47.     SM.Btn2:SetText("Stop Sound")
  48.  
  49.     SM.Btn3 = vgui.Create("DButton", SM.Frame)
  50.     SM.Btn3:SetPos(SM.Tree:GetWide()*2/3 + 18, SM.Frame:GetTall() - 73)
  51.     SM.Btn3:SetSize(SM.Tree:GetWide()/3 - 2, 28)
  52.     SM.Btn3:SetText("Copy Filepath")
  53.  
  54.     SM.Btn4 = vgui.Create("DButton", SM.Frame)
  55.     SM.Btn4:SetPos(15, SM.Frame:GetTall() - 90)
  56.     SM.Btn4:SetSize(SM.Tree:GetWide(), 15)
  57.     SM.Btn4:SetText("Refresh List")
  58.    
  59.     SM.Btn5 = vgui.Create("DBinder", SM.Frame)
  60.     SM.Btn5:SetPos(15, SM.Frame:GetTall() - 43)
  61.     SM.Btn5:SetSize(SM.Tree:GetWide()/3 - 2, 28)
  62.     SM.Btn5:SetTooltip("Bind this sound to a key")
  63.     SM.Btn5:SetDisabled(true)
  64.    
  65.     SM.Btn6 = vgui.Create("DButton", SM.Frame)
  66.     SM.Btn6:SetPos(SM.Tree:GetWide()/3 + 17, SM.Frame:GetTall() - 43)
  67.     SM.Btn6:SetSize(SM.Tree:GetWide()/3 - 2, 28)
  68.     SM.Btn6:SetText("Unbind")
  69.     SM.Btn6:SetDisabled(true)
  70.        
  71.     SM.ChkBox = vgui.Create("DCheckBoxLabel", SM.Frame)
  72.     SM.ChkBox:SetText("Disable All Bindings")
  73.     SM.ChkBox:SizeToContents()
  74.     SM.ChkBox:SetPos(SM.Tree:GetWide()*2/3 + 18, SM.Frame:GetTall() + SM.ChkBox:GetTall()/2 - 43)
  75.    
  76.     SM.TreeNode = SM.Tree:AddNode("sound")
  77.     SM.TreeNode.dir  = "sound/"
  78.     SM.TreeNode.gen = false
  79.  
  80.     //
  81.     local function FindSounds(node, dir)
  82.         local files, dirs = file.Find(dir.."*", "GAME")
  83.    
  84.         for _,v in pairs(dirs) do
  85.             local newNode = node:AddNode(v)
  86.             newNode.dir = dir..v
  87.             newNode.gen = false
  88.        
  89.             newNode.DoClick = function()
  90.                 if !newNode.gen then
  91.                     FindSounds(newNode, dir..v.."/")
  92.                     newNode.gen = true
  93.                 end
  94.             end
  95.         end
  96.    
  97.         local function GenerateNodes()
  98.             local fileCount = 0
  99.  
  100.             for k,v in pairs(files) do
  101.                 if fileCount > fileLimit:GetInt() then break end
  102.                 local format = string.sub(v, -4)
  103.                 if format and table.HasValue(formats, format) then
  104.                     fileCount = fileCount + 1
  105.  
  106.                     local newNode = node:AddNode(v)
  107.                     newNode.file   = v
  108.                     newNode.dir    = dir
  109.                     newNode.IsFile = true
  110.                     newNode.format = format
  111.                     newNode.Icon:SetImage("icon16/sound.png")
  112.  
  113.                     files[k] = ""
  114.                 end
  115.             end
  116.        
  117.             if fileCount > fileLimit:GetInt() then
  118.                 local newNode = node:AddNode("Click to load more files...")
  119.                 newNode.Icon:SetImage("icon16/sound_add.png")
  120.                 newNode.DoClick = function()
  121.                     newNode:Remove()
  122.                     GenerateNodes()
  123.                 end
  124.             end
  125.         end
  126.         GenerateNodes()
  127.     end
  128.     FindSounds(SM.TreeNode, "sound/")
  129.     //
  130.    
  131.     SM.Btn1.DoClick = function()
  132.         local item = SM.Tree:GetSelectedItem()
  133.         if !item or !item.IsFile then return end
  134.         local file = string.sub(item.dir, 7)..item:GetText()
  135.         RunConsoleCommand("stopsound")
  136.         timer.Simple(0.1, function() surface.PlaySound(file) end)
  137.     end
  138.    
  139.     SM.Btn2.DoClick = function()
  140.         RunConsoleCommand("stopsound")
  141.     end
  142.  
  143.     SM.Btn3.DoClick = function()
  144.         local item = SM.Tree:GetSelectedItem()
  145.         if !item or !item.IsFile then return end
  146.         local file = string.sub(item.dir, 7)..item:GetText()
  147.         SetClipboardText(file)
  148.     end
  149.    
  150.     SM.Btn4.DoClick = function()
  151.         SM.TreeNode:Remove()
  152.         SM.TreeNode = SM.Tree:AddNode("sound")
  153.         SM.TreeNode.dir  = "sound/"
  154.         SM.TreeNode.gen = false
  155.    
  156.         FindSounds(SM.TreeNode, "sound/")
  157.     end
  158.    
  159.     local oldClick = SM.Btn5.DoClick
  160.    
  161.     SM.Btn5.DoClick = function()
  162.         oldClick(SM.Btn5)
  163.         local item = SM.Tree:GetSelectedItem()
  164.         local value = SM.Btn5:GetValue()
  165.         hook.Add("Think", "SM_CheckValue", function()
  166.             if SM.Btn5:GetValue() != value then
  167.                 hook.Remove("Think", "SM_CheckValue")
  168.                 local file = string.sub(item.dir, 7)..item:GetText()
  169.                 binds[file] = SM.Btn5:GetValue()
  170.                 SM.Btn6:SetDisabled(false)
  171.             end
  172.         end)
  173.     end
  174.    
  175.     SM.Btn6.DoClick = function()
  176.         local item = SM.Tree:GetSelectedItem()
  177.         local file = string.sub(item.dir, 7)..item:GetText()
  178.         if binds[file] then binds[file] = nil end
  179.         SM.Btn5:SetValue(0)
  180.         SM.Btn6:SetDisabled(true)
  181.     end
  182.    
  183.     SM.Tree.DoClick = function()
  184.         local item = SM.Tree:GetSelectedItem()
  185.         if !item then return end
  186.         if item.IsFile then
  187.             local file = string.sub(item.dir, 7)..item:GetText()
  188.             SM.Btn5:SetDisabled(false)
  189.             if binds[file] and binds[file] != 0 then
  190.                 SM.Btn5:SetValue(binds[file])
  191.                 SM.Btn6:SetDisabled(false)
  192.             else
  193.                 SM.Btn5:SetValue(0)
  194.             end
  195.         else
  196.             SM.Btn5:SetDisabled(true)
  197.         end
  198.     end
  199.    
  200.     SM.Frame.OnClose = function()
  201.         hook.Remove("Think", "SM_PressedSpace")
  202.         hook.Remove("Think", "SM_CheckValue")
  203.     end
  204.    
  205.     hook.Add("Think", "SM_CheckBind", function()
  206.         if SM.ChkBox:GetChecked() then return end
  207.         for k,v in pairs(binds) do
  208.             if input.IsKeyDown(v) and !soundPlaying then
  209.                 soundPlaying = true
  210.                 timer.Simple(0.25, function() soundPlaying = false end)
  211.                 surface.PlaySound(k)
  212.             end
  213.         end
  214.     end)
  215.    
  216. end
  217.  
  218. SoundMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement