Properties Panel & Load Testing

Properties panel elements now have dynamic layout
Added console command to spawn lots of entities for load testing
This commit is contained in:
hyperion51 2020-06-04 01:36:55 +01:00
commit b0d7fa4904
69 changed files with 5299 additions and 3229 deletions

View file

@ -38,8 +38,13 @@ public class ColonyTab : MonoBehaviour
_selectedHardpoint = null;
var entity = Context.Cache.Get<Entity>(_selectedColony);
entity.OnInventoryUpdate += () => PopulateScreen(entity);
PopulateScreen(entity);
Title.text = entity.Name;
UpdateGeneral(entity);
entity.OnInventoryUpdate += () => UpdateInventory(entity);
UpdateInventory(entity);
}
private void OnDisable()
@ -48,32 +53,36 @@ public class ColonyTab : MonoBehaviour
entity.ClearInventoryListeners();
}
public void PopulateScreen(Entity entity){
Title.text = entity.Name;
public void UpdateGeneral(Entity entity)
{
General.Clear();
Inventory.Clear();
Details.Clear();
var corporation = Context.Cache.Get<Corporation>(entity.Corporation);
var blueprints = corporation.UnlockedBlueprints.Select(id => Context.Cache.Get<BlueprintData>(id));
var hull = Context.Cache.Get<Gear>(entity.Hull);
var hullData = Context.Cache.Get<HullData>(hull.Data);
var capacity = Context.Evaluate(hullData.Capacity, hull, entity);
General.AddProperty("Capacity", () => $"{entity.OccupiedCapacity}/{capacity:0}");
General.AddProperty("Capacity", () => $"{entity.OccupiedCapacity}/{entity.Capacity:0}");
General.AddProperty("Mass", () => $"{entity.Mass.SignificantDigits(Context.GlobalData.SignificantDigits)}");
General.AddProperty("Temperature", () => $"{entity.Temperature:0}°K");
General.AddProperty("Energy", () => $"{entity.Energy:0}/{entity.GetBehaviors<Reactor>().First().Capacitance:0}");
General.AddProperty("Population", () => $"{entity.Population}");
General.AddSection("Personality");
foreach (var attribute in entity.Personality)
General.AddPersonalityProperty(Context.Cache.Get<PersonalityAttribute>(attribute.Key),
() => attribute.Value);
var personalityList = General.AddList("Personality");
foreach (var attribute in entity.Personality.Keys)
personalityList.AddPersonalityProperty(Context.Cache.Get<PersonalityAttribute>(attribute),
() => entity.Personality[attribute]);
General.RefreshValues();
}
public void UpdateInventory(Entity entity)
{
Inventory.Clear();
var corporation = Context.Cache.Get<Corporation>(entity.Corporation);
var blueprints = corporation.UnlockedBlueprints.Select(id => Context.Cache.Get<BlueprintData>(id));
var hull = Context.Cache.Get<Gear>(entity.Hull);
var hullData = Context.Cache.Get<HullData>(hull.Data);
// Create item data mapping for cargo similar to what Entity.GearData already does for equipped items
var cargoData = entity.Cargo.ToDictionary(id => _context.Cache.Get<ItemInstance>(id),
id => Context.Cache.Get<ItemData>(_context.Cache.Get<ItemInstance>(id).Data));
//var incompleteGear = entity.IncompleteGear.Keys.Select(id => _context.Cache.Get<Gear>(id));
// var incompleteGear = entity.IncompleteGear.Select(x =>
// {
@ -82,21 +91,35 @@ public class ColonyTab : MonoBehaviour
// var progress = (productionTime - (float) x.Value) / productionTime;
// return (gear, progress);
// });
Inventory.AddSection("Gear");
var gearList = new List<Gear>(entity.GearData.Keys.Concat(entity.IncompleteGear.Keys.Select(id => _context.Cache.Get<Gear>(id))));
var gearList = Inventory.AddList("Gear");
// Create temporary list including both equipped items and incomplete items
// This is necessary because the binding between items and hardpoints is implicit
var equippedItems = new List<Gear>(entity.GearData.Keys.Concat(entity.IncompleteGear.Keys.Select(id => _context.Cache.Get<Gear>(id))));
// Display an entry in the list for each hardpoint on the entity
foreach (var hardpoint in hullData.Hardpoints)
{
var match = gearList.FirstOrDefault(g => g.ItemData.HardpointType == hardpoint.Type);
// This corresponds to the first equipped or incomplete gear which matches the hardpoint type
var match = equippedItems.FirstOrDefault(g => g.ItemData.HardpointType == hardpoint.Type);
// These are the items in the inventory which can be equipped to the current hardpoint
var matchingCargoGear = cargoData
.Where(x => x.Value is EquippableItemData equippableItemData &&
equippableItemData.HardpointType == hardpoint.Type).Select(x=>x.Key as Gear)
.Where(x =>
x.Value is EquippableItemData equippableItemData &&
equippableItemData.HardpointType == hardpoint.Type)
.Select(x => x.Key as Gear)
.ToArray();
// There is no item in the inventory which can fill the current hardpoint
if (match == null)
{
var matchingBlueprints = blueprints.Where(bp =>
bp.FactoryItem == Guid.Empty &&
Context.Cache.Get<ItemData>(bp.Item) is GearData gearData &&
gearData.HardpointType == hardpoint.Type).ToArray();
Action<PointerEventData> onClick = null;
if (matchingCargoGear.Any() || matchingBlueprints.Any())
onClick = data =>
@ -134,13 +157,13 @@ public class ColonyTab : MonoBehaviour
ContextMenu.Show();
}
};
Inventory.AddProperty($"Empty {Enum.GetName(typeof(HardpointType), hardpoint.Type)} Hardpoint", null, onClick, matchingBlueprints.Any());
gearList.AddProperty($"Empty {Enum.GetName(typeof(HardpointType), hardpoint.Type)} Hardpoint", onClick, matchingBlueprints.Any());
}
else
{
if (entity.IncompleteGear.ContainsKey(match.ID))
{
Inventory.AddProgressField(match.Name, () =>
gearList.AddProgressField(match.Name, () =>
{
var productionTime = _context.Cache.Get<BlueprintData>(match.Blueprint).ProductionTime;
return (productionTime - (float) entity.IncompleteGear[match.ID]) / productionTime;
@ -148,7 +171,7 @@ public class ColonyTab : MonoBehaviour
}
else
{
var prop = Inventory.AddProperty(match.Name, null, _ =>
var prop = gearList.AddProperty(match.Name, null, _ =>
{
_selectedHardpoint = hardpoint;
PopulateDetails(match, entity);
@ -158,21 +181,21 @@ public class ColonyTab : MonoBehaviour
}
gearList.Remove(match);
equippedItems.Remove(match);
}
}
if(!entity.Cargo.Any())
Inventory.AddSection("No Cargo");
Inventory.AddProperty("No Cargo");
else
{
Inventory.AddSection("Cargo");
var cargoList = Inventory.AddList("Cargo");
foreach (var x in cargoData)
{
if(x.Key is SimpleCommodity simpleCommodity)
Inventory.AddProperty(x.Value.Name, () => simpleCommodity.Quantity.ToString());
cargoList.AddProperty(x.Value.Name, () => simpleCommodity.Quantity.ToString());
else
Inventory.AddProperty(x.Value.Name);
cargoList.AddProperty(x.Value.Name);
}
}
Inventory.RefreshValues();

View file

@ -19,25 +19,28 @@ public class PropertiesList : PropertiesPanel
private float _targetFoldoutRotation = 0;
private float _foldoutRotation = 0;
public bool Expanded
{
set
{
_expanded = value;
var padding = LayoutGroup.padding;
padding = new RectOffset(padding.left, padding.right, padding.top, _expanded ? ExpandedPadding : FoldedPadding);
LayoutGroup.padding = padding;
foreach (var property in Properties) property.SetActive(_expanded);
_targetFoldoutRotation = _expanded ? -90 : 0;
}
}
public void Start()
{
Button.OnClick += _ => ToggleExpand();
SetExpanded(true, true);
}
public void ToggleExpand() => Expanded = !_expanded;
public void ToggleExpand() => SetExpanded(!_expanded, false);
public void SetExpanded(bool expanded, bool force)
{
_expanded = expanded;
var padding = LayoutGroup.padding;
padding = new RectOffset(padding.left, padding.right, padding.top, _expanded ? ExpandedPadding : FoldedPadding);
LayoutGroup.padding = padding;
foreach (var property in Properties) property.SetActive(_expanded);
_targetFoldoutRotation = _expanded ? -90 : 0;
if (force)
{
_foldoutRotation = _targetFoldoutRotation;
FoldoutIcon.transform.localRotation = Quaternion.Euler(0,0, _foldoutRotation);
}
}
private void Update()
{
@ -46,31 +49,31 @@ public class PropertiesList : PropertiesPanel
FoldoutIcon.transform.localRotation = Quaternion.Euler(0,0, _foldoutRotation);
}
public override PropertyLabel AddProperty(string name, Func<string> read = null, Action<PointerEventData> onClick = null, bool radio = false)
{
var prop = base.AddProperty(name, read, onClick, radio);
prop.gameObject.SetActive(false);
return prop;
}
public override PropertiesList AddList(string name)
{
var list = base.AddList(name);
list.gameObject.SetActive(false);
return list;
}
public override RectTransform AddSection(string name)
{
var section = base.AddSection(name);
section.gameObject.SetActive(false);
return section;
}
public override PropertyButton AddButton(string name, Action<PointerEventData> onClick)
{
var button = base.AddButton(name, onClick);
button.gameObject.SetActive(false);
return button;
}
// public override PropertyLabel AddProperty(string name, Func<string> read = null, Action<PointerEventData> onClick = null, bool radio = false)
// {
// var prop = base.AddProperty(name, read, onClick, radio);
// prop.gameObject.SetActive(false);
// return prop;
// }
//
// public override PropertiesList AddList(string name)
// {
// var list = base.AddList(name);
// list.gameObject.SetActive(false);
// return list;
// }
//
// public override RectTransform AddSection(string name)
// {
// var section = base.AddSection(name);
// section.gameObject.SetActive(false);
// return section;
// }
//
// public override PropertyButton AddButton(string name, Action<PointerEventData> onClick)
// {
// var button = base.AddButton(name, onClick);
// button.gameObject.SetActive(false);
// return button;
// }
}

View file

@ -12,7 +12,8 @@ public class PropertiesPanel : MonoBehaviour
public TextMeshProUGUI Title;
public RectTransform SectionPrefab;
public PropertiesList ListPrefab;
public PropertyLabel PropertyPrefab;
public PropertyButton PropertyPrefab;
public PropertyLabel PropertyLabelPrefab;
public AttributeProperty AttributePrefab;
public InputField InputField;
public RangedFloatField RangedFloatField;
@ -52,7 +53,7 @@ public class PropertiesPanel : MonoBehaviour
SelectedChild = null;
}
public virtual RectTransform AddSection(string name)
public RectTransform AddSection(string name)
{
var section = Instantiate(SectionPrefab, transform);
section.GetComponentInChildren<TextMeshProUGUI>().text = name;
@ -60,9 +61,9 @@ public class PropertiesPanel : MonoBehaviour
return section;
}
public virtual PropertyLabel AddProperty(string name, Func<string> read = null, Action<PointerEventData> onClick = null, bool radio = false)
public PropertyLabel AddProperty(string name, Func<string> read, Action<PointerEventData> onClick = null, bool radio = false)
{
var property = Instantiate(PropertyPrefab, transform);
var property = Instantiate(PropertyLabelPrefab, transform);
property.Name.text = name;
if (radio)
{
@ -86,7 +87,32 @@ public class PropertiesPanel : MonoBehaviour
return property;
}
public virtual PropertiesList AddList(string name) //, IEnumerable<(string, Func<string>)> elements)
public PropertyButton AddProperty(string name, Action<PointerEventData> onClick = null, bool radio = false)
{
var property = Instantiate(PropertyPrefab, transform);
property.Label.text = name;
if (radio)
{
RadioSelection = true;
Buttons.Add(property.Button);
property.Button.OnClick += data =>
{
if (data.button == PointerEventData.InputButton.Left)
{
if (SelectedChild != null)
SelectedChild.CurrentState = FlatButtonState.Unselected;
SelectedChild = property.Button;
SelectedChild.CurrentState = FlatButtonState.Selected;
}
onClick?.Invoke(data);
};
}
else if(onClick!=null) property.Button.OnClick += onClick;
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;