public override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (this.draggedRectSide != PixmapSlicingRectSide.None) { Vector2 atlasPos = this.View.GetAtlasPos(e.Location); Rect draggedAtlasRect = this.View.GetAtlasRect(this.draggedRectIndex); // Move dragged side to mouse switch (this.draggedRectSide) { case PixmapSlicingRectSide.Left: draggedAtlasRect.W += draggedAtlasRect.X - atlasPos.X; draggedAtlasRect.X = atlasPos.X; break; case PixmapSlicingRectSide.Right: draggedAtlasRect.W += atlasPos.X - draggedAtlasRect.RightX; break; case PixmapSlicingRectSide.Top: draggedAtlasRect.H += draggedAtlasRect.Y - atlasPos.Y; draggedAtlasRect.Y = atlasPos.Y; break; case PixmapSlicingRectSide.Bottom: draggedAtlasRect.H += atlasPos.Y - draggedAtlasRect.BottomY; break; } // If width / height has gone negative, switch hover side if (draggedAtlasRect.W < 0 || draggedAtlasRect.H < 0) { draggedAtlasRect = draggedAtlasRect.Normalized(); this.draggedRectSide = this.draggedRectSide.Opposite(); } // Keep the displayRect within bounds of the image Rect pixmapRect = new Rect(0.0f, 0.0f, this.TargetPixmap.Width, this.TargetPixmap.Height); draggedAtlasRect = draggedAtlasRect.Intersection(pixmapRect); // Clamp the atlas rect to full pixel values draggedAtlasRect = new Rect( MathF.RoundToInt(draggedAtlasRect.LeftX), MathF.RoundToInt(draggedAtlasRect.TopY), MathF.RoundToInt(draggedAtlasRect.RightX) - MathF.RoundToInt(draggedAtlasRect.LeftX), MathF.RoundToInt(draggedAtlasRect.BottomY) - MathF.RoundToInt(draggedAtlasRect.TopY)); // Apply the new atlas rect if (draggedAtlasRect.W != 0 && draggedAtlasRect.H != 0) { this.SetAtlasRect(this.draggedRectIndex, draggedAtlasRect); } } }
/// <summary> /// Returns whether or not the specified rect is hovered. Outputs /// the specific side of the rect that was hovered, if any. /// </summary> private bool IsRectHovered(Rectangle displayRect, int x, int y, int maxDistanceToEdge, out PixmapSlicingRectSide hoveredSide) { PixmapSlicingRectSide side = this.GetHoveredRectSide(displayRect, x, y, maxDistanceToEdge); if (side != PixmapSlicingRectSide.None || displayRect.Contains(x, y)) { hoveredSide = side; return(true); } hoveredSide = PixmapSlicingRectSide.None; return(false); }
public static PixmapSlicingRectSide Opposite(this PixmapSlicingRectSide side) { switch (side) { case PixmapSlicingRectSide.Left: return(PixmapSlicingRectSide.Right); case PixmapSlicingRectSide.Right: return(PixmapSlicingRectSide.Left); case PixmapSlicingRectSide.Top: return(PixmapSlicingRectSide.Bottom); case PixmapSlicingRectSide.Bottom: return(PixmapSlicingRectSide.Top); default: return(PixmapSlicingRectSide.None); } }
protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (this.targetPixmap == null || this.targetPixmap.Atlas == null) { return; } PixmapSlicingRectSide prevHoveredSide = this.hoveredRectSide; int prevHoveredIndex = this.hoveredRectIndex; // Update hover states for slice rects. this.hoveredRectIndex = -1; this.hoveredRectSide = PixmapSlicingRectSide.None; int hoveredRectSideIndex = -1; int hoveredRectCenterIndex = -1; // Check the selected rect first if (this.selectedRectIndex != -1) { Rectangle displayRect = this.GetDisplayRect(this.targetPixmap.Atlas[this.selectedRectIndex]); PixmapSlicingRectSide side; if (this.IsRectHovered(displayRect, e.X, e.Y, 3, out side)) { if (side == PixmapSlicingRectSide.None) { hoveredRectCenterIndex = this.selectedRectIndex; } else { this.hoveredRectSide = side; hoveredRectSideIndex = this.selectedRectIndex; } } } // If none of the selected rects sides were hovered if (this.hoveredRectSide == PixmapSlicingRectSide.None) { // We're using reverse order so atlas rects with higher indices get // precedence on overlapping hover checks. for (int i = this.targetPixmap.Atlas.Count - 1; i >= 0; i--) { Rectangle displayRect = this.GetDisplayRect(this.targetPixmap.Atlas[i]); PixmapSlicingRectSide side; if (this.IsRectHovered(displayRect, e.X, e.Y, 3, out side)) { if (side == PixmapSlicingRectSide.None && hoveredRectCenterIndex == -1) { hoveredRectCenterIndex = i; } else if (side != PixmapSlicingRectSide.None) { this.hoveredRectSide = side; hoveredRectSideIndex = i; break; } } } } this.hoveredRectIndex = hoveredRectSideIndex != -1 ? hoveredRectSideIndex : hoveredRectCenterIndex; // Invalidate regions when changing hover states if (this.rectNumbering == PixmapNumberingStyle.Hovered && this.hoveredRectIndex != prevHoveredIndex) { this.InvalidatePixmap(this.GetAtlasRect(this.hoveredRectIndex)); this.InvalidatePixmap(this.GetAtlasRect(prevHoveredIndex)); } // Fire events for hover state changes if (this.hoveredRectIndex != prevHoveredIndex || this.hoveredRectSide != prevHoveredSide) { if (this.HoveredAtlasChanged != null) { this.HoveredAtlasChanged(this, EventArgs.Empty); } } }
private void EndDragRect() { this.draggedRectIndex = -1; this.draggedRectSide = PixmapSlicingRectSide.None; }
private void BeginDragRect(int index, PixmapSlicingRectSide side) { this.draggedRectIndex = index; this.draggedRectSide = side; }