public void OnCanvasRightClick(int gridX, int gridY, int lastGridX, int lastGridY)
        {
            EditorMapLayer activeLayer = GetSelectedLayer();

            if (activeLayer == null)
            {
                return;
            }

            // eyedropper tool

            EditorBrush brush = activeLayer.GetBrushAt(gridX, gridY);

            if (brush != null)
            {
                SetSelectedBrush(brush);
            }
        }
        public EditorMapLayer GetSelectedLayer()
        {
            if (m_selectedMap == null)
            {
                return(null);
            }

            EditorMapLayer activeLayer = null;

            if (m_selectedMap is EditorMapPart)
            {
                activeLayer = ((EditorMapPart)m_selectedMap).Layers.FirstOrDefault();
            }
            else if (m_selectedMap is EditorMapLayer)
            {
                activeLayer = (EditorMapLayer)m_selectedMap;
            }

            return(activeLayer);
        }
        public void OnCanvasLeftClick(int gridX, int gridY, int lastGridX, int lastGridY)
        {
            if (m_selectedBrush == null || m_selectedMap == null)
            {
                return;
            }

            // Get the current layer
            EditorMapLayer activeLayer = GetSelectedLayer();

            // Get the old brush, for redrawing purposes
            EditorBrush       oldBrush            = GetSelectedLayer().GetBrushAt(gridX, gridY);
            ObjectOrientation oldBrushOrientation = null;

            if (oldBrush != null && oldBrush.FrontAsset != null && oldBrush.FrontAsset is StarboundObject)
            {
                oldBrushOrientation = ((StarboundObject)oldBrush.FrontAsset).GetCorrectOrientation(m_selectedMap, gridX,
                                                                                                   gridY);
            }

            // If there's nothing to change, just leave
            if (gridX == lastGridX && gridY == lastGridY)
            {
                return;
            }

            // Change the layer brush
            activeLayer.SetBrushAt(m_selectedBrush, gridX, gridY, true);

            // We need to selectively redraw here
            if (m_gridFactor != 1)
            {
                var additionalRedrawList = new HashSet <List <int> >();

                int xmin = gridX;
                int xmax = gridX;

                int ymin = gridY;
                int ymax = gridY;

                // If the old brush was an object, we must redraw around it
                if (oldBrushOrientation != null)
                {
                    int sizeX   = oldBrushOrientation.GetWidth(m_selectedBrush.Direction, 1);
                    int sizeY   = oldBrushOrientation.GetHeight(m_selectedBrush.Direction, 1);
                    int originX = oldBrushOrientation.GetOriginX(m_selectedBrush.Direction, 1);
                    int originY = oldBrushOrientation.GetOriginY(m_selectedBrush.Direction, 1);

                    // Update the minimum and maximum bounds
                    xmin += originX;
                    xmax += sizeX + originX;

                    ymin += originY;
                    ymax += sizeY + originY;
                }
                // If the old brush isn't an object, just redraw a tile
                else
                {
                    xmax += 1;
                    ymax += 1;
                }

                // If the current brush is an object
                // Extend the range of our bounds, so we encompass the old object, AND the new object
                if (m_selectedBrush.FrontAsset is StarboundObject)
                {
                    ObjectOrientation orientation =
                        ((StarboundObject)m_selectedBrush.FrontAsset).GetCorrectOrientation(m_selectedMap, gridX, gridY);

                    int sizeX   = orientation.GetWidth(m_selectedBrush.Direction, 1);
                    int sizeY   = orientation.GetHeight(m_selectedBrush.Direction, 1);
                    int originX = orientation.GetOriginX(m_selectedBrush.Direction, 1);
                    int originY = orientation.GetOriginY(m_selectedBrush.Direction, 1);

                    int newxmin = xmin + originX;
                    int newxmax = xmax + sizeX + originX;

                    int newymin = ymin + originY;
                    int newymax = ymax + sizeY + originY;

                    if (newxmin < xmin)
                    {
                        xmin = newxmin;
                    }

                    if (newxmax > xmax)
                    {
                        xmax = newxmax;
                    }

                    if (newymin < ymin)
                    {
                        ymin = newymin;
                    }

                    if (newymax > ymax)
                    {
                        ymax = newymax;
                    }
                }

                for (int x = xmin; x < xmax; ++x)
                {
                    for (int y = ymin; y < ymax; ++y)
                    {
                        HashSet <List <int> > collisions = null;

                        if (m_selectedMap is EditorMapPart)
                        {
                            collisions = activeLayer.Parent.GetCollisionsAt(x, y);
                        }
                        else if (m_selectedMap is EditorMapLayer)
                        {
                            collisions = activeLayer.GetCollisionsAt(x, y);
                        }

                        if (collisions == null)
                        {
                            continue;
                        }

                        foreach (List <int> coords in collisions.Where(coords =>
                                                                       (coords[0] != x || coords[1] != y) &&
                                                                       (coords[0] != gridX || coords[1] != gridY)))
                        {
                            additionalRedrawList.Add(coords);
                        }
                    }
                }

                // Selectively redraw the composite image
                if (m_selectedMap is EditorMapPart)
                {
                    activeLayer.Parent.UpdateLayerImageBetween(
                        xmin,
                        ymin,
                        xmax,
                        ymax);

                    foreach (var coords in additionalRedrawList)
                    {
                        activeLayer.Parent.UpdateLayerImageBetween(
                            coords[0],
                            coords[1],
                            coords[0] + 1,
                            coords[1] + 1);
                    }
                }

                // Only selectively redraw the active layer
                else if (m_selectedMap is EditorMapLayer)
                {
                    activeLayer.Parent.UpdateLayerImageBetween(
                        new List <EditorMapLayer> {
                        activeLayer
                    },
                        xmin,
                        ymin,
                        xmax,
                        ymax);

                    foreach (var coords in additionalRedrawList)
                    {
                        activeLayer.Parent.UpdateLayerImageBetween(
                            new List <EditorMapLayer> {
                            activeLayer
                        },
                            coords[0],
                            coords[1],
                            coords[0] + 1,
                            coords[1] + 1);
                    }
                }
            }

            // There MUST be an immediate refresh here
            // Otherwise the system will delay the refresh on large images
            MainPictureBox.Refresh();
        }