Skip to content

Commit

Permalink
fixed much more stuff .... and prepare better keystroke handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWandererProductions committed Nov 7, 2024
1 parent 11dc051 commit ead972d
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 319 deletions.
82 changes: 82 additions & 0 deletions CommonControls/GlobalKeyHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace CommonControls
{
public static class GlobalKeyHandler
{
// DependencyProperty to enable or disable key handling
public static readonly DependencyProperty AttachProperty =
DependencyProperty.RegisterAttached(
"Attach", typeof(bool), typeof(GlobalKeyHandler),
new PropertyMetadata(false, OnAttachChanged));

// DependencyProperty to store custom key bindings
public static readonly DependencyProperty KeyBindingsProperty =
DependencyProperty.RegisterAttached(
"KeyBindings", typeof(Dictionary<Key, ICommand>), typeof(GlobalKeyHandler),
new PropertyMetadata(null));

// Getter and Setter for Attach property
public static void SetAttach(UIElement element, bool value)
{
element.SetValue(AttachProperty, value);
}

public static bool GetAttach(UIElement element)
{
return (bool)element.GetValue(AttachProperty);
}

// Getter and Setter for KeyBindings property
public static void SetKeyBindings(UIElement element, Dictionary<Key, ICommand> value)
{
element.SetValue(KeyBindingsProperty, value);
}

public static Dictionary<Key, ICommand> GetKeyBindings(UIElement element)
{
return (Dictionary<Key, ICommand>)element.GetValue(KeyBindingsProperty);
}

// When the Attach property changes, we subscribe or unsubscribe from the PreviewKeyDown event
private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as UIElement;
if (element == null) return;

if ((bool)e.NewValue)
{
element.PreviewKeyDown += OnPreviewKeyDown;
}
else
{
element.PreviewKeyDown -= OnPreviewKeyDown;
}
}

// This method processes the PreviewKeyDown event
private static void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Handled) return;

var element = sender as UIElement;
if (element == null) return;

var keyBindings = GetKeyBindings(element);
if (keyBindings == null) return;

// Check if the pressed key has a command assigned to it
if (keyBindings.ContainsKey(e.Key))
{
var command = keyBindings[e.Key];
if (command.CanExecute(null))
{
command.Execute(null);
e.Handled = true; // Mark event as handled
}
}
}
}
}
21 changes: 7 additions & 14 deletions CommonControls/Thumbnails.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800" Loaded="UserControl_Loaded">
<Grid x:Name="Thb">
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ImageSource}" Width="{Binding CellSize}" Height="{Binding CellSize}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
<ScrollViewer x:Name="MainScrollViewer"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Grid x:Name="Thb">
<!-- Your dynamically added images will go here -->
</Grid>
</ScrollViewer>
</UserControl>
37 changes: 33 additions & 4 deletions CommonControls/Thumbnails.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -638,19 +638,48 @@ private void ImageClick_MouseDown(object sender, MouseButtonEventArgs e)
/// </summary>
public void Next()
{
int currentIndex = _currentSelectedBorder == null ? -1 : GetCurrentIndex(_currentSelectedBorder.Name);
int newIndex = (currentIndex + 1) % Border.Count; // Loop to the start if at the end
var currentIndex = _currentSelectedBorder == null ? -1 : GetCurrentIndex(_currentSelectedBorder.Name);
var newIndex = (currentIndex + 1) % Border.Count; // Loop to the start if at the end
SelectImageAtIndex(newIndex);

CenterOnItem(newIndex);
}

/// <summary>
/// Previous Border of this instance.
/// </summary>
public void Previous()
{
int currentIndex = _currentSelectedBorder == null ? -1 : GetCurrentIndex(_currentSelectedBorder.Name);
int newIndex = (currentIndex - 1 + Border.Count) % Border.Count; // Loop to the end if at the start
var currentIndex = _currentSelectedBorder == null ? -1 : GetCurrentIndex(_currentSelectedBorder.Name);
var newIndex = (currentIndex - 1 + Border.Count) % Border.Count; // Loop to the end if at the start
SelectImageAtIndex(newIndex);

CenterOnItem(newIndex);
}

/// <summary>
/// Centers the ScrollViewer on a specific item by its ID.
/// </summary>
/// <param name="id">The ID of the item to center on.</param>
public void CenterOnItem(int id)
{
if (MainScrollViewer == null || Border == null) return;

// Check if the item with the specified ID exists
if (Border.TryGetValue(id, out var targetElement) && targetElement != null)
{
// Get the position of the target element relative to the ScrollViewer
GeneralTransform itemTransform = targetElement.TransformToAncestor(MainScrollViewer);
Point itemPosition = itemTransform.Transform(new Point(0, 0));

// Calculate the offsets needed to center the item
double centerOffsetX = itemPosition.X - (MainScrollViewer.ViewportWidth / 2) + (targetElement.RenderSize.Width / 2);
double centerOffsetY = itemPosition.Y - (MainScrollViewer.ViewportHeight / 2) + (targetElement.RenderSize.Height / 2);

// Set the ScrollViewer's offset to center the item
MainScrollViewer.ScrollToHorizontalOffset(centerOffsetX);
MainScrollViewer.ScrollToVerticalOffset(centerOffsetY);
}
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions Imaging/TextureAreas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class TextureAreas
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="filter">The filter.</param>
/// <param name="texture">The filter.</param>
/// <param name="shape">The shape.</param>
/// <param name="imageSettings"></param>
/// <param name="shapeParams">The shape parameters.</param>
Expand All @@ -37,7 +37,7 @@ internal static class TextureAreas
/// </exception>
internal static Bitmap GenerateTexture(int width,
int height,
TextureType filter,
TextureType texture,
TextureShape shape,
ImageRegister imageSettings,
object shapeParams = null,
Expand All @@ -47,13 +47,13 @@ internal static Bitmap GenerateTexture(int width,
var actualStartPoint = startPoint ?? new Point(0, 0);

// Retrieve the settings for the specified filter
var settings = imageSettings.GetSettings(filter);
var settings = imageSettings.GetSettings(texture);

// Create a bitmap to apply the texture
Bitmap textureBitmap;

// Generate texture based on the selected filter
switch (filter)
switch (texture)
{
case TextureType.Noise:
textureBitmap = Texture.GenerateNoiseBitmap(
Expand Down Expand Up @@ -97,10 +97,10 @@ internal static Bitmap GenerateTexture(int width,
textureBitmap = Texture.GenerateWaveBitmap(width, height, settings.Alpha);
break;
case TextureType.Crosshatch:
textureBitmap = Texture.GenerateCrosshatchBitmap(width, height, settings.Alpha);
textureBitmap = Texture.GenerateCrosshatchBitmap(width, height, settings.LineSpacing, settings.LineColor, settings.LineThickness, settings.Angle1, settings.Angle2, settings.Alpha);
break;
default:
throw new ArgumentOutOfRangeException(nameof(filter), filter, null);
throw new ArgumentOutOfRangeException(nameof(texture), texture, null);
}

// Apply the texture to the specified area shape
Expand Down
189 changes: 0 additions & 189 deletions Imaging/TextureConfig.cs

This file was deleted.

Loading

0 comments on commit ead972d

Please sign in to comment.