Skip to content

Commit

Permalink
Merge pull request #288 from PixiEditor/quickfill
Browse files Browse the repository at this point in the history
0.1.6.5-dev release
  • Loading branch information
flabbet committed Dec 13, 2021
2 parents 73a2a43 + 3d7ea9e commit a9a4fe9
Show file tree
Hide file tree
Showing 86 changed files with 2,522 additions and 1,577 deletions.
6 changes: 3 additions & 3 deletions PixiEditor/Helpers/Behaviours/ClearFocusOnClickBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Windows;
using PixiEditor.Models.Controllers.Shortcuts;
using System.Windows;
using System.Windows.Interactivity;
using PixiEditor.Models.Controllers.Shortcuts;

namespace PixiEditor.Helpers.Behaviours
{
Expand All @@ -23,4 +23,4 @@ private void AssociatedObject_MouseDown(object sender, System.Windows.Input.Mous
ShortcutController.BlockShortcutExecution = false;
}
}
}
}
78 changes: 28 additions & 50 deletions PixiEditor/Helpers/Behaviours/TextBoxFocusBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using PixiEditor.Models.Controllers.Shortcuts;

namespace PixiEditor.Helpers.Behaviours
{
internal class TextBoxFocusBehavior : Behavior<TextBox>
{
// Using a DependencyProperty as the backing store for FillSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FillSizeProperty =
public static readonly DependencyProperty SelectOnFocusProperty =
DependencyProperty.Register(
"FillSize",
nameof(SelectOnFocus),
typeof(bool),
typeof(TextBoxFocusBehavior),
new PropertyMetadata(false));
new PropertyMetadata(true));

private string oldText; // Value of textbox before editing
private bool valueConverted; // This bool is used to avoid double convertion if enter is hitted
public static readonly DependencyProperty NextControlProperty =
DependencyProperty.Register(nameof(NextControl), typeof(FrameworkElement), typeof(TextBoxFocusBehavior));

public bool FillSize
public FrameworkElement NextControl
{
get => (bool)GetValue(FillSizeProperty);
set => SetValue(FillSizeProperty, value);
get => (FrameworkElement)GetValue(NextControlProperty);
set => SetValue(NextControlProperty, value);
}

public bool SelectOnFocus
{
get => (bool)GetValue(SelectOnFocusProperty);
set => SetValue(SelectOnFocusProperty, value);
}

protected override void OnAttached()
Expand All @@ -32,7 +37,6 @@ protected override void OnAttached()
AssociatedObject.GotKeyboardFocus += AssociatedObjectGotKeyboardFocus;
AssociatedObject.GotMouseCapture += AssociatedObjectGotMouseCapture;
AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObjectPreviewMouseLeftButtonDown;
AssociatedObject.LostKeyboardFocus += AssociatedObject_LostKeyboardFocus;
AssociatedObject.KeyUp += AssociatedObject_KeyUp;
}

Expand All @@ -42,7 +46,6 @@ protected override void OnDetaching()
AssociatedObject.GotKeyboardFocus -= AssociatedObjectGotKeyboardFocus;
AssociatedObject.GotMouseCapture -= AssociatedObjectGotMouseCapture;
AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObjectPreviewMouseLeftButtonDown;
AssociatedObject.LostKeyboardFocus -= AssociatedObject_LostKeyboardFocus;
AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
}

Expand All @@ -54,39 +57,43 @@ private void AssociatedObject_KeyUp(object sender, KeyEventArgs e)
return;
}

ConvertValue();
RemoveFocus();
}

private void RemoveFocus()
{
DependencyObject scope = FocusManager.GetFocusScope(AssociatedObject);

if (NextControl != null)
{
FocusManager.SetFocusedElement(scope, NextControl);
return;
}

FrameworkElement parent = (FrameworkElement)AssociatedObject.Parent;

while (parent != null && parent is IInputElement element && !element.Focusable)
{
parent = (FrameworkElement)parent.Parent;
}

DependencyObject scope = FocusManager.GetFocusScope(AssociatedObject);
FocusManager.SetFocusedElement(scope, parent);
}

private void AssociatedObjectGotKeyboardFocus(
object sender,
KeyboardFocusChangedEventArgs e)
{
AssociatedObject.SelectAll();
if (FillSize)
{
valueConverted = false;
oldText = AssociatedObject.Text; // Sets old value when keyboard is focused on object
}
if (SelectOnFocus)
AssociatedObject.SelectAll();
}

private void AssociatedObjectGotMouseCapture(
object sender,
MouseEventArgs e)
{
AssociatedObject.SelectAll();
if (SelectOnFocus)
AssociatedObject.SelectAll();
}

private void AssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
Expand All @@ -97,34 +104,5 @@ private void AssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButt
e.Handled = true;
}
}

private void AssociatedObject_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
ConvertValue();
}

