0
0
Fork 0
mirror of https://git.do.srb2.org/STJr/UltimateZoneBuilder.git synced 2024-12-22 18:11:16 +00:00
UltimateZoneBuilder/Source/Types/EnumOptionHandler.cs

195 lines
4.3 KiB
C#
Raw Normal View History

2008-05-29 16:44:20 +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 CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Data;
using System.IO;
using System.Diagnostics;
using CodeImp.DoomBuilder.Config;
#endregion
namespace CodeImp.DoomBuilder.Types
{
[TypeHandler(UniversalType.EnumOption, "Setting", false)]
2008-05-29 16:44:20 +00:00
internal class EnumOptionHandler : TypeHandler
{
#region ================== Constants
#endregion
#region ================== Variables
private EnumList list;
private EnumItem value;
#endregion
#region ================== Properties
public override bool IsBrowseable { get { return true; } }
public override bool IsEnumerable { get { return true; } }
2008-05-29 16:44:20 +00:00
#endregion
#region ================== Constructor
// When set up for an argument
public override void SetupArgument(TypeHandlerAttribute attr, ArgumentInfo arginfo)
2008-05-29 16:44:20 +00:00
{
base.SetupArgument(attr, arginfo);
2008-05-29 16:44:20 +00:00
// Keep enum list reference
list = arginfo.Enum;
}
2008-06-02 20:34:52 +00:00
// When set up for a universal field
public override void SetupField(TypeHandlerAttribute attr, UniversalFieldInfo fieldinfo)
{
base.SetupField(attr, fieldinfo);
// Keep enum list reference
if(fieldinfo != null) list = fieldinfo.Enum; else list = new EnumList();
}
2008-05-29 16:44:20 +00:00
#endregion
#region ================== Methods
2008-05-29 16:44:20 +00:00
public override void SetValue(object value)
{
this.value = null;
// Input null?
if(value == null)
{
this.value = new EnumItem("0", "NULL");
}
else
{
// Compatible type?
if((value is int) || (value is float) || (value is bool))
{
int intvalue = Convert.ToInt32(value);
// First try to match the value against the enum values
foreach(EnumItem item in list)
{
// Matching value?
if(item.GetIntValue() == intvalue)
{
// Set this value
this.value = item;
}
}
}
// No match found yet?
if(this.value == null)
2008-05-29 16:44:20 +00:00
{
// First try to match the value against the enum values
foreach(EnumItem item in list)
{
// Matching value?
if(item.Value == value.ToString())
{
// Set this value
this.value = item;
}
}
2008-05-29 16:44:20 +00:00
}
// No match found yet?
if(this.value == null)
2008-05-29 16:44:20 +00:00
{
// Try to match against the titles
foreach(EnumItem item in list)
2008-05-29 16:44:20 +00:00
{
// Matching value?
if(item.Title.ToLowerInvariant() == value.ToString().ToLowerInvariant())
{
// Set this value
this.value = item;
}
2008-05-29 16:44:20 +00:00
}
}
// Still no match found?
if(this.value == null)
{
// Make a dummy value
this.value = new EnumItem(value.ToString(), value.ToString());
2008-06-02 20:34:52 +00:00
this.value = new EnumItem(this.value.GetIntValue().ToString(CultureInfo.InvariantCulture), value.ToString());
}
2008-05-29 16:44:20 +00:00
}
}
public override object GetValue()
{
return GetIntValue();
}
2008-05-29 16:44:20 +00:00
public override int GetIntValue()
{
if(this.value != null)
{
// Parse the value to integer
int result;
if(int.TryParse(this.value.Value, NumberStyles.Integer,
CultureInfo.InvariantCulture, out result))
{
return result;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
public override string GetStringValue()
{
if(this.value != null) return this.value.Title; else return "NULL";
}
// This returns an enum list
public override EnumList GetEnumList()
{
return list;
}
2008-06-02 20:34:52 +00:00
// This returns the type to display for fixed fields
// Must be a custom usable type
public override TypeHandlerAttribute GetDisplayType()
{
return General.Types.GetAttribute((int)UniversalType.Integer);
}
2008-05-29 16:44:20 +00:00
#endregion
}
}