/// <summary> /// Creates an <see cref="ItemStackRow"/> for the given stack of items. /// </summary> /// <param name="itemStack">Item stack the row is for</param> /// <param name="interactive">Whether to display buttons for altering the item stack's selected count</param> /// <param name="alternateBackground">Whether to alternate the background of each second row</param> /// <param name="onSelectedChanged">Callback invoked when the number of selected items has changed</param> public ItemStackRow(StackedThings itemStack, bool interactive = false, bool alternateBackground = false, Action <int> onSelectedChanged = null) { this.itemStack = itemStack; this.interactive = interactive; this.alternateBackground = alternateBackground; this.onSelectedChanged = onSelectedChanged; }
public override void PreOpen() { base.PreOpen(); // Select all maps that are player homes IEnumerable <Map> homeMaps = Find.Maps.Where(map => map.IsPlayerHome); // Check whether we're configured to get everything or just grab from storage IEnumerable <Thing> things; if (Instance.AllItemsTradable) { // Get *everything* things = homeMaps.SelectMany(map => map.listerThings.AllThings); } else { // From each map, select all haul destinations IEnumerable <SlotGroup> haulDestinations = homeMaps.SelectMany(map => map.haulDestinationManager.AllGroups); // From each haul destination, select everything stored there things = haulDestinations.SelectMany(haulDestination => haulDestination.HeldThings); } // Group all items and cache them for later this.itemStacks = StackedThings.GroupThings(things.Where(thing => thing.def.category == ThingCategory.Item && !thing.def.IsCorpse)); // Update both our and their offers UpdateOffers(); }
/// <summary> /// Groups the given collection of items by their def type and stackability. /// </summary> /// <param name="items">Items to group</param> /// <returns>Grouped items list</returns> public static List <StackedThings> GroupThings(IEnumerable <Thing> items) { // Set up an item dictionary Dictionary <string, List <StackedThings> > groupedItems = new Dictionary <string, List <StackedThings> >(); foreach (Thing item in items) { // Check if this item type already has a group if (groupedItems.ContainsKey(item.def.defName)) { // Loop over all the item stacks in the group bool stacked = false; foreach (StackedThings itemStack in groupedItems[item.def.defName]) { // Check if this item can stack on this stack if (itemStack.CanStack(item)) { // Increment this stack's item count by the item's stack count and break the loop itemStack.Things.Add(item); stacked = true; break; } } // Check if a stack wasn't found within this group if (!stacked) { // Add a new stack with this item in it groupedItems[item.def.defName].Add(new StackedThings(new[] { item })); } } else { // Create a new item stack with this item in it StackedThings itemStack = new StackedThings(new[] { item }); // Add a new group with the item stack groupedItems.Add(item.def.defName, new List <StackedThings> { itemStack }); } } // Return the grouped items dictionary return(groupedItems.SelectMany(pair => pair.Value).ToList()); }
/// <summary> /// Updates <see cref="ourOfferCache"/> and <see cref="theirOfferCache"/> with the items on offer for each party respectively. /// </summary> private void UpdateOffers() { // Try get our items on offer if (Instance.TryGetItemsOnOffer(tradeId, Instance.Uuid, out IEnumerable <ProtoThing> ourItems)) { // Convert our items to their Verse equivalents Verse.Thing[] verseItems = ourItems.Select(TradingThingConverter.ConvertThingFromProtoOrUnknown).ToArray(); lock (ourOfferCacheLock) { // Update our cached offer ourOfferCache = StackedThings.GroupThings(verseItems); } } else { lock (ourOfferCacheLock) { // Just clear our cached offer ourOfferCache.Clear(); } } // Try get their UUID and items on offer if (Instance.TryGetOtherPartyUuid(tradeId, out string otherPartyUuid) && Instance.TryGetItemsOnOffer(tradeId, otherPartyUuid, out IEnumerable <ProtoThing> theirItems)) { // Convert their items to their Verse equivalents Verse.Thing[] verseItems = theirItems.Select(TradingThingConverter.ConvertThingFromProtoOrUnknown).ToArray(); lock (theirOfferCacheLock) { // Update their cached offer theirOfferCache = StackedThings.GroupThings(verseItems); } } else { lock (theirOfferCacheLock) { // Just clear their cached offer theirOfferCache.Clear(); } } }