From bb4538794b2b322963880d9bf39a4df8722194a5 Mon Sep 17 00:00:00 2001 From: k0t9i Date: Fri, 10 Feb 2023 13:58:54 +0400 Subject: [PATCH] feat: change zoom action to mouse wheel --- Client/Application/Components/Map.xaml | 38 +++++++------- Client/Application/Components/Map.xaml.cs | 27 ++++++++++ .../ViewModels/CreatureMapViewModel.cs | 4 +- Client/Application/ViewModels/MapViewModel.cs | 49 +++++++++++++++++++ 4 files changed, 98 insertions(+), 20 deletions(-) diff --git a/Client/Application/Components/Map.xaml b/Client/Application/Components/Map.xaml index 042ea32..fa114be 100644 --- a/Client/Application/Components/Map.xaml +++ b/Client/Application/Components/Map.xaml @@ -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" > @@ -217,23 +220,22 @@ - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/Client/Application/Components/Map.xaml.cs b/Client/Application/Components/Map.xaml.cs index e326a9b..644ee60 100644 --- a/Client/Application/Components/Map.xaml.cs +++ b/Client/Application/Components/Map.xaml.cs @@ -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); + } + } } } diff --git a/Client/Application/ViewModels/CreatureMapViewModel.cs b/Client/Application/ViewModels/CreatureMapViewModel.cs index 39323b4..2534d8a 100644 --- a/Client/Application/ViewModels/CreatureMapViewModel.cs +++ b/Client/Application/ViewModels/CreatureMapViewModel.cs @@ -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); } } diff --git a/Client/Application/ViewModels/MapViewModel.cs b/Client/Application/ViewModels/MapViewModel.cs index fe9c153..fa73387 100644 --- a/Client/Application/ViewModels/MapViewModel.cs +++ b/Client/Application/ViewModels/MapViewModel.cs @@ -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 Creatures { get; } = new ObservableCollection(); public ObservableCollection Drops { get; } = new ObservableCollection(); + public readonly static float MIN_SCALE = 1; + public readonly static float MAX_SCALE = 64; private MapImageSelector selector = new MapImageSelector(); private Dictionary blocks = new Dictionary(); 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; } }