Ejemplo n.º 1
0
        void charInventory_CellMouseUp(object sender, InventoryWindowEventArgs e)
        {
            var keyboard = _inputManager.KeyboardManager.CurKeyboardState;

            if (_dragControl.Slot != null)
            {
                if (_hoverSlot == null)
                {
                    CancelDrag();
                    return;
                }

                if (_hoverSlot.Parent == _toolBar)
                {
                    ToolBarSlotClicked(sender, new InventoryWindowCellMouseEventArgs {
                        Cell = _hoverSlot
                    });
                    CancelDrag();
                    return;
                }

                e = ((InventoryWindow)_hoverSlot.Parent).CreateEventArgs(_hoverSlot);

                // put item

                // check slot we want to put to
                var slot = e.Container.PeekSlot(e.SlotPosition);

                if (slot != null && !slot.CanStackWith(_dragControl.Slot))
                {
                    // exchange

                    // first check if we can perform exchange here, we can't if we took not the whole pack
                    var srcSlot = _sourceContainer.PeekSlot(_dragControl.Slot.GridPosition);
                    if (srcSlot != null)
                    {
                        CancelDrag();
                    }
                    else
                    {
                        var prevPosition = _dragControl.Slot.GridPosition;
                        _dragControl.Slot.GridPosition = e.SlotPosition;
                        ContainedSlot slotTaken;
                        if (!e.Container.PutItemExchange(_dragControl.Slot.Item, _dragControl.Slot.GridPosition, _dragControl.Slot.ItemsCount, out slotTaken))
                        {
                            throw new InvalidOperationException();
                        }

                        OnSlotPut(_dragControl.Slot);

                        slotTaken.GridPosition = prevPosition;
                        UpdateDrag(slotTaken);
                        CancelDrag();
                    }
                }
                else
                {
                    // just put, gonna be okay, da da doo-doo-mmm
                    _dragControl.Slot.GridPosition = e.SlotPosition;

                    var itemsCount = _dragControl.Slot.ItemsCount;

                    if (e.MouseState.RightButton == S33M3CoreComponents.Inputs.MouseHandler.ButtonState.Pressed)
                    {
                        if (keyboard.IsKeyDown(Keys.ShiftKey) && itemsCount > 1)
                        {
                            itemsCount = itemsCount / 2;
                        }
                        else
                        {
                            itemsCount = 1;
                        }
                    }

                    if (!e.Container.PutItem(_dragControl.Slot.Item, _dragControl.Slot.GridPosition, itemsCount))
                    {
                        throw new InvalidOperationException();
                    }

                    OnSlotPut(_dragControl.Slot);

                    _dragControl.Slot.ItemsCount -= itemsCount;
                    if (_dragControl.Slot.ItemsCount == 0)
                    {
                        EndDrag();
                    }
                    else
                    {
                        _dragControl.BringToFront();
                    }
                }
            }
        }