/// <summary> /// Used to draw the currently selected tiles. /// </summary> /// <param name="eventReference"> /// A reference to the <see cref="Event"/>. /// </param> protected virtual void DrawTileSelection(Event eventReference) { // if no event reference provided just exit if (eventReference == null) { return; } // used to store the top left tile coordinates of the tile position Point tilePosition; // determine if the tile set starts with spacing var startOffset = this.StartSpacing ? this.Spacing : 0; // convert the mouse position into a tile position var mousePosition = eventReference.mousePosition; tilePosition.X = (int)((mousePosition.x - startOffset) / ((this.TileWidth * this.Zoom) + this.Spacing)); tilePosition.Y = (int)((mousePosition.y - startOffset) / ((this.TileHeight * this.Zoom) + this.Spacing)); // calculate the min and max positions based on the location of the mouse and the first selected tile location this.min = new Vector2(Math.Min(this.firstTileLocation.X, tilePosition.X), Math.Min(this.firstTileLocation.Y, tilePosition.Y)); if (this.MultipleTileSelection) { this.max = new Vector2(Math.Max(this.firstTileLocation.X, tilePosition.X), Math.Max(this.firstTileLocation.Y, tilePosition.Y)); } else { this.max = this.min; } // cap the min max values to the dimensions of the texture this.min = this.CapValues(this.min); this.max = this.CapValues(this.max) + new Point(1, 1); // determine if user is selecting if (this.isSelecting && eventReference.type == EventType.MouseDrag && eventReference.button == 0) { // this event has been used eventReference.Use(); // clear any previous selection this.selectedTiles.Clear(); // check if allowed to select multiple tiles if (this.MultipleTileSelection) { // add tile entries for the selection for (var idx = this.min.X; idx < this.max.X; idx++) { for (var idy = this.min.Y; idy < this.max.Y; idy++) { this.selectedTiles.Add(new Vector2(idx, idy)); } } } else { this.firstTileLocation = tilePosition; // clear any previously selected tiles and add the first tile location this.selectedTiles.Add(this.firstTileLocation); } // save last mouse position this.selectionStatus = TileSelectionStatus.Selecting; this.OnTileSelection(); } // draw selected tiles float x; float y; foreach (var tile in this.selectedTiles) { // calculate rectangle Top Left for tile location x = startOffset + (tile.X * ((this.TileWidth * this.Zoom) + this.Spacing)); y = startOffset + (tile.Y * ((this.TileHeight * this.Zoom) + this.Spacing)); // draw the selected tile rectangle Helpers.DrawRect( new Rect( x, y, (this.TileWidth * this.Zoom) + this.Spacing - startOffset, (this.TileHeight * this.Zoom) + this.Spacing - startOffset), this.SelectionRectangleColor); } // draw blue rectangle indicating what tile the mouse is hovering over x = startOffset + (tilePosition.X * ((this.TileWidth * this.Zoom) + this.Spacing)); y = startOffset + (tilePosition.Y * ((this.TileHeight * this.Zoom) + this.Spacing)); if (this.ShowHoverRectangle) { Helpers.DrawRect( new Rect(x, y, (this.TileWidth * this.Zoom) + this.Spacing - startOffset, (this.TileHeight * this.Zoom) + this.Spacing - startOffset), this.HoverRectangleColor); } // check if were beginning to select tiles with the left mouse button if (!this.isSelecting && eventReference.isMouse && eventReference.type == EventType.MouseDown && eventReference.button == 0) { // this event has been used eventReference.Use(); this.isSelecting = true; this.firstTileLocation = tilePosition; // clear any previously selected tiles and add the first tile location this.selectedTiles.Clear(); this.selectedTiles.Add(this.firstTileLocation); // save last mouse position this.selectionStatus = TileSelectionStatus.Begin; this.OnTileSelection(); } // check if were releasing left mouse button (IE stop selecting) if (this.isSelecting && eventReference.isMouse && eventReference.type == EventType.MouseUp && eventReference.button == 0) { // this event has been used eventReference.Use(); this.isSelecting = false; this.selectionStatus = TileSelectionStatus.Complete; this.OnTileSelection(); } }
/// <summary> /// Used to draw the current free form selection rectangle. /// </summary> /// <param name="eventReference"> /// A reference to the <see cref="Event"/>. /// </param> protected virtual void DrawFreeFormSelection(Event eventReference) { // get mouse position Point mousePosition = eventReference.mousePosition; // determine if user is selecting if (this.isSelecting && eventReference.type == EventType.MouseDrag && eventReference.button == 0) { // this event has been used eventReference.Use(); // clear any previous selection this.selectedTiles.Clear(); // calculate the min and max positions based on the location of the mouse and the first position of mouse down var minVector = new Vector2(Math.Min(this.firstTileLocation.X, mousePosition.X), Math.Min(this.firstTileLocation.Y, mousePosition.Y)) / new Vector2(1f / this.zoom); var maxVector = new Vector2(Math.Max(this.firstTileLocation.X, mousePosition.X), Math.Max(this.firstTileLocation.Y, mousePosition.Y)) / new Vector2(1f / this.zoom); // set the free form rectangle this.FreeFormRectangle = new Rect(minVector.X, minVector.Y, maxVector.X - minVector.X, maxVector.Y - minVector.Y); // raise the selection changed event this.selectionStatus = TileSelectionStatus.Selecting; this.OnFreeFormSelection(); } // draw the selected free form rectangle Helpers.DrawRect( new Rect( this.FreeFormRectangle.x * this.zoom, this.FreeFormRectangle.y * this.zoom, this.FreeFormRectangle.width * this.zoom, this.FreeFormRectangle.height * this.zoom), this.SelectionRectangleColor); // check if were beginning to select tiles with the left mouse button if (!this.isSelecting && eventReference.isMouse && eventReference.type == EventType.MouseDown && eventReference.button == 0) { this.isSelecting = true; this.firstTileLocation = mousePosition; // clear any previously selected tiles and add the first tile location this.selectedTiles.Clear(); this.selectedTiles.Add(this.firstTileLocation); // raise the selection changed event this.selectionStatus = TileSelectionStatus.Begin; this.OnFreeFormSelection(); // this event has been used eventReference.Use(); } // check if were releasing left mouse button (IE stop selecting) if (this.isSelecting && eventReference.isMouse && eventReference.type == EventType.MouseUp && eventReference.button == 0) { // this event has been used eventReference.Use(); this.isSelecting = false; this.selectionStatus = TileSelectionStatus.Complete; this.OnFreeFormSelection(); } }