2019-12-30 23:08:17 +00:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.Drawing ;
using System.Linq ;
using System.Windows.Forms ;
using CodeImp.DoomBuilder.BuilderModes ;
using CodeImp.DoomBuilder.Geometry ;
using CodeImp.DoomBuilder.Map ;
using CodeImp.DoomBuilder.Rendering ;
namespace CodeImp.DoomBuilder.VisualModes
{
2021-01-30 21:45:08 +00:00
internal class VisualSidedefSlope : BaseVisualSlope // VisualSlope, IVisualEventReceiver
2019-12-30 23:08:17 +00:00
{
#region = = = = = = = = = = = = = = = = = = Variables
private readonly Sidedef sidedef ;
#endregion
#region = = = = = = = = = = = = = = = = = = Constants
private const int SIZE = 8 ;
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
public Sidedef Sidedef { get { return sidedef ; } }
public int NormalizedAngleDeg { get { return ( sidedef . Line . AngleDeg > = 180 ) ? ( sidedef . Line . AngleDeg - 180 ) : sidedef . Line . AngleDeg ; } }
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Destructor
2021-01-30 21:45:08 +00:00
public VisualSidedefSlope ( BaseVisualMode mode , SectorLevel level , Sidedef sidedef , bool up ) : base ( mode , level , up )
2019-12-30 23:08:17 +00:00
{
this . sidedef = sidedef ;
2021-01-30 21:01:55 +00:00
type = VisualSlopeType . Line ;
2020-01-02 19:32:37 +00:00
Update ( ) ;
2019-12-30 23:08:17 +00:00
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
2020-03-19 15:16:08 +00:00
public Vector3D GetCenterPoint ( )
{
Vector2D p = sidedef . Line . GetCenterPoint ( ) ;
return new Vector3D ( p , level . plane . GetZ ( p ) ) ;
}
2020-01-02 19:32:37 +00:00
public override void Update ( )
2019-12-30 23:08:17 +00:00
{
plane = new Plane ( level . plane . Normal , level . plane . Offset - 0.1f ) ;
if ( ! up )
plane = plane . GetInverted ( ) ;
UpdatePosition ( ) ;
2020-02-19 22:03:32 +00:00
length = new Line3D ( new Vector3D ( sidedef . Line . Line . v1 , plane . GetZ ( sidedef . Line . Line . v1 ) ) , new Vector3D ( sidedef . Line . Line . v2 , plane . GetZ ( sidedef . Line . Line . v2 ) ) ) . GetDelta ( ) . GetLength ( ) ;
2019-12-30 23:08:17 +00:00
}
/// <summary>
/// This is called when the thing must be tested for line intersection. This should reject
/// as fast as possible to rule out all geometry that certainly does not touch the line.
/// </summary>
public override bool PickFastReject ( Vector3D from , Vector3D to , Vector3D dir )
{
2020-05-30 14:41:05 +00:00
if ( sidedef . IsDisposed | | sidedef . Sector . IsDisposed )
return false ;
2020-02-19 21:14:29 +00:00
RectangleF bbox = sidedef . Sector . BBox ;
2019-12-30 23:08:17 +00:00
if ( ( up & & plane . Distance ( from ) > 0.0f ) | | ( ! up & & plane . Distance ( from ) < 0.0f ) )
{
if ( plane . GetIntersection ( from , to , ref pickrayu ) )
{
if ( pickrayu > 0.0f )
{
pickintersect = from + ( to - from ) * pickrayu ;
return ( ( pickintersect . x > = bbox . Left ) & & ( pickintersect . x < = bbox . Right ) & &
( pickintersect . y > = bbox . Top ) & & ( pickintersect . y < = bbox . Bottom ) ) ;
}
}
}
return false ;
}
/// <summary>
/// This is called when the thing must be tested for line intersection. This should perform
/// accurate hit detection and set u_ray to the position on the ray where this hits the geometry.
/// </summary>
2020-05-21 12:20:02 +00:00
public override bool PickAccurate ( Vector3D from , Vector3D to , Vector3D dir , ref double u_ray )
2019-12-30 23:08:17 +00:00
{
u_ray = pickrayu ;
Sidedef sd = MapSet . NearestSidedef ( sidedef . Sector . Sidedefs , pickintersect ) ;
if ( sd = = sidedef ) {
2020-05-21 12:20:02 +00:00
double side = sd . Line . SideOfLine ( pickintersect ) ;
2019-12-30 23:08:17 +00:00
if ( ( side < = 0.0f & & sd . IsFront ) | | ( side > 0.0f & & ! sd . IsFront ) )
2020-02-19 21:14:29 +00:00
return true ;
2019-12-30 23:08:17 +00:00
}
return false ;
}
2020-02-16 21:07:20 +00:00
/// <summary>
/// Updates the position. Depending on 3D floors and which side of the linedef the slope handle is on the
/// direction of the line used as a base has to be inverted
/// </summary>
2019-12-30 23:08:17 +00:00
public void UpdatePosition ( )
{
2020-02-16 19:51:16 +00:00
bool invertline = false ;
2019-12-30 23:08:17 +00:00
2020-02-16 19:51:16 +00:00
if ( up )
2019-12-30 23:08:17 +00:00
{
2020-02-16 19:51:16 +00:00
if ( level . extrafloor & & level . type = = SectorLevelType . Ceiling )
{
if ( sidedef . IsFront )
invertline = true ;
}
else
{
if ( ! sidedef . IsFront )
invertline = true ;
}
2019-12-30 23:08:17 +00:00
}
else
{
2020-02-16 19:51:16 +00:00
if ( level . extrafloor & & level . type = = SectorLevelType . Floor )
{
if ( ! sidedef . IsFront )
invertline = true ;
}
else
{
if ( sidedef . IsFront )
invertline = true ;
}
2019-12-30 23:08:17 +00:00
}
2020-02-16 19:51:16 +00:00
if ( invertline )
SetPosition ( new Line2D ( sidedef . Line . End . Position , sidedef . Line . Start . Position ) , level . plane ) ;
else
SetPosition ( sidedef . Line . Line , level . plane ) ;
2019-12-30 23:08:17 +00:00
}
2020-02-16 21:07:20 +00:00
/// <summary>
/// Tries to find a slope handle to pivot around. If possible if finds the handle belonging to a line that has the
/// same angle as the start handle, and is the furthest away. If such a handle does not exist it finds one that's
/// closest to those specs
/// </summary>
/// <returns></returns>
2021-02-13 08:57:09 +00:00
public override VisualSlope GetSmartPivotHandle ( )
2019-12-30 23:08:17 +00:00
{
2021-02-13 11:03:37 +00:00
List < IVisualEventReceiver > selectedsectors = mode . GetSelectedObjects ( true , false , false , false , false ) ;
// Special handling for triangular sectors
if ( selectedsectors . Count = = 0 & & BuilderPlug . Me . UseOppositeSmartPivotHandle & & sidedef . Sector . Sidedefs . Count = = 3 )
{
foreach ( VisualVertexSlope vvs in mode . VertexSlopeHandles [ sidedef . Sector ] )
{
if ( vvs . Level = = level & & ! vvs . Vertex . Linedefs . Contains ( sidedef . Line ) )
return vvs ;
}
}
2021-02-13 08:57:09 +00:00
VisualSlope handle = this ;
2019-12-30 23:08:17 +00:00
List < VisualSidedefSlope > potentialhandles = new List < VisualSidedefSlope > ( ) ;
if ( selectedsectors . Count = = 0 )
{
2020-02-16 21:07:20 +00:00
// No sectors selected, so find all handles that belong to the same level
2021-02-13 08:57:09 +00:00
foreach ( VisualSidedefSlope checkhandle in mode . SidedefSlopeHandles [ sidedef . Sector ] )
2021-01-30 21:01:55 +00:00
{
2021-02-13 08:57:09 +00:00
if ( checkhandle ! = this & & checkhandle . Level = = level )
2019-12-30 23:08:17 +00:00
potentialhandles . Add ( checkhandle ) ;
2021-01-30 21:01:55 +00:00
}
2019-12-30 23:08:17 +00:00
}
else
{
2020-02-16 21:07:20 +00:00
// Sectors are selected, get all handles from those sectors that have the same level
2019-12-30 23:08:17 +00:00
HashSet < Sector > sectors = new HashSet < Sector > ( ) ;
foreach ( BaseVisualGeometrySector bvgs in selectedsectors )
sectors . Add ( bvgs . Sector . Sector ) ;
foreach ( Sector s in sectors )
2021-01-30 21:01:55 +00:00
foreach ( VisualSidedefSlope checkhandle in mode . SidedefSlopeHandles [ s ] )
{
2021-02-13 08:57:09 +00:00
if ( checkhandle ! = this )
2019-12-30 23:08:17 +00:00
foreach ( BaseVisualGeometrySector bvgs in selectedsectors )
if ( bvgs . Level = = checkhandle . Level )
potentialhandles . Add ( checkhandle ) ;
2021-01-30 21:01:55 +00:00
}
2019-12-30 23:08:17 +00:00
}
2020-02-16 21:07:20 +00:00
// Sort potential handles by their angle difference to the start handle. That means that handles with less angle difference will be at the beginning of the list
2021-02-13 08:57:09 +00:00
List < VisualSidedefSlope > anglediffsortedhandles = potentialhandles . OrderBy ( h = > Math . Abs ( NormalizedAngleDeg - h . NormalizedAngleDeg ) ) . ToList ( ) ;
2019-12-30 23:08:17 +00:00
2020-02-16 21:07:20 +00:00
// Get all potential handles that have to same angle as the one that's closest to the start handle, then sort them by distance, and take the one that's furthest away
2019-12-30 23:08:17 +00:00
if ( anglediffsortedhandles . Count > 0 )
2021-02-13 08:57:09 +00:00
handle = anglediffsortedhandles . Where ( h = > h . NormalizedAngleDeg = = anglediffsortedhandles [ 0 ] . NormalizedAngleDeg ) . OrderByDescending ( h = > Math . Abs ( sidedef . Line . Line . GetDistanceToLine ( h . sidedef . Line . GetCenterPoint ( ) , false ) ) ) . First ( ) ;
2019-12-30 23:08:17 +00:00
2021-02-13 08:57:09 +00:00
if ( handle = = this )
2019-12-30 23:08:17 +00:00
return null ;
return handle ;
}
2020-02-23 11:44:59 +00:00
public static void ApplySlope ( SectorLevel level , Plane plane , BaseVisualMode mode )
{
bool applytoceiling = false ;
2021-01-01 21:08:54 +00:00
bool reset = false ;
int height = 0 ;
2020-02-23 11:44:59 +00:00
Vector2D center = new Vector2D ( level . sector . BBox . X + level . sector . BBox . Width / 2 ,
level . sector . BBox . Y + level . sector . BBox . Height / 2 ) ;
if ( level . extrafloor )
{
// The top side of 3D floors is the ceiling of the sector, but it's a "floor" in UDB, so the
// ceiling of the control sector has to be modified
if ( level . type = = SectorLevelType . Floor )
applytoceiling = true ;
}
else
{
if ( level . type = = SectorLevelType . Ceiling )
applytoceiling = true ;
}
2021-01-01 21:08:54 +00:00
// If the plane horizontal remove the slope and set the sector height instead
// Rounding errors can result in offsets of horizontal planes to be a tiny, tiny bit off a whole number,
// assume we want to remove the plane in this case
double diff = Math . Abs ( Math . Round ( plane . d ) - plane . d ) ;
if ( plane . Normal . z = = 1.0 & & diff < 0.000000001 )
{
reset = true ;
height = - Convert . ToInt32 ( plane . d ) ;
}
2020-02-23 11:44:59 +00:00
if ( applytoceiling )
{
2021-01-01 21:08:54 +00:00
if ( reset )
{
level . sector . CeilHeight = height ;
level . sector . CeilSlope = new Vector3D ( ) ;
level . sector . CeilSlopeOffset = double . NaN ;
}
else
{
Plane downplane = plane . GetInverted ( ) ;
level . sector . CeilSlope = downplane . Normal ;
level . sector . CeilSlopeOffset = downplane . Offset ;
}
2020-02-23 11:44:59 +00:00
}
else
{
2021-01-01 21:08:54 +00:00
if ( reset )
{
level . sector . FloorHeight = height ;
level . sector . FloorSlope = new Vector3D ( ) ;
level . sector . FloorSlopeOffset = double . NaN ;
}
else
{
level . sector . FloorSlope = plane . Normal ;
level . sector . FloorSlopeOffset = plane . Offset ;
}
2020-02-23 11:44:59 +00:00
}
// Rebuild sector
BaseVisualSector vs ;
if ( mode . VisualSectorExists ( level . sector ) )
{
vs = ( BaseVisualSector ) mode . GetVisualSector ( level . sector ) ;
}
else
{
vs = mode . CreateBaseVisualSector ( level . sector ) ;
}
if ( vs ! = null ) vs . UpdateSectorGeometry ( true ) ;
}
2021-01-30 21:01:55 +00:00
/// <summary>
/// Gets the pivor point for this slope handle
/// </summary>
/// <returns>The pivot point as Vector3D</returns>
public override Vector3D GetPivotPoint ( )
{
return new Vector3D ( sidedef . Line . Line . GetCoordinatesAt ( 0.5 ) , level . plane . GetZ ( sidedef . Line . Line . GetCoordinatesAt ( 0.5 ) ) ) ;
}
2021-02-13 11:03:37 +00:00
public List < Vector3D > GetPivotPoints ( )
{
return new List < Vector3D > ( )
{
new Vector3D ( sidedef . Line . Start . Position , level . plane . GetZ ( sidedef . Line . Start . Position ) ) ,
new Vector3D ( sidedef . Line . End . Position , level . plane . GetZ ( sidedef . Line . End . Position ) )
} ;
}
2019-12-30 23:08:17 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Events
2021-01-30 21:45:08 +00:00
public override void OnChangeTargetHeight ( int amount )
2019-12-30 23:08:17 +00:00
{
VisualSlope pivothandle = null ;
List < IVisualEventReceiver > selectedsectors = mode . GetSelectedObjects ( true , false , false , false , false ) ;
List < SectorLevel > levels = new List < SectorLevel > ( ) ;
if ( selectedsectors . Count = = 0 )
levels . Add ( level ) ;
else
2020-02-23 10:17:14 +00:00
{
2019-12-30 23:08:17 +00:00
foreach ( BaseVisualGeometrySector bvgs in selectedsectors )
levels . Add ( bvgs . Level ) ;
2020-02-23 10:17:14 +00:00
if ( ! levels . Contains ( level ) )
levels . Add ( level ) ;
}
2020-02-16 21:07:20 +00:00
// Try to find a slope handle the user set to be the pivot handle
// TODO: doing this every time is kind of stupid. Maybe store the pivot handle in the mode?
2019-12-30 23:08:17 +00:00
foreach ( KeyValuePair < Sector , List < VisualSlope > > kvp in mode . AllSlopeHandles )
{
2021-01-30 21:01:55 +00:00
foreach ( VisualSlope handle in kvp . Value )
2019-12-30 23:08:17 +00:00
{
if ( handle . Pivot )
{
pivothandle = handle ;
break ;
}
}
}
2022-03-05 09:37:00 +00:00
// Can't pivot around itself
if ( pivothandle = = this )
{
General . Interface . DisplayStatus ( Windows . StatusType . Warning , "Slope handle to modify can't be the same as the pivot handle" ) ;
return ;
}
2020-02-16 21:07:20 +00:00
// User didn't set a pivot handle, try to find the smart pivot handle
2019-12-30 23:08:17 +00:00
if ( pivothandle = = null )
2021-02-13 08:57:09 +00:00
pivothandle = GetSmartPivotHandle ( ) ;
2019-12-30 23:08:17 +00:00
2020-02-16 21:07:20 +00:00
// Still no pivot handle, cancle
2019-12-30 23:08:17 +00:00
if ( pivothandle = = null )
return ;
2020-10-19 15:32:34 +00:00
// Build a new plane. p1 and p2 are the points of the slope handle that is modified, with the changed amound added; p3 is on the line of the pivot handle
2022-03-05 09:37:00 +00:00
Vector3D p1 = new Vector3D ( sidedef . Line . Start . Position , level . plane . GetZ ( sidedef . Line . Start . Position ) + amount ) ;
Vector3D p2 = new Vector3D ( sidedef . Line . End . Position , level . plane . GetZ ( sidedef . Line . End . Position ) + amount ) ;
2021-01-30 21:01:55 +00:00
Vector3D p3 = pivothandle . GetPivotPoint ( ) ;
2019-12-30 23:08:17 +00:00
Plane plane = new Plane ( p1 , p2 , p3 , true ) ;
2022-03-05 09:37:00 +00:00
// Completely vertical planes are not possible. This can for example happen when trying to pivot around the slope handle on the opposite side of a line
if ( Math . Abs ( plane . a ) = = 1.0 | | Math . Abs ( plane . b ) = = 1.0 )
{
General . Interface . DisplayStatus ( Windows . StatusType . Warning , "Resulting plane is completely vertical, which is impossible. Aborting" ) ;
return ;
}
mode . CreateUndo ( "Change slope" ) ;
2020-02-16 21:07:20 +00:00
// Apply slope to surfaces
2019-12-30 23:08:17 +00:00
foreach ( SectorLevel l in levels )
2020-02-23 11:44:59 +00:00
ApplySlope ( l , plane , mode ) ;
2019-12-30 23:08:17 +00:00
mode . SetActionResult ( "Changed slope." ) ;
}
#endregion
}
}