diff --git a/src/HexManiac.Core/ViewModels/Map/MapEditorViewModel.cs b/src/HexManiac.Core/ViewModels/Map/MapEditorViewModel.cs index 8a4df1a2..0d9a658d 100644 --- a/src/HexManiac.Core/ViewModels/Map/MapEditorViewModel.cs +++ b/src/HexManiac.Core/ViewModels/Map/MapEditorViewModel.cs @@ -83,7 +83,6 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map { public event EventHandler RequestCanDiff; public event EventHandler RequestCanCreatePatch; public event EventHandler RequestCreatePatch; - public event PropertyChangedEventHandler? PropertyChanged; public void Duplicate() { } public void Refresh() { @@ -149,6 +148,20 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map { private double cursorX, cursorY, deltaX, deltaY; + private double highlightCursorX, highlightCursorY, highlightCursorSize; + public double HighlightCursorX { get => highlightCursorX; set => Set(ref highlightCursorX, value); } + public double HighlightCursorY { get => highlightCursorY; set => Set(ref highlightCursorY, value); } + public double HighlightCursorSize { get => highlightCursorSize; set => Set(ref highlightCursorSize, value); } + + public void Hover(double x, double y) { + var map = MapUnderCursor(x, y); + if (map == null) return; + var dx = (int)((x - map.LeftEdge) / 16 / map.SpriteScale) + .5; + var dy = (int)((y - map.TopEdge) / 16 / map.SpriteScale) + .5; + (HighlightCursorX, HighlightCursorY) = (map.LeftEdge + dx * 16 * map.SpriteScale, map.TopEdge + dy * 16 * map.SpriteScale); + HighlightCursorSize = 16 * map.SpriteScale + 4; + } + public void DragDown(double x, double y) { (cursorX, cursorY) = (x, y); (deltaX, deltaY) = (0, 0); @@ -167,6 +180,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map { } deltaX -= (int)deltaX; deltaY -= (int)deltaY; + Hover(x, y); } public void DragUp(double x, double y) { @@ -178,6 +192,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map { public void DrawMove(double x, double y) { var map = MapUnderCursor(x, y); if (map != null) map.DrawBlock(history.CurrentChange, drawBlockIndex, x, y); + Hover(x, y); } public void DrawUp(double x, double y) => history.ChangeCompleted(); diff --git a/src/HexManiac.WPF/Controls/MapTab.xaml b/src/HexManiac.WPF/Controls/MapTab.xaml index d554f0e8..5ad6c380 100644 --- a/src/HexManiac.WPF/Controls/MapTab.xaml +++ b/src/HexManiac.WPF/Controls/MapTab.xaml @@ -11,7 +11,9 @@ + Stroke="{DynamicResource Background}" Fill="{DynamicResource Primary}" + Width="{Binding HighlightBlockSize}" Height="{Binding HighlightBlockSize}" + Canvas.Left="{Binding HighlightBlockX}" Canvas.Top="{Binding HighlightBlockY}"/> + + + + + diff --git a/src/HexManiac.WPF/Controls/MapTab.xaml.cs b/src/HexManiac.WPF/Controls/MapTab.xaml.cs index 03f47891..7ff6dc64 100644 --- a/src/HexManiac.WPF/Controls/MapTab.xaml.cs +++ b/src/HexManiac.WPF/Controls/MapTab.xaml.cs @@ -8,6 +8,8 @@ using System.Windows.Media; namespace HavenSoft.HexManiac.WPF.Controls { public partial class MapTab { + private MapEditorViewModel ViewModel => (MapEditorViewModel)DataContext; + public MapTab() { InitializeComponent(); } @@ -26,7 +28,7 @@ namespace HavenSoft.HexManiac.WPF.Controls { private void ButtonDown(object sender, MouseButtonEventArgs e) { if (withinMapInteraction != MouseButton.XButton1) return; var element = (FrameworkElement)sender; - var vm = (MapEditorViewModel)element.DataContext; + var vm = ViewModel; var p = GetCoordinates(element, e); element.CaptureMouse(); if (e.LeftButton == MouseButtonState.Pressed) { @@ -37,15 +39,17 @@ namespace HavenSoft.HexManiac.WPF.Controls { vm.DragDown(p.X, p.Y); } else if (e.RightButton == MouseButtonState.Pressed) { vm.SelectDown(p.X, p.Y); - UpdateBlockSelection(vm); } } private void ButtonMove(object sender, MouseEventArgs e) { - if (withinMapInteraction == MouseButton.XButton1) return; var element = (FrameworkElement)sender; - var vm = (MapEditorViewModel)element.DataContext; + var vm = ViewModel; var p = GetCoordinates(element, e); + if (withinMapInteraction == MouseButton.XButton1) { + vm.Hover(p.X, p.Y); + return; + } if (withinMapInteraction == MouseButton.Left) { vm.DrawMove(p.X, p.Y); } else if (withinMapInteraction == MouseButton.Middle) { @@ -60,7 +64,7 @@ namespace HavenSoft.HexManiac.WPF.Controls { element.ReleaseMouseCapture(); if (withinMapInteraction == MouseButton.XButton1) return; if (e.ChangedButton != withinMapInteraction) return; - var vm = (MapEditorViewModel)element.DataContext; + var vm = ViewModel; var p = GetCoordinates(element, e); if (withinMapInteraction == MouseButton.Left) { vm.DrawUp(p.X, p.Y); @@ -74,30 +78,19 @@ namespace HavenSoft.HexManiac.WPF.Controls { private void Wheel(object sender, MouseWheelEventArgs e) { var element = (FrameworkElement)sender; - var vm = (MapEditorViewModel)element.DataContext; + var vm = ViewModel; var p = GetCoordinates(element, e); vm.Zoom(p.X, p.Y, e.Delta > 0); } private void BlocksDown(object sender, MouseButtonEventArgs e) { var element = (FrameworkElement)sender; - var mainModel = (MapEditorViewModel)DataContext; + var mainModel = ViewModel; var imageModel = (IPixelViewModel)element.DataContext; var p = e.GetPosition(element); p.X /= 16; p.Y /= 16; mainModel.SelectBlock((int)p.X, (int)p.Y); - - UpdateBlockSelection(mainModel); - } - - private void UpdateBlockSelection(MapEditorViewModel viewModel) { - // couldn't get bindings to update correctly for this Rectangle for some reason - // just do it manually - Canvas.SetLeft(BlockSelectionRect, viewModel.HighlightBlockX); - Canvas.SetTop(BlockSelectionRect, viewModel.HighlightBlockY); - BlockSelectionRect.Width = viewModel.HighlightBlockSize; - BlockSelectionRect.Height = viewModel.HighlightBlockSize; } private Point GetCoordinates(FrameworkElement element, MouseEventArgs e) {