feat: change zoom action to mouse wheel
This commit is contained in:
parent
123d039263
commit
bb4538794b
@ -9,6 +9,9 @@
|
||||
services:SizeObserver.Observe="True"
|
||||
services:SizeObserver.ObservedWidth="{Binding ViewportWidth, Mode=OneWayToSource}"
|
||||
services:SizeObserver.ObservedHeight="{Binding ViewportHeight, Mode=OneWayToSource}"
|
||||
MouseWheel="ContentControl_MouseWheel"
|
||||
MouseLeave="ContentControl_MouseLeave"
|
||||
MouseMove="ContentControl_MouseMove"
|
||||
>
|
||||
<Grid Background="Transparent">
|
||||
<Grid.InputBindings>
|
||||
@ -217,23 +220,22 @@
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
<Grid VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="200" Height="20" Margin="0 0 5 5">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="30"></ColumnDefinition>
|
||||
<ColumnDefinition></ColumnDefinition>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Border Background="White" CornerRadius="3" Margin="0 0 5 0">
|
||||
<TextBlock Text="{Binding Scale, Mode=OneWay}" FontSize="14" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center" />
|
||||
</Border>
|
||||
<Slider
|
||||
TickFrequency="1"
|
||||
IsSnapToTickEnabled="True"
|
||||
Value="{Binding Scale}"
|
||||
Maximum="32"
|
||||
Minimum="1"
|
||||
Grid.Column="1"
|
||||
VerticalAlignment="Center"
|
||||
/>
|
||||
</Grid>
|
||||
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Right" Background="#66ffffff">
|
||||
<Grid Margin="10 5">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="90"></ColumnDefinition>
|
||||
<ColumnDefinition Width="30"></ColumnDefinition>
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Padding="0 0 0 3">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding StringFormat="{}{0:F0}, {1:F0}">
|
||||
<Binding Path="MousePosition.X" Mode="OneWay"/>
|
||||
<Binding Path="MousePosition.Y" Mode="OneWay"/>
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock Grid.Column="1" Text="{Binding Scale,Mode=OneWay,StringFormat='{}1:{0}'}" HorizontalAlignment="Right" />
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</ContentControl>
|
||||
|
@ -24,5 +24,32 @@ namespace Client.Application.Components
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void ContentControl_MouseWheel(object sender, MouseWheelEventArgs e)
|
||||
{
|
||||
if (DataContext is MapViewModel)
|
||||
{
|
||||
var model = (MapViewModel)DataContext;
|
||||
model.OnMouseWheel(sender, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void ContentControl_MouseLeave(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (DataContext is MapViewModel)
|
||||
{
|
||||
var model = (MapViewModel)DataContext;
|
||||
model.OnMouseLeave(sender, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void ContentControl_MouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (DataContext is MapViewModel)
|
||||
{
|
||||
var model = (MapViewModel)DataContext;
|
||||
model.OnMouseMove(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ namespace Client.Application.ViewModels
|
||||
0
|
||||
);
|
||||
public VitalStats VitalStats => creature.VitalStats;
|
||||
public float Radius => MathF.Max(MAX_RADIUS / scale, MIN_RADIUS);
|
||||
public float Radius => MAX_RADIUS - (1/MapViewModel.MIN_SCALE - 1/scale) / (1/MapViewModel.MIN_SCALE - 1/MapViewModel.MAX_SCALE) * (MAX_RADIUS - MIN_RADIUS);
|
||||
public float Scale
|
||||
{
|
||||
get => scale;
|
||||
@ -151,7 +151,7 @@ namespace Client.Application.ViewModels
|
||||
private readonly WorldHandler worldHandler;
|
||||
private float scale = 1;
|
||||
private static readonly float MAX_RADIUS = 10;
|
||||
private static readonly float MIN_RADIUS = 4;
|
||||
private static readonly float MIN_RADIUS = 2;
|
||||
private Vector3 vieportSize = new Vector3(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
@ -49,6 +49,7 @@ namespace Client.Application.ViewModels
|
||||
{
|
||||
viewportWidth = value;
|
||||
UpdateMap();
|
||||
OnPropertyChanged("MousePosition");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -61,6 +62,7 @@ namespace Client.Application.ViewModels
|
||||
{
|
||||
viewportHeight = value;
|
||||
UpdateMap();
|
||||
OnPropertyChanged("MousePosition");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -75,13 +77,24 @@ namespace Client.Application.ViewModels
|
||||
scale = value;
|
||||
UpdateMap();
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged("MousePosition");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 MousePosition
|
||||
{
|
||||
get => mousePosition;
|
||||
set => mousePosition = value;
|
||||
}
|
||||
|
||||
private void HeroPosition_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
||||
{
|
||||
UpdateMap();
|
||||
if (e.PropertyName == "X" || e.PropertyName == "Y")
|
||||
{
|
||||
OnPropertyChanged("MousePosition");
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateMap()
|
||||
@ -149,12 +162,45 @@ namespace Client.Application.ViewModels
|
||||
worldHandler.RequestMoveToLocation(location);
|
||||
}
|
||||
|
||||
public void OnMouseWheel(object sender, MouseWheelEventArgs e)
|
||||
{
|
||||
var newScale = Scale - e.Delta / Mouse.MouseWheelDeltaForOneLine;
|
||||
Scale = MathF.Max(MathF.Min(newScale, MAX_SCALE), MIN_SCALE);
|
||||
}
|
||||
|
||||
public void OnMouseLeave(object sender, MouseEventArgs e)
|
||||
{
|
||||
mousePosition.X = 0;
|
||||
mousePosition.Y = 0;
|
||||
}
|
||||
|
||||
public void OnMouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (hero == null)
|
||||
{
|
||||
mousePosition.X = 0;
|
||||
mousePosition.Y = 0;
|
||||
return;
|
||||
}
|
||||
var el = (IInputElement)sender;
|
||||
var mousePos = e.GetPosition(el);
|
||||
|
||||
mousePosition.X = (float)(mousePos.X - ViewportWidth / 2) * scale + hero.Transform.Position.X;
|
||||
mousePosition.Y = (float)(mousePos.Y - ViewportHeight / 2) * scale + hero.Transform.Position.Y;
|
||||
}
|
||||
|
||||
public MapViewModel(WorldHandler worldHandler)
|
||||
{
|
||||
Creatures.CollectionChanged += Creatures_CollectionChanged;
|
||||
Drops.CollectionChanged += Drops_CollectionChanged;
|
||||
this.worldHandler = worldHandler;
|
||||
MouseLeftClickCommand = new RelayCommand(OnLeftMouseClick);
|
||||
mousePosition.PropertyChanged += MousePosition_PropertyChanged;
|
||||
}
|
||||
|
||||
private void MousePosition_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
||||
{
|
||||
OnPropertyChanged("MousePosition");
|
||||
}
|
||||
|
||||
private void Drops_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
|
||||
@ -187,12 +233,15 @@ namespace Client.Application.ViewModels
|
||||
public ObservableCollection<CreatureMapViewModel> Creatures { get; } = new ObservableCollection<CreatureMapViewModel>();
|
||||
public ObservableCollection<DropMapViewModel> Drops { get; } = new ObservableCollection<DropMapViewModel>();
|
||||
|
||||
public readonly static float MIN_SCALE = 1;
|
||||
public readonly static float MAX_SCALE = 64;
|
||||
private MapImageSelector selector = new MapImageSelector();
|
||||
private Dictionary<uint, MapBlockViewModel> blocks = new Dictionary<uint, MapBlockViewModel>();
|
||||
private Hero? hero;
|
||||
private float scale = 8;
|
||||
private double viewportWidth = 0;
|
||||
private double viewportHeight = 0;
|
||||
private Vector3 mousePosition = new Vector3(0, 0, 0);
|
||||
private readonly WorldHandler worldHandler;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user