Skip to content

Commit

Permalink
Add middle mouse button drag scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
MortonPL committed Jan 26, 2024
1 parent 7c22fa5 commit b66d978
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/TSMapEditor/Rendering/MapView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class MapView : XNAControl, ICursorActionTarget, IMutationTarget, IMapVie
{
private const float RightClickScrollRateDivisor = 64f;
private const double ZoomStep = 0.1;
private const float DragScrollRateDivisor = 16f;

private static Color[] MarbleMadnessTileHeightLevelColors = new Color[]
{
Expand Down Expand Up @@ -174,6 +175,10 @@ public CursorAction CursorAction
private bool isRightClickScrolling = false;
private Point rightClickScrollInitPos = new Point(-1, -1);

private bool isDragScrolling = false;
private Point dragScrollInitPos = new Point(-1, -1);
private Vector2 dragScrollInitCameraPos = new Vector2(-1, -1);

private Point lastClickedPoint;

private List<GameObject> gameObjectsToRender = new List<GameObject>();
Expand Down Expand Up @@ -1157,10 +1162,13 @@ private void DrawRangeIndicator(Point2D cellCoords, double range, Color color)

public override void OnMouseScrolled()
{
if (Cursor.ScrollWheelValue > 0)
Camera.ZoomLevel += ZoomStep;
else
Camera.ZoomLevel -= ZoomStep;
if (!isDragScrolling)
{
if (Cursor.ScrollWheelValue > 0)
Camera.ZoomLevel += ZoomStep;
else
Camera.ZoomLevel -= ZoomStep;
}

base.OnMouseScrolled();
}
Expand Down Expand Up @@ -1273,13 +1281,35 @@ public override void OnMouseMove()
// Right-click scrolling
if (Cursor.RightDown)
{
if (!isRightClickScrolling)
if (!isRightClickScrolling && !isDragScrolling)
{
isRightClickScrolling = true;
rightClickScrollInitPos = GetCursorPoint();
Camera.FloatTopLeftPoint = Camera.TopLeftPoint.ToXNAVector();
}
}

// Drag scrolling
if (Cursor.MiddleDown)
{
if (!isRightClickScrolling)
{
if (!isDragScrolling)
{
dragScrollInitPos = GetCursorPoint();
dragScrollInitCameraPos = Camera.FloatTopLeftPoint;
isDragScrolling = true;
}
else
{
var result = dragScrollInitPos - GetCursorPoint();
float dragScrollRate = (float)((scrollRate / DragScrollRateDivisor) / Camera.ZoomLevel);

Camera.FloatTopLeftPoint = new Vector2(dragScrollInitCameraPos.X + result.X * dragScrollRate,
dragScrollInitCameraPos.Y + result.Y * dragScrollRate);
}
}
}
}

public override void OnLeftClick()
Expand Down Expand Up @@ -1335,6 +1365,13 @@ public override void OnRightClick()
base.OnRightClick();
}

public override void OnMiddleClick()
{
isDragScrolling = false;

base.OnMiddleClick();
}

public override void Update(GameTime gameTime)
{
// Make scroll rate independent of FPS
Expand Down

0 comments on commit b66d978

Please sign in to comment.