Guest User

Untitled

a guest
Nov 9th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1.  
  2. module( "properties", package.seeall )
  3.  
  4. local meta = {
  5. MsgStart = function( self )
  6.  
  7. net.Start( "propertiess" )
  8. net.WriteString( self.InternalName )
  9.  
  10. end,
  11.  
  12. MsgEnd = function( self )
  13.  
  14. net.SendToServer()
  15.  
  16. end
  17. }
  18.  
  19. meta.__index = meta
  20.  
  21. List = {}
  22. -- .MenuLabel [string] Label to show on opened menu
  23. -- .Filter( ent ) [function] Return true if we should show a menu for this entity
  24. -- .Action( ent ) [function] On menu choice selected
  25. -- .Order [int] The order in which it should be shown on the menu
  26. -- .Receive( len, ply ) [function] A message has been received from the clientside version
  27.  
  28. function Add( name, tab )
  29.  
  30. name = name:lower()
  31. tab.InternalName = name
  32. setmetatable( tab, meta )
  33.  
  34. List[ name ] = tab
  35.  
  36. end
  37.  
  38. local function AddToggleOption( data, menu, ent, ply, tr )
  39.  
  40. if ( !menu.ToggleSpacer ) then
  41. menu.ToggleSpacer = menu:AddSpacer()
  42. menu.ToggleSpacer:SetZPos( 500 )
  43. end
  44.  
  45. local option = menu:AddOption( data.MenuLabel, function() data:Action( ent, tr ) end )
  46. option:SetChecked( data:Checked( ent, ply ) )
  47. option:SetZPos( 501 )
  48. return option
  49.  
  50. end
  51.  
  52. local function AddOption( data, menu, ent, ply, tr )
  53.  
  54. if ( data.Type == "toggle" ) then return AddToggleOption( data, menu, ent, ply, tr ) end
  55.  
  56. if ( data.PrependSpacer ) then
  57. menu:AddSpacer()
  58. end
  59.  
  60. local option = menu:AddOption( data.MenuLabel, function() data:Action( ent, tr ) end )
  61.  
  62. if ( data.MenuIcon ) then
  63. option:SetImage( data.MenuIcon )
  64. end
  65.  
  66. if ( data.MenuOpen ) then
  67. data.MenuOpen( data, option, ent, tr )
  68. end
  69.  
  70. return option
  71.  
  72. end
  73.  
  74. function OpenEntityMenu( ent, tr )
  75.  
  76. local menu = DermaMenu()
  77.  
  78. for k, v in SortedPairsByMemberValue( List, "Order" ) do
  79.  
  80. if ( !v.Filter ) then continue end
  81. if ( !v:Filter( ent, LocalPlayer() ) ) then continue end
  82.  
  83. local option = AddOption( v, menu, ent, LocalPlayer(), tr )
  84.  
  85. if ( v.OnCreate ) then v:OnCreate( menu, option ) end
  86.  
  87. end
  88.  
  89. menu:Open()
  90.  
  91. end
  92.  
  93. function GetHovered( eyepos, eyevec )
  94.  
  95. local filter = { LocalPlayer():GetViewEntity() }
  96. if ( LocalPlayer():GetViewEntity() == LocalPlayer() && IsValid( LocalPlayer():GetVehicle() ) && !LocalPlayer():GetVehicle():GetThirdPersonMode() ) then table.insert( filter, LocalPlayer():GetVehicle() ) end
  97.  
  98. local trace = util.TraceLine( {
  99. start = eyepos,
  100. endpos = eyepos + eyevec * 1024,
  101. filter = filter
  102. } )
  103.  
  104. -- Hit COLLISION_GROUP_DEBRIS and stuff
  105. if ( !trace.Hit || !IsValid( trace.Entity ) ) then
  106. trace = util.TraceLine( {
  107. start = eyepos,
  108. endpos = eyepos + eyevec * 1024,
  109. filter = filter,
  110. mask = MASK_ALL
  111. } )
  112. end
  113.  
  114. if ( !trace.Hit || !IsValid( trace.Entity ) ) then return end
  115.  
  116. return trace.Entity, trace
  117.  
  118. end
  119.  
  120. function OnScreenClick( eyepos, eyevec )
  121.  
  122. local ent, tr = GetHovered( eyepos, eyevec )
  123. if ( !IsValid( ent ) ) then return end
  124.  
  125. OpenEntityMenu( ent, tr )
  126.  
  127. end
  128.  
  129. -- Receives commands from clients
  130. if ( SERVER ) then
  131.  
  132. util.AddNetworkString( "propertiess" )
  133.  
  134. net.Receive( "propertiess", function( len, client )
  135. if ( !IsValid( client ) ) then return end
  136.  
  137. local name = net.ReadString()
  138. if ( !name ) then return end
  139.  
  140. local prop = List[ name ]
  141. if ( !prop ) then return end
  142. if ( !prop.Receive ) then return end
  143.  
  144. prop:Receive( len, client )
  145.  
  146. end )
  147.  
  148. end
  149.  
  150. if ( CLIENT ) then
  151.  
  152. hook.Add( "PreDrawHalos", "PropertiesHover", function()
  153.  
  154. if ( !IsValid( vgui.GetHoveredPanel() ) || vgui.GetHoveredPanel() != g_ContextMenu ) then return end
  155.  
  156. local ent = GetHovered( EyePos(), LocalPlayer():GetAimVector() )
  157. if ( !IsValid( ent ) ) then return end
  158.  
  159. local c = Color( 255, 255, 255, 255 )
  160. c.r = 200 + math.sin( RealTime() * 50 ) * 55
  161. c.g = 200 + math.sin( RealTime() * 20 ) * 55
  162. c.b = 200 + math.cos( RealTime() * 60 ) * 55
  163.  
  164. local t = { ent }
  165. if ( ent.GetActiveWeapon && IsValid( ent:GetActiveWeapon() ) ) then table.insert( t, ent:GetActiveWeapon() ) end
  166. halo.Add( t, c, 2, 2, 2, true, false )
  167.  
  168. end )
  169.  
  170. --
  171. -- Hook the GUIMousePressed call, which is called when the client clicks on the
  172. -- gui.
  173. --
  174. hook.Add( "GUIMousePressed", "PropertiesClick", function( code, vector )
  175.  
  176. if ( !IsValid( vgui.GetHoveredPanel() ) || vgui.GetHoveredPanel() != g_ContextMenu ) then return end
  177.  
  178. if ( code == MOUSE_RIGHT && !input.IsButtonDown( MOUSE_LEFT ) ) then
  179. OnScreenClick( EyePos(), vector )
  180. end
  181.  
  182. end )
  183.  
  184. --
  185. -- Hook the GUIMousePressed call, which is called when the client clicks on the
  186. -- gui.
  187. --
  188. local wasPressed = false
  189. hook.Add( "PreventScreenClicks", "PropertiesPreventClicks", function()
  190.  
  191. if ( !input.IsButtonDown( MOUSE_RIGHT ) ) then wasPressed = false end
  192.  
  193. if ( wasPressed && input.IsButtonDown( MOUSE_RIGHT ) && !input.IsButtonDown( MOUSE_LEFT ) ) then return true end
  194.  
  195. if ( !IsValid( vgui.GetHoveredPanel() ) || vgui.GetHoveredPanel() != g_ContextMenu ) then return end
  196.  
  197. local ply = LocalPlayer()
  198. if ( !IsValid( ply ) ) then return end
  199.  
  200. --
  201. -- Are we pressing the right mouse button?
  202. -- (We check whether we're pressing the left too, to allow for physgun freezes)
  203. --
  204. if ( input.IsButtonDown( MOUSE_RIGHT ) && !input.IsButtonDown( MOUSE_LEFT ) ) then
  205.  
  206. --
  207. -- Are we hovering an entity? If so, then stomp the action
  208. --
  209. local hovered = GetHovered( EyePos(), ply:GetAimVector() )
  210.  
  211. if ( IsValid( hovered ) ) then
  212. wasPressed = true
  213. return true
  214. end
  215.  
  216. end
  217.  
  218. end )
  219.  
  220. end
Add Comment
Please, Sign In to add comment