Colony Tab Interface

Properties panel supports read/write properties
Colony tab is now populated with colony data when a colony is selected
This commit is contained in:
hyperion51 2020-06-01 03:11:04 +01:00
commit a99b4e82f3
30 changed files with 7598 additions and 3280 deletions

View file

@ -3,214 +3,255 @@ using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using JM.LinqFaster;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class PropertiesPanel : MonoBehaviour
{
public Prototype LabelField;
public Prototype FreeFloatField;
public Prototype RangedFloatField;
public Prototype EnumField;
public Prototype BoolField;
public Prototype ColorField;
public Prototype Divider;
public TextMeshProUGUI Title;
public RectTransform SectionPrefab;
public PropertiesList ListPrefab;
public PropertyLabel PropertyPrefab;
public AttributeProperty AttributePrefab;
public InputField InputField;
public RangedFloatField RangedFloatField;
public EnumField EnumField;
public BoolField BoolField;
public bool ChildRadioSelection;
public int ResolutionExponentMinumum = 6;
public int ResolutionExponentCount = 5;
protected List<GameObject> Properties = new List<GameObject>();
protected List<FlatFlatButton> Buttons = new List<FlatFlatButton>();
protected FlatFlatButton SelectedChild;
protected event Action RefreshPropertyValues;
private void Update()
{
RefreshValues();
}
public void Clear()
{
foreach(var property in Properties)
Destroy(property);
Properties.Clear();
Buttons.Clear();
SelectedChild = null;
RefreshPropertyValues = null;
}
public void Deselect()
{
if (SelectedChild != null)
SelectedChild.CurrentState = FlatButtonState.Unselected;
SelectedChild = null;
}
public RectTransform AddSection(string name)
{
var section = Instantiate(SectionPrefab, transform);
section.GetComponentInChildren<TextMeshProUGUI>().text = name;
Properties.Add(section.gameObject);
return section;
}
public PropertyLabel AddProperty(string name, Func<string> read = null, Action onClick = null)
{
var property = Instantiate(PropertyPrefab, transform);
property.Name.text = name;
if (ChildRadioSelection)
{
Buttons.Add(property.Button);
property.Button.OnClick += () =>
{
if (SelectedChild != null)
SelectedChild.CurrentState = FlatButtonState.Unselected;
SelectedChild = property.Button;
SelectedChild.CurrentState = FlatButtonState.Selected;
onClick?.Invoke();
};
}
else if(onClick!=null) property.Button.OnClick += onClick;
RefreshPropertyValues += () => property.Value.text = read?.Invoke() ?? "";
Properties.Add(property.gameObject);
return property;
}
public PropertiesList AddList(string name) //, IEnumerable<(string, Func<string>)> elements)
{
var list = Instantiate(ListPrefab, transform);
list.Title.text = name;
// foreach (var element in elements)
// {
// var item = Instantiate(PropertyPrefab, list);
// item.Name.text = element.Item1;
// item.Value.text = element.Item2();
// item.ValueFunction = element.Item2;
// Properties.Add(item.gameObject);
// }
RefreshPropertyValues += () => list.RefreshValues();
Properties.Add(list.gameObject);
return list;
}
public AttributeProperty AddPersonalityProperty(PersonalityAttribute attribute, Func<float> read)
{
var attributeInstance = Instantiate(AttributePrefab, transform);
attributeInstance.Title.text = attribute.Name;
attributeInstance.HighLabel.text = attribute.HighName;
attributeInstance.LowLabel.text = attribute.LowName;
RefreshPropertyValues += () => attributeInstance.Slider.value = read();
Properties.Add(attributeInstance.gameObject);
return attributeInstance;
}
private readonly List<Prototype> _propertyFields = new List<Prototype>();
private event EventHandler RefreshPropertyValues;
public void Clear()
public void AddProperty(string name, Func<string> read, Action<string> write)
{
foreach (var property in _propertyFields.ReverseF())
{
property.ReturnToPool();
}
RefreshPropertyValues = null;
var field = Instantiate(InputField, transform);
field.Label.text = name;
field.Field.contentType = TMP_InputField.ContentType.Standard;
field.Field.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += () => field.Field.text = read();
Properties.Add(field.gameObject);
}
public void Inspect(object obj, bool inspectablesOnly = false, bool readWrite = false, bool topLevel = false)
public void AddProperty(string name, Func<float> read, Action<float> write)
{
if(topLevel)
Clear();
var fields = obj.GetType().GetFields();
foreach (var field in fields)
Inspect(obj, field, inspectablesOnly, readWrite);
if(topLevel)
foreach (var field in _propertyFields)
field.transform.SetSiblingIndex(_propertyFields.IndexOf(field));
RefreshValues();
}
public void Inspect(object obj, FieldInfo field, bool inspectablesOnly = false, bool readWrite = false)
{
var inspectable = field.GetCustomAttribute<InspectableFieldAttribute>();
if (inspectable == null && inspectablesOnly) return;
var type = field.FieldType;
if (type == typeof(float))
{
if (readWrite)
{
var ranged = inspectable as RangedFloatInspectableAttribute;
if (ranged != null)
Inspect(field.Name.SplitCamelCase(), () => (float) field.GetValue(obj), f => field.SetValue(obj, f),
ranged.Min, ranged.Max);
else
Inspect(field.Name.SplitCamelCase(), () => (float) field.GetValue(obj), f => field.SetValue(obj, f));
} else Inspect(field.Name.SplitCamelCase(), () => ((float) field.GetValue(obj)).ToString("0.##"));
}
else if (type == typeof(int))
{
if (readWrite)
{
var ranged = inspectable as RangedIntInspectableAttribute;
if (ranged != null)
Inspect(field.Name.SplitCamelCase(), () => (int) field.GetValue(obj), f => field.SetValue(obj, f),
ranged.Min, ranged.Max);
else
Inspect(field.Name.SplitCamelCase(), () => (int) field.GetValue(obj), f => field.SetValue(obj, f));
} else Inspect(field.Name.SplitCamelCase(), () => ((int) field.GetValue(obj)).ToString());
}
else if (type.IsEnum)
{
if (readWrite)
Inspect(field.Name.SplitCamelCase(), () => (int) field.GetValue(obj), i => field.SetValue(obj, i),
Enum.GetNames(field.FieldType));
else
Inspect(field.Name.SplitCamelCase(), () => Enum.GetName(type, field.GetValue(obj)).SplitCamelCase());
}
else if (type == typeof(string))
{
if (readWrite)
Inspect(field.Name.SplitCamelCase(), () => (string) field.GetValue(obj), i => field.SetValue(obj, i));
else
Inspect(field.Name.SplitCamelCase(), () => (string) field.GetValue(obj));
}
//else if (field.FieldType == typeof(Color)) Inspect(field.Name, () => (Color) field.GetValue(obj), c => field.SetValue(obj, c));
else if (field.FieldType == typeof(bool)) Inspect(field.Name, () => (bool) field.GetValue(obj), b => field.SetValue(obj, b));
else if (type.GetCustomAttribute<InspectableFieldAttribute>() != null)
{
if(!_propertyFields.Any() || !_propertyFields.Last().gameObject.name.ToUpper().Contains("DIVIDER"))
_propertyFields.Add(Divider.Instantiate<Prototype>());
Inspect(field.GetValue(obj), inspectablesOnly, readWrite);
}
else
Debug.Log($"Field \"{field.Name}\" has unknown type {field.FieldType.Name}. No inspector was generated.");
}
public void Inspect(string name, Func<string> read)
{
var labelFieldInstance = LabelField.Instantiate<Prototype>();
_propertyFields.Add(labelFieldInstance);
labelFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
var field = labelFieldInstance.transform.Find("Field").GetComponent<TextMeshProUGUI>();
field.text = read();
RefreshPropertyValues += (sender, args) => field.text = read();
}
public void Inspect(string name, Func<float> read, Action<float> write)
{
var freeFloatFieldInstance = FreeFloatField.Instantiate<Prototype>();
_propertyFields.Add(freeFloatFieldInstance);
freeFloatFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
var inputField = freeFloatFieldInstance.GetComponentInChildren<TMP_InputField>();
inputField.contentType = TMP_InputField.ContentType.DecimalNumber;
inputField.onValueChanged.AddListener(val => write(float.Parse(val)));
RefreshPropertyValues += (sender, args) => inputField.text = read().ToString(CultureInfo.InvariantCulture);
}
public void Inspect(string name, Func<string> read, Action<string> write)
{
var freeFloatFieldInstance = FreeFloatField.Instantiate<Prototype>();
_propertyFields.Add(freeFloatFieldInstance);
freeFloatFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
var inputField = freeFloatFieldInstance.GetComponentInChildren<TMP_InputField>();
inputField.contentType = TMP_InputField.ContentType.Standard;
inputField.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += (sender, args) => inputField.text = read();
var field = Instantiate(InputField, transform);
field.Label.text = name;
field.Field.contentType = TMP_InputField.ContentType.DecimalNumber;
field.Field.onValueChanged.AddListener(val => write(float.Parse(val)));
RefreshPropertyValues += () => field.Field.text = read().ToString(CultureInfo.InvariantCulture);
Properties.Add(field.gameObject);
}
public void Inspect(string name, Func<float> read, Action<float> write, float min, float max)
public void AddProperty(string name, Func<int> read, Action<int> write)
{
var rangedFloatFieldInstance = RangedFloatField.Instantiate<Prototype>();
_propertyFields.Add(rangedFloatFieldInstance);
rangedFloatFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
var slider = rangedFloatFieldInstance.GetComponentInChildren<Slider>();
slider.minValue = min;
slider.maxValue = max;
slider.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += (sender, args) => slider.value = read();
var field = Instantiate(InputField, transform);
field.Label.text = name;
field.Field.contentType = TMP_InputField.ContentType.IntegerNumber;
field.Field.onValueChanged.AddListener(val => write(int.Parse(val)));
RefreshPropertyValues += () => field.Field.text = read().ToString(CultureInfo.InvariantCulture);
Properties.Add(field.gameObject);
}
public void AddProperty(string name, Func<float> read, Action<float> write, float min, float max)
{
var field = Instantiate(RangedFloatField, transform);
field.Label.text = name;
field.Slider.wholeNumbers = false;
field.Slider.minValue = min;
field.Slider.maxValue = max;
field.Slider.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += () => field.Slider.value = read();
Properties.Add(field.gameObject);
}
public void AddProperty(string name, Func<int> read, Action<int> write, int min, int max)
{
var field = Instantiate(RangedFloatField, transform);
field.Label.text = name;
field.Slider.wholeNumbers = true;
field.Slider.minValue = min;
field.Slider.maxValue = max;
field.Slider.onValueChanged.AddListener(val => write(Mathf.RoundToInt(val)));
RefreshPropertyValues += () => field.Slider.value = read();
Properties.Add(field.gameObject);
}
public void AddProperty(string name, Func<bool> read, Action<bool> write)
{
var field = Instantiate(BoolField, transform);
field.Label.text = name;
field.Toggle.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += () => field.Toggle.isOn = read();
Properties.Add(field.gameObject);
}
public void AddProperty(string name, Func<int> read, Action<int> write, string[] enumOptions)
{
var field = Instantiate(EnumField, transform);
field.Label.text = name;
field.Dropdown.options = enumOptions.Select(s => new TMP_Dropdown.OptionData(s)).ToList();
field.Dropdown.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += () => field.Dropdown.value = read();
Properties.Add(field.gameObject);
}
public void Inspect(string name, Func<int> read, Action<int> write)
{
var freeFloatFieldInstance = FreeFloatField.Instantiate<Prototype>();
_propertyFields.Add(freeFloatFieldInstance);
freeFloatFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
var inputField = freeFloatFieldInstance.GetComponentInChildren<TMP_InputField>();
inputField.contentType = TMP_InputField.ContentType.IntegerNumber;
inputField.onValueChanged.AddListener(val => write(int.Parse(val)));
RefreshPropertyValues += (sender, args) => inputField.text = read().ToString(CultureInfo.InvariantCulture);
}
public void Inspect(string name, Func<int> read, Action<int> write, int min, int max)
{
var rangedFloatFieldInstance = RangedFloatField.Instantiate<Prototype>();
_propertyFields.Add(rangedFloatFieldInstance);
rangedFloatFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
var slider = rangedFloatFieldInstance.GetComponentInChildren<Slider>();
slider.wholeNumbers = true;
slider.minValue = min;
slider.maxValue = max;
slider.onValueChanged.AddListener(val => write(Mathf.RoundToInt(val)));
RefreshPropertyValues += (sender, args) => slider.value = read();
}
// public void Inspect(string name, Func<Color> read, Action<Color> write)
// public void Inspect(object obj, bool inspectablesOnly = false, bool readWrite = false, bool topLevel = true)
// {
// var colorFieldInstance = ColorField.Instantiate<Prototype>();
// _propertyFields.Add(colorFieldInstance);
// colorFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
// var colorButton = colorFieldInstance.GetComponentInChildren<ColorButton>();
// colorButton.OnColorChanged.AddListener(col => write(col));
// RefreshPropertyValues += (sender, args) => colorButton.GetComponent<Image>().color = read();
// if(topLevel)
// Clear();
//
// var fields = obj.GetType().GetFields();
// foreach (var field in fields)
// Inspect(obj, field, inspectablesOnly, readWrite);
//
// if(topLevel)
// foreach (var field in _propertyFields)
// field.transform.SetSiblingIndex(_propertyFields.IndexOf(field));
//
// RefreshValues();
// }
//
// public void Inspect(object obj, FieldInfo field, bool inspectablesOnly = false, bool readWrite = false)
// {
// var inspectable = field.GetCustomAttribute<InspectableFieldAttribute>();
// if (inspectable == null && inspectablesOnly) return;
// var type = field.FieldType;
//
// if (type == typeof(float))
// {
// if (readWrite)
// {
// var ranged = inspectable as RangedFloatInspectableAttribute;
// if (ranged != null)
// Inspect(field.Name.SplitCamelCase(), () => (float) field.GetValue(obj), f => field.SetValue(obj, f),
// ranged.Min, ranged.Max);
// else
// Inspect(field.Name.SplitCamelCase(), () => (float) field.GetValue(obj), f => field.SetValue(obj, f));
// } else Inspect(field.Name.SplitCamelCase(), () => ((float) field.GetValue(obj)).ToString("0.##"));
// }
// else if (type == typeof(int))
// {
// if (readWrite)
// {
// var ranged = inspectable as RangedIntInspectableAttribute;
// if (ranged != null)
// Inspect(field.Name.SplitCamelCase(), () => (int) field.GetValue(obj), f => field.SetValue(obj, f),
// ranged.Min, ranged.Max);
// else
// Inspect(field.Name.SplitCamelCase(), () => (int) field.GetValue(obj), f => field.SetValue(obj, f));
// } else Inspect(field.Name.SplitCamelCase(), () => ((int) field.GetValue(obj)).ToString());
// }
// else if (type.IsEnum)
// {
// if (readWrite)
// Inspect(field.Name.SplitCamelCase(), () => (int) field.GetValue(obj), i => field.SetValue(obj, i),
// Enum.GetNames(field.FieldType));
// else
// Inspect(field.Name.SplitCamelCase(), () => Enum.GetName(type, field.GetValue(obj)).SplitCamelCase());
// }
// else if (type == typeof(string))
// {
// if (readWrite)
// Inspect(field.Name.SplitCamelCase(), () => (string) field.GetValue(obj), i => field.SetValue(obj, i));
// else
// Inspect(field.Name.SplitCamelCase(), () => (string) field.GetValue(obj));
// }
// //else if (field.FieldType == typeof(Color)) Inspect(field.Name, () => (Color) field.GetValue(obj), c => field.SetValue(obj, c));
// else if (field.FieldType == typeof(bool)) Inspect(field.Name, () => (bool) field.GetValue(obj), b => field.SetValue(obj, b));
// else if (type.GetCustomAttribute<InspectableFieldAttribute>() != null)
// {
// if(!_propertyFields.Any() || !_propertyFields.Last().gameObject.name.ToUpper().Contains("DIVIDER"))
// _propertyFields.Add(Divider.Instantiate<Prototype>());
// Inspect(field.GetValue(obj), inspectablesOnly, readWrite, false);
// }
// else
// Debug.Log($"Field \"{field.Name}\" has unknown type {field.FieldType.Name}. No inspector was generated.");
// }
public void Inspect(string name, Func<bool> read, Action<bool> write)
{
var boolFieldInstance = BoolField.Instantiate<Prototype>();
_propertyFields.Add(boolFieldInstance);
boolFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
var toggle = boolFieldInstance.GetComponentInChildren<Toggle>();
toggle.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += (sender, args) => toggle.isOn = read();
}
public void Inspect(string name, Func<int> read, Action<int> write, string[] enumOptions)
{
var enumFieldInstance = EnumField.Instantiate<Prototype>();
_propertyFields.Add(enumFieldInstance);
enumFieldInstance.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = name;
var dropDown = enumFieldInstance.GetComponentInChildren<TMP_Dropdown>();
dropDown.options = enumOptions.Select(s => new TMP_Dropdown.OptionData(s)).ToList();
dropDown.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += (sender, args) => dropDown.value = read();
}
public void RefreshValues()
{
RefreshPropertyValues?.Invoke(this, EventArgs.Empty);
RefreshPropertyValues?.Invoke();
}
}
}