/// <summary>
/// Converts number from textbox to format "number px" ex. "15 px".
/// </summary>
private void ConvertValue()
{
if (valueConverted || FillSize == false || AssociatedObject.Text == oldText)
{
return;
}

if (int.TryParse(Regex.Replace(AssociatedObject.Text, "\\p{L}", string.Empty).Trim(), out int result) && result > 0)
{
AssociatedObject.Text = $"{result} px";
}

// If text in textbox isn't number, set it to old value
else
{
AssociatedObject.Text = oldText;
}

valueConverted = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ namespace PixiEditor.Helpers.Converters
{
class DockingManagerActiveContentConverter : IValueConverter
{
private Document cachedDocument = null;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return DependencyProperty.UnsetValue;
if (value is Document document)
{
cachedDocument = document;
return document;
}
return DependencyProperty.UnsetValue;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Document document)
return document;
if (value != null && cachedDocument != null && !cachedDocument.Disposed)
return cachedDocument;
cachedDocument = null;
return DependencyProperty.UnsetValue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using PixiEditor.Models.DataHolders;

namespace PixiEditor.Helpers.Converters
{
Expand All @@ -24,7 +25,7 @@ public override object Convert(object[] values, Type targetType, object paramete

private ObservableCollection<GuidStructureItem> GetSubGroups(IEnumerable<GuidStructureItem> groups)
{
ObservableCollection<GuidStructureItem> finalGroups = new ObservableCollection<GuidStructureItem>();
WpfObservableRangeCollection<GuidStructureItem> finalGroups = new WpfObservableRangeCollection<GuidStructureItem>();
foreach (var group in groups)
{
finalGroups.AddRange(GetSubGroups(group));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Windows;
using System.Windows.Data;
using PixiEditor.Models.DataHolders;

namespace PixiEditor.Helpers.Converters
{
Expand All @@ -16,11 +17,11 @@ public class LayersToStructuredLayersConverter
private static StructuredLayerTree cachedTree;
private List<Guid> lastLayerGuids = new List<Guid>();
private IList<Layer> lastLayers = new List<Layer>();
private ObservableCollection<GuidStructureItem> lastStructure = new ObservableCollection<GuidStructureItem>();
private WpfObservableRangeCollection<GuidStructureItem> lastStructure = new WpfObservableRangeCollection<GuidStructureItem>();

public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] is ObservableCollection<Layer> layers && values[1] is LayerStructure structure)
if (values[0] is WpfObservableRangeCollection<Layer> layers && values[1] is LayerStructure structure)
{
if (cachedTree == null)
{
Expand Down
13 changes: 6 additions & 7 deletions PixiEditor/Helpers/Converters/ToolSizeToIntConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Data;

Expand All @@ -12,24 +11,24 @@ internal class ToolSizeToIntConverter
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("{0} {1}", value, "px");
return value.ToString();
}

public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (string.IsNullOrWhiteSpace(value as string))
if (value is not string s)
{
return null;
}

string slicedString = value.ToString().Split(' ').First();
slicedString = Regex.Replace(slicedString, "\\p{L}", string.Empty);
if (slicedString == string.Empty)
Match match = Regex.Match(s, @"\d+");

if (!match.Success)
{
return null;
}

return int.Parse(slicedString);
return int.Parse(match.Groups[0].ValueSpan);
}
}
}
85 changes: 85 additions & 0 deletions PixiEditor/Helpers/CoordinatesHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using PixiEditor.Models.Position;
using System;
using System.Collections.Generic;

namespace PixiEditor.Helpers
{
internal class CoordinatesHelper
{
public static (Coordinates, Coordinates) GetSquareOrLineCoordinates(IReadOnlyList<Coordinates> coords)
{
if (DoCoordsFormLine(coords))
{
return GetLineCoordinates(coords);
}
return GetSquareCoordiantes(coords);
}

private static bool DoCoordsFormLine(IReadOnlyList<Coordinates> coords)
{
var p1 = coords[0];
var p2 = coords[^1];
//find delta and mirror to first quadrant
float dX = Math.Abs(p2.X - p1.X);
float dY = Math.Abs(p2.Y - p1.Y);

//normalize
float length = (float)Math.Sqrt(dX * dX + dY * dY);
if (length == 0)
return false;
dX = dX / length;
dY = dY / length;

return dX < 0.25f || dY < 0.25f; //angle < 15 deg or angle > 75 deg (sin 15 ~= 0.25)
}

public static (Coordinates, Coordinates) GetLineCoordinates(IReadOnlyList<Coordinates> mouseMoveCords)
{
int xStart = mouseMoveCords[0].X;
int yStart = mouseMoveCords[0].Y;

int xEnd = mouseMoveCords[^1].X;
int yEnd = mouseMoveCords[^1].Y;


if (Math.Abs(xStart - xEnd) > Math.Abs(yStart - yEnd))
{
yEnd = yStart;
}
else
{
xEnd = xStart;
}
return (new(xStart, yStart), new(xEnd, yEnd));
}

/// <summary>
/// Extracts square from rectangle mouse drag, used to draw symmetric shapes.
/// </summary>
public static (Coordinates, Coordinates) GetSquareCoordiantes(IReadOnlyList<Coordinates> mouseMoveCords)
{
var end = mouseMoveCords[^1];
var start = mouseMoveCords[0];

//find delta and mirror to first quadrant
var dX = Math.Abs(start.X - end.X);
var dY = Math.Abs(start.Y - end.Y);

float sqrt2 = (float)Math.Sqrt(2);
//vector of length 1 at 45 degrees;
float diagX, diagY;
diagX = diagY = 1 / sqrt2;

//dot product of delta and diag, returns length of [delta projected onto diag]
float projectedLength = diagX * dX + diagY * dY;
//project above onto axes
float axisLength = projectedLength / sqrt2;

//final coords
float x = -Math.Sign(start.X - end.X) * axisLength;
float y = -Math.Sign(start.Y - end.Y) * axisLength;
end = new Coordinates((int)x + start.X, (int)y + start.Y);
return (start, end);
}
}
}
16 changes: 0 additions & 16 deletions PixiEditor/Helpers/Extensions/ObservableCollectionEx.cs

This file was deleted.

0 comments on commit a9a4fe9

Please sign in to comment.