This file implements the visual preview logic for a wall building tool, drawing snap feedback and ghost geometry. It shows the state the wall graph will enter if the current input operation is completed.
using Sandbox;
using Editor;
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// The preview: the snap's blue feedback, and a ghost of exactly what the next click (or the drag's
/// release) would place, drawn from a clone of the wall graph given the same operation and laid out
/// by the same functions the sync uses. Redrawn every frame.
/// </summary>
public sealed partial class CourseWallBuilderTool
{
/// <summary>Every snap's feedback (the post outline, the grid and tile markers, the guide lines): light blue, semi-transparent.</summary>
static readonly Color SnapColor = new( 80f / 255f, 170f / 255f, 255f / 255f, 0.7f );
static readonly Color GhostColor = Color.White.WithAlpha( 0.45f );
/// <summary>The whole preview of a frame with the cursor on a surface: the snap's feedback, then the ghost of the pending click.</summary>
void DrawPreview( WallGraph graph, SnapResult snap )
{
DrawSnapFeedback( snap );
var preview = graph.Clone();
var joined = PendingClick( preview, snap, out var joinPost );
DrawGhosts( preview );
if ( !joined ) return;
// A click on an existing post places nothing there; the post is outlined instead, and with no
// run open a NuWall post shows the inside the new rails would inherit.
if ( !Continuing && Style == WallStyle.NuWall )
foreach ( var inside in InsidesAt( graph, joinPost ).Take( 1 ) )
{
Gizmo.Draw.Color = SnapColor;
Gizmo.Draw.LineThickness = 3f;
Gizmo.Draw.Line( joinPost.Point + Vector3.Up, joinPost.Point + Vector3.Up + inside * 40f );
}
var postModel = Style == WallStyle.NuWall ? NuWallCornerModel : KitPostModel;
var postBox = postModel is null
? new BBox( new Vector3( -PostSize / 2f, -PostSize / 2f, 0f ), new Vector3( PostSize / 2f, PostSize / 2f, KitShape.WallHeight ) )
: new BBox( postModel.Bounds.Mins - WallPieces.StandOffset( postModel.Bounds ), postModel.Bounds.Maxs - WallPieces.StandOffset( postModel.Bounds ) );
Gizmo.Draw.Color = SnapColor;
Gizmo.Draw.LineThickness = 3f;
using ( Gizmo.Scope( "coursewall-snap", new Transform( joinPost.Point, Rotation.Identity ) ) )
Gizmo.Draw.LineBBox( postBox );
}
/// <summary>
/// Applies to a graph what a click at the snap would do: the loop close lays the last rail to the
/// first post; a snap on a post joins it (with a rail while the run is open, as the run's start
/// otherwise); anything else adds a post there and, with the run open, the rail to it. Returns
/// true when an existing post is joined rather than a post placed, with that post.
/// </summary>
bool PendingClick( WallGraph graph, SnapResult snap, out WallPost joined )
{
joined = null;
if ( snap.Kind == SnapKind.LoopClose )
{
var first = graph.PostAt( _run.First );
var last = graph.PostAt( _run.Last );
if ( first is not null && last is not null ) graph.AddRail( last, first, Style, RailInside( _run.Last, _run.First ) );
joined = first;
return first is not null;
}
if ( snap.Kind == SnapKind.Post && graph.PostAt( snap.Point ) is { } existing )
{
joined = existing;
if ( Continuing && graph.PostAt( _run.Last ) is { } from && from != existing ) graph.AddRail( from, existing, Style, RailInside( _run.Last, existing.Point ) );
return true;
}
if ( Continuing && _run.Last.Distance( snap.Point ) <= PostSize ) { joined = graph.PostAt( _run.Last ); return joined is not null; }
var post = graph.AddPost( snap.Point );
if ( Continuing && graph.PostAt( _run.Last ) is { } last2 && last2 != post ) graph.AddRail( last2, post, Style, RailInside( _run.Last, post.Point ) );
return false;
}
/// <summary>The ghosts of everything a graph would build when applied: a post for every post node without an object, the pieces of every rail without pieces.</summary>
void DrawGhosts( WallGraph graph )
{
foreach ( var post in graph.Posts.Where( p => !p.Object.IsValid() ) )
{
if ( Style == WallStyle.NuWall )
{
if ( NuWallCornerModel is { } corner ) DrawGhost( corner, NuWallCornerTransform( post.Point, graph.RailInfo( post ) ), GhostColor );
}
else if ( KitPostModel is { } model )
DrawGhost( model, new Transform( post.Point - WallPieces.StandOffset( model.Bounds ), Rotation.Identity ), GhostColor );
}
foreach ( var rail in graph.Rails.Where( r => r.Pieces.Count == 0 && r.Length >= 1f ) )
{
if ( rail.Style == WallStyle.NuWall )
{
if ( NuWallEdgeModel is { } edge )
foreach ( var transform in NuWallEdgeTransforms( rail.A.Point, rail.B.Point, RailHand( rail ) ) ) DrawGhost( edge, transform, GhostColor );
}
else if ( KitWallModel is { } wall )
DrawGhost( wall, HolekitRailTransform( wall, rail.A.Point, rail.B.Point ), GhostColor );
}
}
/// <summary>One ghost piece: the model drawn at the transform with a translucent tint.</summary>
static void DrawGhost( Model model, Transform transform, Color tint )
{
Gizmo.Draw.Color = tint;
var sceneModel = Gizmo.Draw.Model( model, transform );
if ( sceneModel is not null ) sceneModel.ColorTint = tint;
}
/// <summary>
/// The snap's blue feedback: a snapped edge or grid line drawn itself, a direction or axis from
/// the post through the point and a little beyond, the alignment from the first post; and a
/// marker on the ground at the point for every point snap but a post (a dot for an edge midpoint,
/// a ring for a tile centre, a cross for a grid or axis point and a line snap's foot, a diamond
/// for a tile corner).
/// </summary>
static void DrawSnapFeedback( SnapResult snap )
{
var kind = snap.Kind;
foreach ( var (lineFrom, lineTo) in snap.Lines )
{
var along = (lineTo - lineFrom).WithZ( 0f );
var end = IsLineKind( kind ) ? lineTo : along.Length > 1f ? lineTo + along.Normal * KitShape.Tile : lineTo;
Gizmo.Draw.Color = SnapColor;
Gizmo.Draw.LineThickness = IsLineKind( kind ) ? 2f : 1f;
Gizmo.Draw.Line( lineFrom + Vector3.Up, end + Vector3.Up );
}
if ( kind is SnapKind.None or SnapKind.Direction or SnapKind.Post or SnapKind.LoopClose ) return;
Gizmo.Draw.Color = SnapColor;
Gizmo.Draw.LineThickness = 2f;
var at = snap.Point + Vector3.Up;
const float arm = 15f;
switch ( kind )
{
case SnapKind.TileMid:
Gizmo.Draw.SolidBox( BBox.FromPositionAndSize( at, 6f ) );
break;
case SnapKind.TileCentre:
const int segments = 16;
for ( var i = 0; i < segments; i++ )
{
var a0 = MathF.Tau * i / segments;
var a1 = MathF.Tau * (i + 1) / segments;
Gizmo.Draw.Line( at + new Vector3( MathF.Cos( a0 ), MathF.Sin( a0 ), 0f ) * 10f, at + new Vector3( MathF.Cos( a1 ), MathF.Sin( a1 ), 0f ) * 10f );
}
break;
case SnapKind.TileCorner:
var n = at + Vector3.Forward * arm;
var s = at - Vector3.Forward * arm;
var w = at + Vector3.Left * arm;
var e = at - Vector3.Left * arm;
Gizmo.Draw.Line( n, w ); Gizmo.Draw.Line( w, s ); Gizmo.Draw.Line( s, e ); Gizmo.Draw.Line( e, n );
break;
default:
Gizmo.Draw.Line( at + Vector3.Left * arm, at - Vector3.Left * arm );
Gizmo.Draw.Line( at + Vector3.Forward * arm, at - Vector3.Forward * arm );
break;
}
}
/// <summary>The outline of a piece's render bounds: the hover highlight, or the stronger selection outline.</summary>
static void DrawPieceOutline( GameObject go, bool selected )
{
var renderer = go.GetComponent<ModelRenderer>( true );
if ( renderer is null ) return;
Gizmo.Draw.Color = selected ? SnapColor.WithAlpha( 1f ) : SnapColor;
Gizmo.Draw.LineThickness = selected ? 3f : 1.5f;
Gizmo.Draw.LineBBox( renderer.Bounds );
}
}