The file implements the wall editing tool logic for the editor. It manages selection state, visual gizmo movement of posts, and performs deletion, restyle, and flip operations on selected wall components.
using Sandbox;
using Editor;
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Editing the walls that stand: the tool's own selection, the move gizmo and its drag (with the
/// merge when a post is released on another), Delete, the style swap and the rail flip. Each is one
/// graph operation applied as one undo step.
/// </summary>
public sealed partial class CourseWallBuilderTool
{
/// <summary>The gizmo stays on the post it was last shown on while the cursor is within this (units, ground plane) of the post's foot, so its arrows can be reached.</summary>
const float GizmoStickDistance = 120f;
/// <summary>The tool's own selection of posts and rail pieces (not the editor's).</summary>
readonly List<GameObject> _selection = new();
/// <summary>The piece under the cursor this frame, or null.</summary>
GameObject _hovered;
/// <summary>The post the move gizmo was last shown on.</summary>
GameObject _gizmoPost;
/// <summary>A post being dragged by the gizmo: its object and group, where it started, where it is, and whether the Z arrow has been used.</summary>
sealed class DragState
{
public GameObject Object;
public GameObject Group;
public Transform Original;
public Vector3 From;
public Vector3 Point;
public Vector3 Delta;
public bool ZUsed;
public bool Sloped;
}
DragState _drag;
// ---------------------------------------------------------------- selection
/// <summary>Select a piece: a rail piece brings its whole rail. Shift adds, or removes an already selected piece.</summary>
void Select( WallGraph graph, GameObject piece, bool add )
{
var pieces = new List<GameObject> { piece };
if ( IsRailPiece( piece ) )
{
var g = graph.Group == GroupOf( piece ) ? graph : ReadGraph( GroupOf( piece ) );
if ( g.RailOf( piece ) is { } rail ) pieces = rail.Pieces.ToList();
}
if ( !add ) _selection.Clear();
else if ( pieces.All( _selection.Contains ) )
{
foreach ( var p in pieces ) _selection.Remove( p );
return;
}
foreach ( var p in pieces ) if ( !_selection.Contains( p ) ) _selection.Add( p );
}
void DrawSelection()
{
_selection.RemoveAll( go => !go.IsValid() );
foreach ( var go in _selection ) DrawPieceOutline( go, true );
}
// ---------------------------------------------------------------- the move gizmo
/// <summary>The post the move gizmo stands on this frame: the one being dragged, else the selected one, else the hovered one, else the last one while the cursor stays near it; null for none.</summary>
GameObject GizmoPost( GameObject hovered, SceneTraceResult trace )
{
var post = _drag?.Object;
if ( !post.IsValid() ) post = _selection.FirstOrDefault( IsPost );
if ( !post.IsValid() && IsPost( hovered ) ) post = hovered;
if ( !post.IsValid() && _gizmoPost.IsValid() && (!trace.Hit || trace.HitPosition.WithZ( 0f ).Distance( PostPoint( _gizmoPost ).WithZ( 0f ) ) <= GizmoStickDistance) ) post = _gizmoPost;
_gizmoPost = post.IsValid() ? post : null;
return _gizmoPost;
}
/// <summary>
/// The editor's arrow gizmo on a post: X and Y arrows always, a Z arrow when the floor under the
/// post is a ramp. Dragging an arrow moves the post by the arrow's travel each frame, the new foot
/// going through the snap pass (Ctrl for none) and, unless the Z arrow was used, onto the floor
/// under its new X and Y (its original height where no tile is). The post itself follows live;
/// its rails, and whatever the move would split or merge, show as ghosts from a preview of the
/// move. Releasing the arrow commits the move as one undo step. Returns true while an arrow is
/// hovered or dragged, so the rest of the frame leaves the mouse alone.
/// </summary>
bool UpdateMoveGizmo( WallGraph graph, GameObject parent, GameObject post )
{
var foot = _drag?.Point ?? PostPoint( post );
var sloped = _drag?.Sloped ?? FloorIsSloped( graph.Group, foot );
var delta = Vector3.Zero;
var moved = false;
var zMoved = false;
bool hovered;
// The arrows sit on top of the post and start outside its log: at the foot they would lie in the floor plane and inside the model, drawn but never seen.
var renderer = post.GetComponent<ModelRenderer>( true );
var top = renderer is null ? foot + Vector3.Up * KitShape.WallHeight : foot.WithZ( renderer.Bounds.Maxs.z + 2f );
var reach = NuWallCornerRadius + 2f;
using ( Gizmo.Scope( "coursewall-move", new Transform( top ) ) )
{
Gizmo.Hitbox.DepthBias = 0.01f;
Gizmo.Draw.Color = Gizmo.Colors.Red;
if ( Gizmo.Control.Arrow( "move-x", Vector3.Forward, out var dx, length: 36f, axisOffset: reach, cullAngle: 0f ) ) { delta += Vector3.Forward * dx; moved = true; }
Gizmo.Draw.Color = Gizmo.Colors.Green;
if ( Gizmo.Control.Arrow( "move-y", Vector3.Left, out var dy, length: 36f, axisOffset: reach, cullAngle: 0f ) ) { delta += Vector3.Left * dy; moved = true; }
if ( sloped )
{
Gizmo.Draw.Color = Gizmo.Colors.Blue;
if ( Gizmo.Control.Arrow( "move-z", Vector3.Up, out var dz, length: 36f, axisOffset: 2f, cullAngle: 0f ) ) { delta += Vector3.Up * dz; moved = true; zMoved = MathF.Abs( dz ) > 0.001f; }
}
hovered = Gizmo.HasHovered || Gizmo.Pressed.Any;
}
if ( _drag is null )
{
if ( !moved ) return hovered;
BeginDrag( post );
_drag.Sloped = sloped;
// The graph of this frame was read before the drag began: read it again with the post at its start.
graph = ReadGraph( graph.Group, (post, _drag.From) );
}
var drag = _drag;
var moving = graph.PostOf( drag.Object );
if ( !drag.Object.IsValid() || moving is null ) { _drag = null; return false; }
drag.Delta += delta;
if ( zMoved ) drag.ZUsed = true;
var target = drag.From + drag.Delta;
if ( !Gizmo.IsCtrlPressed ) target = Resolve( graph, parent, target, Gizmo.IsShiftPressed, false, moving ).Point.WithZ( target.z );
if ( !drag.ZUsed ) target = target.WithZ( FloorUnder( FloorScope( graph.Group ), target, drag.From.z ) is { } floor ? floor.point.z : drag.From.z );
drag.Point = target;
// The post follows live; everything else the move does is a ghost from a preview of it.
var preview = graph.Clone();
var previewPost = preview.PostOf( drag.Object );
preview.MovePost( previewPost, target );
var standing = preview.Posts.Contains( previewPost ) ? previewPost : preview.PostAt( target );
drag.Object.WorldTransform = PostTransform( drag.Object, target, standing is null ? Enumerable.Empty<(Vector3 away, Vector3? inside)>() : preview.RailInfo( standing ) );
DrawPieceOutline( drag.Object, true );
DrawGhosts( preview );
if ( Gizmo.Pressed.Any ) return true;
CommitDrag( graph );
return true;
}
void BeginDrag( GameObject post )
{
var from = PostPoint( post );
_drag = new DragState { Object = post, Group = GroupOf( post ), Original = post.WorldTransform, From = from, Point = from };
if ( !_selection.Contains( post ) ) { _selection.Clear(); _selection.Add( post ); }
if ( DebugLog ) Log.Info( $"Course Wall Builder: drag start on the post at {from}" );
}
/// <summary>
/// The release: the move as one undo step. The post goes back to where it started first, so the
/// step records the whole move; then the graph moves it, which rebuilds its rails to the new
/// point, splits what they now cross, and merges the post into one it was released on (its rails
/// re-attach to the survivor, doubled rails go, the survivor's corner turns to them). The open
/// run's points follow the post.
/// </summary>
void CommitDrag( WallGraph graph )
{
var drag = _drag;
_drag = null;
if ( !drag.Object.IsValid() ) return;
drag.Object.WorldTransform = drag.Original;
if ( drag.Point.Distance( drag.From ) < 0.5f ) return;
var post = graph.PostOf( drag.Object );
if ( post is null ) return;
graph.MovePost( post, drag.Point );
var landed = graph.PostAt( drag.Point )?.Point ?? drag.Point;
if ( _run is not null ) for ( var i = 0; i < _run.Points.Count; i++ ) if ( _run.Points[i].Distance( drag.From ) <= JoinDistance ) _run.Points[i] = landed;
Apply( graph, "Move post" );
_selection.RemoveAll( go => !go.IsValid() );
Log.Info( $"Course Wall Builder: post moved {drag.From} -> {landed}{(drag.ZUsed ? " (Z arrow used)" : "")}{(graph.Posts.Contains( post ) ? "" : " and merged into the post there")}" );
}
// ---------------------------------------------------------------- delete, restyle and flip
/// <summary>Delete: the piece under the cursor when there is one (its whole rail, or the post with its rails), else the selection.</summary>
void DeleteHoveredOrSelection( WallGraph graph )
{
if ( _hovered.IsValid() ) Select( graph, _hovered, false );
DeleteSelection( graph );
}
/// <summary>
/// The selected rails go, a selected post goes with every rail ending at it, and a post left
/// without any rail by this goes too. One undo step per group the selection spans (one, in
/// practice).
/// </summary>
void DeleteSelection( WallGraph graph )
{
_selection.RemoveAll( go => !go.IsValid() );
if ( _selection.Count == 0 ) return;
var removed = 0;
foreach ( var group in _selection.Select( GroupOf ).Distinct().ToList() )
{
var g = group == graph.Group ? graph : ReadGraph( group );
var ends = new HashSet<WallPost>();
foreach ( var piece in _selection.Where( s => GroupOf( s ) == group ) )
{
if ( IsPost( piece ) )
{
if ( g.PostOf( piece ) is not { } post ) continue;
ends.UnionWith( g.RailsAt( post ).Select( r => r.Other( post ) ) );
g.RemovePost( post );
removed++;
}
else if ( g.RailOf( piece ) is { } rail )
{
ends.Add( rail.A );
ends.Add( rail.B );
g.RemoveRail( rail );
removed++;
}
}
foreach ( var end in ends ) if ( g.Posts.Contains( end ) && !g.RailsAt( end ).Any() ) g.RemovePost( end );
Apply( g, "Delete course walls" );
}
_selection.Clear();
Log.Info( $"Course Wall Builder: deleted {removed} piece(s) with what depended on them" );
}
/// <summary>Style changed with rails selected: each selected rail of another style is rebuilt in place in the new style between the same posts, keeping its inside (a rail without one takes the camera's side; a Holekit rail has none). One undo step.</summary>
void SwapSelectedStyle( WallGraph graph )
{
_selection.RemoveAll( go => !go.IsValid() );
var railPieces = _selection.Where( IsRailPiece ).ToList();
if ( railPieces.Count == 0 ) return;
var swapped = 0;
var replaced = new List<GameObject>();
foreach ( var group in railPieces.Select( GroupOf ).Distinct().ToList() )
{
var g = group == graph.Group ? graph : ReadGraph( group );
var targets = g.Rails.Where( r => r.Style != Style && r.Pieces.Any( railPieces.Contains ) ).ToList();
if ( targets.Count == 0 ) continue;
var camera = ViewPosition();
foreach ( var rail in targets )
g.Restyle( rail, Style, Style == WallStyle.NuWall ? rail.Inside ?? InsideFor( rail.A.Point, rail.B.Point, CameraHand( rail.A.Point, rail.B.Point, camera ) ) : null );
Apply( g, "Swap wall style" );
replaced.AddRange( targets.SelectMany( r => r.Pieces ) );
swapped += targets.Count;
}
if ( swapped == 0 ) return;
_selection.RemoveAll( go => !go.IsValid() );
foreach ( var go in replaced ) if ( go.IsValid() && !_selection.Contains( go ) ) _selection.Add( go );
Log.Info( $"Course Wall Builder: {swapped} rail(s) rebuilt as {Style}" );
}
/// <summary>
/// F with rails selected and no run open: each selected NuWall rail is rebuilt with its inside on
/// the other side (its slope swaps sides) between the same posts, and the corners at both ends are
/// turned to the rails as they now stand; the hole's collision is rebuilt. A Holekit rail has no
/// inside and is left alone. One undo step per group the selection spans. The selection keeps the
/// rails through their new pieces.
/// </summary>
void FlipSelectedRails( WallGraph graph )
{
_selection.RemoveAll( go => !go.IsValid() );
var railPieces = _selection.Where( IsRailPiece ).ToList();
if ( railPieces.Count == 0 ) return;
var flipped = 0;
var replaced = new List<GameObject>();
foreach ( var group in railPieces.Select( GroupOf ).Distinct().ToList() )
{
var g = group == graph.Group ? graph : ReadGraph( group );
var targets = g.Rails.Where( r => r.Style == WallStyle.NuWall && r.Inside is { } inside && inside.WithZ( 0f ).Length > 0.01f && r.Pieces.Any( railPieces.Contains ) ).ToList();
if ( targets.Count == 0 ) continue;
foreach ( var rail in targets ) g.Restyle( rail, rail.Style, -rail.Inside.Value );
Apply( g, "Flip rail" );
replaced.AddRange( targets.SelectMany( r => r.Pieces ) );
flipped += targets.Count;
}
if ( flipped == 0 ) return;
_selection.RemoveAll( go => !go.IsValid() );
foreach ( var go in replaced ) if ( go.IsValid() && !_selection.Contains( go ) ) _selection.Add( go );
Log.Info( $"Course Wall Builder: {flipped} rail(s) flipped" );
}
}