Browse Source

Improved smoothing algorithm and handling tablet

Krzysztof Krysiński 4 days ago
parent
commit
9374132288

+ 14 - 3
src/PixiEditor.ChangeableDocument/Changeables/Brushes/BrushEngine.cs

@@ -57,20 +57,31 @@ public class BrushEngine : IDisposable
     /// </summary>
     private float GetSmoothedPressure(double targetPressure)
     {
-        if (pointsHistory.Count == 0)
+        if (pointsHistory.Count <= 0)
             return (float)targetPressure;
 
         double sum = 0;
         int count = 0;
 
-        // Iterate backwards through history
         for (int i = pointsHistory.Count - 1; i >= 0 && count < PressureSmoothingWindowSize; i--)
         {
             sum += pointsHistory[i].PointerInfo.Pressure;
             count++;
         }
 
-        // Add the current target to the average so we pull towards the new value
+        double historicalAverage = sum / count;
+
+        // If the new pressure is significantly higher than history,
+        // the user is trying to make a bold stroke. Minimize smoothing.
+        if (targetPressure > historicalAverage)
+        {
+            // "Lerp" towards the target.
+            // 0.8f means: "Use 80% raw pressure, 20% historical average"
+            float attackFactor = 0.8f;
+            return (float)(historicalAverage + (targetPressure - historicalAverage) * attackFactor);
+        }
+
+        // If pressure is steady or dropping, use full smoothing to hide jitter.
         sum += targetPressure;
         count++;
 

+ 6 - 0
src/PixiEditor/Views/Main/ViewportControls/Viewport.axaml.cs

@@ -569,6 +569,8 @@ internal partial class Viewport : UserControl, INotifyPropertyChanged
 
         if (MouseDownCommand.CanExecute(parameter))
             MouseDownCommand.Execute(parameter);
+
+        e.Handled = true;
     }
 
     private void Image_MouseMove(PointerEventArgs e)
@@ -596,6 +598,8 @@ internal partial class Viewport : UserControl, INotifyPropertyChanged
 
         if (MouseMoveCommand.CanExecute(parameter))
             MouseMoveCommand.Execute(parameter);
+
+        e.Handled = true;
     }
 
     private void Image_MouseUp(object? sender, PointerReleasedEventArgs e)
@@ -609,6 +613,8 @@ internal partial class Viewport : UserControl, INotifyPropertyChanged
             e.GetCurrentPoint(this).Properties, Scene.Scale);
         if (MouseUpCommand.CanExecute(parameter))
             MouseUpCommand.Execute(parameter);
+
+        //e.Handled = true;
     }
 
     private void Image_MouseWheel(object? sender, PointerWheelEventArgs e)