Child Inventory Drag & Drop
Added child selection and child inventory panel to colony tab, and implemented drag and drop functionality between parent and child inventories
This commit is contained in:
parent
5766fe8eba
commit
7b3c0cad64
16 changed files with 1101 additions and 333 deletions
|
|
@ -6,12 +6,15 @@ using System.Reflection;
|
|||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
public class ColonyTab : MonoBehaviour
|
||||
{
|
||||
public TextMeshProUGUI Title;
|
||||
public PropertiesPanel General;
|
||||
public PropertiesPanel Inventory;
|
||||
public PropertiesPanel ChildInventory;
|
||||
public PropertiesPanel Details;
|
||||
public ContextMenu ContextMenu;
|
||||
|
||||
|
|
@ -24,218 +27,337 @@ public class ColonyTab : MonoBehaviour
|
|||
_context = value;
|
||||
General.Context = value;
|
||||
Inventory.Context = value;
|
||||
ChildInventory.Context = value;
|
||||
Details.Context = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Guid _selectedColony;
|
||||
private GameContext _context;
|
||||
private Hardpoint _selectedHardpoint;
|
||||
private IDynamicProperties HardpointChange;
|
||||
private IDynamicProperties CargoChange;
|
||||
private Action OnHardpointsChanged;
|
||||
private Action OnCargoChanged;
|
||||
private Entity _parentEntity;
|
||||
private Entity _selectedChild;
|
||||
|
||||
private IChangeSource _colonyGearChange;
|
||||
private IChangeSource _colonyCargoChange;
|
||||
private Action _onColonyInventoryChanged;
|
||||
|
||||
private IChangeSource _childGearChange;
|
||||
private IChangeSource _childCargoChange;
|
||||
private Action _onChildInventoryChanged;
|
||||
|
||||
public void Open(Guid colony)
|
||||
{
|
||||
_selectedColony = colony;
|
||||
_selectedHardpoint = null;
|
||||
|
||||
var entity = Context.Cache.Get<Entity>(_selectedColony);
|
||||
_parentEntity = Context.Cache.Get<Entity>(colony);
|
||||
|
||||
Title.text = entity.Name;
|
||||
Title.text = _parentEntity.Name;
|
||||
|
||||
UpdateGeneral(entity);
|
||||
UpdateGeneral(_parentEntity);
|
||||
|
||||
UpdateInventory(entity);
|
||||
UpdateInventory(_parentEntity, Inventory);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
HardpointChange.OnChanged -= OnHardpointsChanged;
|
||||
CargoChange.OnChanged -= OnCargoChanged;
|
||||
if (_onColonyInventoryChanged == null)
|
||||
return;
|
||||
_colonyGearChange.OnChanged -= _onColonyInventoryChanged;
|
||||
_colonyCargoChange.OnChanged -= _onColonyInventoryChanged;
|
||||
Details.Clear();
|
||||
DeselectChild();
|
||||
}
|
||||
|
||||
private void DeselectChild()
|
||||
{
|
||||
if (_onChildInventoryChanged == null)
|
||||
return;
|
||||
_childGearChange.OnChanged -= _onChildInventoryChanged;
|
||||
_childGearChange.OnChanged -= _onChildInventoryChanged;
|
||||
ChildInventory.Clear();
|
||||
}
|
||||
|
||||
public void UpdateGeneral(Entity entity)
|
||||
{
|
||||
General.Clear();
|
||||
|
||||
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.AddEntityProperties(entity);
|
||||
General.AddProperty("Assigned Population", () => $"{entity.AssignedPopulation}/{entity.Population}");
|
||||
|
||||
var personalityList = General.AddList("Personality");
|
||||
foreach (var attribute in entity.Personality.Keys)
|
||||
personalityList.AddPersonalityProperty(Context.Cache.Get<PersonalityAttribute>(attribute),
|
||||
() => entity.Personality[attribute]);
|
||||
personalityList.SetExpanded(false, true);
|
||||
|
||||
var childList = General.AddList("Children");
|
||||
childList.InspectChildren(entity, child =>
|
||||
{
|
||||
_selectedChild = child;
|
||||
UpdateInventory(child, ChildInventory);
|
||||
});
|
||||
childList.SetExpanded(false, true);
|
||||
|
||||
General.RefreshValues();
|
||||
}
|
||||
|
||||
public void UpdateInventory(Entity entity)
|
||||
public void UpdateInventory(Entity entity, PropertiesPanel panel)
|
||||
{
|
||||
if (panel == Inventory)
|
||||
{
|
||||
_colonyGearChange = entity.GearEvent;
|
||||
_colonyCargoChange = entity.CargoEvent;
|
||||
_onColonyInventoryChanged = () => RefreshInventory(entity, panel);
|
||||
_colonyGearChange.OnChanged += _onColonyInventoryChanged;
|
||||
_colonyCargoChange.OnChanged += _onColonyInventoryChanged;
|
||||
RefreshInventory(entity, panel);
|
||||
}
|
||||
else
|
||||
{
|
||||
DeselectChild();
|
||||
_childGearChange = entity.GearEvent;
|
||||
_childCargoChange = entity.CargoEvent;
|
||||
_onChildInventoryChanged = () => RefreshInventory(entity, panel, true);
|
||||
_childGearChange.OnChanged += _onChildInventoryChanged;
|
||||
_childCargoChange.OnChanged += _onChildInventoryChanged;
|
||||
RefreshInventory(entity, panel, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void RefreshInventory(Entity entity, PropertiesPanel panel, bool child = false)
|
||||
{
|
||||
//Debug.Log("Refreshing Inventory!");
|
||||
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);
|
||||
|
||||
OnHardpointsChanged = RefreshInventory;
|
||||
HardpointChange = entity.GearEvent;
|
||||
HardpointChange.OnChanged += OnHardpointsChanged;
|
||||
OnCargoChanged = RefreshInventory;
|
||||
CargoChange = entity.CargoEvent;
|
||||
CargoChange.OnChanged += OnCargoChanged;
|
||||
|
||||
RefreshInventory();
|
||||
|
||||
void RefreshInventory()
|
||||
// Create item data mapping for cargo
|
||||
var cargoData = entity.Cargo.ToDictionary(id => _context.Cache.Get<ItemInstance>(id),
|
||||
id => Context.Cache.Get<ItemData>(_context.Cache.Get<ItemInstance>(id).Data));
|
||||
|
||||
panel.Clear();
|
||||
|
||||
if (child)
|
||||
{
|
||||
//Debug.Log("Refreshing Inventory!");
|
||||
panel.Title.text = entity.Name;
|
||||
panel.AddSection("General");
|
||||
panel.AddEntityProperties(entity);
|
||||
}
|
||||
|
||||
panel.AddSection("Hardpoints");
|
||||
var incompleteGearList = entity.IncompleteGear.Keys
|
||||
.Select(id => Context.Cache.Get<Gear>(id))
|
||||
.ToList();
|
||||
// Skip first hardpoint, that one is always the hull
|
||||
foreach (var hardpoint in entity.Hardpoints.Skip(1))
|
||||
{
|
||||
// These are the items in the inventory which can be equipped to the current hardpoint
|
||||
var matchingCargoGear = cargoData
|
||||
.Where(x =>
|
||||
x.Value is GearData gearData &&
|
||||
gearData.HardpointType == hardpoint.HardpointData.Type)
|
||||
.Select(x => x.Key as Gear)
|
||||
.ToArray();
|
||||
|
||||
// Create item data mapping for cargo
|
||||
var cargoData = entity.Cargo.ToDictionary(id => _context.Cache.Get<ItemInstance>(id),
|
||||
id => Context.Cache.Get<ItemData>(_context.Cache.Get<ItemInstance>(id).Data));
|
||||
|
||||
Inventory.Clear();
|
||||
Inventory.AddSection("Hardpoints");
|
||||
var incompleteGearList = entity.IncompleteGear.Keys
|
||||
.Select(id => Context.Cache.Get<Gear>(id))
|
||||
.ToList();
|
||||
// Skip first hardpoint, that one is always the hull
|
||||
foreach (var hardpoint in entity.Hardpoints.Skip(1))
|
||||
// Hardpoint has no gear assigned to it
|
||||
if (hardpoint.Gear == null)
|
||||
{
|
||||
// These are the items in the inventory which can be equipped to the current hardpoint
|
||||
var matchingCargoGear = cargoData
|
||||
.Where(x =>
|
||||
x.Value is GearData gearData &&
|
||||
gearData.HardpointType == hardpoint.HardpointData.Type)
|
||||
.Select(x => x.Key as Gear)
|
||||
.ToArray();
|
||||
|
||||
// Hardpoint has no gear assigned to it
|
||||
if (hardpoint.Gear == null)
|
||||
// Check whether we're currently building some gear for this hardpoint
|
||||
var incompleteGear = incompleteGearList.FirstOrDefault(g => g.ItemData.HardpointType == hardpoint.HardpointData.Type);
|
||||
|
||||
// We're building something for this hardpoint, show a progress bar!
|
||||
if (incompleteGear != null)
|
||||
{
|
||||
// Check whether we're currently building some gear for this hardpoint
|
||||
var incompleteGear = incompleteGearList.FirstOrDefault(g => g.ItemData.HardpointType == hardpoint.HardpointData.Type);
|
||||
incompleteGearList.Remove(incompleteGear);
|
||||
panel.AddProgressField(incompleteGear.Name, () =>
|
||||
{
|
||||
var productionTime = _context.Cache.Get<BlueprintData>(incompleteGear.Blueprint).ProductionTime;
|
||||
return (productionTime - (float) entity.IncompleteGear[incompleteGear.ID]) / productionTime;
|
||||
});
|
||||
}
|
||||
else // We're not building anything
|
||||
{
|
||||
// These are the blueprints we could build onto this hardpoint directly
|
||||
var matchingBlueprints = blueprints.Where(bp =>
|
||||
bp.FactoryItem == Guid.Empty &&
|
||||
Context.Cache.Get<ItemData>(bp.Item) is GearData gearData &&
|
||||
gearData.HardpointType == hardpoint.HardpointData.Type).ToArray();
|
||||
|
||||
// We're building something for this hardpoint, show a progress bar!
|
||||
if (incompleteGear != null)
|
||||
{
|
||||
incompleteGearList.Remove(incompleteGear);
|
||||
Inventory.AddProgressField(incompleteGear.Name, () =>
|
||||
Action<PointerEventData> onClick = null;
|
||||
if (matchingCargoGear.Any() || matchingBlueprints.Any())
|
||||
onClick = data =>
|
||||
{
|
||||
var productionTime = _context.Cache.Get<BlueprintData>(incompleteGear.Blueprint).ProductionTime;
|
||||
return (productionTime - (float) entity.IncompleteGear[incompleteGear.ID]) / productionTime;
|
||||
});
|
||||
}
|
||||
else // We're not building anything
|
||||
{
|
||||
// These are the blueprints we could build onto this hardpoint directly
|
||||
var matchingBlueprints = blueprints.Where(bp =>
|
||||
bp.FactoryItem == Guid.Empty &&
|
||||
Context.Cache.Get<ItemData>(bp.Item) is GearData gearData &&
|
||||
gearData.HardpointType == hardpoint.HardpointData.Type).ToArray();
|
||||
|
||||
Action<PointerEventData> onClick = null;
|
||||
if (matchingCargoGear.Any() || matchingBlueprints.Any())
|
||||
onClick = data =>
|
||||
if (data.button == PointerEventData.InputButton.Left)
|
||||
PopulateDetails(entity, matchingBlueprints);
|
||||
|
||||
else if (data.button == PointerEventData.InputButton.Right)
|
||||
{
|
||||
if (data.button == PointerEventData.InputButton.Left)
|
||||
PopulateDetails(entity, matchingBlueprints);
|
||||
|
||||
else if (data.button == PointerEventData.InputButton.Right)
|
||||
{
|
||||
ContextMenu.gameObject.SetActive(true);
|
||||
ContextMenu.Clear();
|
||||
if (matchingCargoGear.Any())
|
||||
ContextMenu.AddDropdown("Install", matchingCargoGear
|
||||
.Select<Gear, (string, Action, bool)>(gear => (gear.Name, () => entity.Equip(gear, hardpoint), true)));
|
||||
if (matchingBlueprints.Any())
|
||||
ContextMenu.AddDropdown("Build", matchingBlueprints
|
||||
.Select<BlueprintData, (string, Action, bool)>(blueprint =>
|
||||
(blueprint.Name, () =>
|
||||
entity.Build(blueprint, 1, _context.Cache.Get<ItemData>(blueprint.Item).Name, true),
|
||||
entity.GetBlueprintIngredients(blueprint, out _, out _))));
|
||||
ContextMenu.Show();
|
||||
}
|
||||
};
|
||||
Inventory.AddProperty($"Empty {Enum.GetName(typeof(HardpointType), hardpoint.HardpointData.Type)} Hardpoint", null, onClick, matchingBlueprints.Any());
|
||||
ContextMenu.gameObject.SetActive(true);
|
||||
ContextMenu.Clear();
|
||||
if (matchingCargoGear.Any())
|
||||
ContextMenu.AddDropdown("Install", matchingCargoGear
|
||||
.Select<Gear, (string, Action, bool)>(gear => (gear.Name, () => entity.Equip(gear, hardpoint), true)));
|
||||
if (matchingBlueprints.Any())
|
||||
ContextMenu.AddDropdown("Build", matchingBlueprints
|
||||
.Select<BlueprintData, (string, Action, bool)>(blueprint =>
|
||||
(blueprint.Name, () =>
|
||||
entity.Build(blueprint, 1, _context.Cache.Get<ItemData>(blueprint.Item).Name, true),
|
||||
entity.GetBlueprintIngredients(blueprint, out _, out _))));
|
||||
ContextMenu.Show();
|
||||
}
|
||||
};
|
||||
panel.AddProperty($"Empty {Enum.GetName(typeof(HardpointType), hardpoint.HardpointData.Type)} Hardpoint", null, onClick, matchingBlueprints.Any());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var prop = panel.AddProperty(hardpoint.Gear.Name, null, data =>
|
||||
{
|
||||
if (data.button == PointerEventData.InputButton.Left)
|
||||
{
|
||||
if (data.clickCount == 1)
|
||||
{
|
||||
_selectedHardpoint = hardpoint;
|
||||
Details.Inspect(entity, hardpoint);
|
||||
}
|
||||
|
||||
if (data.clickCount == 2)
|
||||
{
|
||||
entity.Unequip(hardpoint);
|
||||
}
|
||||
}
|
||||
}, true);
|
||||
prop.Button.DisableClickWhenSelected = false;
|
||||
|
||||
if (hardpoint == _selectedHardpoint)
|
||||
{
|
||||
panel.SelectedChild = prop.Button;
|
||||
prop.Button.CurrentState = FlatButtonState.Selected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!entity.Cargo.Any())
|
||||
panel.AddSection("No Cargo");
|
||||
else
|
||||
{
|
||||
panel.AddSection("Cargo");
|
||||
foreach (var itemID in entity.Cargo.Where(id => Context.Cache.Get<ItemInstance>(id) is SimpleCommodity))
|
||||
{
|
||||
var simpleCommodity = Context.Cache.Get<SimpleCommodity>(itemID);
|
||||
var data = simpleCommodity.ItemData;
|
||||
panel.AddProperty(data.Name, () => simpleCommodity.Quantity.ToString(), eventData =>
|
||||
{
|
||||
Details.Clear();
|
||||
Details.RemoveListener();
|
||||
Details.AddItemProperties(entity, simpleCommodity);
|
||||
Details.RefreshValues();
|
||||
}, true).Button.DragSuccess = eventData =>
|
||||
{
|
||||
Entity targetEntity = null;
|
||||
if (panel == Inventory &&
|
||||
_selectedChild != null &&
|
||||
((RectTransform) ChildInventory.transform).ContainsWorldPoint(eventData.position))
|
||||
targetEntity = _selectedChild;
|
||||
else if (panel == ChildInventory &&
|
||||
((RectTransform) Inventory.transform).ContainsWorldPoint(eventData.position))
|
||||
targetEntity = _parentEntity;
|
||||
|
||||
if (targetEntity != null)
|
||||
{
|
||||
int quantity = min((int) ((targetEntity.Capacity - targetEntity.OccupiedCapacity) / simpleCommodity.ItemData.Size), simpleCommodity.Quantity);
|
||||
if (quantity > 0)
|
||||
{
|
||||
var newItem = entity.RemoveCargo(simpleCommodity, quantity);
|
||||
targetEntity.AddCargo(newItem);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
foreach (var group in entity.Cargo
|
||||
.Select(id => Context.Cache.Get<ItemInstance>(id))
|
||||
.Where(item => item is CraftedItemInstance)
|
||||
.Cast<CraftedItemInstance>()
|
||||
.GroupBy(craftedItem =>
|
||||
{
|
||||
var corp = "GameCult";
|
||||
if (craftedItem.SourceEntity != Guid.Empty)
|
||||
corp = Context.Cache.Get<Corporation>(Context.Cache.Get<Entity>(craftedItem.SourceEntity).Corporation).Name;
|
||||
return (craftedItem.Data, craftedItem.Name, corp) ;
|
||||
}))
|
||||
{
|
||||
Action<CraftedItemInstance, PointerEventData> onClick = (item, data) =>
|
||||
{
|
||||
if (data.button == PointerEventData.InputButton.Left)
|
||||
{
|
||||
if (data.clickCount == 1)
|
||||
{
|
||||
Details.Clear();
|
||||
Details.RemoveListener();
|
||||
Details.AddItemProperties(entity, item);
|
||||
Details.RefreshValues();
|
||||
}
|
||||
else if (data.clickCount == 2 && item is Gear gear)
|
||||
{
|
||||
// Equip Item
|
||||
entity.Equip(gear.ID, true);
|
||||
}
|
||||
}
|
||||
};
|
||||
Func<PointerEventData, CraftedItemInstance, bool> dragSuccess = (data, item) =>
|
||||
{
|
||||
Entity targetEntity = null;
|
||||
if (panel == Inventory &&
|
||||
_selectedChild != null &&
|
||||
((RectTransform) ChildInventory.transform).ContainsWorldPoint(data.position))
|
||||
targetEntity = _selectedChild;
|
||||
else if (panel == ChildInventory &&
|
||||
((RectTransform) Inventory.transform).ContainsWorldPoint(data.position))
|
||||
targetEntity = _parentEntity;
|
||||
|
||||
if (targetEntity != null)
|
||||
{
|
||||
if (targetEntity.Capacity - targetEntity.OccupiedCapacity >
|
||||
Context.Cache.Get<ItemData>(item.Data).Size)
|
||||
{
|
||||
entity.RemoveCargo(item);
|
||||
targetEntity.AddCargo(item);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
if (group.Count() == 1)
|
||||
{
|
||||
var item = group.First();
|
||||
var button = panel.AddProperty(item.Name, null, eventData => onClick(item, eventData), true).Button;
|
||||
button.DisableClickWhenSelected = false;
|
||||
button.DragSuccess = data => dragSuccess(data, item);
|
||||
}
|
||||
else
|
||||
{
|
||||
var prop = Inventory.AddProperty(hardpoint.Gear.Name, null, _ =>
|
||||
var instanceList = panel.AddList(group.Key.Name);
|
||||
foreach (var item in group)
|
||||
{
|
||||
_selectedHardpoint = hardpoint;
|
||||
Details.Inspect(entity, hardpoint);
|
||||
}, true);
|
||||
|
||||
if (hardpoint == _selectedHardpoint)
|
||||
{
|
||||
Inventory.SelectedChild = prop.Button;
|
||||
prop.Button.CurrentState = FlatButtonState.Selected;
|
||||
var button = instanceList.AddProperty(item.Name, null, eventData => onClick(item, eventData), true).Button;
|
||||
button.DisableClickWhenSelected = false;
|
||||
button.DragSuccess = data => dragSuccess(data, item);
|
||||
}
|
||||
instanceList.SetExpanded(false, true);
|
||||
}
|
||||
}
|
||||
|
||||
if(!entity.Cargo.Any())
|
||||
Inventory.AddSection("No Cargo");
|
||||
else
|
||||
{
|
||||
Inventory.AddSection("Cargo");
|
||||
foreach (var itemID in entity.Cargo.Where(id => Context.Cache.Get<ItemInstance>(id) is SimpleCommodity))
|
||||
{
|
||||
var simpleCommodity = Context.Cache.Get<SimpleCommodity>(itemID);
|
||||
var data = simpleCommodity.ItemData;
|
||||
Inventory.AddProperty(data.Name, () => simpleCommodity.Quantity.ToString(), eventData =>
|
||||
{
|
||||
Details.Clear();
|
||||
Details.AddItemProperties(simpleCommodity);
|
||||
Details.RefreshValues();
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var group in entity.Cargo
|
||||
.Select(id => Context.Cache.Get<ItemInstance>(id))
|
||||
.Where(item => item is CraftedItemInstance)
|
||||
.Cast<CraftedItemInstance>()
|
||||
.GroupBy(craftedItem => (craftedItem.Data, craftedItem.Name, Context.Cache.Get<Corporation>(Context.Cache.Get<Entity>(craftedItem.SourceEntity).Corporation))))
|
||||
{
|
||||
if (group.Count() == 1)
|
||||
{
|
||||
var item = group.First();
|
||||
Inventory.AddProperty(item.Name, null, eventData =>
|
||||
{
|
||||
Details.Clear();
|
||||
Details.AddItemProperties(item);
|
||||
Details.RefreshValues();
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
var instanceList = Inventory.AddList(group.Key.Name);
|
||||
foreach (var item in group)
|
||||
{
|
||||
instanceList.AddProperty(item.Name, null, eventData =>
|
||||
{
|
||||
Details.Clear();
|
||||
Details.AddItemProperties(item);
|
||||
Details.RefreshValues();
|
||||
});
|
||||
}
|
||||
instanceList.SetExpanded(false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Inventory.RefreshValues();
|
||||
}
|
||||
panel.RefreshValues();
|
||||
}
|
||||
|
||||
void PopulateDetails(Entity entity, IEnumerable<BlueprintData> blueprints)
|
||||
{
|
||||
Details.RemoveListener();
|
||||
Details.Clear();
|
||||
foreach (var blueprint in blueprints)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue