public void OnEndDrag(PointerEventData eventData) { if (IsRunning) { return; } if (!IsDragging) { return; } BoardPosition pos = FindPosition(eventData); //Debug.LogFormat("OnEndDrag: {0}", pos); if (pos.place == BoardPosition.Place.BOARD) { // Drop in board Board.Cell targetCell = TheBoard[pos]; if (targetCell.IsEmpty()) { // Set item at target position SetBoardAndTile(pos, DraggingCell); } else if (DraggingFrom.x != -999) { // Move from board if (!targetCell.IsMovable()) { // Cannot swap with immovable object, spring back to original position SetBoardAndTile(DraggingFrom, DraggingCell); } else { // There are something there and is movable, swap them SetBoardAndTile(DraggingFrom, targetCell); SetBoardAndTile(pos, DraggingCell); } } else { // Move from toolbox but dropped on something. Discard it. TheBoard.ReturnTool(DraggingCell); } } else { // Drop outside board, try to return the item to toolbox if (!TheBoard.ReturnTool(DraggingCell)) { // Item cannot be returned, spring back to original position (if there is) if (DraggingFrom.x != -999) { SetBoardAndTile(DraggingFrom, DraggingCell); } } } DragIcon.gameObject.SetActive(false); IsDragging = false; }
public void OnBeginDrag(PointerEventData eventData) { if (IsRunning) { return; } if (eventData.button != PointerEventData.InputButton.Left) { return; } BoardPosition pos = MouseDownPos; // Use position when mouse is down, not start dragging //BoardPosition pos = FindPosition(eventData); //Debug.LogFormat("OnBeginDrag: {0}", pos); do { if (pos.place == BoardPosition.Place.TOOLBOX) { int toolID = pos.x; Board.Cell NewCell = TheBoard.TakeTool(toolID); if (!NewCell.IsEmpty()) { DraggingCell = NewCell; DraggingFrom = new Vector2Int(-999, -999); break; } } else if (pos.place == BoardPosition.Place.BOARD) { // Begin drag on a board Board.Cell cell = TheBoard[pos]; if (cell.IsMovable()) { DraggingCell = cell; DraggingFrom = pos; SetBoardAndTile(DraggingFrom, Board.EmptyCell); break; } } eventData.pointerDrag = null; return; } while (false); IsDragging = true; Sprite draggingSprite = BoardTile.CellToSprite(DraggingCell); DragIcon.gameObject.SetActive(true); DragIcon.GetComponent <SpriteRenderer>().sprite = draggingSprite; DragIcon.position = MainCamera.ScreenToWorldPoint((Vector3)eventData.position).SetZ(0); //Debug.LogFormat("OnBeginDrag: Event position = {0}", eventData.position)); }