UltimateZoneBuilder/Source/Editing/GridSetup.cs

295 lines
7.5 KiB
C#
Raw Normal View History

2008-01-02 21:49:43 +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
2007-11-10 19:24:52 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
2008-01-02 21:49:43 +00:00
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using CodeImp.DoomBuilder.VisualModes;
using CodeImp.DoomBuilder.Windows;
2008-01-02 21:49:43 +00:00
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Rendering;
using System.Diagnostics;
using CodeImp.DoomBuilder.Actions;
2007-11-10 19:24:52 +00:00
using CodeImp.DoomBuilder.Data;
using CodeImp.DoomBuilder.Geometry;
2008-01-02 21:49:43 +00:00
#endregion
2007-11-10 19:24:52 +00:00
namespace CodeImp.DoomBuilder.Editing
{
public class GridSetup
{
#region ================== Constants
private const int DEFAULT_GRID_SIZE = 32;
public const int SOURCE_TEXTURES = 0;
public const int SOURCE_FLATS = 1;
public const int SOURCE_FILE = 2;
2007-11-10 19:24:52 +00:00
#endregion
#region ================== Variables
// Grid
private int gridsize;
private float gridsizef;
private float gridsizefinv;
// Background
private string background = "";
private int backsource;
private ImageData backimage = new UnknownImage(null);
2007-11-10 19:24:52 +00:00
private int backoffsetx, backoffsety;
private float backscalex, backscaley;
2007-11-10 19:24:52 +00:00
2008-06-10 08:35:14 +00:00
// Disposing
private bool isdisposed;
2007-11-10 19:24:52 +00:00
#endregion
#region ================== Properties
public int GridSize { get { return gridsize; } }
public float GridSizeF { get { return gridsizef; } }
2008-01-02 21:49:43 +00:00
internal string BackgroundName { get { return background; } }
internal int BackgroundSource { get { return backsource; } }
internal ImageData Background { get { return backimage; } }
internal int BackgroundX { get { return backoffsetx; } }
internal int BackgroundY { get { return backoffsety; } }
internal float BackgroundScaleX { get { return backscalex; } }
internal float BackgroundScaleY { get { return backscaley; } }
2008-06-10 08:35:14 +00:00
internal bool Disposed { get { return isdisposed; } }
2007-11-10 19:24:52 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
2008-01-02 21:49:43 +00:00
internal GridSetup()
2007-11-10 19:24:52 +00:00
{
// Initialize
SetGridSize(DEFAULT_GRID_SIZE);
backscalex = 1.0f;
backscaley = 1.0f;
2008-06-10 08:35:14 +00:00
// Register actions
General.Actions.BindMethods(this);
2007-11-10 19:24:52 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
2008-06-10 08:35:14 +00:00
// Disposer
internal void Dispose()
{
if(!isdisposed)
{
// Dispose image if needed
if(backimage is FileImage) (backimage as FileImage).Dispose();
2008-06-10 08:35:14 +00:00
// Clean up
backimage = null;
// Unregister actions
General.Actions.UnbindMethods(this);
// Done
isdisposed = true;
}
}
2007-11-10 19:24:52 +00:00
#endregion
#region ================== Methods
// Write settings to configuration
internal void WriteToConfig(Configuration cfg, string path)
{
// Write settings
cfg.WriteSetting(path + ".background", background);
cfg.WriteSetting(path + ".backsource", backsource);
cfg.WriteSetting(path + ".backoffsetx", backoffsetx);
cfg.WriteSetting(path + ".backoffsety", backoffsety);
cfg.WriteSetting(path + ".backscalex", backscalex);
cfg.WriteSetting(path + ".backscaley", backscaley);
cfg.WriteSetting(path + ".gridsize", gridsize);
}
// Read settings from configuration
internal void ReadFromConfig(Configuration cfg, string path)
{
// Read settings
background = cfg.ReadSetting(path + ".background", "");
backsource = cfg.ReadSetting(path + ".backsource", 0);
backoffsetx = cfg.ReadSetting(path + ".backoffsetx", 0);
backoffsety = cfg.ReadSetting(path + ".backoffsety", 0);
backscalex = cfg.ReadSetting(path + ".backscalex", 1.0f);
backscaley = cfg.ReadSetting(path + ".backscaley", 1.0f);
gridsize = cfg.ReadSetting(path + ".gridsize", DEFAULT_GRID_SIZE);
// Setup
SetGridSize(gridsize);
LinkBackground();
}
2007-11-10 19:24:52 +00:00
// This sets the grid size
2008-01-02 21:49:43 +00:00
internal void SetGridSize(int size)
2007-11-10 19:24:52 +00:00
{
// Change grid
this.gridsize = size;
this.gridsizef = (float)gridsize;
this.gridsizefinv = 1f / gridsizef;
// Update in main window
General.MainWindow.UpdateGrid(gridsize);
}
// This sets the background
2008-01-02 21:49:43 +00:00
internal void SetBackground(string name, int source)
2007-11-10 19:24:52 +00:00
{
// Set background
if(name == null) name = "";
this.backsource = source;
this.background = name;
// Find this image
LinkBackground();
}
// This sets the background view
internal void SetBackgroundView(int offsetx, int offsety, float scalex, float scaley)
2007-11-10 19:24:52 +00:00
{
// Set background offset
this.backoffsetx = offsetx;
this.backoffsety = offsety;
this.backscalex = scalex;
this.backscaley = scaley;
2007-11-10 19:24:52 +00:00
}
// This finds and links the background image
2008-01-02 21:49:43 +00:00
internal void LinkBackground()
2007-11-10 19:24:52 +00:00
{
// Dispose image if needed
if(backimage is FileImage) (backimage as FileImage).Dispose();
// Where to load background from?
switch(backsource)
2007-11-10 19:24:52 +00:00
{
case SOURCE_TEXTURES:
backimage = General.Map.Data.GetTextureImage(background);
break;
case SOURCE_FLATS:
backimage = General.Map.Data.GetFlatImage(background);
break;
case SOURCE_FILE:
backimage = new FileImage(background, background, false, 1.0f, 1.0f);
break;
2007-11-10 19:24:52 +00:00
}
2007-11-10 19:24:52 +00:00
// Make sure it is loaded
backimage.LoadImage();
backimage.CreateTexture();
}
// This returns the next higher coordinate
public float GetHigher(float offset)
{
return (float)Math.Round((offset + (gridsizef * 0.5f)) * gridsizefinv) * gridsizef;
}
// This returns the next lower coordinate
public float GetLower(float offset)
{
return (float)Math.Round((offset - (gridsizef * 0.5f)) * gridsizefinv) * gridsizef;
}
2007-11-10 19:24:52 +00:00
// This snaps to the nearest grid coordinate
public Vector2D SnappedToGrid(Vector2D v)
{
return GridSetup.SnappedToGrid(v, gridsizef, gridsizefinv);
}
// This snaps to the nearest grid coordinate
public static Vector2D SnappedToGrid(Vector2D v, float gridsize, float gridsizeinv)
{
return new Vector2D((float)Math.Round(v.x * gridsizeinv) * gridsize,
(float)Math.Round(v.y * gridsizeinv) * gridsize);
}
#endregion
2008-06-10 08:35:14 +00:00
#region ================== Actions
// This shows the grid setup dialog
internal void ShowGridSetup()
{
// Show preferences dialog
GridSetupForm gridform = new GridSetupForm();
if(gridform.ShowDialog(General.MainWindow) == DialogResult.OK)
{
// Redraw display
General.MainWindow.RedrawDisplay();
}
// Done
gridform.Dispose();
}
2008-06-10 08:35:14 +00:00
// This changes grid size
[BeginAction("gridinc")]
internal void IncreaseGrid()
{
// Not lower than 1
if(gridsize >= 2)
2008-06-10 08:35:14 +00:00
{
// Change grid
SetGridSize(gridsize >> 1);
// Redraw display
General.MainWindow.RedrawDisplay();
}
}
// This changes grid size
[BeginAction("griddec")]
internal void DecreaseGrid()
{
// Not higher than 1024
if(gridsize <= 512)
{
// Change grid
SetGridSize(gridsize << 1);
// Redraw display
General.MainWindow.RedrawDisplay();
}
}
#endregion
2007-11-10 19:24:52 +00:00
}
}