public static bool ChangeColor(ListElementController_InventoryGear_NotListView __instance, InventoryItemElement_NotListView theWidget) { try { theWidget.iconBGColors.SetColor(__instance.componentRef); } catch (Exception ex) { Control.LogError(ex); } return(false); }
public static bool ChangeColor(ListElementController_InventoryGear_NotListView __instance, InventoryItemElement_NotListView theWidget) { try { ColorExtentions.ChangeBackColor(__instance.componentRef.Def, theWidget); TColorExtentions.ChangeTextIconColor(__instance.componentRef.Def, theWidget); } catch { return(true); } return(false); }
public static bool OnAddItem_Pre(MechLabInventoryWidget __instance, IMechLabDraggableItem item) { if (state != null && state.inventoryWidget == __instance) { try { var nlv = item as InventoryItemElement_NotListView; var quantity = nlv == null ? 1 : nlv.controller.quantity; var existing = state.FetchItem(item.ComponentRef); if (existing == null) { LogDebug(string.Format("OnAddItem new {0}", quantity)); var controller = nlv == null ? null : nlv.controller; if (controller == null) { if (item.ComponentRef.ComponentDefType == ComponentType.Weapon) { var ncontroller = new ListElementController_InventoryWeapon_NotListView(); ncontroller.InitAndCreate(item.ComponentRef, state.instance.dataManager, state.inventoryWidget, quantity, false); controller = ncontroller; } else { var ncontroller = new ListElementController_InventoryGear_NotListView(); ncontroller.InitAndCreate(item.ComponentRef, state.instance.dataManager, state.inventoryWidget, quantity, false); controller = ncontroller; } } state.rawInventory.Add(controller); state.rawInventory = state.Sort(state.rawInventory); state.FilterChanged(false); } else { LogDebug(string.Format("OnAddItem existing {0}", quantity)); if (existing.quantity != Int32.MinValue) { existing.ModifyQuantity(quantity); } state.Refresh(false); } } catch (Exception e) { LogException(e); } return(false); } else { return(true); } }
/* Fast sort, which works off data, rather than visual elements. * Since only 7 visual elements are allocated, this is required. */ public List <ListElementController_BASE_NotListView> Sort(List <ListElementController_BASE_NotListView> items) { LogSpam($"Sorting: {items.Select(item => GetRef(item).ComponentDefID).ToArray().Dump(false)}"); var sw = Stopwatch.StartNew(); var _a = new ListElementController_InventoryGear_NotListView(); var _b = new ListElementController_InventoryGear_NotListView(); var go = new UnityEngine.GameObject(); var _ac = go.AddComponent <InventoryItemElement_NotListView>(); //new InventoryItemElement_NotListView(); var go2 = new UnityEngine.GameObject(); var _bc = go2.AddComponent <InventoryItemElement_NotListView>(); _ac.controller = _a; _bc.controller = _b; var _cs = new Traverse(inventoryWidget).Field("currentSort").GetValue <Comparison <InventoryItemElement_NotListView> >(); var cst = _cs.Method; LogDebug(string.Format("Sort using {0}::{1}", cst.DeclaringType.FullName, cst.ToString())); var tmp = items.ToList(); tmp.Sort(new Comparison <ListElementController_BASE_NotListView>((l, r) => { _ac.ComponentRef = _a.componentRef = GetRef(l); _bc.ComponentRef = _b.componentRef = GetRef(r); _ac.controller = l; _bc.controller = r; _ac.controller.ItemWidget = _ac; _bc.controller.ItemWidget = _bc; _ac.ItemType = ToDraggableType(l.componentDef); _bc.ItemType = ToDraggableType(r.componentDef); var res = _cs.Invoke(_ac, _bc); LogSpam($"Compare {_a.componentRef.ComponentDefID}({_ac != null},{_ac.controller.ItemWidget != null}) & {_b.componentRef.ComponentDefID}({_bc != null},{_bc.controller.ItemWidget != null}) -> {res}"); return(res); })); UnityEngine.GameObject.Destroy(go); UnityEngine.GameObject.Destroy(go2); var delta = sw.Elapsed.TotalMilliseconds; LogInfo(string.Format("Sorted in {0} ms", delta)); LogSpam($"Sorted: {tmp.Select(item => GetRef(item).ComponentDefID).ToArray().Dump(false)}"); return(tmp); }
public static void Postfix(ListElementController_InventoryGear_NotListView __instance, InventoryItemElement_NotListView theWidget) { try { if (HasOverrideColor(__instance.componentRef.Def, out Color overrideColor)) { UIColorRefTracker[] array = theWidget.iconBGColors; for (int i = 0; i < array.Length; i++) { array[i].OverrideWithColor(overrideColor); } } } catch (Exception e) { Logger.Error(e); } }
public PatchMechlabLimitItems(MechLabPanel instance) { try { var sw = new Stopwatch(); sw.Start(); this.instance = instance; this.inventoryWidget = new Traverse(instance).Field("inventoryWidget") .GetValue <MechLabInventoryWidget>() .LogIfNull("inventoryWidget is null"); LogDebug($"StorageInventory contains {instance.storageInventory.Count}"); if (instance.IsSimGame) { new Traverse(instance).Field("originalStorageInventory").SetValue(instance.storageInventory.LogIfNull("storageInventory is null")); } LogDebug($"Mechbay Patch initialized :simGame? {instance.IsSimGame}"); List <ListElementController_BASE_NotListView> BuildRawInventory() => instance.storageInventory.Select <MechComponentRef, ListElementController_BASE_NotListView>(componentRef => { componentRef.LogIfNull("componentRef is null"); componentRef.DataManager = instance.dataManager.LogIfNull("(MechLabPanel instance).dataManager is null"); componentRef.RefreshComponentDef(); componentRef.Def.LogIfNull("componentRef.Def is null"); var count = (!instance.IsSimGame ? int.MinValue : instance.sim.GetItemCount(componentRef.Def.Description, componentRef.Def.GetType(), instance.sim.GetItemCountDamageType(componentRef))); if (componentRef.ComponentDefType == ComponentType.Weapon) { ListElementController_InventoryWeapon_NotListView controller = new ListElementController_InventoryWeapon_NotListView(); controller.InitAndFillInSpecificWidget(componentRef, null, instance.dataManager, null, count, false); return(controller); } else { ListElementController_InventoryGear_NotListView controller = new ListElementController_InventoryGear_NotListView(); controller.InitAndFillInSpecificWidget(componentRef, null, instance.dataManager, null, count, false); return(controller); } }).ToList(); /* Build a list of data only for all components. */ rawInventory = Sort(BuildRawInventory()); InventoryItemElement_NotListView mkiie(bool nonexistant) { var nlv = instance.dataManager.PooledInstantiate(ListElementController_BASE_NotListView.INVENTORY_ELEMENT_PREFAB_NotListView , BattleTechResourceType.UIModulePrefabs, null, null, null) .LogIfNull("Unable to instantiate INVENTORY_ELEMENT_PREFAB_NotListView") .GetComponent <InventoryItemElement_NotListView>() .LogIfNull("Inventory_Element_prefab does not contain a NLV"); nlv.gameObject.IsDestroyedError("NLV gameObject has been destroyed"); nlv.gameObject.LogIfNull("NLV gameObject has been destroyed"); if (!nonexistant) { nlv.SetRadioParent(new Traverse(inventoryWidget).Field("inventoryRadioSet").GetValue <HBSRadioSet>().LogIfNull("inventoryRadioSet is null")); nlv.gameObject.transform.SetParent(new Traverse(inventoryWidget).Field("listParent").GetValue <UnityEngine.Transform>().LogIfNull("listParent is null"), false); nlv.gameObject.transform.localScale = UnityEngine.Vector3.one; } return(nlv); }; iieTmp = mkiie(true); /* Allocate very few visual elements, as this is extremely slow for both allocation and deallocation. * It's the difference between a couple of milliseconds and several seconds for many unique items in inventory * This is the core of the fix, the rest is just to make it work within HBS's existing code. */ List <InventoryItemElement_NotListView> make_ielCache() => Enumerable.Repeat <Func <InventoryItemElement_NotListView> >(() => mkiie(false), itemLimit) .Select(thunk => thunk()) .ToList(); ielCache = make_ielCache(); var li = new Traverse(inventoryWidget).Field("localInventory").GetValue <List <InventoryItemElement_NotListView> >(); ielCache.ForEach(iw => li.Add(iw)); // End var lp = new Traverse(inventoryWidget).Field("listParent").GetValue <UnityEngine.Transform>(); // DummyStart&End are blank rects stored at the beginning and end of the list so that unity knows how big the scrollrect should be // "placeholders" if (DummyStart == null) { DummyStart = new UnityEngine.GameObject().AddComponent <UnityEngine.RectTransform>(); } if (DummyEnd == null) { DummyEnd = new UnityEngine.GameObject().AddComponent <UnityEngine.RectTransform>(); } DummyStart.SetParent(lp, false); DummyEnd.SetParent(lp, false); LogDebug(string.Format("[LimitItems] inventory cached in {0} ms", sw.Elapsed.TotalMilliseconds)); FilterChanged(); } catch (Exception e) { LogException(e); } }