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

405 lines
11 KiB
C#
Raw Normal View History

#region ================== Namespaces
using System.Collections.Generic;
2013-03-18 13:52:27 +00:00
using System.Windows.Forms;
using CodeImp.DoomBuilder.Geometry;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.VisualModes;
#endregion
2013-03-18 13:52:27 +00:00
namespace CodeImp.DoomBuilder.BuilderModes
{
internal class BaseVisualVertex : VisualVertex, IVisualEventReceiver
{
#region ================== Variables
private readonly BaseVisualMode mode;
private readonly float cageradius2;
2013-03-18 13:52:27 +00:00
private Vector3D boxp1;
private Vector3D boxp2;
// Undo/redo
private int undoticket;
//updating
private static Dictionary<BaseVisualSector, bool> updateList;
2013-03-18 13:52:27 +00:00
#endregion
// Constructor
public BaseVisualVertex(BaseVisualMode mode, Vertex v, bool ceilingVertex)
: base(v, ceilingVertex)
{
2013-03-18 13:52:27 +00:00
this.mode = mode;
cageradius2 = DEFAULT_SIZE * General.Settings.GZVertexScale3D * Angle2D.SQRT2;
2013-03-18 13:52:27 +00:00
cageradius2 = cageradius2 * cageradius2;
// Synchronize selection?
if(mode.UseSelectionFromClassicMode && v.Selected
&& (General.Map.ViewMode == ViewMode.Normal ||
(!ceilingVertex && General.Map.ViewMode == ViewMode.FloorTextures)
|| (ceilingVertex && General.Map.ViewMode == ViewMode.CeilingTextures)))
{
this.selected = true;
mode.AddSelectedObject(this);
}
2013-03-18 13:52:27 +00:00
changed = true;
Update();
}
//this updates the handle itself
public override void Update()
{
2013-03-18 13:52:27 +00:00
if(!changed) return;
float z = ceilingVertex ? vertex.ZCeiling : vertex.ZFloor;
2013-03-18 13:52:27 +00:00
if(!float.IsNaN(z))
{
haveOffset = true;
}
else
{
z = GetSectorHeight();
haveOffset = false;
2013-03-18 13:52:27 +00:00
}
Vector3D pos = new Vector3D(vertex.Position.x, vertex.Position.y, z);
SetPosition(pos);
float radius = DEFAULT_SIZE * General.Settings.GZVertexScale3D;
boxp1 = new Vector3D(pos.x - radius, pos.y - radius, (ceilingVertex ? pos.z - radius : pos.z));
boxp2 = new Vector3D(pos.x + radius, pos.y + radius, (ceilingVertex ? pos.z : pos.z + radius));
2013-03-18 13:52:27 +00:00
changed = false;
}
private void UpdateGeometry(Vertex v)
{
2013-03-18 13:52:27 +00:00
VertexData vd = mode.GetVertexData(v);
foreach(KeyValuePair<Sector, bool> s in vd.UpdateAlso)
{
if(mode.VisualSectorExists(s.Key))
{
2013-03-18 13:52:27 +00:00
BaseVisualSector vs = (BaseVisualSector)mode.GetVisualSector(s.Key);
vs.UpdateSectorGeometry(s.Value);
}
}
}
//get the most appropriate height from sectors
private int GetSectorHeight()
{
int height;
2013-03-18 13:52:27 +00:00
VertexData vd = mode.GetVertexData(vertex);
Sector[] sectors = new Sector[vd.UpdateAlso.Keys.Count];
vd.UpdateAlso.Keys.CopyTo(sectors, 0);
if(ceilingVertex)
{
2013-03-18 13:52:27 +00:00
height = sectors[0].CeilHeight;
for(int i = 1; i < sectors.Length; i++)
{
if(sectors[i].CeilHeight < height)
2013-03-18 13:52:27 +00:00
height = sectors[i].CeilHeight;
}
}
else
{
2013-03-18 13:52:27 +00:00
height = sectors[0].FloorHeight;
for(int i = 1; i < sectors.Length; i++)
{
if(sectors[i].FloorHeight > height)
2013-03-18 13:52:27 +00:00
height = sectors[i].FloorHeight;
}
}
return height;
}
//mxd
public bool IsSelected()
{
return selected;
}
2013-03-18 13:52:27 +00:00
#region ================== Object picking
// This performs a fast test in object picking
public override bool PickFastReject(Vector3D from, Vector3D to, Vector3D dir)
{
2013-03-18 13:52:27 +00:00
float distance2 = Line2D.GetDistanceToLineSq(from, to, vertex.Position, false);
return (distance2 <= cageradius2);
}
// This performs an accurate test for object picking
public override bool PickAccurate(Vector3D from, Vector3D to, Vector3D dir, ref float u_ray)
{
2013-03-18 13:52:27 +00:00
Vector3D delta = to - from;
float tfar = float.MaxValue;
float tnear = float.MinValue;
// Ray-Box intersection code
// See http://www.masm32.com/board/index.php?topic=9941.0
// Check X slab
if(delta.x == 0.0f)
{
if(from.x > boxp2.x || from.x < boxp1.x)
{
2013-03-18 13:52:27 +00:00
// Ray is parallel to the planes & outside slab
return false;
}
}
else
{
2013-03-18 13:52:27 +00:00
float tmp = 1.0f / delta.x;
float t1 = (boxp1.x - from.x) * tmp;
float t2 = (boxp2.x - from.x) * tmp;
if(t1 > t2)
General.Swap(ref t1, ref t2);
if(t1 > tnear)
tnear = t1;
if(t2 < tfar)
tfar = t2;
if(tnear > tfar || tfar < 0.0f)
{
2013-03-18 13:52:27 +00:00
// Ray missed box or box is behind ray
return false;
}
}
// Check Y slab
if(delta.y == 0.0f)
{
if(from.y > boxp2.y || from.y < boxp1.y)
{
2013-03-18 13:52:27 +00:00
// Ray is parallel to the planes & outside slab
return false;
}
}
else
{
2013-03-18 13:52:27 +00:00
float tmp = 1.0f / delta.y;
float t1 = (boxp1.y - from.y) * tmp;
float t2 = (boxp2.y - from.y) * tmp;
if(t1 > t2)
General.Swap(ref t1, ref t2);
if(t1 > tnear)
tnear = t1;
if(t2 < tfar)
tfar = t2;
if(tnear > tfar || tfar < 0.0f)
{
2013-03-18 13:52:27 +00:00
// Ray missed box or box is behind ray
return false;
}
}
// Check Z slab
if(delta.z == 0.0f)
{
if(from.z > boxp2.z || from.z < boxp1.z)
{
2013-03-18 13:52:27 +00:00
// Ray is parallel to the planes & outside slab
return false;
}
}
else
{
2013-03-18 13:52:27 +00:00
float tmp = 1.0f / delta.z;
float t1 = (boxp1.z - from.z) * tmp;
float t2 = (boxp2.z - from.z) * tmp;
if(t1 > t2)
General.Swap(ref t1, ref t2);
if(t1 > tnear)
tnear = t1;
if(t2 < tfar)
tfar = t2;
if(tnear > tfar || tfar < 0.0f)
{
2013-03-18 13:52:27 +00:00
// Ray missed box or box is behind ray
return false;
}
}
// Set interpolation point
u_ray = (tnear > 0.0f) ? tnear : tfar;
return true;
}
#endregion
#region ================== Unused events
// Unused
public void OnSelectBegin() { }
public void OnEditBegin() { }
public void OnMouseMove(MouseEventArgs e) { }
public void OnChangeTargetBrightness(bool up) { }
public void OnChangeTextureOffset(int horizontal, int vertical, bool doSurfaceAngleCorrection) { }
public void OnChangeScale(int incrementX, int incrementY) { }
public void OnSelectTexture() { }
public void OnCopyTexture() { }
public void OnPasteTexture() { }
public void OnCopyTextureOffsets() { }
public void OnPasteTextureOffsets() { }
public void OnTextureAlign(bool alignx, bool aligny) { }
public void OnTextureFit(FitTextureOptions options) { } //mxd
public void OnToggleUpperUnpegged() { }
public void OnToggleLowerUnpegged() { }
public void OnResetTextureOffset() { }
public void OnResetLocalTextureOffset() { } //mxd
public void OnProcess(long deltatime) { }
public void OnTextureFloodfill() { }
public void OnInsert() { }
public void ApplyTexture(string texture) { }
public void ApplyUpperUnpegged(bool set) { }
public void ApplyLowerUnpegged(bool set) { }
public string GetTextureName() { return ""; }
public void SelectNeighbours(bool select, bool withSameTexture, bool withSameHeight) { } //mxd
public virtual void OnPaintSelectBegin() { } // biwa
public virtual void OnPaintSelectEnd() { } // biwa
2013-03-18 13:52:27 +00:00
#endregion
#region ================== Events
// Select or deselect
public void OnSelectEnd()
{
if(this.selected)
{
2013-03-18 13:52:27 +00:00
this.selected = false;
mode.RemoveSelectedObject(this);
}
else
{
2013-03-18 13:52:27 +00:00
this.selected = true;
mode.AddSelectedObject(this);
}
}
// Copy properties
public void OnCopyProperties()
{
2013-03-18 13:52:27 +00:00
BuilderPlug.Me.CopiedVertexProps = new VertexProperties(vertex);
mode.SetActionResult("Copied vertex properties.");
}
// Paste properties
Removed "Paste Properties Options" action. Added "Paste Properties Special" actions in "Classic" and "Visual" categories. They work the same way as "Paste Special" action. Added: "Copy Properties", "Paste Properties" and "Paste Properties Special" options are now shown in the Edit menu if current classic mode supports them. Changed, Paste Properties Special window: only options relevant to current map format are now displayed. Changed, Paste Properties Special window, UDMF: all UI-managed options are now available. Fixed: MAPINFO parser was unable to process "include" directives. Fixed, General interface: selection info was reset to "Nothing selected" after few seconds regardless of current selection. Fixed, Visual mode: thing bounding boxes were not updated when changing things positions using Randomize mode. Fixed, Visual mode: event lines were displayed at incorrect height when entering Visual mode for the first time. Fixed, Texture Browser window: when MixTexturesFlats Game Configuration option is disabled, textures/flats are no longer shown in the Used group when flats/textures with the same names are used in the map. Fixed(?): probably fixed an exception some users reported when trying to initialize a Classic mode after switching from Visual mode with "Sync cameras" option enabled. Changed, Game configurations, Thing Categories: a block must have at least one thing category property to be recognized as a thing category. Changed, Visplane Explorer: the plugin now outputs more info when it fails to initialize vpo.dll. Cosmetic, Thing Edit window, Doom/Hexen map format: adjusted UI layout so thing flags control no longer displays scrollbars in Hexen map format. Internal: merged methods from UDMFTools into UniFields, removed UDMFTools. Updated Inno Setup script (added VC++ 2008 SP1 distributive). Updated ZDoom_DECORATE.cfg (A_CheckBlock). Updated documentation (added "System Requirements" page).
2015-10-09 12:38:12 +00:00
public void OnPasteProperties(bool usecopysettings)
{
if(BuilderPlug.Me.CopiedVertexProps != null)
{
2013-03-18 13:52:27 +00:00
mode.CreateUndo("Paste vertex properties");
mode.SetActionResult("Pasted vertex properties.");
BuilderPlug.Me.CopiedVertexProps.Apply(new List<Vertex> { vertex }, usecopysettings);
2013-03-18 13:52:27 +00:00
//update affected sectors
UpdateGeometry(vertex);
2013-03-18 13:52:27 +00:00
changed = true;
mode.ShowTargetInfo();
}
}
//Delete key pressed - remove zoffset field
public void OnDelete()
{
mode.CreateUndo("Clear vertex height offset");
mode.SetActionResult("Cleared vertex height offset.");
if(ceilingVertex)
{
if(float.IsNaN(vertex.ZCeiling)) return;
vertex.ZCeiling = float.NaN;
//update affected sectors
UpdateGeometry(vertex);
changed = true;
mode.ShowTargetInfo();
}
else
{
if(float.IsNaN(vertex.ZFloor)) return;
vertex.ZFloor = float.NaN;
//update affected sectors
UpdateGeometry(vertex);
changed = true;
mode.ShowTargetInfo();
}
}
2013-03-18 13:52:27 +00:00
// Edit button released
public void OnEditEnd()
{
if(General.Interface.IsActiveWindow)
{
2013-03-18 13:52:27 +00:00
List<Vertex> verts = mode.GetSelectedVertices();
updateList = new Dictionary<BaseVisualSector, bool>();
foreach(Vertex v in verts)
{
VertexData vd = mode.GetVertexData(v);
foreach(KeyValuePair<Sector, bool> s in vd.UpdateAlso)
{
if(mode.VisualSectorExists(s.Key))
{
BaseVisualSector vs = (BaseVisualSector)mode.GetVisualSector(s.Key);
if(!updateList.ContainsKey(vs)) updateList.Add(vs, s.Value);
}
}
}
General.Interface.OnEditFormValuesChanged += Interface_OnEditFormValuesChanged;
mode.StartRealtimeInterfaceUpdate(SelectionType.Vertices);
General.Interface.ShowEditVertices(verts, false);
mode.StopRealtimeInterfaceUpdate(SelectionType.Vertices);
General.Interface.OnEditFormValuesChanged -= Interface_OnEditFormValuesChanged;
updateList.Clear();
updateList = null;
2013-03-18 13:52:27 +00:00
}
}
//mxd
private void Interface_OnEditFormValuesChanged(object sender, System.EventArgs e)
{
foreach(KeyValuePair<BaseVisualSector, bool> group in updateList)
group.Key.UpdateSectorGeometry(group.Value);
changed = true;
Update();
}
2013-03-18 13:52:27 +00:00
// Raise/lower thing
public void OnChangeTargetHeight(int amount)
{
2013-03-18 13:52:27 +00:00
if((General.Map.UndoRedo.NextUndo == null) || (General.Map.UndoRedo.NextUndo.TicketID != undoticket))
undoticket = mode.CreateUndo("Change vertex height");
if(ceilingVertex)
{
vertex.ZCeiling = (float.IsNaN(vertex.ZCeiling) ? GetSectorHeight() + amount : vertex.ZCeiling + amount);
mode.SetActionResult("Changed vertex height to " + vertex.ZCeiling + ".");
}
else
{
vertex.ZFloor = (float.IsNaN(vertex.ZFloor) ? GetSectorHeight() + amount : vertex.ZFloor + amount);
mode.SetActionResult("Changed vertex height to " + vertex.ZFloor + ".");
2013-03-18 13:52:27 +00:00
}
// Update what must be updated
UpdateGeometry(vertex);
changed = true;
2013-03-18 13:52:27 +00:00
}
#endregion
}
}