-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1152 from b-editor/feat/remove-setter-usage
Remove Usage of Setter
- Loading branch information
Showing
6 changed files
with
116 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,51 @@ | ||
using Beutl.Animation; | ||
using System.Runtime.CompilerServices; | ||
using Beutl.Graphics; | ||
using Beutl.Graphics.Effects; | ||
using Beutl.Graphics.Transformation; | ||
using Beutl.Styling; | ||
|
||
namespace Beutl.Operation; | ||
|
||
public sealed class DecorateOperator : StylingOperator | ||
public sealed class DecorateOperator() : PublishOperator<DrawableDecorator>([ | ||
(Drawable.TransformProperty, () => new TransformGroup()), | ||
(Drawable.TransformOriginProperty, RelativePoint.Center), | ||
(Drawable.FilterEffectProperty, () => new FilterEffectGroup()), | ||
(Drawable.BlendModeProperty, BlendMode.SrcOver) | ||
]) | ||
{ | ||
private readonly List<IStyleInstance> _instances = []; | ||
|
||
public Setter<ITransform?> Transform { get; set; } = | ||
new(Drawable.TransformProperty, new TransformGroup()); | ||
|
||
public Setter<RelativePoint> TransformOrigin { get; set; } = | ||
new(Drawable.TransformOriginProperty, RelativePoint.Center); | ||
|
||
public Setter<FilterEffect?> FilterEffect { get; set; } = | ||
new(Drawable.FilterEffectProperty, new FilterEffectGroup()); | ||
|
||
public Setter<BlendMode> BlendMode { get; set; } = | ||
new(Drawable.BlendModeProperty, Graphics.BlendMode.SrcOver); | ||
private readonly ConditionalWeakTable<Drawable, DrawableDecorator> _bag = []; | ||
|
||
public override void Evaluate(OperatorEvaluationContext context) | ||
{ | ||
if (IsEnabled) | ||
if (!IsEnabled) return; | ||
|
||
Value.ApplyAnimations(context.Clock); | ||
for (int i = 0; i < context.FlowRenderables.Count; i++) | ||
{ | ||
int j = 0; | ||
for (int i = 0; i < context.FlowRenderables.Count; i++) | ||
if (context.FlowRenderables[i] is not Drawable drawable) continue; | ||
var decorator = _bag.GetValue(drawable, d => new DrawableDecorator { Child = d }); | ||
decorator.Child = drawable; | ||
context.FlowRenderables[i] = decorator; | ||
|
||
decorator.Transform = (Value.Transform as IMutableTransform)?.ToImmutable() ?? Value.Transform; | ||
decorator.TransformOrigin = Value.TransformOrigin; | ||
decorator.BlendMode = Value.BlendMode; | ||
if (Value.FilterEffect is null) | ||
{ | ||
if (context.FlowRenderables[i] is not Drawable drawable) continue; | ||
IStyleInstance instance = GetInstance(j, drawable); | ||
|
||
if (instance is { Target: DrawableDecorator decorator }) | ||
{ | ||
while (drawable.BatchUpdate) | ||
{ | ||
drawable.EndBatchUpdate(); | ||
} | ||
|
||
ApplyStyle(instance, context.Clock); | ||
context.FlowRenderables[i] = decorator; | ||
j++; | ||
} | ||
else | ||
{ | ||
context.FlowRenderables.RemoveAt(i); | ||
i--; | ||
} | ||
decorator.FilterEffect = null; | ||
} | ||
} | ||
} | ||
|
||
private void ApplyStyle(IStyleInstance instance, IClock clock) | ||
{ | ||
instance.Begin(); | ||
instance.Apply(clock); | ||
instance.End(); | ||
} | ||
|
||
private IStyleInstance GetInstance(int index, Drawable value) | ||
{ | ||
IStyleInstance? instance; | ||
if (index < _instances.Count) | ||
{ | ||
instance = _instances[index]; | ||
if (instance.Target is DrawableDecorator dec) | ||
else | ||
{ | ||
dec.Child = value; | ||
decorator.FilterEffect ??= Value.FilterEffect.CreateDelegatedInstance(); | ||
} | ||
} | ||
else | ||
{ | ||
instance = Style.Instance(new DrawableDecorator { Child = value }); | ||
_instances.Add(instance); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
protected override Style OnInitializeStyle(Func<IList<ISetter>> setters) | ||
{ | ||
var style = new Style<DrawableDecorator>(); | ||
style.Setters.AddRange(setters()); | ||
return style; | ||
} | ||
|
||
public override void Exit() | ||
{ | ||
base.Exit(); | ||
foreach (IStyleInstance instance in _instances) | ||
foreach (var entry in _bag) | ||
{ | ||
if (instance.Target is DrawableDecorator dec) | ||
{ | ||
dec.Child = null; | ||
} | ||
entry.Value.Child = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,36 @@ | ||
using Beutl.Animation; | ||
using Beutl.Graphics; | ||
using Beutl.Graphics; | ||
using Beutl.Graphics.Effects; | ||
using Beutl.Graphics.Rendering; | ||
using Beutl.Graphics.Transformation; | ||
using Beutl.Styling; | ||
|
||
namespace Beutl.Operation; | ||
|
||
public sealed class GroupOperator : StyledSourcePublisher | ||
public sealed class GroupOperator() : PublishOperator<DrawableGroup>([ | ||
(Drawable.TransformProperty, () => new TransformGroup()), | ||
(Drawable.TransformOriginProperty, RelativePoint.Center), | ||
(Drawable.FilterEffectProperty, () => new FilterEffectGroup()), | ||
(Drawable.BlendModeProperty, BlendMode.SrcOver) | ||
]) | ||
{ | ||
public Setter<ITransform?> Transform { get; set; } = new(Drawable.TransformProperty, new TransformGroup()); | ||
|
||
public Setter<RelativePoint> TransformOrigin { get; set; } = new(Drawable.TransformOriginProperty, RelativePoint.Center); | ||
|
||
public Setter<FilterEffect?> FilterEffect { get; set; } = new(Drawable.FilterEffectProperty, new FilterEffectGroup()); | ||
|
||
public Setter<BlendMode> BlendMode { get; set; } = new Setter<BlendMode>(Drawable.BlendModeProperty, Graphics.BlendMode.SrcOver); | ||
|
||
private Renderable? PublishCore(IList<Renderable> value, IClock clock) | ||
public override void Evaluate(OperatorEvaluationContext context) | ||
{ | ||
DrawableGroup? renderable = Instance?.Target as DrawableGroup; | ||
var value = Value; | ||
if (!IsEnabled) return; | ||
|
||
if (!ReferenceEquals(Style, Instance?.Source) || Instance?.Target == null) | ||
{ | ||
renderable = Activator.CreateInstance(Style.TargetType) as DrawableGroup; | ||
if (renderable is ICoreObject coreObj) | ||
{ | ||
Instance?.Dispose(); | ||
Instance = Style.Instance(coreObj); | ||
} | ||
else | ||
{ | ||
renderable = null; | ||
} | ||
} | ||
|
||
OnBeforeApplying(); | ||
if (Instance != null && IsEnabled) | ||
{ | ||
Instance.Begin(); | ||
Instance.Apply(clock); | ||
Instance.End(); | ||
|
||
Drawable[] items = value.OfType<Drawable>().ToArray(); | ||
foreach (Drawable item in items) | ||
{ | ||
while (item.BatchUpdate) | ||
{ | ||
item.EndBatchUpdate(); | ||
} | ||
} | ||
renderable!.Children.Replace(items); | ||
} | ||
|
||
OnAfterApplying(); | ||
|
||
return IsEnabled ? renderable : null; | ||
var items = context.FlowRenderables.OfType<Drawable>().ToArray(); | ||
context.FlowRenderables.Clear(); | ||
value.Children.Replace(items); | ||
context.AddFlowRenderable(value); | ||
} | ||
|
||
public override void Evaluate(OperatorEvaluationContext context) | ||
public override void Enter() | ||
{ | ||
if (PublishCore(context.FlowRenderables, context.Clock) is Renderable renderable) | ||
{ | ||
context.FlowRenderables.Clear(); | ||
context.AddFlowRenderable(renderable); | ||
} | ||
base.Enter(); | ||
Value.Children.Clear(); | ||
} | ||
|
||
protected override Style OnInitializeStyle(Func<IList<ISetter>> setters) | ||
public override void Exit() | ||
{ | ||
var style = new Style<DrawableGroup>(); | ||
style.Setters.AddRange(setters()); | ||
return style; | ||
base.Exit(); | ||
Value.Children.Clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters