UltimateZoneBuilder/Source/Editing/ClassicMode.cs

615 lines
16 KiB
C#
Raw Normal View History

2007-06-15 18:30:55 +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;
2007-06-15 18:30:55 +00:00
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Map;
2007-06-15 22:38:42 +00:00
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Actions;
using CodeImp.DoomBuilder.Geometry;
using System.Drawing;
2007-06-15 18:30:55 +00:00
#endregion
namespace CodeImp.DoomBuilder.Editing
{
/// <summary>
/// Provides specialized functionality for a classic (2D) Doom Builder editing mode.
/// </summary>
2007-10-24 17:25:03 +00:00
public abstract class ClassicMode : EditMode
2007-06-15 18:30:55 +00:00
{
#region ================== Constants
2007-10-21 12:58:56 +00:00
private const float SCALE_MAX = 20f;
private const float SCALE_MIN = 0.01f;
2008-04-13 11:51:09 +00:00
private const float SELECTION_BORDER_SIZE = 2f;
private const int SELECTION_ALPHA = 200;
2007-10-21 12:58:56 +00:00
2007-06-15 18:30:55 +00:00
#endregion
#region ================== Variables
2007-11-10 01:58:08 +00:00
// Cancelled?
protected bool cancelled;
2007-06-15 22:38:42 +00:00
// Graphics
2008-01-02 21:49:43 +00:00
protected IRenderer2D renderer;
private Renderer2D renderer2d;
2007-06-15 22:38:42 +00:00
// Mouse status
protected Vector2D mousepos;
2008-10-28 12:04:28 +00:00
protected Vector2D mouselastpos;
protected Vector2D mousemappos;
2007-11-07 21:14:27 +00:00
protected Vector2D mousedownpos;
protected Vector2D mousedownmappos;
2007-10-20 19:50:03 +00:00
protected MouseButtons mousebuttons;
protected bool mouseinside;
2007-11-10 19:24:52 +00:00
protected MouseButtons mousedragging = MouseButtons.None;
2008-04-13 11:51:09 +00:00
// Selection
protected bool selecting;
private Vector2D selectstart;
protected RectangleF selectionrect;
2008-10-28 12:04:28 +00:00
// View panning
protected bool panning;
2008-04-13 11:51:09 +00:00
2007-06-15 18:30:55 +00:00
#endregion
#region ================== Properties
#endregion
#region ================== Constructor / Disposer
/// <summary>
/// Provides specialized functionality for a classic (2D) Doom Builder editing mode.
/// </summary>
public ClassicMode()
2007-06-15 18:30:55 +00:00
{
// Initialize
this.renderer = General.Map.Renderer2D;
2008-01-02 21:49:43 +00:00
this.renderer2d = (Renderer2D)General.Map.Renderer2D;
// If the current mode is a ClassicMode, copy mouse properties
if(General.Map.Mode is ClassicMode)
{
ClassicMode oldmode = General.Map.Mode as ClassicMode;
// Copy mouse properties
mousepos = oldmode.mousepos;
mousemappos = oldmode.mousemappos;
mousedownpos = oldmode.mousedownpos;
mousedownmappos = oldmode.mousedownmappos;
mousebuttons = oldmode.mousebuttons;
mouseinside = oldmode.mouseinside;
mousedragging = oldmode.mousedragging;
}
2007-06-15 18:30:55 +00:00
}
2008-01-02 21:49:43 +00:00
// Disposer
2007-06-15 18:30:55 +00:00
public override void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
2007-06-15 22:38:42 +00:00
2007-06-15 18:30:55 +00:00
// Dispose base
base.Dispose();
}
}
#endregion
#region ================== Scroll / Zoom
2007-06-15 18:30:55 +00:00
// This scrolls the view north
[BeginAction("scrollnorth", BaseAction = true)]
public virtual void ScrollNorth()
{
// Scroll
2008-01-02 21:49:43 +00:00
ScrollBy(0f, 100f / renderer2d.Scale);
}
// This scrolls the view south
[BeginAction("scrollsouth", BaseAction = true)]
public virtual void ScrollSouth()
{
// Scroll
2008-01-02 21:49:43 +00:00
ScrollBy(0f, -100f / renderer2d.Scale);
}
// This scrolls the view west
[BeginAction("scrollwest", BaseAction = true)]
public virtual void ScrollWest()
{
// Scroll
2008-01-02 21:49:43 +00:00
ScrollBy(-100f / renderer2d.Scale, 0f);
}
// This scrolls the view east
[BeginAction("scrolleast", BaseAction = true)]
public virtual void ScrollEast()
{
// Scroll
2008-01-02 21:49:43 +00:00
ScrollBy(100f / renderer2d.Scale, 0f);
}
// This zooms in
[BeginAction("zoomin", BaseAction = true)]
public virtual void ZoomIn()
{
// Zoom
2007-10-20 19:50:03 +00:00
ZoomBy(1.3f);
}
// This zooms out
[BeginAction("zoomout", BaseAction = true)]
public virtual void ZoomOut()
{
// Zoom
2007-10-20 19:50:03 +00:00
ZoomBy(0.7f);
}
// This scrolls anywhere
private void ScrollBy(float deltax, float deltay)
{
// Scroll now
2008-01-02 21:49:43 +00:00
renderer2d.PositionView(renderer2d.OffsetX + deltax, renderer2d.OffsetY + deltay);
this.OnViewChanged();
2007-11-15 23:38:09 +00:00
// Redraw
2007-10-05 10:00:15 +00:00
General.MainWindow.RedrawDisplay();
// Determine new unprojected mouse coordinates
2008-01-02 21:49:43 +00:00
mousemappos = renderer2d.GetMapCoordinates(mousepos);
General.MainWindow.UpdateCoordinates(mousemappos);
}
2008-10-28 12:04:28 +00:00
// This sets the view to be centered at x,y
private void ScrollTo(float x, float y)
{
// Scroll now
renderer2d.PositionView(x, y);
this.OnViewChanged();
// Redraw
General.MainWindow.RedrawDisplay();
// Determine new unprojected mouse coordinates
mousemappos = renderer2d.GetMapCoordinates(mousepos);
General.MainWindow.UpdateCoordinates(mousemappos);
}
// This zooms
private void ZoomBy(float deltaz)
{
Vector2D zoompos, clientsize, diff;
float newscale;
// This will be the new zoom scale
2008-01-02 21:49:43 +00:00
newscale = renderer2d.Scale * deltaz;
2007-10-21 12:58:56 +00:00
// Limit scale
if(newscale > SCALE_MAX) newscale = SCALE_MAX;
if(newscale < SCALE_MIN) newscale = SCALE_MIN;
// Get the dimensions of the display
2007-09-24 19:54:47 +00:00
clientsize = new Vector2D(General.Map.Graphics.RenderTarget.ClientSize.Width,
General.Map.Graphics.RenderTarget.ClientSize.Height);
// When mouse is inside display
if(mouseinside)
{
// Zoom into or from mouse position
zoompos = (mousepos / clientsize) - new Vector2D(0.5f, 0.5f);
}
else
{
// Zoom into or from center
zoompos = new Vector2D(0f, 0f);
}
// Calculate view position difference
2008-01-02 21:49:43 +00:00
diff = ((clientsize / newscale) - (clientsize / renderer2d.Scale)) * zoompos;
2007-09-24 19:54:47 +00:00
// Zoom now
2008-01-02 21:49:43 +00:00
renderer2d.PositionView(renderer2d.OffsetX - diff.x, renderer2d.OffsetY + diff.y);
renderer2d.ScaleView(newscale);
this.OnViewChanged();
2007-11-15 23:38:09 +00:00
// Redraw
//General.Map.Map.Update();
2007-10-05 10:00:15 +00:00
General.MainWindow.RedrawDisplay();
2007-09-24 19:54:47 +00:00
2007-10-20 19:50:03 +00:00
// Give a new mousemove event to update coordinates
if(mouseinside) OnMouseMove(new MouseEventArgs(mousebuttons, 0, (int)mousepos.x, (int)mousepos.y, 0));
2007-09-24 19:54:47 +00:00
}
// This zooms to a specific level
public void SetZoom(float newscale)
{
// Zoom now
2008-01-02 21:49:43 +00:00
renderer2d.ScaleView(newscale);
this.OnViewChanged();
2007-11-15 23:38:09 +00:00
// Redraw
//General.Map.Map.Update();
2007-10-05 10:00:15 +00:00
General.MainWindow.RedrawDisplay();
2007-10-20 19:50:03 +00:00
// Give a new mousemove event to update coordinates
if(mouseinside) OnMouseMove(new MouseEventArgs(mousebuttons, 0, (int)mousepos.x, (int)mousepos.y, 0));
2007-09-24 19:54:47 +00:00
}
// This zooms and scrolls to fit the map in the window
[BeginAction("centerinscreen", BaseAction = true)]
2007-09-24 19:54:47 +00:00
public void CenterInScreen()
{
float left = float.MaxValue;
float top = float.MaxValue;
float right = float.MinValue;
float bottom = float.MinValue;
float scalew, scaleh, scale;
float width, height;
// Go for all vertices
2007-10-05 11:17:58 +00:00
foreach(Vertex v in General.Map.Map.Vertices)
2007-09-24 19:54:47 +00:00
{
2007-12-01 01:32:56 +00:00
// Vertex used?
if(v.Linedefs.Count > 0)
{
// Adjust boundaries by vertices
if(v.Position.x < left) left = v.Position.x;
if(v.Position.x > right) right = v.Position.x;
if(v.Position.y < top) top = v.Position.y;
if(v.Position.y > bottom) bottom = v.Position.y;
}
2007-09-24 19:54:47 +00:00
}
// Calculate width/height
width = (right - left);
height = (bottom - top);
// Calculate scale to view map at
scalew = (float)General.Map.Graphics.RenderTarget.ClientSize.Width / (width * 1.1f);
scaleh = (float)General.Map.Graphics.RenderTarget.ClientSize.Height / (height * 1.1f);
if(scalew < scaleh) scale = scalew; else scale = scaleh;
// Change the view to see the whole map
2008-01-02 21:49:43 +00:00
renderer2d.ScaleView(scale);
renderer2d.PositionView(left + (right - left) * 0.5f, top + (bottom - top) * 0.5f);
this.OnViewChanged();
2007-11-15 23:38:09 +00:00
// Redraw
//General.Map.Map.Update();
2007-10-05 10:00:15 +00:00
General.MainWindow.RedrawDisplay();
2007-10-20 19:50:03 +00:00
// Give a new mousemove event to update coordinates
if(mouseinside) OnMouseMove(new MouseEventArgs(mousebuttons, 0, (int)mousepos.x, (int)mousepos.y, 0));
}
2007-11-15 23:38:09 +00:00
/// <summary>
/// This is called when the view changes (scroll/zoom), before the display is redrawn.
/// </summary>
protected virtual void OnViewChanged()
2007-11-15 23:38:09 +00:00
{
}
2007-06-15 18:30:55 +00:00
#endregion
2007-11-14 22:53:48 +00:00
#region ================== Input
// Mouse leaves the display
public override void OnMouseLeave(EventArgs e)
{
// Mouse is outside the display
mouseinside = false;
mousepos = new Vector2D(float.NaN, float.NaN);
mousemappos = mousepos;
2007-10-20 19:50:03 +00:00
mousebuttons = MouseButtons.None;
// Determine new unprojected mouse coordinates
General.MainWindow.UpdateCoordinates(mousemappos);
// Let the base class know
base.OnMouseLeave(e);
}
// Mouse moved inside the display
public override void OnMouseMove(MouseEventArgs e)
{
2007-11-07 21:14:27 +00:00
Vector2D delta;
// Record last position
mouseinside = true;
2008-10-28 12:04:28 +00:00
mouselastpos = mousepos;
mousepos = new Vector2D(e.X, e.Y);
2008-01-02 21:49:43 +00:00
mousemappos = renderer2d.GetMapCoordinates(mousepos);
2007-10-20 19:50:03 +00:00
mousebuttons = e.Button;
// Update labels in main window
General.MainWindow.UpdateCoordinates(mousemappos);
2007-11-10 19:24:52 +00:00
// Holding a button?
if(e.Button != MouseButtons.None)
2007-11-07 21:14:27 +00:00
{
// Not dragging?
2007-11-10 19:24:52 +00:00
if(mousedragging == MouseButtons.None)
2007-11-07 21:14:27 +00:00
{
// Check if moved enough pixels for dragging
delta = mousedownpos - mousepos;
if((Math.Abs(delta.x) > DRAG_START_MOVE_PIXELS) ||
(Math.Abs(delta.y) > DRAG_START_MOVE_PIXELS))
{
// Dragging starts now
2007-11-10 19:24:52 +00:00
mousedragging = e.Button;
OnDragStart(e);
2007-11-07 21:14:27 +00:00
}
}
}
2008-04-13 11:51:09 +00:00
// Selecting?
if(selecting) OnUpdateMultiSelection();
2008-10-28 12:04:28 +00:00
// Panning?
if (panning) OnUpdateViewPanning();
2008-04-13 11:51:09 +00:00
// Let the base class know
base.OnMouseMove(e);
}
2007-11-07 21:14:27 +00:00
// Mouse button pressed
public override void OnMouseDown(MouseEventArgs e)
2007-11-07 21:14:27 +00:00
{
// Save mouse down position
mousedownpos = mousepos;
mousedownmappos = mousemappos;
// Let the base class know
base.OnMouseDown(e);
2007-11-07 21:14:27 +00:00
}
2007-11-10 19:24:52 +00:00
// Mouse button released
public override void OnMouseUp(MouseEventArgs e)
2007-11-10 19:24:52 +00:00
{
// Releasing drag button?
if(e.Button == mousedragging)
{
// No longer dragging
OnDragStop(e);
2007-12-26 00:31:32 +00:00
mousedragging = MouseButtons.None;
2007-11-10 19:24:52 +00:00
}
2008-04-13 11:51:09 +00:00
2007-11-10 19:24:52 +00:00
// Let the base class know
base.OnMouseUp(e);
2007-11-10 19:24:52 +00:00
}
/// <summary>
/// Automatically called when dragging operation starts.
/// </summary>
protected virtual void OnDragStart(MouseEventArgs e)
2007-11-07 21:14:27 +00:00
{
2007-11-10 19:24:52 +00:00
}
/// <summary>
/// Automatically called when dragging operation stops.
/// </summary>
protected virtual void OnDragStop(MouseEventArgs e)
2007-11-10 19:24:52 +00:00
{
2007-11-07 21:14:27 +00:00
}
#endregion
#region ================== Display
// This just refreshes the display
public override void OnPresentDisplay()
{
2008-01-02 21:49:43 +00:00
renderer2d.Present();
}
// This sets the view mode
private void SetViewMode(ViewMode mode)
{
General.Map.CRenderer2D.SetViewMode(mode);
General.MainWindow.UpdateInterface();
General.MainWindow.RedrawDisplay();
}
#endregion
2007-11-07 21:14:27 +00:00
#region ================== Methods
/// <summary>
/// Automatically called by the core when this editing mode is engaged.
/// </summary>
public override void OnEngage()
{
// Clear display overlay
renderer.StartOverlay(true);
renderer.Finish();
base.OnEngage();
}
/// <summary>
/// Called when the user requests to cancel this editing mode.
/// </summary>
public override void OnCancel()
2007-11-07 21:14:27 +00:00
{
2007-11-10 01:58:08 +00:00
cancelled = true;
base.OnCancel();
2007-11-07 21:14:27 +00:00
}
/// <summary>
/// This is called automatically when the Edit button is pressed.
/// (in Doom Builder 1, this was always the right mousebutton)
/// </summary>
[BeginAction("classicedit", BaseAction = true)]
protected virtual void OnEditBegin()
{
}
/// <summary>
/// This is called automatically when the Edit button is released.
/// (in Doom Builder 1, this was always the right mousebutton)
/// </summary>
[EndAction("classicedit", BaseAction = true)]
protected virtual void OnEditEnd()
{
}
/// <summary>
/// This is called automatically when the Select button is pressed.
/// (in Doom Builder 1, this was always the left mousebutton)
/// </summary>
[BeginAction("classicselect", BaseAction = true)]
protected virtual void OnSelectBegin()
{
}
/// <summary>
/// This is called automatically when the Select button is released.
/// (in Doom Builder 1, this was always the left mousebutton)
/// </summary>
[EndAction("classicselect", BaseAction = true)]
protected virtual void OnSelectEnd()
{
if(selecting) OnEndMultiSelection();
}
/// <summary>
/// This is called automatically when a rectangular multi-selection ends.
/// </summary>
protected virtual void OnEndMultiSelection()
{
selecting = false;
}
/// <summary>
/// Call this to initiate a rectangular multi-selection.
/// </summary>
protected virtual void StartMultiSelection()
2008-04-13 11:51:09 +00:00
{
selecting = true;
selectstart = mousemappos;
selectionrect = new RectangleF(selectstart.x, selectstart.y, 0, 0);
}
/// <summary>
/// This is called automatically when a multi-selection is updated.
/// </summary>
protected virtual void OnUpdateMultiSelection()
2008-04-13 11:51:09 +00:00
{
selectionrect.X = selectstart.x;
selectionrect.Y = selectstart.y;
selectionrect.Width = mousemappos.x - selectstart.x;
selectionrect.Height = mousemappos.y - selectstart.y;
if(selectionrect.Width < 0f)
{
selectionrect.Width = -selectionrect.Width;
selectionrect.X -= selectionrect.Width;
}
if(selectionrect.Height < 0f)
{
selectionrect.Height = -selectionrect.Height;
selectionrect.Y -= selectionrect.Height;
}
}
/// <summary>
/// Call this to draw the selection on the overlay layer.
/// Must call renderer.StartOverlay first!
/// </summary>
protected virtual void RenderMultiSelection()
2008-04-13 11:51:09 +00:00
{
renderer.RenderRectangle(selectionrect, SELECTION_BORDER_SIZE,
General.Colors.Highlight.WithAlpha(SELECTION_ALPHA), true);
}
2008-10-28 12:04:28 +00:00
/// <summary>
/// This is called automatically when the mouse is moved while panning
/// </summary>
protected virtual void OnUpdateViewPanning()
{
// We can only drag the map when the mouse pointer is inside
// otherwise we don't have coordinates where to drag the map to
if(mouseinside && !float.IsNaN(mouselastpos.x) && !float.IsNaN(mouselastpos.y))
{
// Get the map coordinates of the last mouse posision (before it moved)
Vector2D lastmappos = renderer2d.GetMapCoordinates(mouselastpos);
// Do the scroll
ScrollBy(lastmappos.x - mousemappos.x, lastmappos.y - mousemappos.y);
}
2008-10-28 12:04:28 +00:00
}
2008-04-13 11:51:09 +00:00
2007-11-07 21:14:27 +00:00
#endregion
#region ================== Actions
2008-10-28 12:04:28 +00:00
[BeginAction("pan_view", BaseAction = true)]
protected virtual void BeginViewPan()
{
panning = true;
}
[EndAction("pan_view", BaseAction = true)]
protected virtual void EndViewPan()
{
panning = false;
}
[BeginAction("viewmodenormal", BaseAction = true)]
protected virtual void ViewModeNormal()
{
SetViewMode(ViewMode.Normal);
}
[BeginAction("viewmodebrightness", BaseAction = true)]
protected virtual void ViewModeBrightness()
{
SetViewMode(ViewMode.Brightness);
}
[BeginAction("viewmodefloors", BaseAction = true)]
protected virtual void ViewModeFloors()
{
SetViewMode(ViewMode.FloorTextures);
}
[BeginAction("viewmodeceilings", BaseAction = true)]
protected virtual void ViewModeCeilings()
{
SetViewMode(ViewMode.CeilingTextures);
}
#endregion
2007-06-15 18:30:55 +00:00
}
}