UltimateZoneBuilder/Source/Controls/FieldsEditorRow.cs

282 lines
7.1 KiB
C#
Raw Normal View History

2008-01-25 19:12:34 +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.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using CodeImp.DoomBuilder.Actions;
2008-01-25 19:12:34 +00:00
using CodeImp.DoomBuilder.Data;
using CodeImp.DoomBuilder.Config;
using CodeImp.DoomBuilder.Rendering;
using SlimDX.Direct3D9;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Types;
2008-01-25 19:12:34 +00:00
#endregion
namespace CodeImp.DoomBuilder.Controls
2008-01-25 19:12:34 +00:00
{
internal class FieldsEditorRow : DataGridViewRow
{
#region ================== Constants
#endregion
#region ================== Variables
// This is true when for a fixed field as defined in the game configuration
// This means that the field cannot be deleted (delete will result in a reset)
// and cannot change type.
private bool isfixed;
// Field information (only for fixed fields)
private UniversalFieldInfo fieldinfo;
// This is true when the field is defined. Cannot be false when this field
// is not fixed, because non-fixed fields are deleted from the list when undefined.
private bool isdefined;
2008-01-25 19:12:34 +00:00
// Type
private TypeHandler fieldtype;
2008-01-25 19:12:34 +00:00
#endregion
#region ================== Properties
public bool IsFixed { get { return isfixed; } }
public bool IsDefined { get { return isdefined; } }
public bool IsEmpty { get { return (this.Cells[2].Value == null) || (this.Cells[2].Value.ToString().Length == 0); } }
public string Name { get { return this.Cells[0].Value.ToString(); } }
public TypeHandler TypeHandler { get { return fieldtype; } }
2008-01-25 19:12:34 +00:00
public UniversalFieldInfo Info { get { return fieldinfo; } }
#endregion
2008-06-02 20:34:52 +00:00
#region ================== Constructor
2008-01-25 19:12:34 +00:00
// Constructor for a fixed, undefined field
public FieldsEditorRow(DataGridView view, UniversalFieldInfo fixedfield)
{
// Undefined
this.DefaultCellStyle.ForeColor = SystemColors.GrayText;
isdefined = false;
// Fixed
this.fieldinfo = fixedfield;
isfixed = true;
// Type
2008-06-02 20:34:52 +00:00
this.fieldtype = General.Types.GetFieldHandler(fixedfield);
2008-01-25 19:12:34 +00:00
// Make all cells
base.CreateCells(view);
// Setup property cell
this.Cells[0].Value = fixedfield.Name;
this.Cells[0].ReadOnly = true;
// Setup type cell
2008-06-02 20:34:52 +00:00
this.Cells[1].Value = fieldtype.GetDisplayType();
2008-01-25 19:12:34 +00:00
this.Cells[1].ReadOnly = true;
// Setup value cell
this.Cells[2].Value = fieldtype.GetStringValue();
2008-01-25 19:12:34 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Constructor for a non-fixed, defined field
public FieldsEditorRow(DataGridView view, string name, int type, object value)
2008-01-25 19:12:34 +00:00
{
// Defined
this.DefaultCellStyle.ForeColor = SystemColors.WindowText;
isdefined = true;
// Non-fixed
isfixed = false;
// Type
this.fieldtype = General.Types.GetFieldHandler(type, value);
2008-01-25 19:12:34 +00:00
// Make all cells
base.CreateCells(view);
// Setup property cell
this.Cells[0].Value = name;
this.Cells[0].ReadOnly = true;
// Setup type cell
2008-06-02 20:34:52 +00:00
this.Cells[1].Value = fieldtype.GetDisplayType();
2008-01-25 19:12:34 +00:00
this.Cells[1].ReadOnly = false;
// Setup value cell
this.Cells[2].Value = fieldtype.GetStringValue();
2008-01-25 19:12:34 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
#endregion
#region ================== Methods
// Browse for value
public void Browse(IWin32Window parent)
{
if(fieldtype != null)
{
// Browse for value
fieldtype.Browse(parent);
// This is a fixed field?
if(isfixed)
{
// Does this match the default setting?
if(fieldtype.GetValue().Equals(fieldinfo.Default))
{
// Undefine this field!
Undefine();
}
else
{
// Define
Define(fieldtype.GetValue());
}
}
else
{
// Define
Define(fieldtype.GetValue());
}
}
}
2008-01-25 19:12:34 +00:00
// This is called when a cell is edited
public void CellChanged()
{
// This gdmn grid thing returns the chosen value as string instead
// of the object type I added to the combobox...
if(this.Cells[1].Value is string)
{
// Find the TypeHandlerAttribute with this name
TypeHandlerAttribute attrib = General.Types.GetNamedAttribute(this.Cells[1].Value.ToString());
// Different?
if(attrib.Index != fieldtype.Index)
{
// Change field type!
2008-06-02 20:34:52 +00:00
this.ChangeType(attrib.Index);
}
}
// Anything in the box?
if((this.Cells[2].Value != null) && (this.Cells[2].Value.ToString().Length > 0))
{
// Validate value
fieldtype.SetValue(this.Cells[2].Value);
this.Cells[2].Value = fieldtype.GetStringValue();
// This is a fixed field?
if(isfixed)
{
// Does this match the default setting?
if(fieldtype.GetValue().Equals(fieldinfo.Default))
{
// Undefine this field!
Undefine();
}
}
}
2008-01-25 19:12:34 +00:00
}
// This undefines the field
// ONLY VALID FOR FIXED FIELDS
// You should just delete non-fixed fields
public void Undefine()
{
// Must be fixed!
if(!isfixed) throw new InvalidOperationException();
// Now undefined
fieldtype.SetValue(fieldinfo.Default);
this.Cells[2].Value = fieldtype.GetStringValue();
2008-01-25 19:12:34 +00:00
this.DefaultCellStyle.ForeColor = SystemColors.GrayText;
isdefined = false;
}
// This defines the field
public void Define(object value)
{
// Now defined
fieldtype.SetValue(value);
this.Cells[2].Value = fieldtype.GetStringValue();
2008-01-25 19:12:34 +00:00
this.DefaultCellStyle.ForeColor = SystemColors.WindowText;
isdefined = true;
}
// This changes the type
public void ChangeType(int typeindex)
{
2008-06-02 20:34:52 +00:00
// Can't do this for a fixed field!
if(isfixed) throw new InvalidOperationException();
// Different?
if(typeindex != fieldtype.Index)
{
// Change field type!
TypeHandlerAttribute attrib = General.Types.GetAttribute(typeindex);
fieldtype = General.Types.GetFieldHandler(typeindex, this.Cells[2].Value);
2008-06-02 20:34:52 +00:00
this.Cells[1].Value = fieldtype.GetDisplayType();
}
}
// This clears the field
public void Clear()
{
this.Cells[2].Value = "";
}
// This returns the result
public object GetResult(object value)
{
// Anything in the box?
if((this.Cells[2].Value != null) && (this.Cells[2].Value.ToString().Length > 0))
{
// Return validated value
fieldtype.SetValue(this.Cells[2].Value);
return fieldtype.GetValue();
}
else
{
// Return old value
return value;
}
}
2008-01-25 19:12:34 +00:00
#endregion
}
}