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:
Joonas Rikkonen
2017-11-12 20:24:11 +02:00
parent 8e556f1c76
commit f7298c241e
15 changed files with 514 additions and 130 deletions
@@ -159,7 +159,12 @@ namespace Barotrauma
child.rect.Y + (rect.Y - prevY),
Math.Max(child.rect.Width + (rect.Width - prevWidth),0),
Math.Max(child.rect.Height + (rect.Height - prevHeight),0));
}
}
if (parent != null && parent is GUIListBox)
{
((GUIListBox)parent).UpdateScrollBarSize();
}
}
}
@@ -385,18 +390,9 @@ namespace Barotrauma
{
spriteBatch.Draw(uiSprite.Sprite.Texture, rect, uiSprite.Sprite.SourceRect, currColor * (currColor.A / 255.0f));
}
}
}
}
//Color newColor = color;
//if (state == ComponentState.Selected) newColor = selectedColor;
//if (state == ComponentState.Hover) newColor = hoverColor;
//GUI.DrawRectangle(spriteBatch, rect, newColor*alpha, true);
//DrawChildren(spriteBatch);
}
public void DrawToolTip(SpriteBatch spriteBatch)
@@ -460,7 +456,7 @@ namespace Barotrauma
}
}
public void SetDimensions(Point size, bool expandChildren = false)
public virtual void SetDimensions(Point size, bool expandChildren = false)
{
Point expandAmount = size - rect.Size;
@@ -180,6 +180,12 @@ namespace Barotrauma
}
}
public override void SetDimensions(Point size, bool expandChildren = false)
{
base.SetDimensions(size, expandChildren);
frame.SetDimensions(size, expandChildren);
}
private void UpdateChildrenRect(float deltaTime)
{
int x = rect.X, y = rect.Y;
@@ -329,7 +335,7 @@ namespace Barotrauma
public void UpdateScrollBarSize()
{
totalSize = 0;
totalSize = (int)(padding.Y + padding.W);
foreach (GUIComponent child in children)
{
if (child == frame) continue;
@@ -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;
@@ -72,11 +72,17 @@ namespace Barotrauma
child.Rect = new Rectangle(child.Rect.X + value.X - rect.X, child.Rect.Y + value.Y - rect.Y, child.Rect.Width, child.Rect.Height);
}
Point moveAmount = value.Location - rect.Location;
rect = value;
if (value.Width != rect.Width || value.Height != rect.Height)
{
SetTextPos();
}
else if (moveAmount != Point.Zero)
{
caretPos += moveAmount.ToVector2();
}
}
}
@@ -240,7 +246,6 @@ namespace Barotrauma
{
caretPos = new Vector2(rect.X + size.X, rect.Y) + textPos - origin;
}
}
private Vector2 MeasureText(string text)
@@ -257,6 +262,12 @@ namespace Barotrauma
return size;
}
protected override void SetAlpha(float a)
{
base.SetAlpha(a);
textColor = new Color(textColor.R, textColor.G, textColor.B, a);
}
public override void Draw(SpriteBatch spriteBatch)
{
Draw(spriteBatch, Vector2.Zero);
@@ -11,6 +11,7 @@ namespace Barotrauma
class GUITextBox : GUIComponent, IKeyboardSubscriber
{
public event TextBoxEvent OnSelected;
public event TextBoxEvent OnDeselected;
bool caretVisible;
float caretTimer;
@@ -204,6 +205,8 @@ namespace Barotrauma
{
Selected = false;
if (keyboardDispatcher.Subscriber == this) keyboardDispatcher.Subscriber = null;
OnDeselected?.Invoke(this, Keys.None);
}
public override void Flash(Color? color = null)
@@ -222,19 +225,17 @@ namespace Barotrauma
if (rect.Contains(PlayerInput.MousePosition) && Enabled &&
(MouseOn == null || MouseOn == this || IsParentOf(MouseOn) || MouseOn.IsParentOf(this)))
{
state = ComponentState.Hover;
if (PlayerInput.LeftButtonClicked())
{
Select();
if (OnSelected != null) OnSelected(this, Keys.None);
OnSelected?.Invoke(this, Keys.None);
}
}
else
{
state = ComponentState.None;
}
if (CaretEnabled)
{
@@ -250,7 +251,7 @@ namespace Barotrauma
{
string input = Text;
Text = "";
OnEnterPressed(this, input);
OnEnterPressed(this, input);
}
#if LINUX
else if (PlayerInput.KeyHit(Keys.Back) && Text.Length>0)
@@ -258,7 +259,10 @@ namespace Barotrauma
Text = Text.Substring(0, Text.Length-1);
}
#endif
}
else if (Selected)
{
Deselect();
}
textBlock.State = state;