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.Observe="True"
|
||||||
services:SizeObserver.ObservedWidth="{Binding ViewportWidth, Mode=OneWayToSource}"
|
services:SizeObserver.ObservedWidth="{Binding ViewportWidth, Mode=OneWayToSource}"
|
||||||
services:SizeObserver.ObservedHeight="{Binding ViewportHeight, Mode=OneWayToSource}"
|
services:SizeObserver.ObservedHeight="{Binding ViewportHeight, Mode=OneWayToSource}"
|
||||||
|
MouseWheel="ContentControl_MouseWheel"
|
||||||
|
MouseLeave="ContentControl_MouseLeave"
|
||||||
|
MouseMove="ContentControl_MouseMove"
|
||||||
>
|
>
|
||||||
<Grid Background="Transparent">
|
<Grid Background="Transparent">
|
||||||
<Grid.InputBindings>
|
<Grid.InputBindings>
|
||||||
@ -217,23 +220,22 @@
|
|||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</ItemsControl.ItemTemplate>
|
</ItemsControl.ItemTemplate>
|
||||||
</ItemsControl>
|
</ItemsControl>
|
||||||
<Grid VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="200" Height="20" Margin="0 0 5 5">
|
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Right" Background="#66ffffff">
|
||||||
<Grid.ColumnDefinitions>
|
<Grid Margin="10 5">
|
||||||
<ColumnDefinition Width="30"></ColumnDefinition>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition></ColumnDefinition>
|
<ColumnDefinition Width="90"></ColumnDefinition>
|
||||||
</Grid.ColumnDefinitions>
|
<ColumnDefinition Width="30"></ColumnDefinition>
|
||||||
<Border Background="White" CornerRadius="3" Margin="0 0 5 0">
|
</Grid.ColumnDefinitions>
|
||||||
<TextBlock Text="{Binding Scale, Mode=OneWay}" FontSize="14" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center" />
|
<TextBlock Padding="0 0 0 3">
|
||||||
</Border>
|
<TextBlock.Text>
|
||||||
<Slider
|
<MultiBinding StringFormat="{}{0:F0}, {1:F0}">
|
||||||
TickFrequency="1"
|
<Binding Path="MousePosition.X" Mode="OneWay"/>
|
||||||
IsSnapToTickEnabled="True"
|
<Binding Path="MousePosition.Y" Mode="OneWay"/>
|
||||||
Value="{Binding Scale}"
|
</MultiBinding>
|
||||||
Maximum="32"
|
</TextBlock.Text>
|
||||||
Minimum="1"
|
</TextBlock>
|
||||||
Grid.Column="1"
|
<TextBlock Grid.Column="1" Text="{Binding Scale,Mode=OneWay,StringFormat='{}1:{0}'}" HorizontalAlignment="Right" />
|
||||||
VerticalAlignment="Center"
|
</Grid>
|
||||||
/>
|
</StackPanel>
|
||||||
</Grid>
|
|
||||||
</Grid>
|
</Grid>
|
||||||
</ContentControl>
|
</ContentControl>
|
||||||
|
@ -24,5 +24,32 @@ namespace Client.Application.Components
|
|||||||
{
|
{
|
||||||
InitializeComponent();
|
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
|
0
|
||||||
);
|
);
|
||||||
public VitalStats VitalStats => creature.VitalStats;
|
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
|
public float Scale
|
||||||
{
|
{
|
||||||
get => scale;
|
get => scale;
|
||||||
@ -151,7 +151,7 @@ namespace Client.Application.ViewModels
|
|||||||
private readonly WorldHandler worldHandler;
|
private readonly WorldHandler worldHandler;
|
||||||
private float scale = 1;
|
private float scale = 1;
|
||||||
private static readonly float MAX_RADIUS = 10;
|
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);
|
private Vector3 vieportSize = new Vector3(0, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,7 @@ namespace Client.Application.ViewModels
|
|||||||
{
|
{
|
||||||
viewportWidth = value;
|
viewportWidth = value;
|
||||||
UpdateMap();
|
UpdateMap();
|
||||||
|
OnPropertyChanged("MousePosition");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,6 +62,7 @@ namespace Client.Application.ViewModels
|
|||||||
{
|
{
|
||||||
viewportHeight = value;
|
viewportHeight = value;
|
||||||
UpdateMap();
|
UpdateMap();
|
||||||
|
OnPropertyChanged("MousePosition");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,13 +77,24 @@ namespace Client.Application.ViewModels
|
|||||||
scale = value;
|
scale = value;
|
||||||
UpdateMap();
|
UpdateMap();
|
||||||
OnPropertyChanged();
|
OnPropertyChanged();
|
||||||
|
OnPropertyChanged("MousePosition");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Vector3 MousePosition
|
||||||
|
{
|
||||||
|
get => mousePosition;
|
||||||
|
set => mousePosition = value;
|
||||||
|
}
|
||||||
|
|
||||||
private void HeroPosition_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
private void HeroPosition_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
||||||
{
|
{
|
||||||
UpdateMap();
|
UpdateMap();
|
||||||
|
if (e.PropertyName == "X" || e.PropertyName == "Y")
|
||||||
|
{
|
||||||
|
OnPropertyChanged("MousePosition");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateMap()
|
private void UpdateMap()
|
||||||
@ -149,12 +162,45 @@ namespace Client.Application.ViewModels
|
|||||||
worldHandler.RequestMoveToLocation(location);
|
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)
|
public MapViewModel(WorldHandler worldHandler)
|
||||||
{
|
{
|
||||||
Creatures.CollectionChanged += Creatures_CollectionChanged;
|
Creatures.CollectionChanged += Creatures_CollectionChanged;
|
||||||
Drops.CollectionChanged += Drops_CollectionChanged;
|
Drops.CollectionChanged += Drops_CollectionChanged;
|
||||||
this.worldHandler = worldHandler;
|
this.worldHandler = worldHandler;
|
||||||
MouseLeftClickCommand = new RelayCommand(OnLeftMouseClick);
|
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)
|
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<CreatureMapViewModel> Creatures { get; } = new ObservableCollection<CreatureMapViewModel>();
|
||||||
public ObservableCollection<DropMapViewModel> Drops { get; } = new ObservableCollection<DropMapViewModel>();
|
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 MapImageSelector selector = new MapImageSelector();
|
||||||
private Dictionary<uint, MapBlockViewModel> blocks = new Dictionary<uint, MapBlockViewModel>();
|
private Dictionary<uint, MapBlockViewModel> blocks = new Dictionary<uint, MapBlockViewModel>();
|
||||||
private Hero? hero;
|
private Hero? hero;
|
||||||
private float scale = 8;
|
private float scale = 8;
|
||||||
private double viewportWidth = 0;
|
private double viewportWidth = 0;
|
||||||
private double viewportHeight = 0;
|
private double viewportHeight = 0;
|
||||||
|
private Vector3 mousePosition = new Vector3(0, 0, 0);
|
||||||
private readonly WorldHandler worldHandler;
|
private readonly WorldHandler worldHandler;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user