UltimateZoneBuilder/Source/Plugins/BuilderModes/ClassicModes/FlatAlignMode.cs

943 lines
29 KiB
C#
Raw Normal View History

2011-12-06 15:35:11 +00:00

#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections.Generic;
using System.Drawing;
2011-12-06 15:35:11 +00:00
using System.Windows.Forms;
using CodeImp.DoomBuilder.Actions;
2011-12-07 21:05:09 +00:00
using CodeImp.DoomBuilder.Data;
using CodeImp.DoomBuilder.Geometry;
2011-12-06 15:35:11 +00:00
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Types;
using CodeImp.DoomBuilder.Windows;
2011-12-06 15:35:11 +00:00
#endregion
namespace CodeImp.DoomBuilder.BuilderModes
2011-12-06 15:35:11 +00:00
{
2011-12-07 21:05:09 +00:00
public abstract class FlatAlignMode : BaseClassicMode
2011-12-06 15:35:11 +00:00
{
#region ================== Enums and Structs
2011-12-06 15:35:11 +00:00
private enum ModifyMode
2011-12-08 07:24:21 +00:00
{
None,
Dragging,
Resizing,
Rotating
}
private enum Grip
2011-12-08 07:24:21 +00:00
{
None,
Main,
2011-12-08 14:27:23 +00:00
SizeV,
SizeH,
2011-12-08 07:24:21 +00:00
RotateRT,
RotateLB
}
protected struct SectorInfo
{
2020-05-21 12:20:02 +00:00
public double rotation;
2011-12-08 07:24:21 +00:00
public Vector2D scale;
public Vector2D offset;
}
#endregion
#region ================== Constants
2011-12-08 07:24:21 +00:00
private const float GRIP_SIZE = 9.0f;
2011-12-08 14:27:23 +00:00
private readonly Cursor[] RESIZE_CURSORS = { Cursors.SizeNS, Cursors.SizeNWSE, Cursors.SizeWE, Cursors.SizeNESW };
2011-12-11 18:47:28 +00:00
private const byte RECTANGLE_ALPHA = 60;
2011-12-12 11:55:32 +00:00
private const byte EXTENSION_LINE_ALPHA = 150;
2011-12-08 07:24:21 +00:00
2011-12-06 15:35:11 +00:00
#endregion
#region ================== Variables
2011-12-07 06:57:38 +00:00
private ICollection<Sector> selection;
private Sector editsector;
2011-12-08 07:24:21 +00:00
protected IList<SectorInfo> sectorinfo;
2011-12-07 21:05:09 +00:00
private ImageData texture;
2013-03-18 13:52:27 +00:00
//private Vector2D selectionoffset;
2011-12-08 14:27:23 +00:00
private ModifyMode mode;
2011-12-09 06:58:39 +00:00
private bool autopanning;
2011-12-12 18:52:34 +00:00
private bool modealreadyswitching;
2011-12-08 14:27:23 +00:00
2011-12-08 07:24:21 +00:00
// Modification
2011-12-10 12:07:00 +00:00
// NOTE: This offset is in world space. ZDoom's offset is done before
// rotation (not my idea) so we will transform this when applying
// changes to sectors.
2020-05-21 12:20:02 +00:00
private double rotation;
2011-12-10 14:27:42 +00:00
private Vector2D scale = new Vector2D(1.0f, 1.0f);
2011-12-07 21:05:09 +00:00
private Vector2D offset;
2011-12-08 07:24:21 +00:00
// Rectangle components
private readonly Vector2D[] corners = new Vector2D[4]; // lefttop, righttop, rightbottom, leftbottom
2011-12-11 18:47:28 +00:00
private FlatVertex[] cornerverts = new FlatVertex[6];
private readonly Vector2D[] extends = new Vector2D[2]; // right, bottom
private readonly RectangleF[] resizegrips = new RectangleF[2]; // right, bottom
private readonly RectangleF[] rotategrips = new RectangleF[2]; // righttop, leftbottom
2011-12-12 11:55:32 +00:00
private Line2D extensionline;
2011-12-07 21:05:09 +00:00
2011-12-08 14:27:23 +00:00
// Aligning
private RectangleF alignrect;
2011-12-10 14:27:42 +00:00
private Vector2D alignoffset;
private bool showalignoffset;
2011-12-09 06:58:39 +00:00
private Vector2D dragoffset;
2011-12-10 14:27:42 +00:00
private Vector2D resizevector;
private Vector2D resizefilter;
private Line2D resizeaxis;
2020-05-21 12:20:02 +00:00
private double rotationoffset;
2011-12-11 16:37:15 +00:00
private Vector2D rotationcenter;
2011-12-11 18:47:28 +00:00
//mxd. View mode
private ViewMode prevviewmode;
2011-12-11 18:47:28 +00:00
// Options
private bool snaptogrid; // SHIFT to toggle
private bool snaptonearest; // CTRL to enable
2011-12-08 14:27:23 +00:00
2011-12-07 21:05:09 +00:00
#endregion
#region ================== Properties
protected abstract string XScaleName { get; }
protected abstract string YScaleName { get; }
protected abstract string XOffsetName { get; }
protected abstract string YOffsetName { get; }
protected abstract string RotationName { get; }
protected abstract string UndoDescription { get; }
2011-12-07 06:57:38 +00:00
2011-12-06 15:35:11 +00:00
#endregion
#region ================== Methods
2011-12-07 21:05:09 +00:00
protected abstract ImageData GetTexture(Sector editsector);
2011-12-08 07:24:21 +00:00
// This checks if a point is in a rect
private static bool PointInRectF(RectangleF rect, Vector2D point)
2011-12-08 07:24:21 +00:00
{
return (point.x >= rect.Left) && (point.x <= rect.Right) && (point.y >= rect.Top) && (point.y <= rect.Bottom);
}
2011-12-09 06:58:39 +00:00
// Transforms p from Texture space into World space
private Vector2D TexToWorld(Vector2D p)
2011-12-09 06:58:39 +00:00
{
return TexToWorld(p, sectorinfo[0]);
}
2011-12-07 21:05:09 +00:00
// Transforms p from Texture space into World space
2011-12-08 07:24:21 +00:00
protected Vector2D TexToWorld(Vector2D p, SectorInfo s)
2011-12-07 21:05:09 +00:00
{
2011-12-10 14:27:42 +00:00
p /= scale * s.scale;
2011-12-11 16:37:15 +00:00
Vector2D soffset = s.offset.GetRotated(rotation);
p -= soffset;
2011-12-08 07:24:21 +00:00
p = p.GetRotated(-(rotation + s.rotation));
2011-12-10 12:07:00 +00:00
p -= offset;
2011-12-07 21:05:09 +00:00
return p;
}
2011-12-09 06:58:39 +00:00
// Transforms p from World space into Texture space
private Vector2D WorldToTex(Vector2D p)
2011-12-09 06:58:39 +00:00
{
return WorldToTex(p, sectorinfo[0]);
}
2011-12-07 21:05:09 +00:00
// Transforms p from World space into Texture space
2011-12-08 07:24:21 +00:00
protected Vector2D WorldToTex(Vector2D p, SectorInfo s)
2011-12-07 21:05:09 +00:00
{
2011-12-10 12:07:00 +00:00
p += offset;
2011-12-08 07:24:21 +00:00
p = p.GetRotated(rotation + s.rotation);
2011-12-10 12:07:00 +00:00
p += s.offset;
2011-12-10 14:27:42 +00:00
p *= scale * s.scale;
2011-12-07 21:05:09 +00:00
return p;
}
2011-12-08 14:27:23 +00:00
// This updates all sectors
private void UpdateSectors()
{
int index = 0;
foreach(Sector s in selection)
{
SectorInfo si = sectorinfo[index];
s.Fields.BeforeFieldsChange();
2011-12-10 12:07:00 +00:00
Vector2D toffset = offset.GetRotated((rotation + si.rotation));
2011-12-11 16:37:15 +00:00
Vector2D soffset = si.offset.GetRotated(rotation);
2011-12-08 14:27:23 +00:00
s.Fields[RotationName] = new UniValue(UniversalType.AngleDegreesFloat, Angle2D.RadToDeg(si.rotation + rotation));
2011-12-10 14:27:42 +00:00
s.Fields[XScaleName] = new UniValue(UniversalType.Float, si.scale.x * scale.x);
s.Fields[YScaleName] = new UniValue(UniversalType.Float, si.scale.y * scale.y);
2011-12-11 16:37:15 +00:00
s.Fields[XOffsetName] = new UniValue(UniversalType.Float, soffset.x + toffset.x);
s.Fields[YOffsetName] = new UniValue(UniversalType.Float, -(soffset.y + toffset.y));
2011-12-08 14:27:23 +00:00
s.UpdateNeeded = true;
2011-12-09 06:58:39 +00:00
s.UpdateCache();
2011-12-09 11:26:10 +00:00
index++;
2011-12-08 14:27:23 +00:00
}
}
2011-12-12 18:52:34 +00:00
// This restores all sectors to original values
private void RestoreSectors()
{
int index = 0;
foreach(Sector s in selection)
{
SectorInfo si = sectorinfo[index];
s.Fields.BeforeFieldsChange();
s.Fields[RotationName] = new UniValue(UniversalType.AngleDegreesFloat, Angle2D.RadToDeg(si.rotation));
s.Fields[XScaleName] = new UniValue(UniversalType.Float, si.scale.x);
s.Fields[YScaleName] = new UniValue(UniversalType.Float, si.scale.y);
s.Fields[XOffsetName] = new UniValue(UniversalType.Float, si.offset.x);
s.Fields[YOffsetName] = new UniValue(UniversalType.Float, -si.offset.y);
s.UpdateNeeded = true;
s.UpdateCache();
index++;
}
}
2011-12-08 14:27:23 +00:00
// This updates the selection
private void Update()
{
// Not in any modifying mode?
if(mode == ModifyMode.None)
{
2011-12-10 14:27:42 +00:00
Vector2D prevdragoffset = alignoffset;
2020-05-21 12:20:02 +00:00
alignoffset = new Vector2D(double.MinValue, double.MinValue);
2011-12-10 14:27:42 +00:00
showalignoffset = false;
2011-12-08 14:27:23 +00:00
// Check what grip the mouse is over
// and change cursor accordingly
Grip mousegrip = CheckMouseGrip();
switch(mousegrip)
{
case Grip.Main:
int closestcorner = -1;
2020-05-21 12:20:02 +00:00
double cornerdist = double.MaxValue;
2011-12-08 14:27:23 +00:00
for(int i = 0; i < 4; i++)
{
Vector2D delta = corners[i] - mousemappos;
2020-05-21 12:20:02 +00:00
double d = delta.GetLengthSq();
2011-12-11 18:47:28 +00:00
if(d < cornerdist)
2011-12-08 14:27:23 +00:00
{
closestcorner = i;
cornerdist = d;
}
}
switch(closestcorner)
{
// TODO:
2011-12-10 14:27:42 +00:00
case 0: alignoffset = new Vector2D(0f, 0f); break;
2011-12-11 18:47:28 +00:00
case 1: alignoffset = new Vector2D(texture.ScaledWidth, 0f); break;
case 2: alignoffset = new Vector2D(texture.ScaledWidth, -texture.ScaledHeight); break;
case 3: alignoffset = new Vector2D(0f, -texture.ScaledHeight); break;
2011-12-08 14:27:23 +00:00
}
2011-12-10 14:27:42 +00:00
showalignoffset = true;
2011-12-08 14:27:23 +00:00
General.Interface.SetCursor(Cursors.Hand);
break;
case Grip.RotateLB:
case Grip.RotateRT:
2011-12-11 18:47:28 +00:00
alignoffset = new Vector2D(0f, 0f);
showalignoffset = true;
2011-12-08 14:27:23 +00:00
General.Interface.SetCursor(Cursors.Cross);
break;
case Grip.SizeH:
case Grip.SizeV:
2011-12-11 18:47:28 +00:00
alignoffset = new Vector2D(0f, 0f);
showalignoffset = true;
2011-12-08 14:27:23 +00:00
// Pick the best matching cursor depending on rotation and side
2020-05-21 12:20:02 +00:00
double resizeangle = -(rotation + sectorinfo[0].rotation);
2011-12-08 14:27:23 +00:00
if(mousegrip == Grip.SizeH) resizeangle += Angle2D.PIHALF;
resizeangle = Angle2D.Normalized(resizeangle);
if(resizeangle > Angle2D.PI) resizeangle -= Angle2D.PI;
resizeangle = Math.Abs(resizeangle + Angle2D.PI / 8.000001f);
int cursorindex = (int)Math.Floor((resizeangle / Angle2D.PI) * 4.0f) % 4;
General.Interface.SetCursor(RESIZE_CURSORS[cursorindex]);
break;
default:
General.Interface.SetCursor(Cursors.Default);
break;
}
2011-12-10 14:27:42 +00:00
if(prevdragoffset != alignoffset)
2011-12-08 14:27:23 +00:00
General.Interface.RedrawDisplay();
}
else
{
2011-12-11 18:47:28 +00:00
Vector2D snappedmappos = mousemappos;
bool dosnaptogrid = snaptogrid;
2011-12-12 11:55:32 +00:00
2011-12-11 18:47:28 +00:00
// Options
snaptogrid = General.Interface.ShiftState ^ General.Interface.SnapToGrid;
snaptonearest = General.Interface.CtrlState ^ General.Interface.AutoMerge;
2011-12-08 14:27:23 +00:00
// Change to crosshair cursor so we can clearly see around the mouse cursor
General.Interface.SetCursor(Cursors.Cross);
// Check what modifying mode we are in
switch(mode)
{
case ModifyMode.Dragging:
2011-12-12 11:55:32 +00:00
2011-12-10 12:07:00 +00:00
offset = -mousemappos - dragoffset;
2011-12-12 11:55:32 +00:00
Vector2D transformedpos = TexToWorld(alignoffset);
// Snap to nearest vertex?
if(snaptonearest)
{
float vrange = BuilderPlug.Me.StitchRange / renderer.Scale;
// Try the nearest vertex
Vertex nv = MapSet.NearestVertexSquareRange(General.Map.Map.Vertices, transformedpos, vrange);
if(nv != null)
{
// Change offset to snap to target
offset -= nv.Position - transformedpos;
dosnaptogrid = false;
}
else
{
// Find the nearest line within range
Linedef nl = MapSet.NearestLinedefRange(General.Map.Map.Linedefs, transformedpos, vrange);
if(nl != null)
{
// Snap to grid?
if(dosnaptogrid)
{
// Get grid intersection coordinates
List<Vector2D> coords = nl.GetGridIntersections(General.Map.Grid.GridRotate, General.Map.Grid.GridOriginX, General.Map.Grid.GridOriginY);
2011-12-12 11:55:32 +00:00
// Find nearest grid intersection
2020-05-21 12:20:02 +00:00
double found_distance = double.MaxValue;
2011-12-12 11:55:32 +00:00
Vector2D found_pos = new Vector2D(float.NaN, float.NaN);
foreach(Vector2D v in coords)
{
Vector2D dist = transformedpos - v;
if(dist.GetLengthSq() < found_distance)
{
// Found a better match
found_distance = dist.GetLengthSq();
found_pos = v;
// Do not snap to grid anymore
dosnaptogrid = false;
}
}
// Found something?
2020-05-21 12:20:02 +00:00
if(!double.IsNaN(found_pos.x))
2011-12-12 11:55:32 +00:00
{
// Change offset to snap to target
offset -= found_pos - transformedpos;
}
}
else
{
// Change offset to snap onto the line
offset -= nl.NearestOnLine(transformedpos) - transformedpos;
}
}
}
}
// Snap to grid?
if(dosnaptogrid)
{
// Change offset to align to grid
offset -= General.Map.Grid.SnappedToGrid(transformedpos) - transformedpos;
}
2011-12-08 14:27:23 +00:00
break;
case ModifyMode.Resizing:
2011-12-12 11:55:32 +00:00
// Snap to nearest vertex?
if(snaptonearest)
{
float vrange = BuilderPlug.Me.StitchRange / renderer.Scale;
// Try the nearest vertex
Vertex nv = MapSet.NearestVertexSquareRange(General.Map.Map.Vertices, snappedmappos, vrange);
if(nv != null)
{
snappedmappos = nv.Position;
dosnaptogrid = false;
}
}
// Snap to grid?
if(dosnaptogrid)
{
// Aligned to grid
snappedmappos = General.Map.Grid.SnappedToGrid(snappedmappos);
}
2020-05-21 12:20:02 +00:00
double newscale = 1f / resizeaxis.GetNearestOnLine(snappedmappos);
if(double.IsInfinity(newscale) || double.IsNaN(newscale)) newscale = 99999f;
2011-12-10 14:27:42 +00:00
scale = (newscale * resizefilter) + scale * (1.0f - resizefilter);
2020-05-21 12:20:02 +00:00
if(double.IsInfinity(scale.x) || double.IsNaN(scale.x)) scale.x = 99999f;
if(double.IsInfinity(scale.y) || double.IsNaN(scale.y)) scale.y = 99999f;
2011-12-12 11:55:32 +00:00
// Show the extension line so that the user knows what it is aligning to
UpdateRectangleComponents();
Line2D edgeline;
if(resizefilter.x > resizefilter.y)
edgeline = new Line2D(corners[1], corners[2]);
else
edgeline = new Line2D(corners[3], corners[2]);
2020-05-21 12:20:02 +00:00
double nearestonedge = edgeline.GetNearestOnLine(snappedmappos);
2011-12-12 11:55:32 +00:00
if(nearestonedge > 0.5f)
extensionline = new Line2D(edgeline.v1, snappedmappos);
else
extensionline = new Line2D(edgeline.v2, snappedmappos);
2011-12-08 14:27:23 +00:00
break;
case ModifyMode.Rotating:
2011-12-12 11:55:32 +00:00
// Snap to nearest vertex?
extensionline = new Line2D();
if(snaptonearest)
{
float vrange = BuilderPlug.Me.StitchRange / renderer.Scale;
// Try the nearest vertex
Vertex nv = MapSet.NearestVertexSquareRange(General.Map.Map.Vertices, snappedmappos, vrange);
if(nv != null)
{
snappedmappos = nv.Position;
dosnaptogrid = false;
// Show the extension line so that the user knows what it is aligning to
extensionline = new Line2D(corners[0], snappedmappos);
}
}
Vector2D delta = snappedmappos - rotationcenter;
2020-05-21 12:20:02 +00:00
double deltaangle = -delta.GetAngle();
2011-12-12 11:55:32 +00:00
// Snap to grid?
if(dosnaptogrid)
{
// We make 24 vectors that the rotation can snap to
2020-05-21 12:20:02 +00:00
double founddistance = double.MaxValue;
double foundrotation = rotation;
Vector3D rotvec = Vector2D.FromAngle(deltaangle + rotationoffset);
for(int i = 0; i < 24; i++)
2011-12-12 11:55:32 +00:00
{
// Make the vectors
2020-05-21 12:20:02 +00:00
double angle = i * Angle2D.PI * 0.08333333333f; //mxd. 15-degree increments
2011-12-12 11:55:32 +00:00
Vector2D gridvec = Vector2D.FromAngle(angle);
// Check distance
2020-05-21 12:20:02 +00:00
double dist = 2.0f - Vector2D.DotProduct(gridvec, rotvec);
2011-12-12 11:55:32 +00:00
if(dist < founddistance)
{
foundrotation = angle;
founddistance = dist;
}
}
// Keep rotation
2011-12-12 18:52:34 +00:00
rotation = foundrotation - sectorinfo[0].rotation;
2011-12-12 11:55:32 +00:00
}
else
{
rotation = deltaangle + rotationoffset - sectorinfo[0].rotation;
}
2011-12-08 14:27:23 +00:00
break;
}
2011-12-09 06:58:39 +00:00
UpdateSectors();
General.Interface.RedrawDisplay();
2011-12-08 14:27:23 +00:00
}
}
2011-12-08 07:24:21 +00:00
// This updates the selection rectangle components
private void UpdateRectangleComponents()
{
float gripsize = GRIP_SIZE / renderer.Scale;
2011-12-11 18:47:28 +00:00
PixelColor rectcolor = General.Colors.Highlight.WithAlpha(RECTANGLE_ALPHA);
2011-12-08 07:24:21 +00:00
// Corners in world space
2013-03-18 13:52:27 +00:00
corners[0] = TexToWorld(new Vector2D(0f, 0f));
corners[1] = TexToWorld(new Vector2D(texture.ScaledWidth, 0f));
corners[2] = TexToWorld(new Vector2D(texture.ScaledWidth, -texture.ScaledHeight));
corners[3] = TexToWorld(new Vector2D(0f, -texture.ScaledHeight));
2011-12-08 07:24:21 +00:00
2011-12-11 18:47:28 +00:00
// Vertices
cornerverts = new FlatVertex[6];
for(int i = 0; i < 6; i++)
{
cornerverts[i] = new FlatVertex();
cornerverts[i].z = 1.0f;
cornerverts[i].c = rectcolor.ToInt();
}
2020-05-21 12:20:02 +00:00
cornerverts[0].x = (float)corners[0].x;
cornerverts[0].y = (float)corners[0].y;
cornerverts[1].x = (float)corners[1].x;
cornerverts[1].y = (float)corners[1].y;
cornerverts[2].x = (float)corners[2].x;
cornerverts[2].y = (float)corners[2].y;
cornerverts[3].x = (float)corners[0].x;
cornerverts[3].y = (float)corners[0].y;
cornerverts[4].x = (float)corners[2].x;
cornerverts[4].y = (float)corners[2].y;
cornerverts[5].x = (float)corners[3].x;
cornerverts[5].y = (float)corners[3].y;
2011-12-11 18:47:28 +00:00
2011-12-08 07:24:21 +00:00
// Extended points for rotation corners
2013-03-18 13:52:27 +00:00
extends[0] = TexToWorld(new Vector2D(texture.ScaledWidth + (20f * Math.Sign(scale.x * sectorinfo[0].scale.x)) / renderer.Scale * (scale.x * sectorinfo[0].scale.x), 0f));
extends[1] = TexToWorld(new Vector2D(0f, -texture.ScaledHeight + (-20f * Math.Sign(scale.y * sectorinfo[0].scale.y)) / renderer.Scale * (scale.y * sectorinfo[0].scale.y)));
2011-12-08 07:24:21 +00:00
// Middle points between corners
Vector2D middle12 = corners[1] + (corners[2] - corners[1]) * 0.5f;
Vector2D middle23 = corners[2] + (corners[3] - corners[2]) * 0.5f;
// Resize grips
2020-05-21 12:20:02 +00:00
resizegrips[0] = new RectangleF((float)(middle12.x - gripsize * 0.5f),
(float)(middle12.y - gripsize * 0.5f),
2011-12-08 07:24:21 +00:00
gripsize, gripsize);
2020-05-21 12:20:02 +00:00
resizegrips[1] = new RectangleF((float)(middle23.x - gripsize * 0.5f),
(float)(middle23.y - gripsize * 0.5f),
2011-12-08 07:24:21 +00:00
gripsize, gripsize);
// Rotate grips
2020-05-21 12:20:02 +00:00
rotategrips[0] = new RectangleF((float)(extends[0].x - gripsize * 0.5f),
(float)(extends[0].y - gripsize * 0.5f),
2011-12-08 07:24:21 +00:00
gripsize, gripsize);
2020-05-21 12:20:02 +00:00
rotategrips[1] = new RectangleF((float)(extends[1].x - gripsize * 0.5f),
(float)(extends[1].y - gripsize * 0.5f),
2011-12-08 07:24:21 +00:00
gripsize, gripsize);
2011-12-10 14:27:42 +00:00
if(showalignoffset)
{
2013-03-18 13:52:27 +00:00
Vector2D worldalignoffset = TexToWorld(alignoffset);
2020-05-21 12:20:02 +00:00
alignrect = new RectangleF((float)(worldalignoffset.x - gripsize * 0.5f),
(float)(worldalignoffset.y - gripsize * 0.5f),
2011-12-11 18:47:28 +00:00
gripsize, gripsize);
2011-12-10 14:27:42 +00:00
}
2011-12-08 14:27:23 +00:00
}
// This checks and returns the grip the mouse pointer is in
private Grip CheckMouseGrip()
{
if(PointInRectF(resizegrips[0], mousemappos)) return Grip.SizeH;
if(PointInRectF(resizegrips[1], mousemappos)) return Grip.SizeV;
if(PointInRectF(rotategrips[0], mousemappos)) return Grip.RotateRT;
if(PointInRectF(rotategrips[1], mousemappos)) return Grip.RotateLB;
if(Tools.PointInPolygon(corners, mousemappos)) return Grip.Main;
return Grip.None;
2011-12-08 07:24:21 +00:00
}
2011-12-06 15:35:11 +00:00
#endregion
#region ================== Events
// Mode engages
public override void OnEngage()
{
prevviewmode = General.Map.Renderer2D.ViewMode; //mxd
2011-12-06 15:35:11 +00:00
base.OnEngage();
2011-12-07 06:57:38 +00:00
2011-12-12 18:52:34 +00:00
// We don't want to record this for undoing while we move the geometry around.
// This will be set back to normal when we're done.
General.Map.UndoRedo.IgnorePropChanges = true;
2011-12-07 06:57:38 +00:00
// Presentation
2011-12-06 15:35:11 +00:00
renderer.SetPresentation(Presentation.Standard);
2011-12-07 06:57:38 +00:00
// Selection
General.Map.Map.ConvertSelection(SelectionType.Sectors);
General.Map.Map.SelectionType = SelectionType.Sectors;
if(General.Map.Map.SelectedSectorsCount == 0)
{
// Find the nearest linedef within highlight range
Linedef l = General.Map.Map.NearestLinedef(mousemappos);
if(l != null)
{
Sector selectsector = null;
// Check on which side of the linedef the mouse is and which sector there is
2020-05-21 12:20:02 +00:00
double side = l.SideOfLine(mousemappos);
2011-12-07 06:57:38 +00:00
if((side > 0) && (l.Back != null))
selectsector = l.Back.Sector;
else if((side <= 0) && (l.Front != null))
selectsector = l.Front.Sector;
// Select the sector!
if(selectsector != null)
{
selectsector.Selected = true;
foreach(Sidedef sd in selectsector.Sidedefs)
sd.Line.Selected = true;
}
}
}
// Get sector selection
selection = General.Map.Map.GetSelectedSectors(true);
if(selection.Count == 0)
{
General.Interface.MessageBeep(MessageBeepType.Default);
General.Interface.DisplayStatus(StatusType.Action, "A selected sector is required for this action.");
2011-12-07 06:57:38 +00:00
General.Editing.CancelMode();
2011-12-07 21:05:09 +00:00
return;
2011-12-07 06:57:38 +00:00
}
2011-12-07 21:05:09 +00:00
editsector = General.GetByIndex(selection, 0);
2011-12-07 06:57:38 +00:00
2011-12-07 21:05:09 +00:00
// Get the texture
texture = GetTexture(editsector);
if((texture == null) || (texture == General.Map.Data.WhiteTexture) ||
(texture.Width <= 0) || (texture.Height <= 0) || !texture.IsImageLoaded)
{
General.Interface.MessageBeep(MessageBeepType.Default);
General.Interface.DisplayStatus(StatusType.Action, "The selected sector must have a loaded texture to align.");
2011-12-07 21:05:09 +00:00
General.Editing.CancelMode();
return;
}
// Cache the transformation values
2011-12-08 07:24:21 +00:00
sectorinfo = new List<SectorInfo>(selection.Count);
foreach(Sector s in selection)
{
SectorInfo si;
si.rotation = Angle2D.DegToRad(s.Fields.GetValue(RotationName, 0.0));
si.scale.x = s.Fields.GetValue(XScaleName, 1.0);
si.scale.y = s.Fields.GetValue(YScaleName, 1.0);
si.offset.x = s.Fields.GetValue(XOffsetName, 0.0);
si.offset.y = -s.Fields.GetValue(YOffsetName, 0.0);
2011-12-08 07:24:21 +00:00
sectorinfo.Add(si);
}
2011-12-07 21:05:09 +00:00
// We want the texture corner nearest to the center of the sector
Vector2D fp;
fp.x = (editsector.BBox.Left + editsector.BBox.Right) / 2;
fp.y = (editsector.BBox.Top + editsector.BBox.Bottom) / 2;
2011-12-07 06:57:38 +00:00
2011-12-07 21:05:09 +00:00
// Transform the point into texture space
2011-12-09 06:58:39 +00:00
fp = WorldToTex(fp);
2011-12-07 21:05:09 +00:00
// Snap to the nearest left-top corner
fp.x = Math.Floor(fp.x / texture.ScaledWidth) * texture.ScaledWidth;
fp.y = Math.Ceiling(fp.y / texture.ScaledHeight) * texture.ScaledHeight;
2011-12-11 16:37:15 +00:00
// Now move the offset so that the 0,0 point is at this location
// We want to work with the 0,0 location because it makes things easier.
SectorInfo si0 = sectorinfo[0];
si0.offset -= fp / si0.scale;
sectorinfo[0] = si0;
2011-12-07 21:05:09 +00:00
2011-12-08 07:24:21 +00:00
UpdateRectangleComponents();
2011-12-11 16:37:15 +00:00
UpdateSectors();
2011-12-06 15:35:11 +00:00
}
// Mode disengages
public override void OnDisengage()
{
switch(prevviewmode)
{
case ViewMode.Normal: General.Actions.InvokeAction("builder_viewmodenormal"); break;
case ViewMode.FloorTextures: General.Actions.InvokeAction("builder_viewmodefloors"); break;
case ViewMode.CeilingTextures: General.Actions.InvokeAction("builder_viewmodeceilings"); break;
case ViewMode.Brightness: General.Actions.InvokeAction("builder_viewmodebrightness"); break;
}
2011-12-06 15:35:11 +00:00
base.OnDisengage();
2011-12-12 18:52:34 +00:00
// When not cancelled manually, we assume it is accepted
if(!cancelled)
{
modealreadyswitching = true;
General.Editing.AcceptMode();
}
2011-12-06 15:35:11 +00:00
// Hide highlight info
2011-12-08 14:27:23 +00:00
General.Interface.SetCursor(Cursors.Default);
2011-12-06 15:35:11 +00:00
General.Interface.HideInfo();
2011-12-12 18:52:34 +00:00
General.Interface.RedrawDisplay();
}
// When accepted
public override void OnAccept()
{
base.OnAccept();
if(!modealreadyswitching)
{
modealreadyswitching = true;
// Restore original values
RestoreSectors();
General.Map.Map.Update();
2011-12-12 18:52:34 +00:00
// Make undo
General.Map.UndoRedo.CreateUndo(UndoDescription);
2011-12-12 18:52:34 +00:00
// Resume normal undo/redo recording
General.Map.UndoRedo.IgnorePropChanges = false;
2011-12-12 18:52:34 +00:00
// Apply changes
UpdateSectors();
General.Map.Map.Update();
2011-12-12 18:52:34 +00:00
// Clear selection
if(selection.Count == 1)
General.Map.Map.ClearAllSelected();
2011-12-12 18:52:34 +00:00
// Done
General.Map.IsChanged = true;
selection = null;
sectorinfo = null;
2011-12-12 18:52:34 +00:00
// Return to previous stable mode
General.Editing.ChangeMode(General.Editing.PreviousStableMode.Name);
}
2011-12-06 15:35:11 +00:00
}
2011-12-08 14:27:23 +00:00
// Cancel mode
public override void OnCancel()
{
base.OnCancel();
modealreadyswitching = true;
2011-12-12 18:52:34 +00:00
// Restore original values
RestoreSectors();
General.Map.Map.Update();
2011-12-08 14:27:23 +00:00
2011-12-12 18:52:34 +00:00
// Resume normal undo/redo recording
General.Map.UndoRedo.IgnorePropChanges = false;
2011-12-08 14:27:23 +00:00
// Return to previous stable mode
General.Editing.ChangeMode(General.Editing.PreviousStableMode.Name);
}
// Mouse moves
public override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if(panning) return; //mxd. Skip all this jazz while panning
2011-12-08 14:27:23 +00:00
Update();
}
// Mouse leaves the display
public override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
// Reset cursor
General.Interface.SetCursor(Cursors.Default);
}
2011-12-09 06:58:39 +00:00
// When edit button is pressed
protected override void OnEditBegin()
{
base.OnEditBegin();
OnSelectBegin();
}
// When edit button is released
protected override void OnEditEnd()
{
base.OnEditEnd();
OnSelectEnd();
}
// When select button is pressed
protected override void OnSelectBegin()
{
base.OnSelectBegin();
if(mode != ModifyMode.None) return;
// Check what grip the mouse is over
switch(CheckMouseGrip())
{
// Drag main rectangle
case Grip.Main:
2011-12-10 12:07:00 +00:00
dragoffset = -mousemappos - offset;
2011-12-09 06:58:39 +00:00
mode = ModifyMode.Dragging;
EnableAutoPanning();
autopanning = true;
break;
2011-12-10 14:27:42 +00:00
// Scale
case Grip.SizeH:
// The resize vector is a unit vector in the direction of the resize.
// We multiply this with the sign of the current size, because the
// corners may be reversed when the selection is flipped.
resizevector = corners[1] - corners[0];
resizevector = resizevector.GetNormal() * Math.Sign(scale.x);
// Make the resize axis. This is a line with the length and direction
// of basesize used to calculate the resize percentage.
resizeaxis = new Line2D(corners[0], corners[0] + resizevector * texture.ScaledWidth / Math.Abs(sectorinfo[0].scale.x));
// Original axis filter
resizefilter = new Vector2D(1.0f, 0.0f);
mode = ModifyMode.Resizing;
break;
// Scale
case Grip.SizeV:
// See description above
resizevector = corners[2] - corners[1];
resizevector = resizevector.GetNormal() * Math.Sign(scale.y);
resizeaxis = new Line2D(corners[1], corners[1] + resizevector * texture.ScaledHeight / Math.Abs(sectorinfo[0].scale.y));
resizefilter = new Vector2D(0.0f, 1.0f);
mode = ModifyMode.Resizing;
break;
2011-12-11 16:37:15 +00:00
// Rotate
case Grip.RotateRT:
rotationoffset = Angle2D.PIHALF;
if(Math.Sign(scale.x * sectorinfo[0].scale.x) < 0)
rotationoffset += Angle2D.PI;
rotationcenter = corners[0];
mode = ModifyMode.Rotating;
break;
// Rotate
case Grip.RotateLB:
rotationoffset = 0f;
if(Math.Sign(scale.y * sectorinfo[0].scale.y) < 0)
rotationoffset += Angle2D.PI;
rotationcenter = corners[0];
mode = ModifyMode.Rotating;
break;
2011-12-09 06:58:39 +00:00
// Outside the selection?
default:
// Accept and be done with it
General.Editing.AcceptMode();
break;
}
}
// When selected button is released
protected override void OnSelectEnd()
{
base.OnSelectEnd();
2011-12-12 11:55:32 +00:00
// Remove extension line
extensionline = new Line2D();
2011-12-09 06:58:39 +00:00
if(autopanning)
{
DisableAutoPanning();
autopanning = false;
}
// No modifying mode
mode = ModifyMode.None;
// Redraw
General.Map.Map.Update();
General.Interface.RedrawDisplay();
}
2011-12-06 15:35:11 +00:00
// This redraws the display
public override void OnRedrawDisplay()
{
if(sectorinfo != null) UpdateRectangleComponents();
2011-12-06 15:35:11 +00:00
renderer.RedrawSurface();
// Render lines
if(renderer.StartPlotter(true))
{
renderer.PlotLinedefSet(General.Map.Map.Linedefs);
renderer.PlotVerticesSet(General.Map.Map.Vertices);
renderer.Finish();
}
// Render things
if(renderer.StartThings(true))
{
renderer.RenderThingSet(General.Map.ThingsFilter.HiddenThings, General.Settings.HiddenThingsAlpha);
renderer.RenderThingSet(General.Map.ThingsFilter.VisibleThings, General.Settings.ActiveThingsAlpha);
renderer.RenderSRB2Extras();
2011-12-06 15:35:11 +00:00
renderer.Finish();
}
// Render overlay
if(sectorinfo != null)
2011-12-06 15:35:11 +00:00
{
if(renderer.StartOverlay(true))
{
// Rectangle
PixelColor rectcolor = General.Colors.Highlight.WithAlpha(RECTANGLE_ALPHA);
renderer.RenderGeometry(cornerverts, null, true);
if(extensionline.GetLengthSq() > 0.0f)
renderer.RenderLine(extensionline.v1, extensionline.v2, 1, General.Colors.Indication.WithAlpha(EXTENSION_LINE_ALPHA), true);
renderer.RenderLine(corners[0], corners[1], 4, rectcolor, true);
renderer.RenderLine(corners[1], corners[2], 4, rectcolor, true);
renderer.RenderLine(corners[2], corners[3], 4, rectcolor, true);
renderer.RenderLine(corners[3], corners[0], 4, rectcolor, true);
// Lines
renderer.RenderLine(corners[0], extends[0], 1f, General.Colors.Highlight, true);
renderer.RenderLine(corners[0], extends[1], 1f, General.Colors.Highlight, true);
renderer.RenderLine(corners[1], corners[2], 0.5f, General.Colors.Highlight, true);
renderer.RenderLine(corners[2], corners[3], 0.5f, General.Colors.Highlight, true);
// Handles
renderer.RenderRectangleFilled(rotategrips[0], General.Colors.Background, true);
renderer.RenderRectangleFilled(rotategrips[1], General.Colors.Background, true);
renderer.RenderRectangle(rotategrips[0], 2f, General.Colors.Indication, true);
renderer.RenderRectangle(rotategrips[1], 2f, General.Colors.Indication, true);
renderer.RenderRectangleFilled(resizegrips[0], General.Colors.Background, true);
renderer.RenderRectangleFilled(resizegrips[1], General.Colors.Background, true);
renderer.RenderRectangle(resizegrips[0], 2f, General.Colors.Highlight, true);
renderer.RenderRectangle(resizegrips[1], 2f, General.Colors.Highlight, true);
// Rotate/align point
if(showalignoffset)
renderer.RenderRectangleFilled(alignrect, General.Colors.Selection, true);
renderer.Finish();
}
2011-12-06 15:35:11 +00:00
}
2011-12-06 15:35:11 +00:00
renderer.Present();
}
#endregion
2011-12-08 14:27:23 +00:00
#region ================== Actions
// This clears the selection
[BeginAction("clearselection", BaseAction = true)]
public void ClearSelection()
{
// Accept changes
General.Editing.AcceptMode();
General.Map.Map.ClearAllSelected();
General.Interface.RedrawDisplay();
}
#endregion
2011-12-06 15:35:11 +00:00
}
}