Skip to content

Commit

Permalink
Removed references to PintaCore in all effects in Pinta.Effects (P…
Browse files Browse the repository at this point in the history
…intaProject#696)

* Consistent constructors for effects

* Removed references to `PintaCore` in all effects in `Pinta.Effects`

* IWorkspaceService now exposes only the essentials for effects
  • Loading branch information
Lehonti authored Jan 18, 2024
1 parent 41ce2ea commit 2493e8e
Show file tree
Hide file tree
Showing 57 changed files with 562 additions and 354 deletions.
8 changes: 7 additions & 1 deletion Pinta.Core/Managers/ChromeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@

namespace Pinta.Core;

public sealed class ChromeManager
public interface IChromeService
{
Window MainWindow { get; }
void LaunchSimpleEffectDialog (BaseEffect effect, IAddinLocalizer localizer);
}

public sealed class ChromeManager : IChromeService
{
private PointI last_canvas_cursor_point;
private bool main_window_busy;
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Core/PintaCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static PintaCore ()

Services.AddService (LivePreview);
Services.AddService<IPaletteService> (Palette);
Services.AddService (Chrome);
Services.AddService<IChromeService> (Chrome);
Services.AddService (Effects);
}

Expand Down
6 changes: 4 additions & 2 deletions Pinta.Effects/Adjustments/AutoLevelEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public sealed class AutoLevelEffect : BaseEffect

public override string AdjustmentMenuKey => "L";

public AutoLevelEffect (IServiceManager _) { }

public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<RectangleI> rois)
{
if (op == null) {
HistogramRgb histogram = new HistogramRgb ();
if (op is null) {
HistogramRgb histogram = new ();
histogram.UpdateHistogram (src, new RectangleI (0, 0, src.Width, src.Height));

op = histogram.MakeLevelsAuto ();
Expand Down
2 changes: 2 additions & 0 deletions Pinta.Effects/Adjustments/BlackAndWhiteEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public sealed class BlackAndWhiteEffect : BaseEffect

public override string AdjustmentMenuKey => "G";

public BlackAndWhiteEffect (IServiceManager _) { }

public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<RectangleI> rois)
{
op.Apply (dest, src, rois);
Expand Down
9 changes: 6 additions & 3 deletions Pinta.Effects/Adjustments/BrightnessContrastEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public sealed class BrightnessContrastEffect : BaseEffect

public BrightnessContrastData Data => (BrightnessContrastData) EffectData!; // NRT - Set in constructor

public BrightnessContrastEffect ()
private readonly IChromeService chrome;

public BrightnessContrastEffect (IServiceManager services)
{
chrome = services.GetService<IChromeService> ();
EffectData = new BrightnessContrastData ();
EffectData.PropertyChanged += HandleEffectDataPropertyChanged;
}
Expand All @@ -49,7 +52,7 @@ void HandleEffectDataPropertyChanged (object? sender, System.ComponentModel.Prop

public override void LaunchConfiguration ()
{
EffectHelper.LaunchSimpleEffectDialog (this);
chrome.LaunchSimpleEffectDialog (this);
}

public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<RectangleI> rois)
Expand All @@ -61,7 +64,7 @@ public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<R
var dst_data = dest.GetPixelData ();
int width = src.Width;

foreach (Core.RectangleI rect in rois) {
foreach (RectangleI rect in rois) {
for (int y = rect.Top; y <= rect.Bottom; y++) {
var src_row = src_data.Slice (y * width + rect.Left, rect.Width);
var dst_row = dst_data.Slice (y * width + rect.Left, rect.Width);
Expand Down
12 changes: 8 additions & 4 deletions Pinta.Effects/Adjustments/CurvesEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ public sealed class CurvesEffect : BaseEffect

public CurvesData Data => (CurvesData) EffectData!; // NRT - Set in constructor

public CurvesEffect ()
private readonly IChromeService chrome;

public CurvesEffect (IServiceManager services)
{
chrome = services.GetService<IChromeService> ();

EffectData = new CurvesData ();
}

public override void LaunchConfiguration ()
{
var dialog = new CurvesDialog (Data) {
CurvesDialog dialog = new (chrome, Data) {
Title = Name,
IconName = Icon,
};
Expand Down Expand Up @@ -115,15 +119,15 @@ public sealed class CurvesData : EffectData

public ColorTransferMode Mode { get; set; }

public override EffectData Clone ()
public override CurvesData Clone ()
{
// Not sure if we have to copy contents of ControlPoints
// var controlPoints = new SortedList<int, int> [ControlPoints.Length];
//
// for (int i = 0; i < ControlPoints.Length; i++)
// controlPoints[i] = new SortedList<int, int> (ControlPoints[i]);

return new CurvesData () {
return new () {
Mode = Mode,
ControlPoints = ControlPoints
};
Expand Down
21 changes: 10 additions & 11 deletions Pinta.Effects/Adjustments/HueSaturationEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@ public sealed class HueSaturationEffect : BaseEffect

public override string AdjustmentMenuKey => "U";

public HueSaturationEffect ()
private readonly IChromeService chrome;

public HueSaturationEffect (IServiceManager services)
{
chrome = services.GetService<IChromeService> ();
EffectData = new HueSaturationData ();
}

public override void LaunchConfiguration ()
{
EffectHelper.LaunchSimpleEffectDialog (this);
chrome.LaunchSimpleEffectDialog (this);
}

private UnaryPixelOp CreateOptimalOp ()
{
if (Data.IsDefault) {
return new UnaryPixelOps.Identity ();
} else {
return new UnaryPixelOps.HueSaturationLightness (
=>
Data.IsDefault
? new UnaryPixelOps.Identity ()
: new UnaryPixelOps.HueSaturationLightness (
hueDelta: Data.Hue,
satDelta: Data.Saturation,
lightness: Data.Lightness
);
}
}
lightness: Data.Lightness);

public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<RectangleI> rois)
{
Expand Down
6 changes: 3 additions & 3 deletions Pinta.Effects/Adjustments/InvertColorsEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public sealed class InvertColorsEffect : BaseEffect

public override string AdjustmentMenuKey => "I";

public InvertColorsEffect (IServiceManager _) { }

public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<RectangleI> rois)
{
op.Apply (dest, src, rois);
}
=> op.Apply (dest, src, rois);
}
26 changes: 16 additions & 10 deletions Pinta.Effects/Adjustments/LevelsEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,31 @@ public sealed class LevelsEffect : BaseEffect

public LevelsData Data => (LevelsData) EffectData!; // NRT - Set in constructor

public LevelsEffect ()
private readonly IChromeService chrome;
private readonly IWorkspaceService workspace;

public LevelsEffect (IServiceManager services)
{
chrome = services.GetService<IChromeService> ();
workspace = services.GetService<IWorkspaceService> ();

EffectData = new LevelsData ();
}

public override void LaunchConfiguration ()
{
var dialog = new LevelsDialog (Data) {
LevelsDialog dialog = new (chrome, workspace, Data) {
Title = Name,
IconName = Icon,
};

dialog.OnResponse += (_, args) => {
if (args.ResponseId != (int) Gtk.ResponseType.None) {
OnConfigDialogResponse (args.ResponseId == (int) Gtk.ResponseType.Ok);
dialog.Destroy ();
}

if (args.ResponseId == (int) Gtk.ResponseType.None)
return;

OnConfigDialogResponse (args.ResponseId == (int) Gtk.ResponseType.Ok);
dialog.Destroy ();
};

dialog.Present ();
Expand All @@ -66,8 +74,6 @@ public LevelsData ()
Levels = new UnaryPixelOps.Level ();
}

public override EffectData Clone ()
{
return new LevelsData { Levels = (UnaryPixelOps.Level) Levels.Clone () };
}
public override LevelsData Clone ()
=> new () { Levels = (UnaryPixelOps.Level) Levels.Clone () };
}
8 changes: 6 additions & 2 deletions Pinta.Effects/Adjustments/PosterizeEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ public sealed class PosterizeEffect : BaseEffect

public PosterizeData Data => (PosterizeData) EffectData!; // NRT - Set in constructor

public PosterizeEffect ()
private readonly IChromeService chrome;

public PosterizeEffect (IServiceManager services)
{
chrome = services.GetService<IChromeService> ();

EffectData = new PosterizeData ();
}

public override void LaunchConfiguration ()
{
var dialog = new PosterizeDialog () {
PosterizeDialog dialog = new (chrome) {
Title = Name,
IconName = Icon,
EffectData = Data
Expand Down
4 changes: 2 additions & 2 deletions Pinta.Effects/Adjustments/SepiaEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public sealed class SepiaEffect : BaseEffect

public override string AdjustmentMenuKey => "E";

public SepiaEffect ()
public SepiaEffect (IServiceManager _)
{
desat = new UnaryPixelOps.Desaturate ();
level = new UnaryPixelOps.Level (
ColorBgra.Black,
ColorBgra.White,
new float[] { 1.2f, 1.0f, 0.8f },
new[] { 1.2f, 1.0f, 0.8f },
ColorBgra.Black,
ColorBgra.White);
}
Expand Down
72 changes: 36 additions & 36 deletions Pinta.Effects/CoreEffectsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,47 @@ public void Initialize ()
IServiceManager services = PintaCore.Services;

// Add the adjustments
PintaCore.Effects.RegisterAdjustment (new AutoLevelEffect ());
PintaCore.Effects.RegisterAdjustment (new BlackAndWhiteEffect ());
PintaCore.Effects.RegisterAdjustment (new BrightnessContrastEffect ());
PintaCore.Effects.RegisterAdjustment (new CurvesEffect ());
PintaCore.Effects.RegisterAdjustment (new HueSaturationEffect ());
PintaCore.Effects.RegisterAdjustment (new InvertColorsEffect ());
PintaCore.Effects.RegisterAdjustment (new LevelsEffect ());
PintaCore.Effects.RegisterAdjustment (new PosterizeEffect ());
PintaCore.Effects.RegisterAdjustment (new SepiaEffect ());
PintaCore.Effects.RegisterAdjustment (new AutoLevelEffect (services));
PintaCore.Effects.RegisterAdjustment (new BlackAndWhiteEffect (services));
PintaCore.Effects.RegisterAdjustment (new BrightnessContrastEffect (services));
PintaCore.Effects.RegisterAdjustment (new CurvesEffect (services));
PintaCore.Effects.RegisterAdjustment (new HueSaturationEffect (services));
PintaCore.Effects.RegisterAdjustment (new InvertColorsEffect (services));
PintaCore.Effects.RegisterAdjustment (new LevelsEffect (services));
PintaCore.Effects.RegisterAdjustment (new PosterizeEffect (services));
PintaCore.Effects.RegisterAdjustment (new SepiaEffect (services));

// Add the effects
PintaCore.Effects.RegisterEffect (new AddNoiseEffect ());
PintaCore.Effects.RegisterEffect (new BulgeEffect ());
PintaCore.Effects.RegisterEffect (new AddNoiseEffect (services));
PintaCore.Effects.RegisterEffect (new BulgeEffect (services));
PintaCore.Effects.RegisterEffect (new CloudsEffect (services));
PintaCore.Effects.RegisterEffect (new EdgeDetectEffect ());
PintaCore.Effects.RegisterEffect (new EmbossEffect ());
PintaCore.Effects.RegisterEffect (new DitheringEffect ());
PintaCore.Effects.RegisterEffect (new FragmentEffect ());
PintaCore.Effects.RegisterEffect (new FrostedGlassEffect ());
PintaCore.Effects.RegisterEffect (new GaussianBlurEffect ());
PintaCore.Effects.RegisterEffect (new GlowEffect ());
PintaCore.Effects.RegisterEffect (new InkSketchEffect ());
PintaCore.Effects.RegisterEffect (new EdgeDetectEffect (services));
PintaCore.Effects.RegisterEffect (new EmbossEffect (services));
PintaCore.Effects.RegisterEffect (new DitheringEffect (services));
PintaCore.Effects.RegisterEffect (new FragmentEffect (services));
PintaCore.Effects.RegisterEffect (new FrostedGlassEffect (services));
PintaCore.Effects.RegisterEffect (new GaussianBlurEffect (services));
PintaCore.Effects.RegisterEffect (new GlowEffect (services));
PintaCore.Effects.RegisterEffect (new InkSketchEffect (services));
PintaCore.Effects.RegisterEffect (new JuliaFractalEffect (services));
PintaCore.Effects.RegisterEffect (new MandelbrotFractalEffect (services));
PintaCore.Effects.RegisterEffect (new MedianEffect ());
PintaCore.Effects.RegisterEffect (new MotionBlurEffect ());
PintaCore.Effects.RegisterEffect (new OilPaintingEffect ());
PintaCore.Effects.RegisterEffect (new OutlineEffect ());
PintaCore.Effects.RegisterEffect (new PencilSketchEffect ());
PintaCore.Effects.RegisterEffect (new PixelateEffect ());
PintaCore.Effects.RegisterEffect (new PolarInversionEffect ());
PintaCore.Effects.RegisterEffect (new RadialBlurEffect ());
PintaCore.Effects.RegisterEffect (new RedEyeRemoveEffect ());
PintaCore.Effects.RegisterEffect (new ReduceNoiseEffect ());
PintaCore.Effects.RegisterEffect (new ReliefEffect ());
PintaCore.Effects.RegisterEffect (new SharpenEffect ());
PintaCore.Effects.RegisterEffect (new SoftenPortraitEffect ());
PintaCore.Effects.RegisterEffect (new TileEffect ());
PintaCore.Effects.RegisterEffect (new TwistEffect ());
PintaCore.Effects.RegisterEffect (new UnfocusEffect ());
PintaCore.Effects.RegisterEffect (new ZoomBlurEffect ());
PintaCore.Effects.RegisterEffect (new MedianEffect (services));
PintaCore.Effects.RegisterEffect (new MotionBlurEffect (services));
PintaCore.Effects.RegisterEffect (new OilPaintingEffect (services));
PintaCore.Effects.RegisterEffect (new OutlineEffect (services));
PintaCore.Effects.RegisterEffect (new PencilSketchEffect (services));
PintaCore.Effects.RegisterEffect (new PixelateEffect (services));
PintaCore.Effects.RegisterEffect (new PolarInversionEffect (services));
PintaCore.Effects.RegisterEffect (new RadialBlurEffect (services));
PintaCore.Effects.RegisterEffect (new RedEyeRemoveEffect (services));
PintaCore.Effects.RegisterEffect (new ReduceNoiseEffect (services));
PintaCore.Effects.RegisterEffect (new ReliefEffect (services));
PintaCore.Effects.RegisterEffect (new SharpenEffect (services));
PintaCore.Effects.RegisterEffect (new SoftenPortraitEffect (services));
PintaCore.Effects.RegisterEffect (new TileEffect (services));
PintaCore.Effects.RegisterEffect (new TwistEffect (services));
PintaCore.Effects.RegisterEffect (new UnfocusEffect (services));
PintaCore.Effects.RegisterEffect (new ZoomBlurEffect (services));
}

public void Uninitialize ()
Expand Down
4 changes: 2 additions & 2 deletions Pinta.Effects/Dialogs/Effects.CurvesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public SortedList<int, int>[] ControlPoints {

public CurvesData EffectData { get; }

public CurvesDialog (CurvesData effectData)
public CurvesDialog (IChromeService chrome, CurvesData effectData)
{
Title = Translations.GetString ("Curves");
TransientFor = PintaCore.Chrome.MainWindow;
TransientFor = chrome.MainWindow;
Modal = true;
this.AddCancelOkButtons ();
this.SetDefaultResponse (ResponseType.Ok);
Expand Down
13 changes: 9 additions & 4 deletions Pinta.Effects/Dialogs/Effects.LevelsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ public bool this[int index] {
private readonly ColorPanelWidget colorpanel_out_low;
private readonly HistogramWidget histogram_input;
private readonly HistogramWidget histogram_output;
private readonly IChromeService chrome;
private readonly IWorkspaceService workspace;

public LevelsData EffectData { get; }

public LevelsDialog (LevelsData effectData)
public LevelsDialog (IChromeService chrome, IWorkspaceService workspace, LevelsData effectData)
{
this.chrome = chrome;
this.workspace = workspace;

Title = Translations.GetString ("Levels Adjustment");
TransientFor = PintaCore.Chrome.MainWindow;
TransientFor = chrome.MainWindow;
Modal = true;

const int spacing = 6;
Expand Down Expand Up @@ -239,7 +244,7 @@ private void UpdateLivePreview ()

private void UpdateInputHistogram ()
{
var doc = PintaCore.Workspace.ActiveDocument;
var doc = workspace.ActiveDocument;

ImageSurface surface = doc.Layers.CurrentUserLayer.Surface;
RectangleI rect = doc.Selection.SelectionPath.GetBounds ();
Expand Down Expand Up @@ -549,7 +554,7 @@ private void HandleColorPanelButtonPressEvent (GestureClick controller, GestureC
return;

ColorPanelWidget panel = (ColorPanelWidget) controller.GetWidget ();
var ccd = Gtk.ColorChooserDialog.New (Translations.GetString ("Choose Color"), PintaCore.Chrome.MainWindow);
var ccd = Gtk.ColorChooserDialog.New (Translations.GetString ("Choose Color"), chrome.MainWindow);
ccd.UseAlpha = true;
ccd.SetColor (panel.CairoColor);

Expand Down
Loading

0 comments on commit 2493e8e

Please sign in to comment.