Skip to content

Commit

Permalink
Further tinker with the menu, way to old can't remember what i was do…
Browse files Browse the repository at this point in the history
…ing.

Tried to implement free forms
  • Loading branch information
LoneWandererProductions committed Nov 23, 2024
1 parent 6f5b11b commit 80462a8
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 34 deletions.
18 changes: 14 additions & 4 deletions CommonControls/ImageZoom.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public sealed partial class ImageZoom : IDisposable
/// <summary>
/// The tools
/// </summary>
public static readonly DependencyProperty Tools = DependencyProperty.Register(nameof(SelectionTool),
public static readonly DependencyProperty SelectionToolProperty = DependencyProperty.Register(nameof(SelectionTool),
typeof(SelectionTools),
typeof(ImageZoom), null);

Expand Down Expand Up @@ -200,8 +200,8 @@ public string ImageGifPath
/// </value>
public SelectionTools SelectionTool
{
get => (SelectionTools)GetValue(Tools);
set => SetValue(Tools, value);
get => (SelectionTools)GetValue(SelectionToolProperty);
set => SetValue(SelectionToolProperty, value);
}

/// <summary>
Expand Down Expand Up @@ -394,8 +394,9 @@ private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
switch (SelectionTool)
{
case SelectionTools.Move:
break;
case SelectionTools.Trace:
// nothing
_selectionAdorner.IsTracing = true;
break;

case SelectionTools.Rectangle:
Expand Down Expand Up @@ -443,6 +444,13 @@ private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
}
break;
case SelectionTools.Trace:
_selectionAdorner.IsTracing = false;
var points = _selectionAdorner.FreeFormPoints;
//?.Invoke(frame);
//SelectedFrameCommand.Execute(frame);
break;

case SelectionTools.Dot:
SetClickedPoint(e);

var endpoint = e.GetPosition(BtmImage);
Expand Down Expand Up @@ -515,6 +523,8 @@ private void Canvas_MouseMove(object sender, MouseEventArgs e)
case SelectionTools.Trace:
// Handle pixel selection if needed
break;
case SelectionTools.Dot:
break;
default:
// Nothing
return;
Expand Down
72 changes: 65 additions & 7 deletions CommonControls/SelectionAdorner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
* PROGRAMER: Peter Geinitz (Wayfarer)
*/

// ReSharper disable BadBracesSpaces

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace CommonControls
Expand All @@ -24,7 +27,12 @@ internal sealed class SelectionAdorner : Adorner
/// <summary>
/// The free form points
/// </summary>
private readonly List<Point> _freeFormPoints = new();
public List<Point> FreeFormPoints { get; set; } = new();

/// <summary>
/// The is tracing
/// </summary>
public bool IsTracing { get; set; }

/// <summary>
/// The end point
Expand Down Expand Up @@ -55,6 +63,11 @@ public SelectionAdorner(UIElement adornedElement, SelectionTools tool, Transform
Tool = tool;
_imageTransform =
transform ?? Transform.Identity; // Use the provided transform, or default to Identity if none provided

// Hook into mouse events
adornedElement.PreviewMouseDown += OnMouseDown;
adornedElement.PreviewMouseMove += OnMouseMove;
adornedElement.PreviewMouseUp += OnMouseUp;
}

/// <summary>
Expand Down Expand Up @@ -93,7 +106,7 @@ public void UpdateSelection(Point start, Point end)
/// <param name="point">The free form point.</param>
public void AddFreeFormPoint(Point point)
{
_freeFormPoints.Add(_imageTransform.Transform(point));
FreeFormPoints.Add(_imageTransform.Transform(point));
InvalidateVisual();
}

Expand All @@ -102,7 +115,7 @@ public void AddFreeFormPoint(Point point)
/// </summary>
public void ClearFreeFormPoints()
{
_freeFormPoints.Clear();
FreeFormPoints.Clear();
InvalidateVisual();
}

Expand All @@ -117,6 +130,51 @@ public void UpdateImageTransform(Transform transform)
InvalidateVisual();
}

/// <summary>
/// Called when [mouse down].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (Tool == SelectionTools.Trace && e.LeftButton == MouseButtonState.Pressed)
{
IsTracing = true;
FreeFormPoints.Clear(); // Clear existing points for a new trace
FreeFormPoints.Add(_imageTransform.Transform(e.GetPosition(this)));
CaptureMouse(); // Ensure we capture all mouse events
}
}

/// <summary>
/// Called when [mouse move].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (IsTracing && e.LeftButton == MouseButtonState.Pressed)
{
var currentPoint = _imageTransform.Transform(e.GetPosition(this));
FreeFormPoints.Add(currentPoint);
InvalidateVisual(); // Redraw to show the updated trace
}
}

/// <summary>
/// Called when [mouse up].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
if (IsTracing && e.LeftButton == MouseButtonState.Released)
{
IsTracing = false;
ReleaseMouseCapture(); // Release mouse capture
}
}

/// <inheritdoc />
/// <summary>
/// When overridden in a derived class, participates in rendering operations that are directed by the layout system.
Expand Down Expand Up @@ -169,21 +227,21 @@ protected override void OnRender(DrawingContext drawingContext)
selectionRect.Height / 2);
break;

