Skip to content

Commit

Permalink
Refactored AlignObjectEffect (#987)
Browse files Browse the repository at this point in the history
* Refactored `AlignObjectEffect`

* Fixing compilation error
  • Loading branch information
Lehonti committed Sep 21, 2024
1 parent 2728dc4 commit f48e4ec
Showing 1 changed file with 46 additions and 58 deletions.
104 changes: 46 additions & 58 deletions Pinta.Effects/Effects/AlignObjectEffect.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Cairo;
using Gtk;
using Pinta.Core;
using Pinta.Gui.Widgets;

Expand Down Expand Up @@ -29,14 +28,12 @@ public AlignObjectEffect (IServiceProvider services)
}
public override void LaunchConfiguration ()
{
var dialog = new AlignmentDialog (chrome);
AlignmentDialog dialog = new (chrome);

// Align to the default position
Data.Position = dialog.SelectedPosition;

dialog.PositionChanged += (sender, e) => {
Data.Position = dialog.SelectedPosition;
};
dialog.PositionChanged += (_, _) => Data.Position = dialog.SelectedPosition;

dialog.OnResponse += (_, args) => {
OnConfigDialogResponse (args.ResponseId == (int) Gtk.ResponseType.Ok);
Expand All @@ -61,55 +58,49 @@ public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<R
MoveObject (src, dest, objectBounds, newPosition, selection);
}

private PointI CalculateNewPosition (RectangleI objectBounds, AlignPosition align, RectangleI selectionBounds)
private static PointI CalculateNewPosition (
RectangleI objectBounds,
AlignPosition align,
RectangleI selectionBounds)
{
int x = 0;
int y = 0;

// Align with the selection bounds
switch (align) {
case AlignPosition.TopLeft:
x = selectionBounds.X;
y = selectionBounds.Y;
break;
case AlignPosition.TopCenter:
x = selectionBounds.X + selectionBounds.Width / 2 - objectBounds.Width / 2;
y = selectionBounds.Y;
break;
case AlignPosition.TopRight:
x = selectionBounds.Right - objectBounds.Width;
y = selectionBounds.Y;
break;
case AlignPosition.CenterLeft:
x = selectionBounds.X;
y = selectionBounds.Y + selectionBounds.Height / 2 - objectBounds.Height / 2;
break;
case AlignPosition.Center:
x = selectionBounds.X + selectionBounds.Width / 2 - objectBounds.Width / 2;
y = selectionBounds.Y + selectionBounds.Height / 2 - objectBounds.Height / 2;
break;
case AlignPosition.CenterRight:
x = selectionBounds.Right - objectBounds.Width;
y = selectionBounds.Y + selectionBounds.Height / 2 - objectBounds.Height / 2;
break;
case AlignPosition.BottomLeft:
x = selectionBounds.X;
y = selectionBounds.Bottom - objectBounds.Height;
break;
case AlignPosition.BottomCenter:
x = selectionBounds.X + selectionBounds.Width / 2 - objectBounds.Width / 2;
y = selectionBounds.Bottom - objectBounds.Height;
break;
case AlignPosition.BottomRight:
x = selectionBounds.Right - objectBounds.Width;
y = selectionBounds.Bottom - objectBounds.Height;
break;
}

return new PointI (x, y);
return align switch {
AlignPosition.TopLeft => new (
selectionBounds.X,
selectionBounds.Y),
AlignPosition.TopCenter => new (
selectionBounds.X + selectionBounds.Width / 2 - objectBounds.Width / 2,
selectionBounds.Y),
AlignPosition.TopRight => new (
selectionBounds.Right - objectBounds.Width,
selectionBounds.Y),
AlignPosition.CenterLeft => new (
selectionBounds.X,
selectionBounds.Y + selectionBounds.Height / 2 - objectBounds.Height / 2),
AlignPosition.Center => new (
selectionBounds.X + selectionBounds.Width / 2 - objectBounds.Width / 2,
selectionBounds.Y + selectionBounds.Height / 2 - objectBounds.Height / 2),
AlignPosition.CenterRight => new (
selectionBounds.Right - objectBounds.Width,
selectionBounds.Y + selectionBounds.Height / 2 - objectBounds.Height / 2),
AlignPosition.BottomLeft => new (
selectionBounds.X,
selectionBounds.Bottom - objectBounds.Height),
AlignPosition.BottomCenter => new (
selectionBounds.X + selectionBounds.Width / 2 - objectBounds.Width / 2,
selectionBounds.Bottom - objectBounds.Height),
AlignPosition.BottomRight => new (
selectionBounds.Right - objectBounds.Width,
selectionBounds.Bottom - objectBounds.Height),
_ => PointI.Zero,
};
}

private void MoveObject (ImageSurface src, ImageSurface dest, RectangleI objectBounds, PointI newPosition, RectangleI selectionBounds)
private static void MoveObject (
ImageSurface src,
ImageSurface dest,
RectangleI objectBounds,
PointI newPosition,
RectangleI selectionBounds)
{
var src_data = src.GetReadOnlyPixelData ();
var dst_data = dest.GetPixelData ();
Expand All @@ -119,15 +110,13 @@ private void MoveObject (ImageSurface src, ImageSurface dest, RectangleI objectB
var backgroundColor = src.GetColorBgra (new PointI (selectionBounds.Left, selectionBounds.Top));
for (int y = 0; y < selectionBounds.Height; y++) {
var dst_row = dst_data.Slice ((selectionBounds.Y + y) * width + selectionBounds.X, selectionBounds.Width);

dst_row.Fill (backgroundColor);
}

// Draw the object in the new position
for (int y = 0; y < objectBounds.Height; y++) {
var src_row = src_data.Slice ((objectBounds.Y + y) * width + objectBounds.X, objectBounds.Width);
var dst_row = dst_data.Slice ((newPosition.Y + y) * width + newPosition.X, objectBounds.Width);

src_row.CopyTo (dst_row);
}
}
Expand All @@ -140,10 +129,9 @@ public sealed class AlignObjectData : EffectData
public AlignPosition Position {
get => position;
set {
if (value != position) {
position = value;
FirePropertyChanged (nameof (Position));
}
if (value == position) return;
position = value;
FirePropertyChanged (nameof (Position));
}
}

Expand All @@ -162,5 +150,5 @@ public enum AlignPosition
CenterRight,
BottomLeft,
BottomCenter,
BottomRight
BottomRight,
}

0 comments on commit f48e4ec

Please sign in to comment.