private void Initialize()
    {
        floatingSlot = new GUIFloatingSlot
        {
            ZAsRelative = true,
            ZIndex      = FLOATING_SLOT_Z
        };

        // this is rather ad-hoc and in general unsafe without thinking about how the slots are synchronized with the inventory
        // TODO: short-term think of a better solution here
        // TODO: long-term think of a better slot sync scheme
        bool offerToMainInventory(ItemStack iStack)
        {
            SaveSlotState();
            bool result = subInventories[iStack.Item.IType].TryAddItemStack(iStack);

            UpdateSlots();
            return(result);
        }

        handSlot = new GUIInventorySlot(floatingSlot, Item.ItemType.ANY, -2, HAND_SLOT_OFFSET, offerToMainInventory)
        {
            ZAsRelative = true,
            ZIndex      = HAND_SLOT_Z
        };

        foreach (Item.ItemType type in new List <Item.ItemType>(subInvSlots.Keys))
        {
            Vector2 empty = new Vector2();

            subInvSlots[type] = new GUILabeledSlotArray(floatingSlot, type, subInventoryNames[type], SLOT_COUNT,
                                                        empty, empty, iStack => handSlot.OfferItemStack(iStack) == null)
            {
                ZAsRelative = true,
                ZIndex      = ARRAY_SLOT_Z
            };
        }

        box = new GUIBox(GetViewportDimensions() / 2, BOX_SIZE)
        {
            ZAsRelative = true,
            ZIndex      = BOX_Z
        };

        AddChild(box);
        foreach (GUILabeledSlotArray slotArr in subInvSlots.Values)
        {
            box.AddChild(slotArr);
        }
        box.AddChild(handSlot);
        AddChild(floatingSlot);
    }
Exemple #2
0
    // TOOD: inventory / GUI synchronization is a mess
    // rewrite it (shouldn't change too much,
    //              mainly SaveSlots() and UpdateSlots() methods and immediate neighbours)
    // using delegates / lambdas / anonymous functions / whatever your favorite term is
    private void Initialize()
    {
        Hide();

        Vector2 empty = new Vector2();

        bool offerToMainInventory(ItemStack iStack)
        {
            SaveSlotState();
            bool result = subInventories[iStack.Item.IType].TryAddItemStack(iStack);

            UpdateSlots();
            return(result);
        }

        floatingSlot = new GUIFloatingSlot();
        inSlotArray  = new GUIInventorySlotArray(floatingSlot, Item.ItemType.ANY, IN_SLOT_COUNT, empty,
                                                 offerToMainInventory,
                                                 () => inSlotArray.SaveToInventory(defossiliser.InInventory));
        progressBar = new GUIVerticalBar(empty, PROGRESS_BAR_HEIGHT, new Color(0, 0.6f, 0));
        progressBar.Rotate(Mathf.PI);
        outSlotArray = new GUIInventorySlotArray(floatingSlot, Item.ItemType.ANY, OUT_SLOT_COUNT, empty,
                                                 offerToMainInventory,
                                                 () => outSlotArray.SaveToInventory(defossiliser.OutInventory));

        bool offerToInputInventory(ItemStack iStack)
        {
            SaveSlotState();
            bool result = defossiliser.InInventory.TryAddItemStack(iStack);

            UpdateSlots();
            return(result);
        }

        foreach (Item.ItemType type in new List <Item.ItemType>(subArrays.Keys))
        {
            subArrays[type] = new GUILabeledSlotArray(floatingSlot, type, subInventoryNames[type], INVENTORY_SLOT_COUNT,
                                                      empty, empty, offerToInputInventory);
        }
        box = new GUIBox(this.GetViewportDimensions() / 2, BOX_SIZE);
        AddChild(box);
        foreach (GUILabeledSlotArray slotArr in subArrays.Values)
        {
            box.AddChild(slotArr);
        }
        box.AddChild(inSlotArray);
        box.AddChild(progressBar);
        box.AddChild(outSlotArray);
        AddChild(floatingSlot);
    }