UltimateZoneBuilder/Source/Core/Windows/VertexEditForm.cs

213 lines
No EOL
6.1 KiB
C#

#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.Generic;
using System.Windows.Forms;
using CodeImp.DoomBuilder.Geometry;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.GZBuilder.Tools;
#endregion
namespace CodeImp.DoomBuilder.Windows
{
public partial class VertexEditForm : DelayedForm
{
#region ================== Constants
#endregion
#region ================== Variables
private ICollection<Vertex> vertices;
#endregion
#region ================== Properties
#endregion
#region ================== Constructor
// Constructor
public VertexEditForm()
{
InitializeComponent();
// Fill universal fields list
fieldslist.ListFixedFields(General.Map.Config.VertexFields);
// Custom fields?
if(!General.Map.FormatInterface.HasCustomFields)
tabs.TabPages.Remove(tabcustom);
// Decimals allowed?
if(General.Map.FormatInterface.VertexDecimals > 0)
{
positionx.AllowDecimal = true;
positiony.AllowDecimal = true;
//mxd
zceiling.AllowDecimal = true;
zfloor.AllowDecimal = true;
}
if(!General.Map.UDMF) panelHeightControls.Visible = false;
// Initialize custom fields editor
fieldslist.Setup("vertex");
}
#endregion
#region ================== Methods
// This sets up the form to edit the given vertices
public void Setup(ICollection<Vertex> vertices, bool allowPositionChange)
{
// Keep this list
this.vertices = vertices;
if(vertices.Count > 1) this.Text = "Edit Vertices (" + vertices.Count + ")";
////////////////////////////////////////////////////////////////////////
// Set all options to the first vertex properties
////////////////////////////////////////////////////////////////////////
// Get first vertex
Vertex vc = General.GetByIndex(vertices, 0);
// Position
positionx.Text = vc.Position.x.ToString();
positiony.Text = vc.Position.y.ToString();
//mxd
positionx.Enabled = allowPositionChange;
positiony.Enabled = allowPositionChange;
// Custom fields
fieldslist.SetValues(vc.Fields, true);
////////////////////////////////////////////////////////////////////////
// Now go for all sectors and change the options when a setting is different
////////////////////////////////////////////////////////////////////////
// Go for all vertices
foreach(Vertex v in vertices)
{
// Position
if(positionx.Text != v.Position.x.ToString()) positionx.Text = "";
if(positiony.Text != v.Position.y.ToString()) positiony.Text = "";
// Custom fields
fieldslist.SetValues(v.Fields, false);
}
//mxd. Height offsets
if(General.Map.UDMF) {
zceiling.Text = vc.Fields.GetValue("zceiling", 0f).ToString();
zfloor.Text = vc.Fields.GetValue("zfloor", 0f).ToString();
foreach(Vertex v in vertices) {
if(zceiling.Text != v.Fields.GetValue("zceiling", 0f).ToString())
zceiling.Text = "";
if(zfloor.Text != v.Fields.GetValue("zfloor", 0f).ToString())
zfloor.Text = "";
}
}
}
#endregion
#region ================== Events
// OK clicked
private void apply_Click(object sender, EventArgs e)
{
string undodesc = "vertex";
// Verify the coordinates
if((positionx.GetResultFloat(0.0f) < General.Map.FormatInterface.MinCoordinate) || (positionx.GetResultFloat(0.0f) > General.Map.FormatInterface.MaxCoordinate) ||
(positiony.GetResultFloat(0.0f) < General.Map.FormatInterface.MinCoordinate) || (positiony.GetResultFloat(0.0f) > General.Map.FormatInterface.MaxCoordinate))
{
General.ShowWarningMessage("Vertex coordinates must be between " + General.Map.FormatInterface.MinCoordinate + " and " + General.Map.FormatInterface.MaxCoordinate + ".", MessageBoxButtons.OK);
return;
}
// Make undo
if(vertices.Count > 1) undodesc = vertices.Count + " vertices";
General.Map.UndoRedo.CreateUndo("Edit " + undodesc);
// Go for all vertices
foreach(Vertex v in vertices)
{
// Apply position
Vector2D p = new Vector2D();
p.x = General.Clamp(positionx.GetResultFloat(v.Position.x), (float)General.Map.FormatInterface.MinCoordinate, (float)General.Map.FormatInterface.MaxCoordinate);
p.y = General.Clamp(positiony.GetResultFloat(v.Position.y), (float)General.Map.FormatInterface.MinCoordinate, (float)General.Map.FormatInterface.MaxCoordinate);
v.Move(p);
// Custom fields
fieldslist.Apply(v.Fields);
}
//mxd. Height offsets
if(General.Map.UDMF) {
foreach(Vertex v in vertices) {
if(string.IsNullOrEmpty(zceiling.Text)) {
if(v.Fields.ContainsKey("zceiling"))
v.Fields.Remove("zceiling");
} else {
float oldCeil = v.Fields.GetValue("zceiling", 0f);
UDMFTools.SetFloat(v.Fields, "zceiling", zceiling.GetResultFloat(oldCeil), 0, false);
}
if(string.IsNullOrEmpty(zfloor.Text)) {
if(v.Fields.ContainsKey("zfloor"))
v.Fields.Remove("zfloor");
} else {
float oldFloor = v.Fields.GetValue("zfloor", 0f);
UDMFTools.SetFloat(v.Fields, "zfloor", zfloor.GetResultFloat(oldFloor), 0, false);
}
}
}
// Done
General.Map.IsChanged = true;
this.DialogResult = DialogResult.OK;
this.Close();
}
// Cancel clicked
private void cancel_Click(object sender, EventArgs e)
{
// Just close
this.DialogResult = DialogResult.Cancel;
this.Close();
}
// Help requested
private void VertexEditForm_HelpRequested(object sender, HelpEventArgs hlpevent)
{
General.ShowHelp("w_vertexeditor.html");
hlpevent.Handled = true;
}
#endregion
}
}