Progress:
- Input fields for vectors & colors in SerializableEntityEditor. - SerializableProperty tooltips. - GUINumberInput supports floats. - EditingHUD for structures (atm the only editable property is the color of the sprite)
This commit is contained in:
@@ -1,50 +1,96 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class GUINumberInput : GUIComponent
|
||||
{
|
||||
public delegate void OnValueChangedHandler(GUINumberInput numberInput, int number);
|
||||
public enum NumberType
|
||||
{
|
||||
Int, Float
|
||||
}
|
||||
|
||||
public delegate void OnValueChangedHandler(GUINumberInput numberInput);
|
||||
public OnValueChangedHandler OnValueChanged;
|
||||
|
||||
private GUITextBox textBox;
|
||||
private GUIButton plusButton, minusButton;
|
||||
|
||||
public int? MinValue, MaxValue;
|
||||
|
||||
private int value;
|
||||
public int Value
|
||||
private NumberType inputType;
|
||||
public NumberType InputType
|
||||
{
|
||||
get { return value; }
|
||||
get { return inputType; }
|
||||
set
|
||||
{
|
||||
if (value == this.value) return;
|
||||
|
||||
this.value = value;
|
||||
if (MinValue != null)
|
||||
{
|
||||
this.value = Math.Max(this.value, (int)MinValue);
|
||||
minusButton.Enabled = this.value > MinValue;
|
||||
}
|
||||
if (MaxValue != null)
|
||||
{
|
||||
this.value = Math.Min(this.value, (int)MaxValue);
|
||||
plusButton.Enabled = this.value < MaxValue;
|
||||
}
|
||||
textBox.Text = this.value.ToString();
|
||||
|
||||
if (OnValueChanged != null) OnValueChanged(this, this.value);
|
||||
inputType = value;
|
||||
plusButton.Visible = inputType == NumberType.Int;
|
||||
minusButton.Visible = inputType == NumberType.Int;
|
||||
}
|
||||
}
|
||||
|
||||
public GUINumberInput(Rectangle rect, string style, int? minValue = null, int? maxValue = null, GUIComponent parent = null)
|
||||
: this(rect, style, Alignment.TopLeft, minValue, maxValue, parent)
|
||||
public float? MinValueFloat, MaxValueFloat;
|
||||
|
||||
private float floatValue;
|
||||
public float FloatValue
|
||||
{
|
||||
get { return floatValue; }
|
||||
set
|
||||
{
|
||||
if (value == floatValue) return;
|
||||
|
||||
floatValue = value;
|
||||
if (MinValueFloat != null)
|
||||
{
|
||||
floatValue = Math.Max(floatValue, MinValueFloat.Value);
|
||||
minusButton.Enabled = floatValue > MinValueFloat;
|
||||
}
|
||||
if (MaxValueFloat != null)
|
||||
{
|
||||
floatValue = Math.Min(floatValue, MaxValueFloat.Value);
|
||||
plusButton.Enabled = floatValue < MaxValueFloat;
|
||||
}
|
||||
textBox.Text = floatValue.ToString("G", CultureInfo.InvariantCulture);
|
||||
|
||||
OnValueChanged?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
public int? MinValueInt, MaxValueInt;
|
||||
|
||||
private int intValue;
|
||||
public int IntValue
|
||||
{
|
||||
get { return intValue; }
|
||||
set
|
||||
{
|
||||
if (value == intValue) return;
|
||||
|
||||
intValue = value;
|
||||
if (MinValueInt != null)
|
||||
{
|
||||
intValue = Math.Max(intValue, MinValueInt.Value);
|
||||
minusButton.Enabled = intValue > MinValueInt;
|
||||
}
|
||||
if (MaxValueInt != null)
|
||||
{
|
||||
intValue = Math.Min(intValue, MaxValueInt.Value);
|
||||
plusButton.Enabled = intValue < MaxValueInt;
|
||||
}
|
||||
textBox.Text = this.intValue.ToString();
|
||||
|
||||
OnValueChanged?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
public GUINumberInput(Rectangle rect, string style, NumberType inputType, GUIComponent parent = null)
|
||||
: this(rect, style, inputType, Alignment.TopLeft, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUINumberInput(Rectangle rect, string style, Alignment alignment, int? minValue = null, int? maxValue = null, GUIComponent parent = null)
|
||||
public GUINumberInput(Rectangle rect, string style, NumberType inputType, Alignment alignment, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
@@ -55,30 +101,46 @@ namespace Barotrauma
|
||||
parent.AddChild(this);
|
||||
|
||||
textBox = new GUITextBox(Rectangle.Empty, style, this);
|
||||
|
||||
textBox.OnTextChanged += TextChanged;
|
||||
|
||||
|
||||
plusButton = new GUIButton(new Rectangle(0, 0, 15, rect.Height / 2), "+", null, Alignment.TopRight, Alignment.Center, style, this);
|
||||
plusButton.OnClicked += ChangeValue;
|
||||
plusButton.OnClicked += ChangeIntValue;
|
||||
plusButton.Visible = inputType == NumberType.Int;
|
||||
minusButton = new GUIButton(new Rectangle(0, 0, 15, rect.Height / 2), "-", null, Alignment.BottomRight, Alignment.Center, style, this);
|
||||
minusButton.OnClicked += ChangeValue;
|
||||
minusButton.OnClicked += ChangeIntValue;
|
||||
minusButton.Visible = inputType == NumberType.Int;
|
||||
|
||||
MinValue = minValue;
|
||||
MaxValue = maxValue;
|
||||
if (inputType == NumberType.Int)
|
||||
{
|
||||
textBox.Text = "0";
|
||||
textBox.OnDeselected += (txtBox, key) =>
|
||||
{
|
||||
textBox.Text = IntValue.ToString();
|
||||
};
|
||||
}
|
||||
else if (inputType == NumberType.Float)
|
||||
{
|
||||
textBox.Text = "0.0";
|
||||
textBox.OnDeselected += (txtBox, key) =>
|
||||
{
|
||||
textBox.Text = FloatValue.ToString("G", CultureInfo.InvariantCulture);
|
||||
};
|
||||
}
|
||||
|
||||
value = int.MaxValue;
|
||||
Value = minValue != null ? (int)minValue : 0;
|
||||
|
||||
|
||||
InputType = inputType;
|
||||
}
|
||||
|
||||
private bool ChangeValue(GUIButton button, object userData)
|
||||
|
||||
private bool ChangeIntValue(GUIButton button, object userData)
|
||||
{
|
||||
if (button == plusButton)
|
||||
{
|
||||
Value++;
|
||||
IntValue++;
|
||||
}
|
||||
else
|
||||
{
|
||||
Value--;
|
||||
IntValue--;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -86,19 +148,44 @@ namespace Barotrauma
|
||||
|
||||
private bool TextChanged(GUITextBox textBox, string text)
|
||||
{
|
||||
int newValue = Value;
|
||||
if (text == "" || text == "-")
|
||||
switch (InputType)
|
||||
{
|
||||
Value = 0;
|
||||
textBox.Text = text;
|
||||
}
|
||||
else if (int.TryParse(text, out newValue))
|
||||
{
|
||||
Value = newValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
textBox.Text = Value.ToString();
|
||||
case NumberType.Int:
|
||||
int newIntValue = IntValue;
|
||||
if (text == "" || text == "-")
|
||||
{
|
||||
IntValue = 0;
|
||||
textBox.Text = text;
|
||||
}
|
||||
else if (int.TryParse(text, out newIntValue))
|
||||
{
|
||||
IntValue = newIntValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
textBox.Text = IntValue.ToString();
|
||||
}
|
||||
break;
|
||||
case NumberType.Float:
|
||||
float newFloatValue = FloatValue;
|
||||
|
||||
text = new string(text.Where(c => char.IsDigit(c) || c == '.' || c == '-').ToArray());
|
||||
|
||||
if (text == "" || text == "-")
|
||||
{
|
||||
FloatValue = 0;
|
||||
textBox.Text = text;
|
||||
}
|
||||
else if (float.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out newFloatValue))
|
||||
{
|
||||
FloatValue = newFloatValue;
|
||||
textBox.Text = text;
|
||||
}
|
||||
/*else
|
||||
{
|
||||
textBox.Text = FloatValue.ToString("G", CultureInfo.InvariantCulture);
|
||||
}*/
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user