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

575 lines
18 KiB
C#
Raw Normal View History

#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.Collections.ObjectModel;
using System.Drawing;
using System.Globalization;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Geometry;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Types;
using CodeImp.DoomBuilder.VisualModes;
using CodeImp.DoomBuilder.GZBuilder.Tools;
using CodeImp.DoomBuilder.Data;
#endregion
namespace CodeImp.DoomBuilder.BuilderModes
{
internal sealed class VisualFloor : BaseVisualGeometrySector
{
#region ================== Constants
#endregion
#region ================== Variables
2013-03-18 13:52:27 +00:00
private bool innerSide; //mxd
#endregion
#region ================== Properties
#endregion
#region ================== Constructor / Setup
// Constructor
public VisualFloor(BaseVisualMode mode, VisualSector vs) : base(mode, vs)
{
//mxd
Visual mode: "Fit Textures" action can now fit textures across multiple selected surfaces. A number of times to repeat a texture can now be specified. Visual mode: removed "Fit Texture's Width" and "Fit Texture's Height" actions. Visual mode: "Auto-align texture offsets" actions were incorrectly aligning double-sided middle walls in some cases. Visual mode: "Auto-align texture offsets" actions now align non-wrapped double-sided middle walls to vertical offset closest to their initial vertical offset. Visual mode: middle parts of double-sided walls were ignored when Shift-selecting walls. Nodebuilders/Game configurations: GL nodes definitions were missing from game configurations. Nodebuilders/Game configurations: "~MAP" wildcard can now be a part of a lump name. Nodebuilders: GL nodes were not properly handled by the editor. Main Window: the window is now moved into the view when stored position is ouside of screen bounds. Classic and Visual modes: changing thing pitch was ignored in some cases. Visual mode: raising and lowering a thing with "+SPAWNCEILING" flag now works the same way as when raising/lowering a regular thing. Visual mode: using "Raise/Lower Floor/Ceiling to adjacent sector" actions on a thing with "+SPAWNCEILING" flag now works the same way as when using them on a regular thing. Rendering: even more fixes to MODELDEF and UDMF properties-related model rendering logic. Internal, ResourceListEditor: rewritten resource validation check in a more OOP-ish way. Configurations: fixed an infinite loop crash when a file was trying to include() itself. UDMF thing flags: added Skill 6-8 to the flags list (because there are thing filters for these). ZDoom_ACS.cfg: added definitions for SetTeleFog and SwapTeleFog. ZDoom_DECORATE.cfg: added definitions for A_SetTeleFog and A_SwapTeleFog. Updated ZDoom ACC. Updated documentation.
2014-12-22 21:36:49 +00:00
geometrytype = VisualGeometryType.FLOOR;
partname = "floor";
2013-03-18 13:52:27 +00:00
//mxd
if(mode.UseSelectionFromClassicMode && vs != null && vs.Sector.Selected
&& (General.Map.ViewMode == ViewMode.FloorTextures || General.Map.ViewMode == ViewMode.Normal))
{
2013-03-18 13:52:27 +00:00
this.selected = true;
mode.AddSelectedObject(this);
}
// We have no destructor
GC.SuppressFinalize(this);
}
// This builds the geometry. Returns false when no geometry created.
public override bool Setup(SectorLevel level, Effect3DFloor extrafloor)
{
2013-03-18 13:52:27 +00:00
return Setup(level, extrafloor, innerSide);
}
//mxd
public bool Setup(SectorLevel level, Effect3DFloor extrafloor, bool innerSide)
{
Sector s = level.sector;
Vector2D texscale;
2013-03-18 13:52:27 +00:00
this.innerSide = innerSide;
base.Setup(level, extrafloor);
// Fetch ZDoom fields
float rotate = Angle2D.DegToRad(s.Fields.GetValue("rotationfloor", 0.0f));
Vector2D offset = new Vector2D(s.Fields.GetValue("xpanningfloor", 0.0f),
s.Fields.GetValue("ypanningfloor", 0.0f));
Vector2D scale = new Vector2D(s.Fields.GetValue("xscalefloor", 1.0f),
s.Fields.GetValue("yscalefloor", 1.0f));
2013-03-18 13:52:27 +00:00
//Load floor texture
if (s.LongFloorTexture != MapSet.EmptyLongName)
{
base.Texture = General.Map.Data.GetFlatImage(s.LongFloorTexture);
if(base.Texture == null || base.Texture is UnknownImage)
{
base.Texture = General.Map.Data.UnknownTexture3D;
setuponloadedtexture = s.LongFloorTexture;
}
else
{
if (!base.Texture.IsImageLoaded)
setuponloadedtexture = s.LongFloorTexture;
}
}
else
{
// Use missing texture
base.Texture = General.Map.Data.MissingTexture3D;
setuponloadedtexture = 0;
}
// Determine texture scale
if(base.Texture.IsImageLoaded)
texscale = new Vector2D(1.0f / base.Texture.ScaledWidth, 1.0f / base.Texture.ScaledHeight);
else
texscale = new Vector2D(1.0f / 64.0f, 1.0f / 64.0f);
//mxd. Sky is always bright
int color;
if(s.FloorTexture == General.Map.Config.SkyFlatName)
color = -1; // That's white. With alpha. Not very impressive, eh?
else
color = PixelColor.FromInt(level.color).WithAlpha((byte)General.Clamp(level.alpha, 0, 255)).ToInt();
// Make vertices
ReadOnlyCollection<Vector2D> triverts = base.Sector.Sector.Triangles.Vertices;
WorldVertex[] verts = new WorldVertex[triverts.Count];
for(int i = 0; i < triverts.Count; i++)
{
// Color shading
verts[i].c = color; //mxd
// Vertex coordinates
verts[i].x = triverts[i].x;
verts[i].y = triverts[i].y;
verts[i].z = level.plane.GetZ(triverts[i]);
// Texture coordinates
Vector2D pos = triverts[i];
pos = pos.GetRotated(rotate);
pos.y = -pos.y;
pos = (pos + offset) * scale * texscale;
verts[i].u = pos.x;
verts[i].v = pos.y;
}
// The sector triangulation created clockwise triangles that
// are right up for the floor. For the ceiling we must flip
// the triangles upside down.
2013-03-18 13:52:27 +00:00
if((extrafloor != null) && !extrafloor.VavoomType && !innerSide)
SwapTriangleVertices(verts);
// Determine render pass
if(extrafloor != null)
{
if(extrafloor.Sloped3dFloor) //mxd
this.RenderPass = RenderPass.Mask;
else if((extrafloor.Linedef.Args[2] & (int)Effect3DFloor.Flags.RenderAdditive) != 0) //mxd
2013-03-18 13:52:27 +00:00
this.RenderPass = RenderPass.Additive;
else if(level.alpha < 255)
this.RenderPass = RenderPass.Alpha;
else
this.RenderPass = RenderPass.Mask;
}
else
{
this.RenderPass = RenderPass.Solid;
}
// Apply vertices
base.SetVertices(verts);
return (verts.Length > 0);
}
#endregion
#region ================== Methods
// Return texture coordinates
protected override Point GetTextureOffset()
{
Point p = new Point();
p.X = (int)Sector.Sector.Fields.GetValue("xpanningfloor", 0.0f);
p.Y = (int)Sector.Sector.Fields.GetValue("ypanningfloor", 0.0f);
return p;
}
// Move texture coordinates
protected override void MoveTextureOffset(Point xy)
{
//mxd
Sector s = GetControlSector();
s.Fields.BeforeFieldsChange();
float nx = (s.Fields.GetValue("xpanningfloor", 0.0f) + xy.X) % (Texture.ScaledWidth / s.Fields.GetValue("xscalefloor", 1.0f));
float ny = (s.Fields.GetValue("ypanningfloor", 0.0f) + xy.Y) % (Texture.ScaledHeight / s.Fields.GetValue("yscalefloor", 1.0f));
s.Fields["xpanningfloor"] = new UniValue(UniversalType.Float, nx);
s.Fields["ypanningfloor"] = new UniValue(UniversalType.Float, ny);
s.UpdateNeeded = true;
mode.SetActionResult("Changed floor texture offsets to " + nx + ", " + ny + ".");
}
//mxd. Texture scale change
protected override void ChangeTextureScale(int incrementX, int incrementY)
{
Sector s = GetControlSector();
float scaleX = s.Fields.GetValue("xscalefloor", 1.0f);
float scaleY = s.Fields.GetValue("yscalefloor", 1.0f);
s.Fields.BeforeFieldsChange();
if(incrementX != 0)
{
float pix = (int)Math.Round(Texture.Width * scaleX) - incrementX;
float newscaleX = (float)Math.Round(pix / Texture.Width, 3);
scaleX = (newscaleX == 0 ? scaleX * -1 : newscaleX);
UDMFTools.SetFloat(s.Fields, "xscalefloor", scaleX, 1.0f);
}
if(incrementY != 0)
{
float pix = (int)Math.Round(Texture.Height * scaleY) - incrementY;
float newscaleY = (float)Math.Round(pix / Texture.Height, 3);
scaleY = (newscaleY == 0 ? scaleY * -1 : newscaleY);
UDMFTools.SetFloat(s.Fields, "yscalefloor", scaleY, 1.0f);
}
//update geometry
OnTextureChanged();
s.UpdateNeeded = true;
s.UpdateCache();
if(s.Index != Sector.Sector.Index)
{
Sector.Sector.UpdateNeeded = true;
Sector.Sector.UpdateCache();
}
mode.SetActionResult("Floor scale changed to " + scaleX.ToString("F03", CultureInfo.InvariantCulture) + ", " + scaleY.ToString("F03", CultureInfo.InvariantCulture) + " (" + (int)Math.Round(Texture.Width / scaleX) + " x " + (int)Math.Round(Texture.Height / scaleY) + ").");
}
//mxd
public override void OnResetTextureOffset()
{
ClearFields(new[] { "xpanningfloor", "ypanningfloor" }, "Reset texture offsets", "Texture offsets reset.");
}
//mxd
public override void OnResetLocalTextureOffset()
{
ClearFields(new[] { "xpanningfloor", "ypanningfloor", "xscalefloor", "yscalefloor", "rotationfloor", "lightfloor", "lightfloorabsolute" },
"Reset texture offsets, scale, rotation and brightness", "Texture offsets, scale, rotation and brightness reset.");
}
// Paste texture
public override void OnPasteTexture()
{
if(BuilderPlug.Me.CopiedFlat != null)
{
mode.CreateUndo("Paste floor '" + BuilderPlug.Me.CopiedFlat + "'");
mode.SetActionResult("Pasted flat '" + BuilderPlug.Me.CopiedFlat + "' on floor.");
//mxd. Glow effect may require SectorData and geometry update
bool prevtextureglows = General.Map.Data.GlowingFlats.ContainsKey(Sector.Sector.LongFloorTexture);
SetTexture(BuilderPlug.Me.CopiedFlat);
2013-03-18 13:52:27 +00:00
//mxd. Glow effect may require SectorData and geometry update
if(prevtextureglows && !General.Map.Data.GlowingFlats.ContainsKey(Sector.Sector.LongFloorTexture))
{
SectorData sd = mode.GetSectorData(level.sector);
sd.UpdateForced();
if(mode.VisualSectorExists(level.sector))
{
BaseVisualSector vs = (BaseVisualSector)mode.GetVisualSector(level.sector);
vs.UpdateSectorGeometry(false);
}
}
2013-03-18 13:52:27 +00:00
//mxd. 3D floors may need updating...
OnTextureChanged();
}
}
// Call to change the height
public override void OnChangeTargetHeight(int amount)
{
// Only do this when not done yet in this call
// Because we may be able to select the same 3D floor multiple times through multiple sectors
SectorData sd = mode.GetSectorData(level.sector);
if(!sd.FloorChanged)
{
sd.FloorChanged = true;
base.OnChangeTargetHeight(amount);
}
}
// This changes the height
protected override void ChangeHeight(int amount)
{
mode.CreateUndo("Change floor height", UndoGroup.FloorHeightChange, level.sector.FixedIndex);
level.sector.FloorHeight += amount;
if(General.Map.UDMF)
{
//mxd. Modify vertex offsets?
if(level.sector.Sidedefs.Count == 3)
{
ChangeVertexHeight(amount);
}
//mxd. Modify slope offset?
if(level.sector.FloorSlope.GetLengthSq() > 0)
{
Vector3D center = new Vector3D(level.sector.BBox.X + level.sector.BBox.Width / 2,
level.sector.BBox.Y + level.sector.BBox.Height / 2,
level.sector.FloorHeight);
Plane p = new Plane(center,
level.sector.FloorSlope.GetAngleXY() + Angle2D.PIHALF,
-level.sector.FloorSlope.GetAngleZ(),
true);
level.sector.FloorSlopeOffset = p.Offset;
}
}
mode.SetActionResult("Changed floor height to " + level.sector.FloorHeight + ".");
}
//mxd
private void ChangeVertexHeight(int amount)
{
List<Vertex> verts = new List<Vertex>(3);
//do this only if all 3 verts have offsets
foreach(Sidedef side in level.sector.Sidedefs)
{
if(float.IsNaN(side.Line.Start.ZFloor) || float.IsNaN(side.Line.End.ZFloor)) return;
if(!verts.Contains(side.Line.Start)) verts.Add(side.Line.Start);
if(!verts.Contains(side.Line.End)) verts.Add(side.Line.End);
}
foreach (Vertex v in verts)
mode.GetVisualVertex(v, true).OnChangeTargetHeight(amount);
}
//mxd. Sector brightness change
public override void OnChangeTargetBrightness(bool up)
{
if (level != null)
{
if (level.sector != Sector.Sector) //this floor is part of 3D-floor
((BaseVisualSector)mode.GetVisualSector(level.sector)).Floor.OnChangeTargetBrightness(up);
else if (Sector.ExtraFloors.Count > 0) //this is actual floor of a sector with extrafloors
Sector.ExtraFloors[0].OnChangeTargetBrightness(up);
else
base.OnChangeTargetBrightness(up);
}
else
{
base.OnChangeTargetBrightness(up);
}
}
// This performs a fast test in object picking
public override bool PickFastReject(Vector3D from, Vector3D to, Vector3D dir)
{
// Check if our ray starts at the correct side of the plane
2013-03-18 13:52:27 +00:00
if((!innerSide && level.plane.Distance(from) > 0.0f) || (innerSide && level.plane.Distance(from) < 0.0f))
{
// Calculate the intersection
if(level.plane.GetIntersection(from, to, ref pickrayu))
{
if(pickrayu > 0.0f)
{
pickintersect = from + (to - from) * pickrayu;
// Intersection point within bbox?
RectangleF bbox = Sector.Sector.BBox;
return ((pickintersect.x >= bbox.Left) && (pickintersect.x <= bbox.Right) &&
(pickintersect.y >= bbox.Top) && (pickintersect.y <= bbox.Bottom));
}
}
}
return false;
}
// This performs an accurate test for object picking
public override bool PickAccurate(Vector3D from, Vector3D to, Vector3D dir, ref float u_ray)
{
u_ray = pickrayu;
// Check on which side of the nearest sidedef we are
Sidedef sd = MapSet.NearestSidedef(Sector.Sector.Sidedefs, pickintersect);
float side = sd.Line.SideOfLine(pickintersect);
return (((side <= 0.0f) && sd.IsFront) || ((side > 0.0f) && !sd.IsFront));
}
// Return texture name
public override string GetTextureName()
{
return level.sector.FloorTexture;
}
// This changes the texture
protected override void SetTexture(string texturename)
{
level.sector.SetFloorTexture(texturename);
General.Map.Data.UpdateUsedTextures();
}
//mxd
public override void SelectNeighbours(bool select, bool withSameTexture, bool withSameHeight)
{
if (!withSameTexture && !withSameHeight) return;
if (select && !selected)
{
selected = true;
mode.AddSelectedObject(this);
}
else if (!select && selected)
{
selected = false;
mode.RemoveSelectedObject(this);
}
List<Sector> neighbours = new List<Sector>();
bool regularorvavoom = extrafloor == null || (extrafloor != null && extrafloor.VavoomType);
//collect neighbour sectors
foreach(Sidedef side in Sector.Sector.Sidedefs)
{
if (side.Other != null && side.Other.Sector != Sector.Sector && !neighbours.Contains(side.Other.Sector))
{
BaseVisualSector vs = mode.GetVisualSector(side.Other.Sector) as BaseVisualSector;
if (vs == null) continue;
bool add;
// When current floor is part of a 3d floor, it looks like a ceiling, so we need to select adjacent ceilings
if (level.sector != Sector.Sector && !regularorvavoom)
{
add = (withSameTexture && side.Other.Sector.CeilTexture == level.sector.FloorTexture);
if (withSameHeight)
{
add = ((withSameTexture && add) || !withSameTexture) && side.Other.Sector.CeilHeight == level.sector.FloorHeight;
}
if (add)
{
neighbours.Add(side.Other.Sector);
//(de)select regular visual ceiling?
if (select != vs.Ceiling.Selected)
{
vs.Ceiling.SelectNeighbours(select, withSameTexture, withSameHeight);
}
}
}
else // Regular floor or vavoom-type extrafloor
{
// (De)select adjacent floor
add = (withSameTexture && side.Other.Sector.FloorTexture == level.sector.FloorTexture);
if (withSameHeight)
{
add = ((withSameTexture && add) || !withSameTexture) && side.Other.Sector.FloorHeight == level.sector.FloorHeight;
}
if (add)
{
neighbours.Add(side.Other.Sector);
//(de)select regular visual floor?
if (select != vs.Floor.Selected)
{
vs.Floor.SelectNeighbours(select, withSameTexture, withSameHeight);
}
}
}
// (De)select adjacent extra floors
foreach(VisualFloor ef in vs.ExtraFloors)
{
if (select == ef.Selected || ef.extrafloor.VavoomType != regularorvavoom) continue;
add = (withSameTexture && level.sector.FloorTexture == ef.level.sector.FloorTexture);
if (withSameHeight)
{
add = ((withSameTexture && add) || !withSameTexture) && level.sector.FloorHeight == ef.level.sector.FloorHeight;
}
if (add)
{
ef.SelectNeighbours(select, withSameTexture, withSameHeight);
}
}
// (De)select adjacent vavoom type extra ceilings
foreach(VisualCeiling ec in vs.ExtraCeilings)
{
if(select == ec.Selected || ec.ExtraFloor.VavoomType == regularorvavoom) continue;
add = (withSameTexture && level.sector.FloorTexture == ec.Level.sector.CeilTexture);
if (withSameHeight)
{
add = ((withSameTexture && add) || !withSameTexture) && level.sector.FloorHeight == ec.Level.sector.CeilHeight;
}
if (add)
{
ec.SelectNeighbours(select, withSameTexture, withSameHeight);
}
}
}
}
}
//mxd
public void AlignTexture(bool alignx, bool aligny)
{
if(!General.Map.UDMF) return;
//is is a surface with line slope?
float slopeAngle = level.plane.Normal.GetAngleZ() - Angle2D.PIHALF;
if(slopeAngle == 0) //it's a horizontal plane
{
AlignTextureToClosestLine(alignx, aligny);
}
else //it can be a surface with line slope
{
Linedef slopeSource = null;
bool isFront = false;
foreach(Sidedef side in Sector.Sector.Sidedefs)
{
if(side.Line.Action == 181)
{
if(side.Line.Args[0] == 1 && side.Line.Front != null && side.Line.Front == side)
{
slopeSource = side.Line;
isFront = true;
break;
}
else if(side.Line.Args[0] == 2 && side.Line.Back != null && side.Line.Back == side)
{
slopeSource = side.Line;
break;
}
}
}
if(slopeSource != null && slopeSource.Front != null && slopeSource.Front.Sector != null && slopeSource.Back != null && slopeSource.Back.Sector != null)
AlignTextureToSlopeLine(slopeSource, slopeAngle, isFront, alignx, aligny);
else
AlignTextureToClosestLine(alignx, aligny);
}
}
#endregion
}
}