Skip to content

Commit

Permalink
LayerEffectConfig stale reference fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Feb 13, 2024
1 parent 2d14226 commit 32e0eaf
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public override void Reset()
Freeform = new FreeFormObject(0, 0, 1600, 400),
Type = KeySequenceType.FreeForm
},
_GradientConfig = new LayerEffectConfig
GradientConfig = new LayerEffectConfig
{
Angle = 90,
Speed = 1,
Expand Down Expand Up @@ -415,7 +415,7 @@ public override void Reset()
Freeform = new FreeFormObject(0, 0, 1600, 400),
Type = KeySequenceType.FreeForm
},
_GradientConfig = new LayerEffectConfig
GradientConfig = new LayerEffectConfig
{
Angle = 0,
Speed = 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand All @@ -13,7 +14,7 @@ namespace Aurora.Settings.Layers.Controls;
/// </summary>
public partial class Control_GradientLayer
{
private bool settingsset;
private bool _settingsSet;

public Control_GradientLayer()
{
Expand All @@ -27,17 +28,17 @@ public Control_GradientLayer(GradientLayerHandler dataContext)
DataContext = dataContext;
}

public void SetSettings()
private void SetSettings()
{
if (DataContext is not GradientLayerHandler || settingsset) return;
if (DataContext is not GradientLayerHandler || _settingsSet) return;
wave_size_slider.Value = ((GradientLayerHandler)DataContext).Properties.GradientConfig.GradientSize;
wave_size_label.Text = ((GradientLayerHandler)DataContext).Properties.GradientConfig.GradientSize + " %";
effect_speed_slider.Value = ((GradientLayerHandler)DataContext).Properties._GradientConfig.Speed;
effect_speed_label.Text = "x " + ((GradientLayerHandler)DataContext).Properties._GradientConfig.Speed;
effect_angle.Text = ((GradientLayerHandler)DataContext).Properties._GradientConfig.Angle.ToString();
effect_animation_type.SelectedValue = ((GradientLayerHandler)DataContext).Properties._GradientConfig.AnimationType;
effect_animation_reversed.IsChecked = ((GradientLayerHandler)DataContext).Properties._GradientConfig.AnimationReverse;
var brush = ((GradientLayerHandler)DataContext).Properties._GradientConfig.Brush.GetMediaBrush();
effect_speed_slider.Value = ((GradientLayerHandler)DataContext).Properties.GradientConfig.Speed;
effect_speed_label.Text = "x " + ((GradientLayerHandler)DataContext).Properties.GradientConfig.Speed;
effect_angle.Text = ((GradientLayerHandler)DataContext).Properties.GradientConfig.Angle.ToString(CultureInfo.InvariantCulture);
effect_animation_type.SelectedValue = ((GradientLayerHandler)DataContext).Properties.GradientConfig.AnimationType;
effect_animation_reversed.IsChecked = ((GradientLayerHandler)DataContext).Properties.GradientConfig.AnimationReverse;
var brush = ((GradientLayerHandler)DataContext).Properties.GradientConfig.Brush.GetMediaBrush();
try
{
gradient_editor.Brush = brush;
Expand All @@ -49,20 +50,20 @@ public void SetSettings()

KeySequence_keys.Sequence = ((GradientLayerHandler)DataContext).Properties._Sequence;

settingsset = true;
_settingsSet = true;
}

private void Gradient_editor_BrushChanged(object? sender, BrushChangedEventArgs e)
{
if (IsLoaded && settingsset && DataContext is GradientLayerHandler && sender is ColorBox.ColorBox colorBox)
((GradientLayerHandler)DataContext).Properties._GradientConfig.Brush = new EffectBrush(colorBox.Brush);
if (IsLoaded && _settingsSet && DataContext is GradientLayerHandler && sender is ColorBox.ColorBox colorBox)
((GradientLayerHandler)DataContext).Properties.GradientConfig.Brush = new EffectBrush(colorBox.Brush);
}

private void Button_SetGradientRainbow_Click(object? sender, RoutedEventArgs e)
{
((GradientLayerHandler)DataContext).Properties._GradientConfig.Brush = new EffectBrush(ColorSpectrum.Rainbow);
((GradientLayerHandler)DataContext).Properties.GradientConfig.Brush = new EffectBrush(ColorSpectrum.Rainbow);

var brush = ((GradientLayerHandler)DataContext).Properties._GradientConfig.Brush.GetMediaBrush();
var brush = ((GradientLayerHandler)DataContext).Properties.GradientConfig.Brush.GetMediaBrush();
try
{
gradient_editor.Brush = brush;
Expand All @@ -75,9 +76,9 @@ private void Button_SetGradientRainbow_Click(object? sender, RoutedEventArgs e)

private void Button_SetGradientRainbowLoop_Click(object? sender, RoutedEventArgs e)
{
((GradientLayerHandler)DataContext).Properties._GradientConfig.Brush = new EffectBrush(ColorSpectrum.RainbowLoop);
((GradientLayerHandler)DataContext).Properties.GradientConfig.Brush = new EffectBrush(ColorSpectrum.RainbowLoop);

var brush = ((GradientLayerHandler)DataContext).Properties._GradientConfig.Brush.GetMediaBrush();
var brush = ((GradientLayerHandler)DataContext).Properties.GradientConfig.Brush.GetMediaBrush();
try
{
gradient_editor.Brush = brush;
Expand All @@ -89,16 +90,16 @@ private void Button_SetGradientRainbowLoop_Click(object? sender, RoutedEventArgs
}
private void effect_speed_slider_ValueChanged(object? sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!IsLoaded || !settingsset || DataContext is not GradientLayerHandler || sender is not Slider slider) return;
((GradientLayerHandler)DataContext).Properties._GradientConfig.Speed = (float)slider.Value;
if (!IsLoaded || !_settingsSet || DataContext is not GradientLayerHandler || sender is not Slider slider) return;
((GradientLayerHandler)DataContext).Properties.GradientConfig.Speed = (float)slider.Value;

if (effect_speed_label != null)
effect_speed_label.Text = "x " + slider.Value;
}

private void wave_size_slider_ValueChanged(object? sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!IsLoaded || !settingsset || DataContext is not GradientLayerHandler || sender is not Slider) return;
if (!IsLoaded || !_settingsSet || DataContext is not GradientLayerHandler || sender is not Slider) return;
((GradientLayerHandler)DataContext).Properties.GradientConfig.GradientSize = (float)e.NewValue;

if (wave_size_label != null)
Expand All @@ -114,13 +115,13 @@ private void wave_size_slider_ValueChanged(object? sender, RoutedPropertyChanged

private void effect_angle_ValueChanged(object? sender, RoutedPropertyChangedEventArgs<object> e)
{
if (!IsLoaded || !settingsset || DataContext is not GradientLayerHandler || sender is not IntegerUpDown integerUpDown) return;
if (!IsLoaded || !_settingsSet || DataContext is not GradientLayerHandler || sender is not IntegerUpDown integerUpDown) return;

if (float.TryParse(integerUpDown.Text, out var outval))
{
integerUpDown.Background = new SolidColorBrush(Color.FromArgb(255, 24, 24, 24));

((GradientLayerHandler)DataContext).Properties._GradientConfig.Angle = outval;
((GradientLayerHandler)DataContext).Properties.GradientConfig.Angle = outval;
}
else
{
Expand All @@ -132,23 +133,23 @@ private void effect_angle_ValueChanged(object? sender, RoutedPropertyChangedEven

private void effect_animation_type_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (!IsLoaded || !settingsset || DataContext is not GradientLayerHandler || sender is not ComboBox comboBox) return;
if (!IsLoaded || !_settingsSet || DataContext is not GradientLayerHandler || sender is not ComboBox comboBox) return;

((GradientLayerHandler)DataContext).Properties._GradientConfig.AnimationType = (AnimationType)comboBox.SelectedValue;
((GradientLayerHandler)DataContext).Properties.GradientConfig.AnimationType = (AnimationType)comboBox.SelectedValue;
TriggerPropertyChanged();
}

private void effect_animation_reversed_Checked(object? sender, RoutedEventArgs e)
{
if (!IsLoaded || !settingsset || DataContext is not GradientLayerHandler || sender is not CheckBox checkBox) return;
if (!IsLoaded || !_settingsSet || DataContext is not GradientLayerHandler || sender is not CheckBox checkBox) return;

((GradientLayerHandler)DataContext).Properties._GradientConfig.AnimationReverse = checkBox.IsChecked.HasValue ? checkBox.IsChecked.Value : false;
((GradientLayerHandler)DataContext).Properties.GradientConfig.AnimationReverse = checkBox.IsChecked.HasValue && checkBox.IsChecked.Value;
TriggerPropertyChanged();
}

private void KeySequence_keys_SequenceUpdated(object? sender, RoutedPropertyChangedEventArgs<KeySequence> e)
{
if (!IsLoaded || !settingsset || DataContext is not GradientLayerHandler) return;
if (!IsLoaded || !_settingsSet || DataContext is not GradientLayerHandler) return;

((GradientLayerHandler)DataContext).Properties._Sequence = e.NewValue;
TriggerPropertyChanged();
Expand All @@ -161,7 +162,7 @@ private void UserControl_Loaded(object? sender, RoutedEventArgs e)
Loaded -= UserControl_Loaded;
}

protected void TriggerPropertyChanged()
private void TriggerPropertyChanged()
{
var layerHandler = (GradientLayerHandler) DataContext;
layerHandler.Properties.OnPropertiesChanged(this);
Expand Down
139 changes: 73 additions & 66 deletions Project-Aurora/Project-Aurora/Settings/Layers/GradientLayerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,100 @@
using Aurora.Settings.Overrides;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Controls;
using Aurora.Settings.Layers.Controls;
using Common.Utils;

namespace Aurora.Settings.Layers
namespace Aurora.Settings.Layers;

public class GradientLayerHandlerProperties : LayerHandlerProperties2Color<GradientLayerHandlerProperties>
{
public class GradientLayerHandlerProperties : LayerHandlerProperties2Color<GradientLayerHandlerProperties>
{
[LogicOverridable("Gradient")]
public LayerEffectConfig _GradientConfig { get; set; }
private LayerEffectConfig? _gradientConfig;

[JsonIgnore]
public LayerEffectConfig GradientConfig => Logic._GradientConfig ?? _GradientConfig;
[LogicOverridable("Gradient")]
[JsonProperty("_GradientConfig")]
public LayerEffectConfig GradientConfig
{
get => Logic._gradientConfig ?? (_gradientConfig ??= DefaultGradientConfig());
set => _gradientConfig = value;
}

public GradientLayerHandlerProperties()
{ }
public GradientLayerHandlerProperties()
{ }

public GradientLayerHandlerProperties(bool assign_default = false) : base(assign_default) { }
public GradientLayerHandlerProperties(bool assignDefault = false) : base(assignDefault) { }

public override void Default()
{
base.Default();
_GradientConfig = new LayerEffectConfig(CommonColorUtils.GenerateRandomColor(), CommonColorUtils.GenerateRandomColor()) { AnimationType = AnimationType.None };
}
public override void Default()
{
base.Default();
_gradientConfig = DefaultGradientConfig();
}

[LogicOverrideIgnoreProperty("_PrimaryColor")]
[LogicOverrideIgnoreProperty("_SecondaryColor")]
public class GradientLayerHandler : LayerHandler<GradientLayerHandlerProperties>
private static LayerEffectConfig DefaultGradientConfig()
{
private readonly EffectLayer _tempLayerBitmap = new("GradientLayer - Colors", true);
private bool _invalidated;
return new LayerEffectConfig(CommonColorUtils.GenerateRandomColor(), CommonColorUtils.GenerateRandomColor()) { AnimationType = AnimationType.None };
}
}

public GradientLayerHandler(): base("GradientLayer")
{
Properties.PropertyChanged += PropertiesChanged;
}
[LogicOverrideIgnoreProperty("_PrimaryColor")]
[LogicOverrideIgnoreProperty("_SecondaryColor")]
public class GradientLayerHandler : LayerHandler<GradientLayerHandlerProperties>
{
private readonly EffectLayer _tempLayerBitmap = new("GradientLayer - Colors", true);
private bool _invalidated;

protected override void PropertiesChanged(object? sender, PropertyChangedEventArgs args)
{
base.PropertiesChanged(sender, args);
_invalidated = true;
}
public GradientLayerHandler(): base("GradientLayer")
{
Properties.PropertyChanged += PropertiesChanged;
}

protected override UserControl CreateControl()
protected override void PropertiesChanged(object? sender, PropertyChangedEventArgs args)
{
base.PropertiesChanged(sender, args);
_invalidated = true;
}

protected override UserControl CreateControl()
{
return new Control_GradientLayer(this);
}
public override EffectLayer Render(IGameState gamestate)
{
if (_invalidated)
{
return new Control_GradientLayer(this);
EffectLayer.Clear();
_invalidated = false;
}
public override EffectLayer Render(IGameState gamestate)
//If Wave Size 0 Gradiant Stop Moving Animation
if (Properties.GradientConfig.GradientSize == 0)
{
if (_invalidated)
{
EffectLayer.Clear();
_invalidated = false;
}
//If Wave Size 0 Gradiant Stop Moving Animation
if (Properties.GradientConfig.GradientSize == 0)
{
Properties.GradientConfig.ShiftAmount += (Utils.Time.GetMillisecondsSinceEpoch() - Properties.GradientConfig.LastEffectCall) / 1000.0f * 5.0f * Properties.GradientConfig.Speed;
Properties.GradientConfig.ShiftAmount %= Effects.Canvas.BiggestSize;
Properties.GradientConfig.LastEffectCall = Utils.Time.GetMillisecondsSinceEpoch();
Properties.GradientConfig.ShiftAmount += (Utils.Time.GetMillisecondsSinceEpoch() - Properties.GradientConfig.LastEffectCall) / 1000.0f * 5.0f * Properties.GradientConfig.Speed;
Properties.GradientConfig.ShiftAmount %= Effects.Canvas.BiggestSize;
Properties.GradientConfig.LastEffectCall = Utils.Time.GetMillisecondsSinceEpoch();

Color selectedColor = Properties.GradientConfig.Brush.GetColorSpectrum().GetColorAt(Properties.GradientConfig.ShiftAmount, Effects.Canvas.BiggestSize);
var selectedColor = Properties.GradientConfig.Brush.GetColorSpectrum().GetColorAt(Properties.GradientConfig.ShiftAmount, Effects.Canvas.BiggestSize);

EffectLayer.Set(Properties.Sequence, selectedColor);
}
else
{
_tempLayerBitmap.DrawGradient(LayerEffects.GradientShift_Custom_Angle, Properties.GradientConfig);
EffectLayer.Clear();
EffectLayer.DrawTransformed(
Properties.Sequence,
g =>
{
g.FillRectangle(_tempLayerBitmap.TextureBrush, _tempLayerBitmap.Dimension);
}
);
}
return EffectLayer;
EffectLayer.Set(Properties.Sequence, selectedColor);
}

public override void Dispose()
else
{
Properties.PropertyChanged -= PropertiesChanged;
EffectLayer.Dispose();
base.Dispose();
_tempLayerBitmap.DrawGradient(LayerEffects.GradientShift_Custom_Angle, Properties.GradientConfig);
EffectLayer.Clear();
EffectLayer.DrawTransformed(
Properties.Sequence,
g =>
{
g.FillRectangle(_tempLayerBitmap.TextureBrush, _tempLayerBitmap.Dimension);
}
);
}
return EffectLayer;
}
}

public override void Dispose()
{
Properties.PropertyChanged -= PropertiesChanged;
EffectLayer.Dispose();
base.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public PercentGradientLayerHandlerProperties(bool empty = false) : base(empty) {
[LogicOverridable("Gradient")]
public EffectBrush Gradient
{
get => Logic._gradient ?? _gradient ?? new EffectBrush().SetBrushType(EffectBrush.BrushType.Linear);
get => Logic._gradient ?? (_gradient ??= new EffectBrush().SetBrushType(EffectBrush.BrushType.Linear));
set => _gradient = value;
}

Expand Down

0 comments on commit 32e0eaf

Please sign in to comment.