/// <summary> /// /// </summary> public virtual bool DoAddUnits(int amount) { // TODO: 'do' shouldn't run 'try'. it should be the other way around m_PrevCount = Count; m_Result = TryGiveUnits(amount); if (m_Inventory.SpaceEnabled && (m_Result == true) && ((!IsInternal) && m_Inventory.SpaceMode == vp_Inventory.Mode.Weight)) { m_Inventory.UsedSpace += ((Count - m_PrevCount) * UnitType.Space); } m_Inventory.SetDirty(); return(m_Result); }
/// <summary> /// /// </summary> protected float DoItemsFoldout(Rect pos, SerializedProperty prop) { m_InitialY = pos.y; pos.height = 16; vp_Inventory inventory = ((vp_Inventory)prop.serializedObject.targetObject); GUI.backgroundColor = Color.white; pos.y += (( (inventory.ItemInstances.Count > 0) || (inventory.UnitBankInstances.Count > 0) || (inventory.InternalUnitBanks.Count > 0)) ? 5 : -5); pos.x += 20; pos.width -= 15; // --- draw internal unit banks --- for (int v = 0; v < inventory.InternalUnitBanks.Count; v++) { vp_UnitBankInstance ibank = inventory.InternalUnitBanks[v]; if ((ibank == null) || (ibank.UnitType == null)) { inventory.InternalUnitBanks.Remove(ibank); inventory.Refresh(); continue; } string name = ibank.UnitType.ToString(); name = name.Remove(name.IndexOf(" (")) + "s (Internal UnitBank)"; int unitCount = inventory.GetItemCount(ibank.UnitType); vp_PropertyDrawerUtility.ItemCard(pos, ((ibank == null) ? null : ibank.UnitType.Icon), name, ((ibank == null) ? null : ibank.UnitType), ref unitCount, "Units", delegate() { inventory.TrySetUnitCount(ibank.UnitType, unitCount); }, ref NO_VALUE, "", null, delegate() { m_UnitBankToRemove = ibank; }); pos.y += 21; } // --- draw item unit bank instances (such as weapons) --- for (int v = 0; v < inventory.UnitBankInstances.Count; v++) { vp_UnitBankInstance bank = inventory.UnitBankInstances[v]; int unitCount = bank.Count; if ((bank == null) || (bank.Type == null)) { inventory.UnitBankInstances.Remove(bank); inventory.Refresh(); continue; } string name = bank.Type.ToString(); if (vp_PropertyDrawerUtility.ItemCard(pos, ((bank == null) ? null : bank.Type.Icon), name, ((bank == null) ? null : bank.Type), ref unitCount, "Units", delegate() { inventory.TrySetUnitCount(bank, unitCount); }, ref bank.ID, "ID", null, // no need to act on value change since it is handled by the ref above delegate() { m_UnitBankToRemove = bank; })) { inventory.ClearItemDictionaries(); inventory.SetDirty(); } pos.y += 21; } // --- draw item instances --- for (int v = 0; v < inventory.ItemInstances.Count; v++) { vp_ItemInstance item = inventory.ItemInstances[v]; if ((item == null) || (item.Type == null)) { inventory.ItemInstances.Remove(item); inventory.Refresh(); continue; } string name = item.Type.ToString(); if (vp_PropertyDrawerUtility.ItemCard(pos, ((item == null) ? null : item.Type.Icon), name, ((item == null) ? null : item.Type), ref item.ID, "ID", null, // no need to act on value change since it is handled by the ref above ref NO_VALUE, "", null, delegate() { m_ItemToRemove = item; }, 45)) { inventory.ClearItemDictionaries(); inventory.SetDirty(); } pos.y += 21; } // --- draw 'add object' box --- pos.y += 16; vp_PropertyDrawerUtility.AddObjectBox(pos, "n ItemType", typeof(vp_ItemType), delegate(Object itemType) { if (itemType is vp_UnitBankType) { vp_UnitBankType bank = (vp_UnitBankType)itemType; int cap = inventory.GetItemCap((vp_UnitBankType)itemType); if ((cap == -1) || (inventory.GetItemCount((vp_UnitBankType)itemType) < cap)) { if (!inventory.TryGiveUnitBank(bank, bank.Capacity, 0)) { EditorUtility.DisplayDialog(m_InventoryFullCaption, string.Format(m_InventoryFullMessage, itemType), "OK"); } } else { EditorUtility.DisplayDialog(m_InventoryFullCaption, string.Format(m_InventoryFullMessage, itemType), "OK"); } } else if (itemType is vp_UnitType) { if (!inventory.HaveInternalUnitBank((vp_UnitType)itemType)) { vp_UnitType unitType = (vp_UnitType)itemType; if (!inventory.TryGiveUnits(unitType, 10)) { EditorUtility.DisplayDialog(m_InventoryFullCaption, string.Format(m_InventoryFullMessage, itemType), "OK"); } } else { EditorUtility.DisplayDialog(m_UnitBankAlreadyExistsCaption, string.Format(m_UnitBankAlreadyExistsMessage, itemType), "OK"); } } else { if (!inventory.TryGiveItem((vp_ItemType)itemType, 0)) { EditorUtility.DisplayDialog(m_InventoryFullCaption, string.Format(m_InventoryFullMessage, itemType), "OK"); } } EditorUtility.SetDirty(inventory); inventory.Refresh(); }); pos.y += vp_PropertyDrawerUtility.CalcAddObjectBoxHeight - 5; // handle removed items if (m_UnitBankToRemove != null) { inventory.TryRemoveUnitBank(m_UnitBankToRemove); m_UnitBankToRemove = null; } else if (m_ItemToRemove != null) { inventory.TryRemoveItem(m_ItemToRemove); m_ItemToRemove = null; } return(pos.y - m_InitialY); }