2008-05-28 18:55:01 +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;
|
2008-05-29 21:09:43 +00:00
|
|
|
using Microsoft.Win32;
|
2008-05-28 18:55:01 +00:00
|
|
|
using CodeImp.DoomBuilder.Config;
|
2008-05-29 21:09:43 +00:00
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
|
|
|
using CodeImp.DoomBuilder.Types;
|
|
|
|
using System.Globalization;
|
2008-05-28 18:55:01 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2008-05-29 11:54:45 +00:00
|
|
|
namespace CodeImp.DoomBuilder.Controls
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
public partial class ArgumentBox : UserControl
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
2008-05-29 21:09:43 +00:00
|
|
|
private TypeHandler typehandler;
|
|
|
|
|
2008-05-28 18:55:01 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2008-05-29 21:09:43 +00:00
|
|
|
#region ================== Constructor
|
2008-05-28 18:55:01 +00:00
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public ArgumentBox()
|
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Initialize
|
|
|
|
InitializeComponent();
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2008-05-29 10:17:26 +00:00
|
|
|
|
2008-05-29 21:09:43 +00:00
|
|
|
#region ================== Events
|
2008-05-28 18:55:01 +00:00
|
|
|
|
2008-05-29 21:09:43 +00:00
|
|
|
// When control resizes
|
|
|
|
private void ArgumentBox_Resize(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if(button.Visible)
|
|
|
|
combobox.Width = ClientRectangle.Width - button.Width - 2;
|
2008-05-28 18:55:01 +00:00
|
|
|
else
|
2008-05-29 21:09:43 +00:00
|
|
|
combobox.Width = ClientRectangle.Width;
|
2008-05-28 18:55:01 +00:00
|
|
|
|
2008-05-29 21:09:43 +00:00
|
|
|
button.Left = ClientRectangle.Width - button.Width;
|
|
|
|
Height = button.Height;
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
2008-05-29 21:09:43 +00:00
|
|
|
|
|
|
|
// When control layout is aplied
|
|
|
|
private void ArgumentBox_Layout(object sender, LayoutEventArgs e)
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
ArgumentBox_Resize(sender, e);
|
|
|
|
}
|
2008-05-28 18:55:01 +00:00
|
|
|
|
2008-05-29 21:09:43 +00:00
|
|
|
// When the entered value needs to be validated
|
|
|
|
private void combobox_Validating(object sender, CancelEventArgs e)
|
|
|
|
{
|
|
|
|
string str = combobox.Text.Trim().ToLowerInvariant();
|
|
|
|
str = str.TrimStart('+', '-');
|
|
|
|
int num;
|
|
|
|
|
2008-05-28 18:55:01 +00:00
|
|
|
// Anything in the box?
|
2008-05-29 21:09:43 +00:00
|
|
|
if(combobox.Text.Trim().Length > 0)
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Prefixed?
|
|
|
|
if(combobox.Text.Trim().StartsWith("++") ||
|
|
|
|
combobox.Text.Trim().StartsWith("--"))
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Try parsing to number
|
|
|
|
if(!int.TryParse(str, NumberStyles.Integer, CultureInfo.CurrentCulture, out num))
|
|
|
|
{
|
|
|
|
// Invalid relative number
|
|
|
|
combobox.SelectedItem = null;
|
|
|
|
combobox.Text = "";
|
|
|
|
}
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Set the value. The type handler will validate it
|
|
|
|
// and make the best possible choice.
|
|
|
|
typehandler.SetValue(combobox.Text);
|
|
|
|
combobox.SelectedItem = null;
|
|
|
|
combobox.Text = typehandler.GetStringValue();
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-29 21:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When browse button is clicked
|
|
|
|
private void button_Click(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
// Browse for a value
|
|
|
|
typehandler.Browse(this);
|
|
|
|
combobox.SelectedItem = null;
|
|
|
|
combobox.Text = typehandler.GetStringValue();
|
|
|
|
combobox.Focus();
|
|
|
|
combobox.SelectAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This sets up the control for a specific argument
|
|
|
|
public void Setup(ArgumentInfo arginfo)
|
|
|
|
{
|
|
|
|
int oldvalue = 0;
|
|
|
|
|
|
|
|
// Get the original value
|
|
|
|
if(typehandler != null) oldvalue = typehandler.GetIntValue();
|
|
|
|
|
|
|
|
// Get the type handler
|
|
|
|
typehandler = General.Types.GetArgumentHandler(arginfo);
|
|
|
|
|
|
|
|
// Clear combobox
|
|
|
|
combobox.SelectedItem = null;
|
|
|
|
combobox.Items.Clear();
|
|
|
|
|
|
|
|
// Check if this supports enumerated options
|
|
|
|
if(typehandler.IsEnumerable)
|
|
|
|
{
|
|
|
|
// Show the combobox
|
|
|
|
button.Visible = false;
|
|
|
|
combobox.DropDownStyle = ComboBoxStyle.DropDown;
|
|
|
|
combobox.Items.AddRange(typehandler.GetEnumList().ToArray());
|
|
|
|
}
|
|
|
|
// Check if browsable
|
|
|
|
else if(typehandler.IsBrowseable)
|
|
|
|
{
|
|
|
|
// Show the button
|
|
|
|
button.Visible = true;
|
|
|
|
combobox.DropDownStyle = ComboBoxStyle.Simple;
|
|
|
|
}
|
2008-05-28 18:55:01 +00:00
|
|
|
else
|
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Show just a textbox
|
|
|
|
button.Visible = false;
|
|
|
|
combobox.DropDownStyle = ComboBoxStyle.Simple;
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
2008-05-29 21:09:43 +00:00
|
|
|
|
|
|
|
// Setup layout
|
|
|
|
ArgumentBox_Resize(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
// Re-apply value
|
|
|
|
SetValue(oldvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This sets the value
|
|
|
|
public void SetValue(int value)
|
|
|
|
{
|
|
|
|
typehandler.SetValue(value);
|
|
|
|
combobox.SelectedItem = null;
|
|
|
|
combobox.Text = typehandler.GetStringValue();
|
|
|
|
combobox_Validating(this, new CancelEventArgs());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This clears the value
|
|
|
|
public void ClearValue()
|
|
|
|
{
|
|
|
|
typehandler.SetValue("");
|
|
|
|
combobox.SelectedItem = null;
|
|
|
|
combobox.Text = "";
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-29 21:09:43 +00:00
|
|
|
// This returns the selected value
|
|
|
|
public int GetResult(int original)
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
|
|
|
// Strip prefixes
|
2008-05-29 21:09:43 +00:00
|
|
|
string str = combobox.Text.Trim().ToLowerInvariant();
|
2008-05-28 18:55:01 +00:00
|
|
|
str = str.TrimStart('+', '-');
|
2008-05-29 21:09:43 +00:00
|
|
|
int num = original;
|
2008-05-28 18:55:01 +00:00
|
|
|
|
2008-05-29 21:09:43 +00:00
|
|
|
// Anything in the box?
|
|
|
|
if(combobox.Text.Trim().Length > 0)
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Prefixed with ++?
|
|
|
|
if(combobox.Text.Trim().StartsWith("++"))
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Add number to original
|
|
|
|
if(!int.TryParse(str, out num)) num = 0;
|
|
|
|
return original + num;
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
2008-05-29 21:09:43 +00:00
|
|
|
// Prefixed with --?
|
|
|
|
else if(combobox.Text.Trim().StartsWith("--"))
|
2008-05-28 18:55:01 +00:00
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Subtract number from original
|
|
|
|
if(!int.TryParse(str, out num)) num = 0;
|
|
|
|
return original - num;
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-05-29 21:09:43 +00:00
|
|
|
// Return the value
|
|
|
|
return typehandler.GetIntValue();
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-29 21:09:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Just return the original
|
|
|
|
return original;
|
|
|
|
}
|
2008-05-28 18:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|