/// <summary> /// On mouse down /// </summary> protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); Focus(); if (Canvas == null || _pixels.Count <= 0) { return; } Point origin = GetOrigin(); Rectangle rect = new Rectangle(origin.X * ImageScale + AutoScrollPosition.X, origin.Y * ImageScale + AutoScrollPosition.Y, Canvas.Width * ImageScale, Canvas.Height * ImageScale); if (rect.Contains(e.Location) == false) { return; } int x = (e.Location.X - rect.X) / ImageScale / SnapSize.Width * SnapSize.Width; int y = (e.Location.Y - rect.Y) / ImageScale / SnapSize.Height * SnapSize.Height; Rectangle selection = new Rectangle(new Point(x, y), SnapSize); int cols = Canvas.Width / SnapSize.Width; int col = selection.X / SnapSize.Width; int row = selection.Y / SnapSize.Height; int tileID = (row * cols) + col; int count = _pixels.Count / (SnapSize.Width * SnapSize.Height); if (tileID >= count) { return; } _selection = selection; if (e.Button == MouseButtons.Right) { _source = tileID; TileSelectionChanged?.Invoke(); return; } if (_source == -1) { _source = tileID; TileSelectionChanged?.Invoke(); } else { _target = tileID; SwapTiles(); } UpdateBackBuffer(); }
/// <summary> /// Mouse down override /// </summary> protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); Focus(); if (Image == null || !AllowSelection) { return; } if (e.Button != MouseButtons.Left) { return; } Point snap = GetSnappedPoint(e.Location); if (new Rectangle(0, 0, Image.Width, Image.Height).Contains(snap) == false) { return; } int cols = Image.Width / SnapSize.Width; int col = snap.X / SnapSize.Width; int row = snap.Y / SnapSize.Height; int tileID = (row * cols) + col; if (tileID >= TileCount) { return; } _selection = new Rectangle(snap, SnapSize); _tileID = tileID; TileSelectionChanged?.Invoke(); }