UltimateZoneBuilder/Source/BuilderModes/VisualModes/BaseVisualMode.cs

313 lines
7.5 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;
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.Actions;
2008-11-19 16:18:36 +00:00
using CodeImp.DoomBuilder.VisualModes;
#endregion
2008-11-27 13:42:18 +00:00
namespace CodeImp.DoomBuilder.BuilderModes
{
2008-11-27 13:42:18 +00:00
[EditMode(DisplayName = "Visual Mode",
SwitchAction = "visualmode", // Action name used to switch to this mode
ButtonImage = "VisualMode.png", // Image resource name for the button
2008-11-27 19:25:13 +00:00
ButtonOrder = 0, // Position of the button (lower is more to the left)
UseByDefault = true)]
public class BaseVisualMode : VisualMode
{
#region ================== Constants
2008-12-02 15:47:06 +00:00
// Object picking interval
private const double PICK_INTERVAL = 100.0d;
private const float PICK_RANGE = 0.98f;
2008-12-02 15:47:06 +00:00
#endregion
2008-12-02 15:47:06 +00:00
#region ================== Variables
2008-12-02 15:47:06 +00:00
// Object picking
private VisualPickResult target;
private double lastpicktime;
2008-12-05 06:52:20 +00:00
private bool locktarget;
2008-12-02 15:47:06 +00:00
#endregion
2008-12-02 15:47:06 +00:00
#region ================== Properties
#endregion
2008-12-02 15:47:06 +00:00
#region ================== Constructor / Disposer
// Constructor
public BaseVisualMode()
{
// Initialize
// We have no destructor
GC.SuppressFinalize(this);
}
// Disposer
public override void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
// Done
base.Dispose();
}
}
#endregion
2008-12-02 15:47:06 +00:00
#region ================== Methods
2008-12-02 15:47:06 +00:00
2008-11-25 21:54:50 +00:00
// This creates a visual sector
protected override VisualSector CreateVisualSector(Sector s)
{
2008-12-10 16:00:34 +00:00
BaseVisualSector vs = new BaseVisualSector(s);
return vs;
}
2008-12-09 15:45:24 +00:00
// This creates a visual thing
protected override VisualThing CreateVisualThing(Thing t)
{
2008-12-10 16:00:34 +00:00
BaseVisualThing vt = new BaseVisualThing(t);
if(vt.Setup()) return vt; else return null;
2008-12-09 15:45:24 +00:00
}
2008-12-05 06:52:20 +00:00
// This locks the target so that it isn't changed until unlocked
public void LockTarget()
{
locktarget = true;
}
// This unlocks the target so that is changes to the aimed geometry again
public void UnlockTarget()
{
locktarget = false;
}
2008-12-02 15:47:06 +00:00
// This picks a new target
private void PickTarget()
{
2008-12-03 07:04:57 +00:00
// Find the object we are aiming at
2008-12-02 15:47:06 +00:00
Vector3D start = CameraPosition;
Vector3D delta = CameraTarget - CameraPosition;
delta = delta.GetFixedLength(General.Settings.ViewDistance * PICK_RANGE);
2008-12-02 15:47:06 +00:00
VisualPickResult newtarget = PickObject(start, start + delta);
// Object changed?
2008-12-09 15:45:24 +00:00
if(newtarget.picked != target.picked)
2008-12-02 15:47:06 +00:00
{
// Any result?
2008-12-09 15:45:24 +00:00
if(newtarget.picked != null)
2008-12-02 15:47:06 +00:00
{
2008-12-09 15:45:24 +00:00
VisualGeometry prevgeo = null;
VisualThing prevthing = null;
2008-12-09 15:45:24 +00:00
if((target.picked != null) && (target.picked is VisualGeometry))
prevgeo = (target.picked as VisualGeometry);
else if(target.picked is VisualThing)
prevthing = (target.picked as VisualThing);
2008-12-09 15:45:24 +00:00
// Geometry picked?
if(newtarget.picked is VisualGeometry)
2008-12-03 07:04:57 +00:00
{
2008-12-09 15:45:24 +00:00
VisualGeometry pickedgeo = (newtarget.picked as VisualGeometry);
if(pickedgeo.Sidedef != null)
{
if((prevgeo == null) || (prevgeo.Sidedef == null)) General.Interface.HideInfo();
2008-12-09 15:45:24 +00:00
General.Interface.ShowLinedefInfo(pickedgeo.Sidedef.Line);
}
else if(pickedgeo.Sidedef == null)
{
if((prevgeo == null) || (prevgeo.Sidedef != null)) General.Interface.HideInfo();
General.Interface.ShowSectorInfo(pickedgeo.Sector.Sector);
}
else
{
General.Interface.HideInfo();
}
}
// Thing picked?
if(newtarget.picked is VisualThing)
{
VisualThing pickedthing = (newtarget.picked as VisualThing);
if(prevthing == null) General.Interface.HideInfo();
General.Interface.ShowThingInfo(pickedthing.Thing);
2008-12-03 07:04:57 +00:00
}
}
else
{
General.Interface.HideInfo();
2008-12-02 15:47:06 +00:00
}
}
// Apply new target
target = newtarget;
}
2008-12-10 16:00:34 +00:00
2008-12-06 00:28:49 +00:00
// This changes the target's height
private void ChangeTargetHeight(int amount)
{
2008-12-09 15:45:24 +00:00
if(target.picked is BaseVisualGeometrySector)
2008-12-06 00:28:49 +00:00
{
2008-12-09 15:45:24 +00:00
BaseVisualGeometrySector vgs = (target.picked as BaseVisualGeometrySector);
2008-12-06 00:28:49 +00:00
vgs.ChangeHeight(amount);
// Rebuild sector
(vgs.Sector as BaseVisualSector).Rebuild();
// Also rebuild surrounding sectors, because outside sidedefs may need to be adjusted
foreach(Sidedef sd in vgs.Sector.Sector.Sidedefs)
{
if((sd.Other != null) && VisualSectorExists(sd.Other.Sector))
{
BaseVisualSector bvs = (BaseVisualSector)GetVisualSector(sd.Other.Sector);
bvs.Rebuild();
}
}
}
}
2008-12-02 15:47:06 +00:00
#endregion
#region ================== Events
// Processing
public override void OnProcess(double deltatime)
{
// Do processing
base.OnProcess(deltatime);
// Time to pick a new target?
if(General.Clock.CurrentTime > (lastpicktime + PICK_INTERVAL))
{
2008-12-05 06:52:20 +00:00
if(!locktarget) PickTarget();
2008-12-02 15:47:06 +00:00
lastpicktime = General.Clock.CurrentTime;
}
}
// This draws a frame
public override void OnRedrawDisplay()
{
// Start drawing
if(renderer.Start())
{
// Use fog!
renderer.SetFogMode(true);
// Begin with geometry
renderer.StartGeometry();
2008-12-02 15:47:06 +00:00
2008-12-01 06:51:00 +00:00
// This adds all visible geometry for rendering
AddGeometry();
2008-12-02 15:47:06 +00:00
// Done rendering geometry
renderer.FinishGeometry();
2008-12-02 15:47:06 +00:00
2008-12-03 07:04:57 +00:00
// Render crosshair
renderer.RenderCrosshair();
// Present!
renderer.Finish();
}
}
2008-12-10 22:58:58 +00:00
// After resources were reloaded
protected override void ResourcesReloaded()
{
2008-12-10 22:58:58 +00:00
base.ResourcesReloaded();
PickTarget();
}
2008-12-10 22:58:58 +00:00
#endregion
2008-12-05 06:52:20 +00:00
#region ================== Actions
[BeginAction("visualselect", BaseAction = true)]
public void BeginSelect()
{
if(target.picked != null) (target.picked as IVisualEventReceiver).OnSelectBegin();
2008-12-05 06:52:20 +00:00
}
[EndAction("visualselect", BaseAction = true)]
public void EndSelect()
{
if(target.picked != null) (target.picked as IVisualEventReceiver).OnSelectEnd();
2008-12-05 06:52:20 +00:00
}
[BeginAction("visualedit", BaseAction = true)]
public void BeginEdit()
{
if(target.picked != null) (target.picked as IVisualEventReceiver).OnEditBegin();
2008-12-05 06:52:20 +00:00
}
[EndAction("visualedit", BaseAction = true)]
public void EndEdit()
{
if(target.picked != null) (target.picked as IVisualEventReceiver).OnEditEnd();
2008-12-05 06:52:20 +00:00
}
2008-12-06 00:28:49 +00:00
[BeginAction("raisesector8")]
public void RaiseSector8()
{
ChangeTargetHeight(8);
}
[BeginAction("lowersector8")]
public void LowerSector8()
{
ChangeTargetHeight(-8);
}
[BeginAction("raisesector1")]
public void RaiseSector1()
{
ChangeTargetHeight(1);
}
[BeginAction("lowersector1")]
public void LowerSector1()
{
ChangeTargetHeight(-1);
}
2008-12-05 06:52:20 +00:00
#endregion
}
}