Skip to content

Commit

Permalink
Input field to set map center #236 + Disable zoom while moving map #185
Browse files Browse the repository at this point in the history
  • Loading branch information
jetelain committed Jun 8, 2024
1 parent c7b76ee commit f12022c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 25 deletions.
59 changes: 53 additions & 6 deletions GameRealisticMap.Studio/Controls/GrmMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public sealed class GrmMap : GrmMapLayerGroup
{
private Point start;
private Point origin;
private bool isChangeFromMap;

private static readonly double[] ZoomLevelToScale = new double[] { 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 6, 8, 12, 16, 20, 28, 36, 42, 50, 75, 100 };
private int zoomLevel = 3;
Expand Down Expand Up @@ -38,6 +39,40 @@ public GrmMap()

public MatrixTransform BaseDrawTransform { get; } = new MatrixTransform(1, 0, 0, -1, 0, 2500);

public TerrainPoint? ViewCenter
{
get { return (TerrainPoint)GetValue(ViewCenterProperty); }
set { SetValue(ViewCenterProperty, value); }
}

public static readonly DependencyProperty ViewCenterProperty =
DependencyProperty.Register("ViewCenter", typeof(TerrainPoint), typeof(GrmMap), new PropertyMetadata(new TerrainPoint(0, 0), ViewCenterChanged));

private static void ViewCenterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is TerrainPoint tp)
{
((GrmMap)d).ViewCenterChanged(tp);
}
}

private void ViewCenterChanged(TerrainPoint newValue)
{
if (!isChangeFromMap)
{
SetViewCenter(newValue);
}
}

public void SetViewCenter(TerrainPoint point)
{
var size = BaseDrawTransform.Matrix.OffsetY;
var actualSize = RenderSize;
Translate.X = - point.X * Scale + (actualSize.Width/2);
Translate.Y = (point.Y - size) * Scale + (actualSize.Height / 2);
ViewportChanged(false);
}

protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
{
base.OnMouseRightButtonDown(e);
Expand Down Expand Up @@ -71,6 +106,10 @@ protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)

protected override void OnMouseWheel(MouseWheelEventArgs e)
{
if (IsMouseCaptured)
{
return;
}
if ( e.Delta > 0 )
{
if (zoomLevel < ZoomLevelToScale.Length - 1)
Expand Down Expand Up @@ -132,11 +171,6 @@ private Envelope GetViewportEnveloppe(Size actualSize, double size)

public ITerrainEnvelope GetViewportEnveloppe() => GetViewportEnveloppe(RenderSize, SizeInMetersInternal);

public static Point ProjectToPoint(TerrainPoint point, float size)
{
return new Point(point.X, size - point.Y);
}

protected override void OnRender(DrawingContext dc)
{
var renderSize = RenderSize;
Expand Down Expand Up @@ -164,10 +198,16 @@ protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
base.OnRenderSizeChanged(sizeInfo);
}

private void ViewportChanged()
private void ViewportChanged(bool updateCenter = true)
{
InvalidateVisual();
RenderEnvelope = GetViewportEnveloppe(RenderSize, BaseDrawTransform.Matrix.OffsetY);
if (updateCenter)
{
isChangeFromMap = true;
ViewCenter = new TerrainPoint((RenderEnvelope.MaxPoint.Vector + RenderEnvelope.MinPoint.Vector) / 2);
isChangeFromMap = false;
}
OnViewportChanged();
}

Expand All @@ -186,5 +226,12 @@ public Point ProjectViewport(TerrainPoint point)
{
return Translate.Transform(ScaleTr.Transform(BaseDrawTransform.Transform(new Point(point.X, point.Y))));
}

public TerrainPoint UnprojectViewport(Point point)
{
return new TerrainPoint(
(float)((DeltaX + point.X) / Scale),
(float)(SizeInMetersInternal - ((DeltaY + point.Y) / Scale)));
}
}
}
40 changes: 40 additions & 0 deletions GameRealisticMap.Studio/Controls/TerrainPointStringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Globalization;
using System.Windows.Data;
using GameRealisticMap.Geometries;

