UltimateZoneBuilder/Source/Plugins/BuilderModes/VisualModes/VisualSidedefSlope.cs

385 lines
12 KiB
C#
Raw Normal View History

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
{
internal class VisualSidedefSlope : VisualSlope, IVisualEventReceiver
{
#region ================== Variables
private readonly BaseVisualMode mode;
private readonly Sidedef sidedef;
private readonly SectorLevel level;
private readonly bool up;
private Vector3D pickintersect;
private float pickrayu;
private Plane plane;
#endregion
#region ================== Constants
private const int SIZE = 8;
#endregion
#region ================== Properties
public Sidedef Sidedef { get { return sidedef; } }
public SectorLevel Level { get { return level; } }
public int NormalizedAngleDeg { get { return (sidedef.Line.AngleDeg >= 180) ? (sidedef.Line.AngleDeg - 180) : sidedef.Line.AngleDeg; } }
#endregion
#region ================== Constructor / Destructor
public VisualSidedefSlope(BaseVisualMode mode, SectorLevel level, Sidedef sidedef, bool up) : base()
{
this.mode = mode;
this.sidedef = sidedef;
this.level = level;
this.up = up;
length = sidedef.Line.Length;
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-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();
}
/// <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)
{
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>
public override bool PickAccurate(Vector3D from, Vector3D to, Vector3D dir, ref float u_ray)
{
u_ray = pickrayu;
Sidedef sd = MapSet.NearestSidedef(sidedef.Sector.Sidedefs, pickintersect);
if (sd == sidedef) {
float side = sd.Line.SideOfLine(pickintersect);
if ((side <= 0.0f && sd.IsFront) || (side > 0.0f && !sd.IsFront))
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()
{
bool invertline = false;
2019-12-30 23:08:17 +00:00
if (up)
2019-12-30 23:08:17 +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
{
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
}
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>
/// <param name="starthandle">The slope handle to start from (the one we need to find a pivot handle for)</param>
/// <returns></returns>
2019-12-30 23:08:17 +00:00
internal VisualSidedefSlope GetSmartPivotHandle(VisualSidedefSlope starthandle)
{
VisualSidedefSlope handle = starthandle;
List<VisualSidedefSlope> potentialhandles = new List<VisualSidedefSlope>();
List<IVisualEventReceiver> selectedsectors = mode.GetSelectedObjects(true, false, false, false, false);
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
2019-12-30 23:08:17 +00:00
foreach (VisualSidedefSlope checkhandle in mode.AllSlopeHandles[starthandle.Sidedef.Sector])
if (checkhandle != starthandle && checkhandle.Level == starthandle.Level)
potentialhandles.Add(checkhandle);
}
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)
foreach (VisualSidedefSlope checkhandle in mode.AllSlopeHandles[s])
if(checkhandle != starthandle)
foreach (BaseVisualGeometrySector bvgs in selectedsectors)
if (bvgs.Level == checkhandle.Level)
potentialhandles.Add(checkhandle);
}
foreach (KeyValuePair<Sector, List<VisualSlope>> kvp in mode.AllSlopeHandles)
foreach (VisualSidedefSlope checkhandle in kvp.Value)
checkhandle.SmartPivot = false;
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
2019-12-30 23:08:17 +00:00
List<VisualSidedefSlope> anglediffsortedhandles = potentialhandles.OrderBy(h => Math.Abs(starthandle.NormalizedAngleDeg - h.NormalizedAngleDeg)).ToList();
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)
handle = anglediffsortedhandles.Where(h => h.NormalizedAngleDeg == anglediffsortedhandles[0].NormalizedAngleDeg).OrderByDescending(h => Math.Abs(starthandle.Sidedef.Line.Line.GetDistanceToLine(h.sidedef.Line.GetCenterPoint(), false))).First();
if (handle == starthandle)
return null;
if(handle != null)
handle.SmartPivot = true;
return handle;
}
#endregion
#region ================== Events
public void OnChangeTargetHeight(int amount)
{
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
foreach (BaseVisualGeometrySector bvgs in selectedsectors)
levels.Add(bvgs.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)
{
foreach (VisualSidedefSlope handle in kvp.Value)
{
if (handle.Pivot)
{
pivothandle = handle;
break;
}
}
}
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)
pivothandle = GetSmartPivotHandle(this);
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;
mode.CreateUndo("Change slope");
Plane originalplane = level.plane;
Plane pivotplane = ((VisualSidedefSlope)pivothandle).Level.plane;
2020-02-16 21:07:20 +00:00
// Build a new plane. p1 and p2 are the points of the slope handle that is modified, p3 is on the line of the pivot handle
2019-12-30 23:08:17 +00:00
Vector3D p1 = new Vector3D(sidedef.Line.Start.Position, (float)Math.Round(originalplane.GetZ(sidedef.Line.Start.Position)));
Vector3D p2 = new Vector3D(sidedef.Line.End.Position, (float)Math.Round(originalplane.GetZ(sidedef.Line.End.Position)));
Vector3D p3 = new Vector3D(((VisualSidedefSlope)pivothandle).Sidedef.Line.Line.GetCoordinatesAt(0.5f), (float)Math.Round(pivotplane.GetZ(((VisualSidedefSlope)pivothandle).Sidedef.Line.Line.GetCoordinatesAt(0.5f))));
2020-02-16 21:07:20 +00:00
// Move the points of the handle up/down
2019-12-30 23:08:17 +00:00
p1 += new Vector3D(0f, 0f, amount);
p2 += new Vector3D(0f, 0f, amount);
Plane plane = new Plane(p1, p2, p3, true);
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)
{
bool applytoceiling = false;
Vector2D center = new Vector2D(l.sector.BBox.X + l.sector.BBox.Width / 2,
l.sector.BBox.Y + l.sector.BBox.Height / 2);
2020-01-03 00:11:23 +00:00
if(l.extrafloor)
{
2020-02-16 21:07:20 +00:00
// 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 (l.type == SectorLevelType.Floor)
applytoceiling = true;
}
else
{
if (l.type == SectorLevelType.Ceiling)
applytoceiling = true;
}
if (applytoceiling)
{
Plane downplane = plane.GetInverted();
l.sector.CeilSlope = downplane.Normal;
l.sector.CeilSlopeOffset = downplane.Offset;
l.sector.CeilHeight = (int)new Plane(l.sector.CeilSlope, l.sector.CeilSlopeOffset).GetZ(center);
}
else
{
l.sector.FloorSlope = plane.Normal;
l.sector.FloorSlopeOffset = plane.Offset;
l.sector.FloorHeight = (int)new Plane(l.sector.FloorSlope, l.sector.FloorSlopeOffset).GetZ(center);
}
2019-12-30 23:08:17 +00:00
// Rebuild sector
BaseVisualSector vs;
if (mode.VisualSectorExists(l.sector))
{
vs = (BaseVisualSector)mode.GetVisualSector(l.sector);
}
else
{
vs = mode.CreateBaseVisualSector(l.sector);
}
if (vs != null) vs.UpdateSectorGeometry(true);
}
mode.SetActionResult("Changed slope.");
}
// Select or deselect
public void OnSelectEnd()
{
if (this.selected)
{
this.selected = false;
mode.RemoveSelectedObject(this);
}
else
{
this.selected = true;
mode.AddSelectedObject(this);
}
}
public void OnEditEnd()
{
// We can only have one pivot handle, so remove it from all first
foreach (KeyValuePair<Sector, List<VisualSlope>> kvp in mode.AllSlopeHandles)
{
foreach (VisualSlope handle in kvp.Value)
{
if (handle == mode.HighlightedTarget)
handle.Pivot = !handle.Pivot;
else
handle.Pivot = false;
}
}
}
2019-12-30 23:08:17 +00:00
// Return texture name
public string GetTextureName() { return ""; }
// Unused
public void OnSelectBegin() { }
public void OnEditBegin() { }
public void OnChangeTargetBrightness(bool up) { }
public void OnChangeTextureOffset(int horizontal, int vertical, bool doSurfaceAngleCorrection) { }
public void OnSelectTexture() { }
public void OnCopyTexture() { }
public void OnPasteTexture() { }
public void OnCopyTextureOffsets() { }
public void OnPasteTextureOffsets() { }
public void OnTextureAlign(bool alignx, bool aligny) { }
public void OnToggleUpperUnpegged() { }
public void OnToggleLowerUnpegged() { }
public void OnProcess(long deltatime) { }
public void OnTextureFloodfill() { }
public void OnInsert() { }
public void OnTextureFit(FitTextureOptions options) { } //mxd
public void ApplyTexture(string texture) { }
public void ApplyUpperUnpegged(bool set) { }
public void ApplyLowerUnpegged(bool set) { }
public void SelectNeighbours(bool select, bool withSameTexture, bool withSameHeight) { } //mxd
public virtual void OnPaintSelectEnd() { } // biwa
public void OnChangeScale(int x, int y) { }
public void OnResetTextureOffset() { }
public void OnResetLocalTextureOffset() { }
public void OnCopyProperties() { }
public void OnPasteProperties(bool usecopysetting) { }
public void OnDelete() { }
public void OnPaintSelectBegin() { }
public void OnMouseMove(MouseEventArgs e) { }
#endregion
}
}