- Renamed a bunch of ObjectProperty-related stuff (ObjectProperty -> SerializableProperty, IPropertyObject -> ISerializableEntity, the "SerializableProperty" attribute -> Serialize).

- Rectangle serialization.
- Option to restrict numeric properties to a range of values.
- WIP generic ISerializableEntity editor.
This commit is contained in:
Joonas Rikkonen
2017-11-08 21:15:03 +02:00
parent a0d606ef5f
commit 8e556f1c76
60 changed files with 1035 additions and 678 deletions
@@ -11,7 +11,7 @@ using System.Xml.Linq;
namespace Barotrauma
{
partial class Character : Entity, IDamageable, IPropertyObject, IClientSerializable, IServerSerializable
partial class Character : Entity, IDamageable, ISerializableEntity, IClientSerializable, IServerSerializable
{
protected float soundTimer;
protected float soundInterval;
@@ -59,7 +59,7 @@ namespace Barotrauma
public static GUIComponent UpdateMouseOn()
{
MouseOn = null;
for (int i=ComponentsToUpdate.Count-1;i>=0;i--)
for (int i = ComponentsToUpdate.Count - 1; i >= 0; i--)
{
GUIComponent c = ComponentsToUpdate[i];
if (c.MouseRect.Contains(PlayerInput.MousePosition))
@@ -149,7 +149,9 @@ namespace Barotrauma
rect = value;
if (prevX == rect.X && prevY == rect.Y && rect.Width == prevWidth && rect.Height == prevHeight) return;
//TODO: fix this (or replace with something better in the new GUI system)
//simply expanding the rects by the same amount as their parent only works correctly in some special cases
foreach (GUIComponent child in children)
{
child.Rect = new Rectangle(
@@ -166,9 +168,7 @@ namespace Barotrauma
get { return CanBeFocused ? rect : Rectangle.Empty; }
}
public Dictionary<GUIComponent.ComponentState, List<UISprite>> sprites;
//public Alignment SpriteAlignment { get; set; }
//public bool RepeatSpriteX, RepeatSpriteY;
public Dictionary<ComponentState, List<UISprite>> sprites;
public virtual Color OutlineColor { get; set; }
@@ -271,15 +271,44 @@ namespace Barotrauma
return false;
}
protected virtual void SetAlpha(float a)
{
color = new Color(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, a);
}
public virtual void Flash(Color? color = null)
{
flashTimer = FlashDuration;
flashColor = (color == null) ? Color.Red * 0.8f : (Color)color;
}
//foreach (GUIComponent child in children)
//{
// child.Flash();
//}
public void FadeOut(float duration, bool removeAfter)
{
CoroutineManager.StartCoroutine(LerpAlpha(0.0f, duration, removeAfter));
}
private IEnumerable<object> LerpAlpha(float to, float duration, bool removeAfter)
{
float t = 0.0f;
float startA = color.A;
while (t < duration)
{
t += CoroutineManager.DeltaTime;
SetAlpha(MathHelper.Lerp(startA, to, t / duration));
yield return CoroutineStatus.Running;
}
SetAlpha(to);
if (removeAfter && parent != null)
{
parent.RemoveChild(this);
}
yield return CoroutineStatus.Success;
}
public virtual void Draw(SpriteBatch spriteBatch)
@@ -431,13 +460,34 @@ namespace Barotrauma
}
}
public void SetDimensions(Point size, bool expandChildren = false)
{
Point expandAmount = size - rect.Size;
rect = new Rectangle(rect.X, rect.Y, size.X, size.Y);
if (expandChildren)
{
//TODO: fix this (or replace with something better in the new GUI system)
//simply expanding the rects by the same amount as their parent only works correctly in some special cases
foreach (GUIComponent child in children)
{
child.Rect = new Rectangle(
child.rect.X,
child.rect.Y,
child.rect.Width + expandAmount.X,
child.rect.Height + expandAmount.Y);
}
}
}
protected virtual void UpdateDimensions(GUIComponent parent = null)
{
Rectangle parentRect = (parent==null) ? new Rectangle(0,0,GameMain.GraphicsWidth, GameMain.GraphicsHeight) : parent.rect;
Rectangle parentRect = (parent == null) ? new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight) : parent.rect;
Vector4 padding = (parent == null) ? Vector4.Zero : parent.padding;
if (rect.Width == 0) rect.Width = parentRect.Width - rect.X
if (rect.Width == 0) rect.Width = parentRect.Width - rect.X
- (int)padding.X - (int)padding.Z;
if (rect.Height == 0) rect.Height = parentRect.Height - rect.Y
@@ -445,7 +495,7 @@ namespace Barotrauma
if (alignment.HasFlag(Alignment.CenterX))
{
rect.X += parentRect.X + (int)parentRect.Width/2 - (int)rect.Width/2;
rect.X += parentRect.X + (int)parentRect.Width / 2 - (int)rect.Width / 2;
}
else if (alignment.HasFlag(Alignment.Right))
{
@@ -72,15 +72,20 @@ namespace Barotrauma
}
public GUIDropDown(Rectangle rect, string text, string style, GUIComponent parent = null)
: this(rect, text, style, Alignment.TopLeft, parent)
{
}
public GUIDropDown(Rectangle rect, string text, string style, Alignment alignment, GUIComponent parent = null)
: base(style)
{
this.rect = rect;
if (parent != null) parent.AddChild(this);
button = new GUIButton(this.rect, text, Color.White, Alignment.TopLeft, Alignment.CenterLeft, "GUIDropDown", null);
button = new GUIButton(this.rect, text, Color.White, alignment, Alignment.CenterLeft, "GUIDropDown", null);
GUI.Style.Apply(button, style, this);
button.OnClicked = OnClicked;
listBox = new GUIListBox(new Rectangle(this.rect.X, this.rect.Bottom, this.rect.Width, 200), style, null);
@@ -33,7 +33,7 @@ namespace Barotrauma.Items.Components
}
}
partial class ItemComponent : IPropertyObject
partial class ItemComponent : ISerializableEntity
{
private Dictionary<ActionType, List<ItemSound>> sounds;
@@ -109,13 +109,13 @@ namespace Barotrauma.Items.Components
if (sound == null) return 0.0f;
if (sound.VolumeProperty == "") return 1.0f;
ObjectProperty op = null;
if (properties.TryGetValue(sound.VolumeProperty.ToLowerInvariant(), out op))
SerializableProperty property = null;
if (properties.TryGetValue(sound.VolumeProperty.ToLowerInvariant(), out property))
{
float newVolume = 0.0f;
try
{
newVolume = (float)op.GetValue();
newVolume = (float)property.GetValue();
}
catch
{
@@ -128,12 +128,7 @@ namespace Barotrauma.Items.Components
return 0.0f;
}
//public virtual void Draw(SpriteBatch spriteBatch, bool editing = false)
//{
// item.drawableComponents = Array.FindAll(item.drawableComponents, i => i != this);
//}
public virtual void DrawHUD(SpriteBatch spriteBatch, Character character) { }
public virtual void AddToGUIUpdateList() { }
@@ -10,7 +10,7 @@ namespace Barotrauma.Items.Components
private Color textColor;
[SerializableProperty("", true), Editable(100)]
[Serialize("", true), Editable(100)]
public string Text
{
get { return textBlock.Text.Replace("\n", ""); }
@@ -27,7 +27,7 @@ namespace Barotrauma.Items.Components
}
}
[Editable, SerializableProperty("0.0,0.0,0.0,1.0", true)]
[Editable, Serialize("0.0,0.0,0.0,1.0", true)]
public Color TextColor
{
get { return textColor; }
@@ -37,7 +37,7 @@ namespace Barotrauma.Items.Components
}
}
[Editable, SerializableProperty(1.0f, true)]
[Editable, Serialize(1.0f, true)]
public float TextScale
{
get { return textBlock == null ? 1.0f : textBlock.TextScale; }
@@ -12,7 +12,7 @@ using System.Linq;
namespace Barotrauma
{
partial class Item : MapEntity, IDamageable, IPropertyObject, IServerSerializable, IClientSerializable
partial class Item : MapEntity, IDamageable, ISerializableEntity, IServerSerializable, IClientSerializable
{
public override Sprite Sprite
{
@@ -145,12 +145,12 @@ namespace Barotrauma
public override void DrawEditing(SpriteBatch spriteBatch, Camera cam)
{
if (editingHUD != null) editingHUD.Draw(spriteBatch);
if (editingHUD != null && editingHUD.UserData == this) editingHUD.Draw(spriteBatch);
}
private GUIComponent CreateEditingHUD(bool inGame = false)
{
List<ObjectProperty> editableProperties = inGame ? GetProperties<InGameEditable>() : GetProperties<Editable>();
/*List<SerializableProperty> editableProperties = inGame ? GetProperties<InGameEditable>() : GetProperties<Editable>();
int requiredItemCount = 0;
if (!inGame)
@@ -159,27 +159,29 @@ namespace Barotrauma
{
requiredItemCount += ic.requiredItems.Count;
}
}
}*/
int width = 450;
int height = 80 + requiredItemCount * 20;
int height = 150;
int x = GameMain.GraphicsWidth / 2 - width / 2, y = 10;
foreach (var objectProperty in editableProperties)
/*foreach (var objectProperty in editableProperties)
{
var editable = objectProperty.Attributes.OfType<Editable>().FirstOrDefault();
if (editable != null) height += (int)(Math.Ceiling(editable.MaxLength / 40.0f) * 18.0f) + 5;
}
}*/
editingHUD = new GUIFrame(new Rectangle(x, y, width, height), "");
editingHUD.Padding = new Vector4(10, 10, 0, 0);
editingHUD = new GUIListBox(new Rectangle(x, y, width, height), "");
//editingHUD.Padding = new Vector4(10, 10, 0, 0);
editingHUD.UserData = this;
new GUITextBlock(new Rectangle(0, 0, 100, 20), prefab.Name, "",
Alignment.TopLeft, Alignment.TopLeft, editingHUD, false, GUI.LargeFont);
/*new GUITextBlock(new Rectangle(0, 0, 0, 20), prefab.Name, "",
Alignment.TopLeft, Alignment.TopLeft, editingHUD, false, GUI.LargeFont);*/
y += 25;
new SerializableEntityEditor(this, inGame, editingHUD);
if (!inGame)
//y += 25;
/*if (!inGame)
{
if (prefab.IsLinkable)
{
@@ -198,7 +200,7 @@ namespace Barotrauma
PropertyDescriptor property = properties.Find("JoinedNames", false);
namesBox.Text = relatedItem.JoinedNames;
namesBox.UserData = new ObjectProperty(property, relatedItem);
namesBox.UserData = new SerializableProperty(property, relatedItem);
namesBox.OnEnterPressed = EnterProperty;
namesBox.OnTextChanged = PropertyChanged;
@@ -206,9 +208,15 @@ namespace Barotrauma
}
}
if (requiredItemCount > 0) y += 10;
}*/
foreach (ItemComponent ic in components)
{
if (SerializableProperty.GetProperties<Editable>(ic).Count == 0) continue;
new SerializableEntityEditor(ic, inGame, editingHUD);
}
foreach (var objectProperty in editableProperties)
/*foreach (var objectProperty in editableProperties)
{
int boxHeight = 18;
var editable = objectProperty.Attributes.OfType<Editable>().FirstOrDefault();
@@ -272,8 +280,7 @@ namespace Barotrauma
}
y = y + boxHeight + 5;
}
}*/
return editingHUD;
}
@@ -346,10 +353,10 @@ namespace Barotrauma
private bool EnterProperty(GUITickBox tickBox)
{
var objectProperty = tickBox.UserData as ObjectProperty;
if (objectProperty == null) return false;
var property = tickBox.UserData as SerializableProperty;
if (property == null) return false;
objectProperty.TrySetValue(tickBox.Selected);
property.TrySetValue(tickBox.Selected);
return true;
}
@@ -358,24 +365,24 @@ namespace Barotrauma
{
textBox.Color = Color.DarkGreen;
var objectProperty = textBox.UserData as ObjectProperty;
if (objectProperty == null) return false;
var property = textBox.UserData as SerializableProperty;
if (property == null) return false;
object prevValue = objectProperty.GetValue();
object prevValue = property.GetValue();
textBox.Deselect();
if (objectProperty.TrySetValue(text))
if (property.TrySetValue(text))
{
textBox.Text = text;
if (GameMain.Server != null)
{
GameMain.Server.CreateEntityEvent(this, new object[] { NetEntityEvent.Type.ChangeProperty, objectProperty });
GameMain.Server.CreateEntityEvent(this, new object[] { NetEntityEvent.Type.ChangeProperty, property });
}
else if (GameMain.Client != null)
{
GameMain.Client.CreateEntityEvent(this, new object[] { NetEntityEvent.Type.ChangeProperty, objectProperty });
GameMain.Client.CreateEntityEvent(this, new object[] { NetEntityEvent.Type.ChangeProperty, property });
}
return true;
@@ -7,7 +7,7 @@ using System.Collections.Generic;
namespace Barotrauma
{
partial class Hull : MapEntity, IPropertyObject, IServerSerializable
partial class Hull : MapEntity, ISerializableEntity, IServerSerializable
{
public const int MaxDecalsPerHull = 10;
@@ -5,7 +5,7 @@ using System.Linq;
namespace Barotrauma.Networking
{
partial class GameServer : NetworkMember, IPropertyObject
partial class GameServer : NetworkMember, ISerializableEntity
{
private GUIFrame settingsFrame;
private GUIFrame[] settingsTabs;
@@ -0,0 +1,196 @@
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Barotrauma
{
class SerializableEntityEditor : GUIComponent
{
private GUIComponent editingHUD;
public SerializableEntityEditor(ISerializableEntity entity, bool inGame, GUIComponent parent) : base("")
{
List<SerializableProperty> editableProperties = inGame ?
SerializableProperty.GetProperties<InGameEditable>(entity) :
SerializableProperty.GetProperties<Editable>(entity);
/*int requiredItemCount = 0;
if (!inGame)
{
foreach (ItemComponent ic in components)
{
requiredItemCount += ic.requiredItems.Count;
}
}
foreach (var objectProperty in editableProperties)
{
var editable = objectProperty.Attributes.OfType<Editable>().FirstOrDefault();
if (editable != null) height += (int)(Math.Ceiling(editable.MaxLength / 40.0f) * 18.0f) + 5;
}*/
editingHUD = new GUIFrame(new Rectangle(0, 0, 0, 0), null, parent);
editingHUD.Padding = new Vector4(10, 10, 00, 20);
editingHUD.UserData = this;
new GUITextBlock(new Rectangle(0, 0, 100, 20), entity.Name, "",
Alignment.TopLeft, Alignment.TopLeft, editingHUD, false, GUI.Font);
int y = 20, padding = 5;
foreach (var property in editableProperties)
{
//int boxHeight = 18;
//var editable = property.Attributes.OfType<Editable>().FirstOrDefault();
//if (editable != null) boxHeight = (int)(Math.Ceiling(editable.MaxLength / 40.0f) * 18.0f);
object value = property.GetValue();
GUIComponent propertyField = null;
if (value is bool)
{
propertyField = CreateBoolField(entity, property, (bool)value, y, editingHUD);
}
else if (value.GetType().IsEnum)
{
propertyField = CreateEnumField(entity, property, value, y, editingHUD);
}
else if (value is string)
{
propertyField = CreateStringField(entity, property, (string)value, y, editingHUD);
}
else if (value is int)
{
propertyField = CreateIntField(entity, property, (int)value, y, editingHUD);
}
else if (value is float)
{
propertyField = CreateFloatField(entity, property, (float)value, y, editingHUD);
}
if (propertyField != null)
{
y += propertyField.Rect.Height + padding;
}
}
editingHUD.SetDimensions(new Point(editingHUD.Rect.Width, editingHUD.children.Sum(c => c.Rect.Height) + 10), false);
}
private GUIComponent CreateBoolField(ISerializableEntity entity, SerializableProperty property, bool value, int yPos, GUIComponent parent)
{
GUITickBox propertyTickBox = new GUITickBox(new Rectangle(10, yPos, 18, 18), property.Name, Alignment.Left, parent);
propertyTickBox.Font = GUI.SmallFont;
propertyTickBox.Selected = value;
propertyTickBox.OnSelected = (tickBox) =>
{
if (property.TrySetValue(tickBox.Selected))
{
TrySendNetworkUpdate(entity, property);
}
return true;
};
return propertyTickBox;
}
private GUIComponent CreateIntField(ISerializableEntity entity, SerializableProperty property, int value, int yPos, GUIComponent parent)
{
new GUITextBlock(new Rectangle(10, yPos, 100, 18), property.Name, "", parent, GUI.SmallFont);
GUINumberInput numberInput = new GUINumberInput(new Rectangle(180, yPos, 0, 18), "", Alignment.Left, null, null, parent);
numberInput.Font = GUI.SmallFont;
var editableAttribute = property.GetAttribute<Editable>();
numberInput.MinValue = editableAttribute.MinValueInt;
numberInput.MinValue = editableAttribute.MaxValueInt;
numberInput.OnValueChanged += (numInput, number) =>
{
if (property.TrySetValue(number))
{
TrySendNetworkUpdate(entity, property);
}
};
return numberInput;
}
private GUIComponent CreateFloatField(ISerializableEntity entity, SerializableProperty property, float value, int yPos, GUIComponent parent)
{
new GUITextBlock(new Rectangle(10, yPos, 100, 18), property.Name, "", parent, GUI.SmallFont);
GUINumberInput numberInput = new GUINumberInput(new Rectangle(180, yPos, 0, 18), "", Alignment.Left, null, null, parent);
numberInput.Font = GUI.SmallFont;
return numberInput;
}
private GUIComponent CreateEnumField(ISerializableEntity entity, SerializableProperty property, object value, int yPos, GUIComponent parent)
{
new GUITextBlock(new Rectangle(0, yPos, 100, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, editingHUD, false, GUI.SmallFont);
GUIDropDown enumDropDown = new GUIDropDown(new Rectangle(180, yPos, 0, 18), "", "", Alignment.TopLeft, parent);
foreach (object enumValue in Enum.GetValues(value.GetType()))
{
var enumTextBlock = new GUITextBlock(new Rectangle(0, 0, 200, 25), enumValue.ToString(), "", enumDropDown);
enumTextBlock.UserData = enumValue;
}
enumDropDown.OnSelected += (selected, val) =>
{
if (property.TrySetValue(val))
{
TrySendNetworkUpdate(entity, property);
}
return true;
};
enumDropDown.SelectItem(value);
return enumDropDown;
}
private GUIComponent CreateStringField(ISerializableEntity entity, SerializableProperty property, string value, int yPos, GUIComponent parent)
{
new GUITextBlock(new Rectangle(0, yPos, 100, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, parent, false, GUI.SmallFont);
GUITextBox propertyBox = new GUITextBox(new Rectangle(0, yPos, 250, 18), Alignment.Right, "", parent);
propertyBox.Font = GUI.SmallFont;
//if (boxHeight > 18) propertyBox.Wrap = true;
propertyBox.Text = value;
propertyBox.OnEnterPressed = (textBox, text) =>
{
if (property.TrySetValue(value))
{
TrySendNetworkUpdate(entity, property);
}
return true;
};
return propertyBox;
}
private void TrySendNetworkUpdate(ISerializableEntity entity, SerializableProperty property)
{
if (GameMain.Server != null)
{
IServerSerializable serverSerializable = entity as IServerSerializable;
if (serverSerializable != null)
{
GameMain.Server.CreateEntityEvent(serverSerializable, new object[] { NetEntityEvent.Type.ChangeProperty, property });
}
}
else if (GameMain.Client != null)
{
IClientSerializable clientSerializable = entity as IClientSerializable;
if (clientSerializable != null)
{
GameMain.Client.CreateEntityEvent(clientSerializable, new object[] { NetEntityEvent.Type.ChangeProperty, property });
}
}
}
}
}