Skip to content

Commit

Permalink
Fix minimap mouse interaction (#553)
Browse files Browse the repository at this point in the history
* Rework minimap renderer

* Fix dragging

* Fix mouse over detection
  • Loading branch information
AlexMacocian authored Jan 29, 2024
1 parent d9aa760 commit f065223
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Daybreak/Controls/Minimap/GuildwarsMinimap.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<Grid>
<Grid RenderTransformOrigin="0.5 0.5">
<Grid.RenderTransform>
<RotateTransform Angle="{Binding ElementName=_this, Path=Angle, Mode=OneWay}"/>
<RotateTransform x:Name="RotateTransform" Angle="{Binding ElementName=_this, Path=Angle, Mode=OneWay}"/>
</Grid.RenderTransform>
<Image x:Name="MapDrawingHost" VerticalAlignment="Top" HorizontalAlignment="Left" Stretch="Fill" />
<Image x:Name="EntitiesDrawingHost" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
Expand Down
38 changes: 33 additions & 5 deletions Daybreak/Controls/Minimap/GuildwarsMinimap.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Daybreak.Models.FocusView;
using Daybreak.Models.LaunchConfigurations;
using System.Configuration;
using System.Runtime.CompilerServices;

namespace Daybreak.Controls.Minimap;

Expand Down Expand Up @@ -375,8 +376,10 @@ private bool MouseOverEntity(IPositionalEntity entity, Point mousePosition)
{
var x = (int)((entity.Position!.Value.X - this.originPoint.X) * this.Zoom);
var y = 0 - (int)((entity.Position!.Value.Y - this.originPoint.Y) * this.Zoom);

return Math.Pow(mousePosition.X - x, 2) + Math.Pow(mousePosition.Y - y, 2) < Math.Pow(EntitySize * this.Zoom, 2);
var entityPoint = new Point(x, y);
var centerPoint = new Point(this.ActualWidth / 2, this.ActualHeight / 2);
var finalEntityPoint = RotatePointAroundPivot(entityPoint, centerPoint, this.Angle);
return Math.Pow(mousePosition.X - finalEntityPoint.X, 2) + Math.Pow(mousePosition.Y - finalEntityPoint.Y, 2) < Math.Pow(EntitySize * this.Zoom, 2);
}

private void DragMinimap()
Expand All @@ -386,9 +389,14 @@ private void DragMinimap()
return;
}

var rad = -this.Angle * Math.PI / 180;
var mousePosition = Mouse.GetPosition(this);
var offset = mousePosition - this.initialClickPoint;
var transformedOffset = new Vector((offset.X * Math.Cos(rad)) - (offset.Y * Math.Sin(rad)),
(offset.X * Math.Sin(rad)) + (offset.Y * Math.Cos(rad)));
this.initialClickPoint = mousePosition;
this.offsetRevert = 0;
this.originOffset = mousePosition - this.initialClickPoint;
this.originOffset += transformedOffset;
this.offsetRevertTime = DateTime.Now + this.offsetRevertDelay;
this.UpdateGameData();
}
Expand Down Expand Up @@ -525,7 +533,7 @@ private Position ForceOnScreenPosition(Position entityPosition)

private void GuildwarsMinimap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.initialClickPoint = Mouse.GetPosition(this) - this.originOffset;
this.initialClickPoint = Mouse.GetPosition(this);
this.offsetRevert = 0;
this.offsetRevertTime = DateTime.Now + this.offsetRevertDelay;
this.dragging = true;
Expand Down Expand Up @@ -701,7 +709,7 @@ private void GuildwarsMinimap_MouseWheel(object sender, MouseWheelEventArgs e)
{
var delta = e.Delta > 0 ? 0.1 : -0.1;
var previousZoom = this.Zoom;
var newZoom = this.Zoom + this.Zoom * delta;
var newZoom = this.Zoom + (this.Zoom * delta);
this.originOffset *= newZoom / previousZoom;
this.initialClickPoint = new Point(
this.initialClickPoint.X * newZoom / previousZoom,
Expand Down Expand Up @@ -792,6 +800,26 @@ private void MaximizeButton_Clicked(object sender, EventArgs e)
}
}

private static Point RotatePointAroundPivot(Point point, Point pivot, double angle)
{
// Translate the point to the pivot point
point = new Point(point.X - pivot.X, point.Y - pivot.Y);

// Convert angle to radians for the rotation
double radians = Math.PI / 180 * angle;

// Rotate the point around the pivot
double rotatedX = (point.X * Math.Cos(radians)) - (point.Y * Math.Sin(radians));
double rotatedY = (point.X * Math.Sin(radians)) + (point.Y * Math.Cos(radians));

// Translate the point back
rotatedX += pivot.X;
rotatedY += pivot.Y;

return new Point(rotatedX, rotatedY);
}


private static bool PositionsCollide(Position position1, Position position2)
{
var a = PositionRadius + PositionRadius;
Expand Down
2 changes: 1 addition & 1 deletion Daybreak/Daybreak.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<LangVersion>preview</LangVersion>
<ApplicationIcon>Daybreak.ico</ApplicationIcon>
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
<Version>0.9.9.10</Version>
<Version>0.9.9.11</Version>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<UserSecretsId>cfb2a489-db80-448d-a969-80270f314c46</UserSecretsId>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Expand Down
1 change: 0 additions & 1 deletion Daybreak/Views/FocusView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Daybreak.Views.Trade;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Core.Extensions;
using System.Extensions;
Expand Down

0 comments on commit f065223

Please sign in to comment.