Skip to content

Commit

Permalink
TilesetView Scrolling (#687)
Browse files Browse the repository at this point in the history
#ADD: Added middle mouse scrolling to TIlemapView.
#ADD: Added Shift+click to allow user to continue selection with same 'anchor'.
  • Loading branch information
helle253 authored and ilexp committed Oct 13, 2018
1 parent 42da23f commit 4f19c4c
Showing 1 changed file with 74 additions and 6 deletions.
80 changes: 74 additions & 6 deletions Source/Plugins/Tilemaps/Editor/Modules/SourcePaletteTilesetView.cs
Expand Up @@ -16,7 +16,11 @@ public class SourcePaletteTilesetView : TilesetView
private Rectangle selectedArea = Rectangle.Empty;
private Grid<Tile> selectedTiles = new Grid<Tile>();
private Point actionBeginTilePos = Point.Empty;
private bool areTilesSelected = false;
private bool isUserSelecting = false;
private bool isUserScrolling = false;
private int lastMouseX = -1;
private int lastMouseY = -1;

public event EventHandler SelectedAreaChanged = null;
public event EventHandler SelectedAreaEditingFinished = null;
Expand Down Expand Up @@ -186,30 +190,84 @@ protected override void OnPaintTiles(PaintEventArgs e)
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
if (e.Button == MouseButtons.Left && Control.ModifierKeys == Keys.Shift && !this.isUserScrolling)
{
int tileIndex = this.PickTileIndexAt(e.X, e.Y);
if (tileIndex != -1)
{
this.isUserSelecting = true;
this.HoveredTileIndex = -1;
if (tileIndex != -1 && !this.areTilesSelected)
{
this.areTilesSelected = true;
this.actionBeginTilePos = this.GetTilePos(tileIndex);
this.isUserSelecting = true;
this.SelectedArea = new Rectangle(
this.GetDisplayedTilePos(this.actionBeginTilePos.X, this.actionBeginTilePos.Y),
new Size(1, 1));
this.HoveredTileIndex = -1;
}
else if (tileIndex != -1)
{
Point tilePos = this.GetTilePos(tileIndex);
Point displayedBeginPos = this.GetDisplayedTilePos(this.actionBeginTilePos.X, this.actionBeginTilePos.Y);
Point displayedCurrentPos = this.GetDisplayedTilePos(tilePos.X, tilePos.Y);

Point selectionTopLeft = new Point(
Math.Min(displayedBeginPos.X, displayedCurrentPos.X),
Math.Min(displayedBeginPos.Y, displayedCurrentPos.Y));
Point selectionBottomRight = new Point(
Math.Max(displayedBeginPos.X, displayedCurrentPos.X),
Math.Max(displayedBeginPos.Y, displayedCurrentPos.Y));

this.SelectedArea = new Rectangle(
selectionTopLeft.X,
selectionTopLeft.Y,
selectionBottomRight.X - selectionTopLeft.X + 1,
selectionBottomRight.Y - selectionTopLeft.Y + 1);
}
}
}
else if (e.Button == MouseButtons.Left && !this.isUserScrolling)
{
int tileIndex = this.PickTileIndexAt(e.X, e.Y);
if (tileIndex != -1)
{
this.areTilesSelected = true;
this.actionBeginTilePos = this.GetTilePos(tileIndex);
this.isUserSelecting = true;
this.SelectedArea = new Rectangle(
this.GetDisplayedTilePos(this.actionBeginTilePos.X, this.actionBeginTilePos.Y),
this.GetDisplayedTilePos(this.actionBeginTilePos.X, this.actionBeginTilePos.Y),
new Size(1, 1));
this.HoveredTileIndex = -1;
}
}
else if (e.Button == MouseButtons.Middle && !this.isUserSelecting)
{
this.isUserScrolling = true;
this.lastMouseX = e.X;
this.lastMouseY = e.Y;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (!this.isUserSelecting)
if (!this.isUserSelecting && !this.isUserScrolling)
{
this.SelectedArea = Rectangle.Empty;
this.areTilesSelected = false;
}
if (e.Button == MouseButtons.Middle)
{
this.isUserScrolling = false;
this.lastMouseX = -1;
this.lastMouseY = -1;
}
if (e.Button == MouseButtons.Left)
{
this.isUserSelecting = false;
this.RaiseSelectedAreaEditingFinished();
}
this.actionBeginTilePos = Point.Empty;
this.isUserSelecting = false;
this.RaiseSelectedAreaEditingFinished();
}
protected override void OnMouseMove(MouseEventArgs e)
{
Expand All @@ -236,6 +294,16 @@ protected override void OnMouseMove(MouseEventArgs e)
selectionBottomRight.Y - selectionTopLeft.Y + 1);
}
}
else if (this.isUserScrolling)
{
int newHoz = this.HorizontalScroll.Value - e.X + this.lastMouseX;
MathF.Clamp(newHoz, this.HorizontalScroll.Minimum, this.HorizontalScroll.Maximum);
int newVert = this.VerticalScroll.Value - e.Y + this.lastMouseY;
MathF.Clamp(newVert, this.VerticalScroll.Minimum, this.VerticalScroll.Maximum);
this.AutoScrollPosition = new Point(newHoz, newVert);
this.lastMouseX = e.X;
this.lastMouseY = e.Y;
}
else
{
int lastHovered = this.HoveredTileIndex;
Expand Down

0 comments on commit 4f19c4c

Please sign in to comment.