namespace GameRealisticMap.Studio.Controls
{
public class TerrainPointStringConverter : IValueConverter
{
public string Format { get; set; } = "00000";

public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var sourceValue = value as TerrainPoint;
if (sourceValue != null)
{
return Math.Max(0, sourceValue.X).ToString(Format, culture) + " - " + Math.Max(0, sourceValue.Y).ToString(Format, culture);
}
return null;
}

public object? ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var sourceValue = value as string;
if (sourceValue != null)
{
var parts = sourceValue.Split('-');
if (parts.Length == 2)
{
if (float.TryParse(parts[0].Trim(), culture, out var x) && float.TryParse(parts[1].Trim(), culture, out var y))
{
return new TerrainPoint(x, y);
}
}
}
return null;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
xmlns:cal="http://caliburnmicro.com"
Foreground="{DynamicResource EnvironmentWindowText}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
d:DesignHeight="600" d:DesignWidth="1000">
<UserControl.Resources>
<a3:TexturePreviewConverter x:Key="TexturePreviewConverter" />
<grm:MultipleConverter Scale="10" x:Key="X10" />
<grm:TerrainPointStringConverter x:Key="TerrainPointStringConverter" />
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>
<Grid>
Expand Down Expand Up @@ -104,17 +105,20 @@
</RadioButton.ContextMenu>
</RadioButton>

<Rectangle Width="2" Margin="7 0" Fill="{x:Static SystemColors.ActiveBorderBrush}" />
<Image Source="pack://application:,,,/GameRealisticMap.Studio;component/Resources/Tools/center.png" Height="20" />
<TextBox Text="{Binding ViewCenter, Source={x:Reference GrmMap}, Converter={StaticResource TerrainPointStringConverter}, Mode=TwoWay}" Width="100" Margin="4" Keyboard.KeyDown="TextBox_KeyDown" TextAlignment="Center" />

</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5" Grid.Column="1">
<Button Padding="5 0" cal:Message.Attach="TakeAerialImages">
<Run Text="{x:Static r:Labels.TakeAerialImages}"/>
</Button>
</StackPanel>
<grm:GrmMap Grid.Row="1" SizeInMeters="{Binding SizeInMeters}" Grid.ColumnSpan="2">
<grm:GrmMap Grid.Row="1" SizeInMeters="{Binding SizeInMeters}" Grid.ColumnSpan="2" x:Name="GrmMap">

<grm:GrmMapEditLayer EditPoints="{Binding EditPoints}" ClearSelection="{Binding SelectItemCommand}" InsertPointCommand="{Binding InsertPointCommand}" EditMode="{Binding GrmMapEditMode, Mode=TwoWay}" Outline="{Binding SelectedItems}">

<grm:GrmMapHugeImageLayer Image="{Binding BackgroundImage}" Resolution="{Binding BackgroundResolution}" Opacity="0.8" IsHitTestVisible="False" />

<grm:GrmMapArma3 x:Name="A3Layer" Roads="{Binding Roads}" Objects="{Binding Objects}" SelectItem="{Binding SelectItemCommand}" AddToSelectionCommand="{Binding AddToSelectionCommand}" />
Expand All @@ -133,11 +137,9 @@
</MenuItem>
</ContextMenu>
</grm:GrmMapEditLayer.SelectionContextMenu>

</grm:GrmMapEditLayer>

</grm:GrmMap>


</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GameRealisticMap.Studio.Modules.Arma3WorldEditor.Views
{
Expand All @@ -24,5 +13,13 @@ public Arma3WorldMapView()
{
InitializeComponent();
}

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if ( e.Key == Key.Enter)
{
BindingOperations.GetBindingExpression((TextBox)sender, TextBox.TextProperty).UpdateSource();
}
}
}
}
2 changes: 1 addition & 1 deletion GameRealisticMap.Studio/Resources/Tools/CREDITS.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Icons made by Freepik from https://www.flaticon.com/
Icons made by Freepik and Dewi Sari from https://www.flaticon.com/
Binary file added GameRealisticMap.Studio/Resources/Tools/center.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f12022c

Please sign in to comment.