case SelectionTools.Trace:
case SelectionTools.Dot:
// Select a single pixel (this can be visualized as a very small rectangle)
drawingContext.DrawRectangle(Brushes.Red, dashedPen,
new Rect(_startPoint.Value, new Size(1, 1)));
break;

case SelectionTools.FreeForm:
if (_freeFormPoints.Count > 1)
if (FreeFormPoints.Count > 1)
{
var geometry = new StreamGeometry();

using (var ctx = geometry.Open())
{
ctx.BeginFigure(_freeFormPoints[0], false, false);
ctx.PolyLineTo(_freeFormPoints.Skip(1).ToArray(), true, false);
ctx.BeginFigure(FreeFormPoints[0], false, false);
ctx.PolyLineTo(FreeFormPoints.Skip(1).ToArray(), true, false);
}

drawingContext.DrawGeometry(null, dashedPen, geometry);
Expand Down
9 changes: 7 additions & 2 deletions CommonControls/SelectionTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ public enum SelectionTools
/// </summary>
Trace = 2,

/// <summary>
/// The select Dot tool
/// </summary>
Dot = 3,

/// <summary>
/// The select ellipse tool
/// </summary>
Ellipse = 3,
Ellipse = 4,

/// <summary>
/// The free form selection tool
/// </summary>
FreeForm = 4
FreeForm = 5
}
}
2 changes: 1 addition & 1 deletion SlimViewer/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@
ItemsSource="{Binding Bmp}"
ImageGifPath="{Binding GifPath}"
Name="ImageZoom"
SelectionTool="{Binding ImageZoomTool}"
SelectionTool="{Binding SelectionTool}"
SelectedFrameCommand="{Binding SelectedFrameCommand}"
SelectedPointCommand="{Binding SelectedPointCommand}" />
</Grid>
Expand Down
44 changes: 24 additions & 20 deletions SlimViews/ImageView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ public sealed class ImageView : ViewModelBase
private double _tolerance;

/// <summary>
/// The image zoom tool
/// The selection tool
/// </summary>
private SelectionTools _imageZoomTool;
private SelectionTools _selectionTool;

/// <summary>
/// Initializes a new instance of the <see cref="ImageView" /> class.
Expand Down Expand Up @@ -548,19 +548,6 @@ public ImageTools SelectedTool
set => SetProperty(ref _selectedTool, value, nameof(SelectedTool));
}

/// <summary>
/// Gets or sets the image zoom tool.
/// </summary>
/// <value>
/// The image zoom tool.
/// </value>
public SelectionTools ImageZoomTool
{
get => _imageZoomTool;
set => SetProperty(ref _imageZoomTool, value, nameof(ImageZoomTool));
}


/// <summary>
/// Gets or sets the type of the selected tool.
/// </summary>
Expand Down Expand Up @@ -877,6 +864,18 @@ public string GifPath
set => SetProperty(ref _gifPath, value, nameof(GifPath));
}

/// <summary>
/// Gets or sets the selection tool.
/// </summary>
/// <value>
/// The selection tool.
/// </value>
public SelectionTools SelectionTool
{
get => _selectionTool;
set => SetProperty(ref _selectionTool, value, nameof(SelectionTool));
}

/// <summary>
/// Gets the close command.
/// </summary>
Expand Down Expand Up @@ -1318,14 +1317,16 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
switch (SelectedTool)
{
case ImageTools.Move:
ImageZoomTool = SelectionTools.Move;
SelectionTool = SelectionTools.Move;

break;
case ImageTools.Paint:
case ImageTools.Erase:
case ImageTools.ColorSelect:
ImageZoomTool = SelectionTools.Trace;
SelectionTool = SelectionTools.Trace;
break;
case ImageTools.Area:
// no need to handle anything here
break;
}

Expand All @@ -1336,6 +1337,9 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
case nameof(SelectedFilter):
CurrentFilter = Translator.GetFilterFromString(SelectedFilter);
break;
case nameof(SelectedToolType):
SelectionTool = Translator.GetToolsFromString(SelectedToolType);
break;
}
}

Expand All @@ -1354,7 +1358,7 @@ private void ThumbImageClickedAction(ImageEventArgs obj)
/// <param name="wPoint">The w point.</param>
private void SelectedPointAction(Point wPoint)
{
if (ImageZoomTool != SelectionTools.Trace)
if (SelectionTool != SelectionTools.Trace)
return;

var point = new System.Drawing.Point((int)wPoint.X, (int)wPoint.Y);
Expand Down Expand Up @@ -1385,9 +1389,9 @@ private void SelectedPointAction(Point wPoint)
/// <param name="frame">The selected area.</param>
private void SelectedFrameAction(SelectionFrame frame)
{
if (ImageZoomTool == SelectionTools.Move)
if (SelectionTool == SelectionTools.Move)
return;
if (ImageZoomTool == SelectionTools.Trace)
if (SelectionTool == SelectionTools.Trace)
return;

var point = new System.Drawing.Point(frame.X, frame.Y);
Expand Down

0 comments on commit 80462a8

Please sign in to comment.