hover selection

also fix the data binding issue (PropertyChanged event was in both the subclass and the baseclass).
This commit is contained in:
Haven1433 2022-09-05 20:44:44 -05:00
parent 8ff604125c
commit 8c3beaa4df
3 changed files with 37 additions and 20 deletions

View File

@ -83,7 +83,6 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Map {
public event EventHandler<CanDiffEventArgs> RequestCanDiff;
public event EventHandler<CanPatchEventArgs> RequestCanCreatePatch;
public event EventHandler<CanPatchEventArgs> 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();

View File

@ -11,7 +11,9 @@
<Canvas Width="{Binding Blocks.ScaledWidth}" Height="{Binding Blocks.ScaledHeight}">
<local:PixelImage DataContext="{Binding Blocks}" MouseLeftButtonDown="BlocksDown"/>
<Rectangle Name="BlockSelectionRect" Margin="-1,-1,0,0" StrokeThickness="1" Opacity=".5"
Stroke="{DynamicResource Background}" Fill="{DynamicResource Primary}" Width="0" Height="0"/>
Stroke="{DynamicResource Background}" Fill="{DynamicResource Primary}"
Width="{Binding HighlightBlockSize}" Height="{Binding HighlightBlockSize}"
Canvas.Left="{Binding HighlightBlockX}" Canvas.Top="{Binding HighlightBlockY}"/>
</Canvas>
</ScrollViewer>
<ItemsControl Grid.Column="1" Name="MapView" ItemsSource="{Binding VisibleMaps}" SnapsToDevicePixels="True" ClipToBounds="True"
@ -36,5 +38,12 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Rectangle Grid.Column="1" Name="HighlightCursorRect" StrokeThickness="2" IsHitTestVisible="False" Margin="-2,-2,0,0"
HorizontalAlignment="Center" VerticalAlignment="Center" Stroke="{DynamicResource Backlight}"
Width="{Binding HighlightCursorSize, Mode=OneWay}" Height="{Binding HighlightCursorSize, Mode=OneWay}">
<Rectangle.RenderTransform>
<TranslateTransform X="{Binding HighlightCursorX, Mode=OneWay}" Y="{Binding HighlightCursorY, Mode=OneWay}"/>
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</UserControl>

View File

@ -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) {