public void OnHoverEnd(Transform hoverObject)
 {
     m_hoverSnapCell = null;
     m_activeHoverGuide.transform.SetParent(null);
     GameObject.Destroy(m_activeHoverGuide);
     //LayoutRebuilder.MarkLayoutForRebuild((RectTransform)m_snapGrid.transform);
 }
 public void OnHoverStart(Transform hoverObject)
 {
     m_activeHoverGuide = GameObject.Instantiate(m_hoverGuideGraphics);
     m_activeHoverGuide.transform.SetParent(transform, false);
     m_hoverSnapCell = m_activeHoverGuide.AddComponent<SnapGridCell>();
     m_hoverSnapCell.forcedOverlap = true;
     m_hoverSnapCell.x = m_hoverSnapCell.y = m_hoverSnapCell.width = m_hoverSnapCell.height = 1;
     UpdateHoverGraphics(hoverObject);
 }
    private void SetChildGridposition(Transform child, SnapGridCell cell)
    {
        RectTransform rect = (RectTransform)child;
        Vector3[] corners = new Vector3[4];
        rect.GetWorldCorners(corners);

        int[] coordinates = m_snapGrid.PositionToCellCoordinates(corners[0]);
        int[] sizes = m_snapGrid.RectToCellSizes((RectTransform)child);
        cell.width = sizes[0];
        cell.height = sizes[1];
        int[] offset = m_snapGrid.CalculateOverlapOffset(cell, coordinates[0], coordinates[1]);
        cell.x = coordinates[0] + offset[0];
        cell.y = coordinates[1] + offset[1];
    }
    public int[] CalculateOverlapOffset(SnapGridCell cell, int xPos, int yPos)
    {
        xPos = Math.Max(0, Math.Min(xPos, m_cellColumns));
        yPos = Math.Max(0, Math.Min(yPos, m_cellRows));
        int[] overlapOffset = new int[2];

        if (xPos > m_cellColumns-1 || yPos > m_cellRows-1) return overlapOffset;

        for (int x = xPos; x < xPos + cell.width; x++)
        {
            for (int y = yPos; y < yPos + cell.height; y++)
            {
                if (x >= m_cellColumns || y >= m_cellRows)
                    continue;

                if (m_cells[x, y] != null &&
                    m_cells[x, y] != cell &&
                    !m_cells[x, y].forcedOverlap &&
                    !m_allowOverlap
                    )
                {
                    return NearestValidPosition(cell, xPos, yPos);
                }
            }
        }

        return overlapOffset;
    }
 public int[] CalculateOverlapOffset(SnapGridCell cell)
 {
     return CalculateOverlapOffset(cell, cell.x, cell.y);
 }
    private int[] NearestValidPosition(SnapGridCell cell, int xPos, int yPos)
    {
        int[] overlapOffset = new int[2];
        int shortestOffset = -1;
        OverflowDirection shortestDir = m_overflow;
        int offset = 0;
        OverflowDirection cDir = 0;
        foreach (OverflowDirection dir in Enum.GetValues(typeof(OverflowDirection)))
        {
            offset = 0;
            switch (dir)
            {
                case OverflowDirection.DOWN:
                    cDir = OverflowDirection.DOWN;

                    while (m_cells[xPos, yPos + offset] != null &&
                        m_cells[xPos, yPos + offset] != cell &&
                        yPos + offset <= m_cellRows)
                    {
                        offset++;
                    }
                    break;
                case OverflowDirection.RIGHT:
                    cDir = OverflowDirection.RIGHT;
                    while (m_cells[xPos + offset, yPos] != null &&
                        m_cells[xPos + offset, yPos] != cell &&
                        xPos + offset <= m_cellColumns)
                    {
                        offset++;
                    }
                    break;
                case OverflowDirection.UP:
                    while (m_cells[xPos, yPos + offset] != null &&
                        m_cells[xPos, yPos + offset] != cell)
                    {
                        offset -= cell.height;
                        if (yPos + offset <= 0)
                        {
                            offset = m_cellRows; //Ran out of space. Don't use this direction.
                            break;
                        }
                    }
                    break;
                case OverflowDirection.LEFT:
                    cDir = OverflowDirection.RIGHT;
                    while (m_cells[xPos + offset, yPos] != null &&
                        m_cells[xPos + offset, yPos] != cell)
                    {
                        offset -= cell.width;
                        if (xPos + offset <= 0)
                        {
                            offset = m_cellColumns; //Ran out of space. Don't use this direction.
                            break;
                        }
                    }
                    break;
            }

            if (offset < shortestOffset || shortestOffset < 0)
            {
                shortestOffset = offset;
                shortestDir = cDir;
            }
        }
        if (shortestDir == OverflowDirection.DOWN || shortestDir == OverflowDirection.UP)
            overlapOffset[1] = shortestOffset;
        else if (shortestDir == OverflowDirection.LEFT || shortestDir == OverflowDirection.RIGHT)
            overlapOffset[0] = shortestOffset;
        return overlapOffset;
    }