UltimateZoneBuilder/Source/Editing/ClassicMode.cs

454 lines
11 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.Interface;
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Map;
2007-06-15 22:38:42 +00:00
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Controls;
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;
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;
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;
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
2008-01-02 21:49:43 +00:00
[Action("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
2008-01-02 21:49:43 +00:00
[Action("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
2008-01-02 21:49:43 +00:00
[Action("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
2008-01-02 21:49:43 +00:00
[Action("scrolleast", BaseAction = true)]
public virtual void ScrollEast()
{
// Scroll
2008-01-02 21:49:43 +00:00
ScrollBy(100f / renderer2d.Scale, 0f);
}
// This zooms in
2008-01-02 21:49:43 +00:00
[Action("zoomin", BaseAction = true)]
public virtual void ZoomIn()
{
// Zoom
2007-10-20 19:50:03 +00:00
ZoomBy(1.3f);
}
// This zooms out
2008-01-02 21:49:43 +00:00
[Action("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);
2007-11-15 23:38:09 +00:00
this.ViewChanged();
// 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);
}
// 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);
2007-11-15 23:38:09 +00:00
this.ViewChanged();
// 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) MouseMove(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);
2007-11-15 23:38:09 +00:00
this.ViewChanged();
// 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) MouseMove(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
[Action("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);
2007-11-15 23:38:09 +00:00
this.ViewChanged();
// 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) MouseMove(new MouseEventArgs(mousebuttons, 0, (int)mousepos.x, (int)mousepos.y, 0));
}
2007-11-15 23:38:09 +00:00
// This is called when the view changes (scroll/zoom)
protected virtual void ViewChanged()
{
}
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 MouseLeave(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.MouseLeave(e);
}
// Mouse moved inside the display
public override void MouseMove(MouseEventArgs e)
{
2007-11-07 21:14:27 +00:00
Vector2D delta;
// Record last position
mouseinside = true;
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 == EditMode.EDIT_BUTTON) ||
(e.Button == EditMode.SELECT_BUTTON))
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;
2007-11-07 21:14:27 +00:00
DragStart(e);
}
}
}
2008-04-13 11:51:09 +00:00
// Selecting?
if(selecting) UpdateSelection();
// Let the base class know
base.MouseMove(e);
}
2007-11-07 21:14:27 +00:00
// Mouse button pressed
public override void MouseDown(MouseEventArgs e)
{
// Save mouse down position
mousedownpos = mousepos;
mousedownmappos = mousemappos;
// Let the base class know
base.MouseDown(e);
}
2007-11-10 19:24:52 +00:00
// Mouse button released
public override void MouseUp(MouseEventArgs e)
{
// Releasing drag button?
if(e.Button == mousedragging)
{
// No longer dragging
DragStop(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
// Selection stops
if(selecting) EndSelection();
2007-11-10 19:24:52 +00:00
// Let the base class know
base.MouseUp(e);
}
2007-11-07 21:14:27 +00:00
// This is called when the mouse is moved enough pixels and holding one or more buttons
protected virtual void DragStart(MouseEventArgs e)
{
2007-11-10 19:24:52 +00:00
}
// This is called when a drag is ended because the mouse buton is released
protected virtual void DragStop(MouseEventArgs e)
{
2007-11-07 21:14:27 +00:00
}
#endregion
#region ================== Display
// This just refreshes the display
public override void RefreshDisplay()
{
2008-01-02 21:49:43 +00:00
renderer2d.Present();
}
#endregion
2007-11-07 21:14:27 +00:00
#region ================== Methods
// Cancelling
2007-11-07 21:14:27 +00:00
public override void Cancel()
{
2007-11-10 01:58:08 +00:00
cancelled = true;
2007-11-07 21:14:27 +00:00
base.Cancel();
}
2008-04-13 11:51:09 +00:00
// This starts a selection
protected virtual void StartSelection()
{
selecting = true;
selectstart = mousemappos;
selectionrect = new RectangleF(selectstart.x, selectstart.y, 0, 0);
}
// This updates a selection
protected virtual void UpdateSelection()
{
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;
}
}
// This is called when a selection is released
protected virtual void EndSelection()
{
selecting = false;
}
// This draws the selection on the overlay layer
// Must call renderer.StartOverlay first!
protected virtual void RenderSelection()
{
renderer.RenderRectangle(selectionrect, SELECTION_BORDER_SIZE,
General.Colors.Highlight.WithAlpha(SELECTION_ALPHA), true);
}
2007-11-07 21:14:27 +00:00
#endregion
2007-06-15 18:30:55 +00:00
}
}