S&box Wiki

Revision Difference

Hammer_Gizmos#549767

<title>Hammer Gizmos</title>⤶ ⤶ # Hammer Gizmos⤶ ⤶ You can create interactive [Gizmos](Gizmos) in Hammer for your [game's map entities](Linking_Entities_to_Hammer).⤶ ⤶ This gives you a great way to visualize what your entities will do before baking your map.⤶ ⤶ <upload src="a5727/8db4d842202997c.mp4" size="2142726" name="gizmos_hammer.mp4" />⤶ ⤶ ⤶ ```csharp⤶ public partial class DoorEntity⤶ public static void DrawGizmos( EditorContext context )⤶ {⤶ // Get some key values from the entity⤶ var model = context.Target.GetProperty( "model" ).GetValue<string>();⤶ var distance = context.Target.GetProperty( "distance" ).GetValue<int>();⤶ ⤶ // Animate the distance⤶ var angle = MathF.Abs( MathF.Sin( RealTime.Now ) ) * distance;⤶ ⤶ // Ghost⤶ Gizmo.Draw.Color = Color.White.WithAlpha( 0.6f );⤶ Gizmo.Draw.Model( model, new Transform( default, Rotation.FromYaw( angle ) ) );⤶ }⤶ }⤶ ```⤶ ⤶ ## Interactive Gizmos⤶ ⤶ As well as displaying your entities' properties, you can also set them directly from interacting with gizmos.⤶ ⤶ ```csharp⤶ public static void DrawGizmos( EditorContext context )⤶ {⤶ var radiusProp = context.Target.GetProperty( "Radius" );⤶ var radius = radiusProp.As.Float;⤶ ⤶ // Draw custom circle radius control⤶ using ( Gizmo.Scope() )⤶ {⤶ var cameraDelta = Gizmo.Transform.Position - Gizmo.Camera.Position;⤶ ⤶ Gizmo.Transform = Gizmo.Transform.WithRotation( Rotation.LookAt( cameraDelta ) );⤶ ⤶ Gizmo.Hitbox.Circle( 0, Vector3.Forward, radius + 5, radius );⤶ ⤶ Gizmo.Draw.Color = Gizmo.Colors.Left.WithAlpha( Gizmo.IsHovered ? 1 : 0.3f );⤶ Gizmo.Draw.LineThickness = 4;⤶ Gizmo.Draw.IgnoreDepth = true;⤶ Gizmo.Draw.LineCircle( 0, radius + 4, sections: 64 );⤶ ⤶ if ( Gizmo.IsPressed )⤶ {⤶ var pos = Gizmo.GetPositionOnPlane( 0, Gizmo.Transform.NormalToLocal( cameraDelta.Normal ), Gizmo.CurrentRay ) ?? 0;⤶ ⤶ // update value⤶ radius = pos.Length;⤶ radiusProp.SetValue( radius );⤶ }⤶ }⤶ }⤶ ```⤶ ⤶ ## EditorContext⤶ ⤶ EditorContext provides a way for your game code to know details about what's going on in the map editor.⤶ ⤶ ### Selection⤶ ⤶ ```⤶ public static void DrawGizmos( EditorContext context )⤶ {⤶ // Display only if our entity is selected⤶ if ( context.IsSelected )⤶ return;⤶ ⤶ // Draw gizmos ..⤶ }⤶ ```⤶ ⤶ ### Finding Targets⤶ ⤶ Many entities target other entities by name, you can use FindTarget for this.⤶ ⤶ ```csharp⤶ [Property( "target" ), Title( "Target Entity" )]⤶ public EntityTarget TargetEntity { get; set; } ⤶ ⤶ public static void DrawGizmos( EditorContext context )⤶ {⤶ var targetProp = context.Target.GetProperty( "target" );⤶ var targetName = targetProp.GetValue<string>();⤶ ⤶ // Find the target entity by name⤶ var targetObject = context.FindTarget( targetName );⤶ ⤶ if ( targetObject != null )⤶ {⤶ // get the position of the target in local space⤶ var local = Gizmo.Transform.ToLocal( targetObject.Transform );⤶ var arrowSize = local.Position.Normal * 20.0f;⤶ ⤶ // draw an arrow to it⤶ Gizmo.Draw.Color = Color.Random;⤶ Gizmo.Draw.LineThickness = 2;⤶ Gizmo.Draw.Line( 0, local.Position );⤶ Gizmo.Draw.SolidCone( local.Position - arrowSize, arrowSize, 10.0f );⤶ }⤶ }⤶ ```⤶