2008-12-09 15:45:24 +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;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Text;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using System.IO;
|
|
|
|
using System.Reflection;
|
|
|
|
using CodeImp.DoomBuilder.Windows;
|
|
|
|
using CodeImp.DoomBuilder.IO;
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
|
|
|
using CodeImp.DoomBuilder.Editing;
|
|
|
|
using CodeImp.DoomBuilder.VisualModes;
|
2008-12-10 16:00:34 +00:00
|
|
|
using CodeImp.DoomBuilder.Config;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
2008-12-09 15:45:24 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.BuilderModes
|
|
|
|
{
|
2008-12-15 18:32:36 +00:00
|
|
|
internal class BaseVisualThing : VisualThing, IVisualEventReceiver
|
2008-12-09 15:45:24 +00:00
|
|
|
{
|
|
|
|
#region ================== Constants
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
#endregion
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
#region ================== Variables
|
2008-12-17 10:58:57 +00:00
|
|
|
|
|
|
|
protected BaseVisualMode mode;
|
2008-12-10 16:00:34 +00:00
|
|
|
|
|
|
|
private ThingTypeInfo info;
|
|
|
|
private bool isloaded;
|
|
|
|
private ImageData sprite;
|
2008-12-14 23:36:24 +00:00
|
|
|
private float cageradius2;
|
|
|
|
private Vector2D pos2d;
|
2008-12-15 15:54:22 +00:00
|
|
|
private Vector3D boxp1;
|
|
|
|
private Vector3D boxp2;
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-24 18:27:13 +00:00
|
|
|
// Undo/redo
|
|
|
|
private int undoticket;
|
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
#endregion
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
#region ================== Properties
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
#endregion
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-10 22:58:58 +00:00
|
|
|
#region ================== Constructor / Setup
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
// Constructor
|
2008-12-17 10:58:57 +00:00
|
|
|
public BaseVisualThing(BaseVisualMode mode, Thing t) : base(t)
|
2008-12-09 15:45:24 +00:00
|
|
|
{
|
2008-12-17 10:58:57 +00:00
|
|
|
this.mode = mode;
|
|
|
|
|
2008-12-15 19:05:21 +00:00
|
|
|
Rebuild();
|
2008-12-09 15:45:24 +00:00
|
|
|
|
2008-12-10 16:00:34 +00:00
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
2008-12-09 15:45:24 +00:00
|
|
|
}
|
2008-12-10 16:00:34 +00:00
|
|
|
|
|
|
|
// This builds the thing geometry. Returns false when nothing was created.
|
|
|
|
public virtual bool Setup()
|
|
|
|
{
|
2008-12-15 15:54:22 +00:00
|
|
|
PixelColor sectorcolor = new PixelColor(255, 255, 255, 255);
|
|
|
|
|
2008-12-10 16:00:34 +00:00
|
|
|
if(sprite != null)
|
|
|
|
{
|
|
|
|
// Find the sector in which the thing resides
|
|
|
|
Thing.DetermineSector();
|
2008-12-10 22:58:58 +00:00
|
|
|
if(Thing.Sector != null)
|
|
|
|
{
|
|
|
|
// Use sector brightness for color shading
|
2008-12-15 15:54:22 +00:00
|
|
|
sectorcolor = new PixelColor(255, unchecked((byte)Thing.Sector.Brightness),
|
2008-12-10 22:58:58 +00:00
|
|
|
unchecked((byte)Thing.Sector.Brightness),
|
|
|
|
unchecked((byte)Thing.Sector.Brightness));
|
|
|
|
}
|
2008-12-10 16:00:34 +00:00
|
|
|
|
|
|
|
// Check if the texture is loaded
|
2008-12-24 18:27:13 +00:00
|
|
|
sprite.LoadImage();
|
2008-12-10 16:00:34 +00:00
|
|
|
isloaded = sprite.IsImageLoaded;
|
|
|
|
if(isloaded)
|
|
|
|
{
|
|
|
|
base.Texture = sprite;
|
|
|
|
|
|
|
|
// Determine sprite size
|
|
|
|
float radius = sprite.ScaledWidth * 0.5f;
|
|
|
|
float height = sprite.ScaledHeight;
|
|
|
|
|
|
|
|
// Make vertices
|
|
|
|
WorldVertex[] verts = new WorldVertex[6];
|
2008-12-15 15:54:22 +00:00
|
|
|
verts[0] = new WorldVertex(-radius, 0.0f, 0.0f, sectorcolor.ToInt(), 0.0f, 1.0f);
|
|
|
|
verts[1] = new WorldVertex(-radius, 0.0f, height, sectorcolor.ToInt(), 0.0f, 0.0f);
|
|
|
|
verts[2] = new WorldVertex(+radius, 0.0f, height, sectorcolor.ToInt(), 1.0f, 0.0f);
|
2008-12-10 16:00:34 +00:00
|
|
|
verts[3] = verts[0];
|
|
|
|
verts[4] = verts[2];
|
2008-12-15 15:54:22 +00:00
|
|
|
verts[5] = new WorldVertex(+radius, 0.0f, 0.0f, sectorcolor.ToInt(), 1.0f, 1.0f);
|
2008-12-10 16:00:34 +00:00
|
|
|
SetVertices(verts);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
base.Texture = General.Map.Data.Hourglass3D;
|
|
|
|
|
|
|
|
// Determine sprite size
|
2008-12-20 10:30:18 +00:00
|
|
|
float radius = Math.Min(info.Width, info.Height / 2f);
|
|
|
|
float height = Math.Min(info.Width * 2f, info.Height);
|
2008-12-10 16:00:34 +00:00
|
|
|
|
|
|
|
// Make vertices
|
|
|
|
WorldVertex[] verts = new WorldVertex[6];
|
2008-12-15 15:54:22 +00:00
|
|
|
verts[0] = new WorldVertex(-radius, 0.0f, 0.0f, sectorcolor.ToInt(), 0.0f, 1.0f);
|
|
|
|
verts[1] = new WorldVertex(-radius, 0.0f, height, sectorcolor.ToInt(), 0.0f, 0.0f);
|
|
|
|
verts[2] = new WorldVertex(+radius, 0.0f, height, sectorcolor.ToInt(), 1.0f, 0.0f);
|
2008-12-10 16:00:34 +00:00
|
|
|
verts[3] = verts[0];
|
|
|
|
verts[4] = verts[2];
|
2008-12-15 15:54:22 +00:00
|
|
|
verts[5] = new WorldVertex(+radius, 0.0f, 0.0f, sectorcolor.ToInt(), 1.0f, 1.0f);
|
2008-12-10 16:00:34 +00:00
|
|
|
SetVertices(verts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 10:58:57 +00:00
|
|
|
// Determine position
|
2008-12-10 16:00:34 +00:00
|
|
|
Vector3D pos = Thing.Position;
|
2008-12-17 10:58:57 +00:00
|
|
|
if(info.Hangs)
|
|
|
|
{
|
|
|
|
// Hang from ceiling
|
|
|
|
if(Thing.Sector != null) pos.z = Thing.Sector.CeilHeight - info.Height;
|
|
|
|
if(Thing.Position.z > 0) pos.z -= Thing.Position.z;
|
|
|
|
|
|
|
|
// Check if below floor
|
|
|
|
if((Thing.Sector != null) && (pos.z < Thing.Sector.FloorHeight))
|
|
|
|
{
|
|
|
|
// Put thing on the floor
|
|
|
|
pos.z = Thing.Sector.FloorHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Stand on floor
|
|
|
|
if(Thing.Sector != null) pos.z = Thing.Sector.FloorHeight;
|
|
|
|
if(Thing.Position.z > 0) pos.z += Thing.Position.z;
|
|
|
|
|
|
|
|
// Check if above ceiling
|
|
|
|
if((Thing.Sector != null) && ((pos.z + info.Height) > Thing.Sector.CeilHeight))
|
|
|
|
{
|
|
|
|
// Put thing against ceiling
|
|
|
|
pos.z = Thing.Sector.CeilHeight - info.Height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply settings
|
2008-12-10 16:00:34 +00:00
|
|
|
SetPosition(pos);
|
2008-12-14 23:24:40 +00:00
|
|
|
SetCageSize(info.Width, info.Height);
|
|
|
|
SetCageColor(Thing.Color);
|
2008-12-15 15:54:22 +00:00
|
|
|
|
2008-12-14 23:36:24 +00:00
|
|
|
// Keep info for object picking
|
|
|
|
cageradius2 = info.Width * Angle2D.SQRT2;
|
|
|
|
cageradius2 = cageradius2 * cageradius2;
|
2008-12-15 15:54:22 +00:00
|
|
|
pos2d = pos;
|
|
|
|
boxp1 = new Vector3D(pos.x - info.Width, pos.y - info.Width, pos.z);
|
|
|
|
boxp2 = new Vector3D(pos.x + info.Width, pos.y + info.Width, pos.z + info.Height);
|
2008-12-10 16:00:34 +00:00
|
|
|
|
|
|
|
// Done
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disposing
|
|
|
|
public override void Dispose()
|
|
|
|
{
|
|
|
|
if(!IsDisposed)
|
|
|
|
{
|
2008-12-10 22:58:58 +00:00
|
|
|
if(sprite != null)
|
|
|
|
{
|
|
|
|
sprite.RemoveReference();
|
|
|
|
sprite = null;
|
|
|
|
}
|
2008-12-10 16:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
base.Dispose();
|
|
|
|
}
|
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
#endregion
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
#region ================== Methods
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-15 19:05:21 +00:00
|
|
|
// This forces to rebuild the whole thing
|
|
|
|
public void Rebuild()
|
|
|
|
{
|
|
|
|
// Find thing information
|
|
|
|
info = General.Map.Config.GetThingInfo(Thing.Type);
|
|
|
|
|
|
|
|
// Find sprite texture
|
|
|
|
if(info.Sprite.Length > 0)
|
|
|
|
{
|
|
|
|
sprite = General.Map.Data.GetSpriteImage(info.Sprite);
|
|
|
|
if(sprite != null) sprite.AddReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup visual thing
|
|
|
|
Setup();
|
|
|
|
}
|
|
|
|
|
2008-12-10 16:00:34 +00:00
|
|
|
// This updates the thing when needed
|
|
|
|
public override void Update()
|
|
|
|
{
|
|
|
|
if(!isloaded)
|
|
|
|
{
|
|
|
|
// Rebuild sprite geometry when sprite is loaded
|
|
|
|
if(sprite.IsImageLoaded)
|
|
|
|
{
|
|
|
|
Setup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let the base update
|
|
|
|
base.Update();
|
|
|
|
}
|
2008-12-14 23:36:24 +00:00
|
|
|
|
|
|
|
// This performs a fast test in object picking
|
|
|
|
public override bool PickFastReject(Vector3D from, Vector3D to, Vector3D dir)
|
|
|
|
{
|
|
|
|
float distance2 = Line2D.GetDistanceToLineSq(from, to, pos2d, 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)
|
|
|
|
{
|
2008-12-15 15:54:22 +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?PHPSESSID=eee672d82a12b8b8f1871268f652be82&topic=9941.0
|
|
|
|
|
|
|
|
// Check X slab
|
|
|
|
if(delta.x == 0.0f)
|
|
|
|
{
|
|
|
|
if(from.x > boxp2.x || from.x < boxp1.x)
|
|
|
|
{
|
|
|
|
// Ray is parallel to the planes & outside slab
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float tmp = 1.0f / delta.x;
|
|
|
|
float t1 = (boxp1.x - from.x) * tmp;
|
|
|
|
float t2 = (boxp2.x - from.x) * tmp;
|
2008-12-15 19:05:21 +00:00
|
|
|
if(t1 > t2) General.Swap(ref t1, ref t2);
|
2008-12-15 15:54:22 +00:00
|
|
|
if(t1 > tnear) tnear = t1;
|
|
|
|
if(t2 < tfar) tfar = t2;
|
|
|
|
if(tnear > tfar || tfar < 0.0f)
|
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
// Ray is parallel to the planes & outside slab
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float tmp = 1.0f / delta.y;
|
|
|
|
float t1 = (boxp1.y - from.y) * tmp;
|
|
|
|
float t2 = (boxp2.y - from.y) * tmp;
|
2008-12-15 19:05:21 +00:00
|
|
|
if(t1 > t2) General.Swap(ref t1, ref t2);
|
2008-12-15 15:54:22 +00:00
|
|
|
if(t1 > tnear) tnear = t1;
|
|
|
|
if(t2 < tfar) tfar = t2;
|
|
|
|
if(tnear > tfar || tfar < 0.0f)
|
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
// Ray is parallel to the planes & outside slab
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float tmp = 1.0f / delta.z;
|
|
|
|
float t1 = (boxp1.z - from.z) * tmp;
|
|
|
|
float t2 = (boxp2.z - from.z) * tmp;
|
2008-12-15 19:05:21 +00:00
|
|
|
if(t1 > t2) General.Swap(ref t1, ref t2);
|
2008-12-15 15:54:22 +00:00
|
|
|
if(t1 > tnear) tnear = t1;
|
|
|
|
if(t2 < tfar) tfar = t2;
|
|
|
|
if(tnear > tfar || tfar < 0.0f)
|
|
|
|
{
|
|
|
|
// Ray missed box or box is behind ray
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set interpolation point
|
|
|
|
u_ray = (tnear > 0.0f) ? tnear : tfar;
|
|
|
|
return true;
|
2008-12-14 23:36:24 +00:00
|
|
|
}
|
2008-12-10 16:00:34 +00:00
|
|
|
|
2008-12-09 15:45:24 +00:00
|
|
|
#endregion
|
2008-12-15 18:32:36 +00:00
|
|
|
|
|
|
|
#region ================== Events
|
|
|
|
|
|
|
|
// Unused
|
|
|
|
public virtual void OnSelectBegin() { }
|
|
|
|
public virtual void OnSelectEnd() { }
|
|
|
|
public virtual void OnEditBegin() { }
|
2008-12-17 15:30:50 +00:00
|
|
|
public virtual void OnMouseMove(MouseEventArgs e) { }
|
2008-12-24 14:21:54 +00:00
|
|
|
public virtual void OnChangeTargetBrightness(int amount) { }
|
2008-12-24 18:27:13 +00:00
|
|
|
public virtual void OnChangeTextureOffset(int horizontal, int vertical) { }
|
2008-12-15 19:05:21 +00:00
|
|
|
|
|
|
|
// Edit button released
|
|
|
|
public virtual void OnEditEnd()
|
|
|
|
{
|
|
|
|
List<Thing> things = new List<Thing>();
|
|
|
|
things.Add(this.Thing);
|
|
|
|
DialogResult result = General.Interface.ShowEditThings(things);
|
|
|
|
if(result == DialogResult.OK) this.Rebuild();
|
|
|
|
}
|
2008-12-15 18:32:36 +00:00
|
|
|
|
2008-12-24 18:27:13 +00:00
|
|
|
// Raise/lower thing
|
|
|
|
public virtual void OnChangeTargetHeight(int amount)
|
|
|
|
{
|
|
|
|
if((General.Map.UndoRedo.NextUndo == null) || (General.Map.UndoRedo.NextUndo.TicketID != undoticket))
|
|
|
|
undoticket = General.Map.UndoRedo.CreateUndo("Change thing height");
|
|
|
|
|
|
|
|
Thing.Move(Thing.Position + new Vector3D(0.0f, 0.0f, (float)amount));
|
|
|
|
|
|
|
|
this.Setup();
|
|
|
|
}
|
|
|
|
|
2008-12-15 18:32:36 +00:00
|
|
|
#endregion
|
2008-12-09 15:45:24 +00:00
|
|
|
}
|
|
|
|
}
|