/// <summary> /// On mouse move /// </summary> protected override void OnMouseMove(MouseEventArgs e) { // Do base mouse move base.OnMouseMove(e); // If not pressing the left mouse button or if the image is empty or fixed selection, return if (_avoidMouseEvents || e.Button != MouseButtons.Left || Image == null || _selectMode == SelectType.Fixed) { _avoidMouseEvents = false; return; } // Get the snapped point Point snap = GetSnappedPoint(e.Location); snap.X += snap.X >= _origin.X ? SnapSize.Width : 0; snap.Y += snap.Y >= _origin.Y ? SnapSize.Height : 0; // Get selection rectangle Rectangle rect = new Rectangle(); rect.X = Math.Max(Math.Min(_origin.X, snap.X), 0); rect.Y = Math.Max(Math.Min(_origin.Y, snap.Y), 0); rect.Width = Math.Abs(Math.Max(snap.X, 0) - _origin.X); rect.Height = Math.Abs(Math.Max(snap.Y, 0) - _origin.Y); // Stay in bounds rect.Width = rect.Right > Image.Width ? Image.Width - rect.X : rect.Width; rect.Height = rect.Bottom > Image.Height ? Image.Height - rect.Y : rect.Height; rect.Width = rect.Width <= 0 ? SnapSize.Width : rect.Width; rect.Height = rect.Height <= 0 ? SnapSize.Height : rect.Height; // If there has been no change in movement, return if (rect == _selection) { return; } // Set new selection rectangle _selection = rect; // Create tiles based on rectangle _tileBrush = GMareBrush.RectangleToTileBrush(_selection, ImageWidthUnscaled, SnapSize); // Update UpdateBackBuffer(); }
/// <summary> /// On image set /// </summary> /// <param name="image">Image reference</param> protected override void OnImageSet(ref Bitmap image) { // Scale the image by the image scale value ScaleImage(); // Set new selection rectangle _selection = new Rectangle(Point.Empty, SnapSize); // Create new tile brush if (!DesignMode) { _tileBrush = new GMareBrush(); } // Reset selection _tileBrush = GMareBrush.RectangleToTileBrush(new Rectangle(Point.Empty, SnapSize), SnapSize.Width, SnapSize); // Update UpdateBackBuffer(); // Scroll to zero AutoScrollPosition = Point.Empty; }
/// <summary> /// On mouse down /// </summary> protected override void OnMouseDown(MouseEventArgs e) { // Do base mouse down base.OnMouseDown(e); // Set focus on this control Focus(); // If the image is empty or not pressing the left mouse button, return if (Image == null) { return; } // If the right mouse button was clicked, show brush information if (e.Button == MouseButtons.Right && _tileBrush != null) { _tileIdTip.Hide(this); _tileIdTip.Show(_tileBrush.To2DArrayString(), this, 5000); } // If the left mouse button was not clicked, return if (e.Button != MouseButtons.Left) { return; } // Get the snapped point Point snap = GetSnappedPoint(e.Location); // If not within bounds if (new Rectangle(0, 0, Image.Width, Image.Height).Contains(snap) == false) { return; } // Starting point of the selection _origin = snap; // Set new selection rectangle switch (_selectMode) { case SelectType.Normal: _selection = new Rectangle(_origin, SnapSize); break; case SelectType.Fixed: _selection = new Rectangle(_origin, _selection.Size); // If the selection is out of bounds, reposition till it is not while (true) { // Move more in bounds _selection.X -= _selection.Right > Image.Width ? SnapSize.Width : 0; _selection.Y -= _selection.Bottom > Image.Height ? SnapSize.Height : 0; // If within bounds, break if (new Rectangle(0, 0, Image.Width, Image.Height).Contains(_selection) == true) { break; } } break; } // Create tiles based on rectangle _tileBrush = GMareBrush.RectangleToTileBrush(_selection, ImageWidthUnscaled, SnapSize); // Update UpdateBackBuffer(); }