All pastes #278937 Raw Edit

Someone

public text v1 · immutable
·published 2006-12-14 15:22 UTC
rendered paste body
TOOL.Category		= "Advanced Construction"
TOOL.Name			= "#Macro Button"
TOOL.Command		= nil
TOOL.ConfigName		= ""

TOOL.ClientConVar[ "model" ] = "models/props_c17/clock01.mdl"
TOOL.ClientConVar[ "keygroup" ] = "1"
TOOL.ClientConVar[ "description" ] = ""

cleanup.Register( "buttons" )

if ( CLIENT ) then

	language.Add( "Tool_mb_name", "Macro button tool" )
	language.Add( "Tool_mb_desc", "Creates a chain of key-presses" )
	language.Add( "Tool_mb_0", "Aim and shoot to place the button!" )
	language.Add( "Tool_mb_col", "Select colour" )
	language.Add( "Tool_mb_tog", "Toggles alpha on/off (Transparency)" )

end

function TOOL:RightClick( trace )

	if trace.Entity && trace.Entity:IsPlayer() then return false end
	if (CLIENT) then return true end
	
	local ply = self:GetOwner()
	
	local model				= self:GetClientInfo( "model" )
	local key 				= self:GetClientNumber( "keygroup" )
	local description		= self:GetClientInfo( "description" )

	// If we shot a button change its keygroup
	if ( trace.Entity:IsValid() && 
		 trace.Entity:GetClass() == "macro_button" && 
		 trace.Entity:GetTable():GetPlayer() == ply ) then

		trace.Entity:GetTable():SetKey( key )
		trace.Entity:GetTable():SetLabel( description )
		
		return true, NULL, true
		
	end
	
	if ( !self:GetSWEP():CheckLimit( "buttons" ) ) then return false end

	if (not util.IsValidModel(model)) then return false end
	if (not util.IsValidProp(model)) then return false end		// Allow ragdolls to be used?

	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90

	button = MakeButton( ply, model, Ang, trace.HitPos, key, description )
	
	local min = button:OBBMins()
	button:SetPos( trace.HitPos - trace.HitNormal * min.z )
	
	local const
	
	undo.Create("Button")
		undo.AddEntity( button )
		undo.SetPlayer( ply )
	undo.Finish()

	ply:AddCleanup( "buttons", button )
	
	return true, button

end

function TOOL:LeftClick( trace )

	bool, button, set_key = self:RightClick( trace, true )
	if (CLIENT) then return bool end

	if ( set_key ) then return true end
	if ( !button || !button:IsValid() ) then return false end
	if ( !trace.Entity:IsValid() && !trace.Entity:IsWorld() ) then return false end

	local weld = constraint.Weld( button, trace.Entity, 0, trace.PhysicsBone, 0 )
	trace.Entity:DeleteOnRemove( button )
	trace.Entity:DeleteOnRemove( weld )
	button:DeleteOnRemove( weld )

	button:GetPhysicsObject():EnableCollisions( false )
	button:GetTable().nocollide = true
	
	return true

end

if (SERVER) then

	function MakeButton( pl, Model, Ang, Pos, key, description, nocollide, Vel, aVel, frozen )
	
		if ( !pl:CheckLimit( "buttons" ) ) then return false end
	
		local button = ents.Create( "macro_button" )
		if (!button:IsValid()) then return false end
		button:SetModel( Model )

		button:SetAngles( Ang )
		button:SetPos( Pos )
		button:Spawn()
		
		button:GetTable():SetPlayer( pl )
		button:GetTable():SetKey( key )
		button:GetTable():SetLabel( description )

		if nocollide then button:GetPhysicsObject():EnableCollisions( false ) end

		local ttable = 
			{
				key	= key,
				pl	= pl,
				nocollide = nocollide,
				description = description
			}

		table.Merge(button:GetTable(), ttable )
		
		pl:AddCount( "buttons", button )

		return button
		
	end
	
	duplicator.RegisterEntityClass( "macro_button", MakeButton, "Model", "Ang", "Pos", "key", "description", "nocollide", "Vel", "aVel", "frozen" )

end

function TOOL:UpdateGhostButton( ent, player )

	if ( !ent ) then return end
	if ( !ent:IsValid() ) then return end

	local tr 	= utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace 	= util.TraceLine( tr )
	if (!trace.Hit) then return end
	
	if (trace.Entity && trace.Entity:GetClass() == "macro_button" || trace.Entity:IsPlayer()) then
	
		ent:SetNoDraw( true )
		return
		
	end

	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	
	local min = ent:OBBMins()
	 ent:SetPos( trace.HitPos - trace.HitNormal * min.z )
	ent:SetAngles( Ang )
	
	ent:SetNoDraw( false )
	
end


function TOOL:Think()

	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != self:GetClientInfo( "model" )) then
		self:MakeGhostEntity( self:GetClientInfo( "model" ), Vector(0,0,0), Angle(0,0,0) )
	end
	
	self:UpdateGhostButton( self.GhostEntity, self:GetOwner() )
	
end

function TOOL.BuildCPanel( cp )
	cp:AddControl( "Header", { Text = "#Tool_mb_name", Description	= "#Tool_mb_desc" }  )
	//cp:AddControl("Slider", { Label = "#Tool_macro_button_timer", Type = "Numeric", Min = "1", Max = "60", Command = "macro_button_timer1" } )
	//cp:AddControl("CheckBox", { Label = "#Tool_macro_button_tog", Command = "macro_button_toggle" } )
end