Skip to content

Commit

Permalink
Merge pull request #1842 from Wibble199/feature/particle-layer
Browse files Browse the repository at this point in the history
Particle layer
  • Loading branch information
diogotr7 authored Apr 24, 2020
2 parents 0fc2222 + f204615 commit 80ac565
Show file tree
Hide file tree
Showing 13 changed files with 881 additions and 45 deletions.
24 changes: 24 additions & 0 deletions Project-Aurora/Project-Aurora/Controls/Control_LayerPreview.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<UserControl x:Class="Aurora.Controls.Control_LayerPreview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:u="clr-namespace:Aurora.Utils"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded">
<UserControl.Resources>
<u:ColorToBrushConverter x:Key="ColorToBrushConv" />
</UserControl.Resources>

<DockPanel LastChildFill="True">
<DockPanel LastChildFill="True" DockPanel.Dock="Bottom" Height="28" Margin="0,8,0,0">
<Label Content="Preview background" />
<xctk:ColorPicker x:Name="previewBg" Margin="4,2" SelectedColor="Black" />
</DockPanel>
<Border Background="{Binding ElementName=previewBg, Path=SelectedColor, Converter={StaticResource ColorToBrushConv}}">
<Image x:Name="imagePreview" />
</Border>
</DockPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Aurora.Settings.Layers;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Aurora.Controls {

/// <summary>
/// A control that is capable of rendering the preview of any layer that correctly implements <see cref="INotifyRender"/>.
/// </summary>
public partial class Control_LayerPreview : UserControl {

private bool eventsAttached;

public Control_LayerPreview() {
InitializeComponent();
}

#region Target Layer Property
public INotifyRender TargetLayer {
get => (INotifyRender)GetValue(TargetLayerProperty);
set => SetValue(TargetLayerProperty, value);
}
public static readonly DependencyProperty TargetLayerProperty =
DependencyProperty.Register("TargetLayer", typeof(INotifyRender), typeof(Control_LayerPreview), new PropertyMetadata(null, TargetLayerChanged));

private static void TargetLayerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var control = (Control_LayerPreview)d;
// If the events are currently attached (I.E. a preview is currently running, then remove the handler from the old target and add to the new one)
if (control.eventsAttached) {
if (e.OldValue is INotifyRender old)
old.LayerRender -= control.RenderLayerPreview;
if (e.NewValue is INotifyRender @new)
@new.LayerRender += control.RenderLayerPreview;
}
}
#endregion

// Start listening for when the particle layer is rendered so we can update the preview
private void UserControl_Loaded(object sender, RoutedEventArgs e) {
if (TargetLayer != null)
TargetLayer.LayerRender += RenderLayerPreview;
eventsAttached = true;
}

// Stop listenting for when the particle layer is rendered (since you can't see the image now anyways)
private void UserControl_Unloaded(object sender, RoutedEventArgs e) {
if (TargetLayer != null)
TargetLayer.LayerRender -= RenderLayerPreview;
eventsAttached = false;
}

// Take the bitmap from the layer and transform it into a format that can be used by WPF
private void RenderLayerPreview(object sender, System.Drawing.Bitmap bitmap) =>
Dispatcher.Invoke(delegate {
using (var ms = new MemoryStream()) {
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
var bitmapImg = new BitmapImage();
bitmapImg.BeginInit();
bitmapImg.StreamSource = ms;
bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
bitmapImg.EndInit();
imagePreview.Source = bitmapImg;
}
});
}
}
27 changes: 25 additions & 2 deletions Project-Aurora/Project-Aurora/Controls/KeySequence.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public bool FreestyleEnabled
}
}

#region ShowOnCanvas property
// Drawn freeform object bounds will only appear if this is true.
public bool ShowOnCanvas {
get => (bool)GetValue(ShowOnCanvasProperty);
set => SetValue(ShowOnCanvasProperty, value);
}

public static readonly DependencyProperty ShowOnCanvasProperty =
DependencyProperty.Register("ShowOnCanvas", typeof(bool), typeof(KeySequence), new FrameworkPropertyMetadata(true, ShowOnCanvasPropertyChanged));

private static void ShowOnCanvasPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) =>
((KeySequence)target).sequence_updateToLayerEditor();
#endregion


/// <summary>Fired whenever the KeySequence object is changed or re-created. Does NOT trigger when keys are changed.</summary>
public event EventHandler SequenceUpdated;
/// <summary>Fired whenever keys are changed.</summary>
Expand All @@ -129,7 +144,15 @@ public bool FreestyleEnabled
public KeySequence()
{
InitializeComponent();
this.DataContext = this;

/* BAD BAD BAD!!! Don't do this! Doing this overrides the DataContext of the control, so if you were to use a binding on this control
* from another control, the binding would try access 'this' instead. E.G., in the following example, the binding is attempting to
* access KeySequence.SomeProperty, which is not what is expected. By looking at this code (and if it were a proper control), the
* binding should be accessing SomeContext.SomeProperty.
* <Grid DataContext="SomeContext">
* <KeySequence Sequence="{Binding SomeProperty}" />
* </Grid> */
//this.DataContext = this;
}

private void sequence_remove_keys_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -238,7 +261,7 @@ public void sequence_updateToLayerEditor()
{
if (Sequence != null && IsInitialized && IsVisible && IsEnabled)
{
if (Sequence.type == Settings.KeySequenceType.FreeForm)
if (Sequence.type == Settings.KeySequenceType.FreeForm && ShowOnCanvas)
{
Sequence.freeform.ValuesChanged += freeform_updated;
LayerEditor.AddKeySequenceElement(Sequence.freeform, Color.FromRgb(255, 255, 255), Title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ public bool Initialize()
new LayerHandlerEntry("ToggleKey", "Toggle Key Layer", typeof(ToggleKeyLayerHandler)),
new LayerHandlerEntry("Timer", "Timer Layer", typeof(TimerLayerHandler)),
new LayerHandlerEntry("Toolbar", "Toolbar Layer", typeof(ToolbarLayerHandler)),
new LayerHandlerEntry("BinaryCounter", "Binary Counter Layer", typeof(BinaryCounterLayerHandler))
new LayerHandlerEntry("BinaryCounter", "Binary Counter Layer", typeof(BinaryCounterLayerHandler)),
new LayerHandlerEntry("Particle", "Particle Layer", typeof(SimpleParticleLayerHandler)),
new LayerHandlerEntry("InteractiveParticle", "Interactive Particle Layer", typeof(InteractiveParticleLayerHandler))
}, true);

RegisterLayerHandler(new LayerHandlerEntry("WrapperLights", "Wrapper Lighting Layer", typeof(WrapperLightsLayerHandler)), false);
Expand Down
18 changes: 18 additions & 0 deletions Project-Aurora/Project-Aurora/Project-Aurora.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@
<Compile Include="Profiles\EliteDangerous\Layers\EliteDangerousAnimationLayerHandler.cs" />
<Compile Include="Devices\Ducky\DuckyDevice.cs" />
<Compile Include="Devices\Ducky\DuckyRGBMappings.cs" />
<Compile Include="Controls\Control_LayerPreview.xaml.cs">
<DependentUpon>Control_LayerPreview.xaml</DependentUpon>
</Compile>
<Compile Include="Devices\UnifiedHID\RoccatTyon.cs" />
<Compile Include="Profiles\CSGO\Layers\Control_CSGODeathLayer.xaml.cs">
<DependentUpon>Control_CSGODeathLayer.xaml</DependentUpon>
Expand Down Expand Up @@ -773,6 +776,12 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Settings\Layers\Control_ParticleLayer.xaml.cs">
<DependentUpon>Control_ParticleLayer.xaml</DependentUpon>
</Compile>
<Compile Include="Settings\Layers\InteractiveParticleLayerHandler.cs" />
<Compile Include="Settings\Layers\SimpleParticleLayerHandler.cs" />
<Compile Include="Settings\Layers\ParticleLayerHandlerBase.cs" />
<Compile Include="Settings\Layers\RazerLayerHandler.cs" />
<Compile Include="Settings\Layers\Control_RazerLayer.xaml.cs">
<DependentUpon>Control_RazerLayer.xaml</DependentUpon>
Expand Down Expand Up @@ -1871,6 +1880,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\Control_LayerPreview.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Profiles\Skype\Control_Skype.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -2279,6 +2292,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Settings\Layers\Control_ParticleLayer.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Settings\Layers\Control_RazerLayer.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -2577,6 +2594,7 @@
<Folder Include="Profiles\Slime Rancher\Layers\" />
<Folder Include="Profiles\Subnautica\Layers\" />
<Folder Include="Properties\DataSources\" />
<Folder Include="Settings\Overrides\Logic\Special\" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\RemoveProfile_Icon.png" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<UserControl x:Class="Aurora.Settings.Layers.Control_ParticleLayer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Aurora.Settings.Layers"
xmlns:ncore="http://schemas.ncore.com/wpf/xaml/colorbox"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:u="clr-namespace:Aurora.Utils"
xmlns:controls="clr-namespace:Aurora.Controls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Loaded="UserControl_Loaded">
<UserControl.Resources>
<local:ParticleSpawnLocationsIsRegionConverter x:Key="ParticleSpawnLocationsIsRegionConv" />
</UserControl.Resources>

<Grid u:GridHelper.Columns="140px,80px,80px,40px,280px"
u:GridHelper.Rows="28px,28px,32px,28px,28px,28px,28px,28px,28px,28px,28px,28px,28px,28px,28px,1*">

<Label Content="Spawn location" HorizontalAlignment="Right" />
<ComboBox Grid.Column="1" Grid.ColumnSpan="2" Margin="4,2" SelectedValue="{Binding Properties._SpawnLocation}" ItemsSource="{u:EnumToItemsSource {x:Type local:ParticleSpawnLocations}}" DisplayMemberPath="Text" SelectedValuePath="Value" />

<Label Content="Color (over time)" Grid.Row="1" HorizontalAlignment="Right" />
<ncore:ColorBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Margin="4,2" x:Name="gradientEditor" BrushChanged="GradientEditor_BrushChanged" />

<Label Content="Min" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
<Label Content="Max" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Bottom" />

<Label Content="Spawn time" Grid.Row="3" HorizontalAlignment="Right" />
<xctk:SingleUpDown Grid.Row="3" Grid.Column="1" x:Name="minSpawnTime" Value="{Binding Properties._MinSpawnTime}" Maximum="{Binding ElementName=maxSpawnTime, Path=Value}" Minimum="0" Increment=".1" Margin="4,2" />
<xctk:SingleUpDown Grid.Row="3" Grid.Column="2" x:Name="maxSpawnTime" Value="{Binding Properties._MaxSpawnTime}" Minimum="{Binding ElementName=minSpawnTime, Path=Value}" Increment=".1" Margin="4,2" />

<Label Content="Spawn amount" Grid.Row="4" HorizontalAlignment="Right" />
<xctk:IntegerUpDown Grid.Row="4" Grid.Column="1" x:Name="minSpawnAmount" Value="{Binding Properties._MinSpawnAmount}" Maximum="{Binding ElementName=maxSpawnAmount, Path=Value}" Minimum="1" Margin="4,2" />
<xctk:IntegerUpDown Grid.Row="4" Grid.Column="2" x:Name="maxSpawnAmount" Value="{Binding Properties._MaxSpawnAmount}" Minimum="{Binding ElementName=minSpawnAmount, Path=Value}" Margin="4,2" />

<Label Content="Lifetime" Grid.Row="5" HorizontalAlignment="Right" />
<xctk:SingleUpDown Grid.Row="5" Grid.Column="1" x:Name="minLifetime" Value="{Binding Properties._MinLifetime}" Maximum="{Binding ElementName=maxLifetime, Path=Value}" Increment=".1" Margin="4,2" />
<xctk:SingleUpDown Grid.Row="5" Grid.Column="2" x:Name="maxLifetime" Value="{Binding Properties._MaxLifetime}" Minimum="{Binding ElementName=minLifetime, Path=Value}" Increment=".1" Margin="4,2" />

<Label Content="Initial horizontal velocity" Grid.Row="6" HorizontalAlignment="Right" />
<xctk:SingleUpDown Grid.Row="6" Grid.Column="1" x:Name="minInitialVelocityX" Value="{Binding Properties._MinInitialVelocityX}" Maximum="{Binding ElementName=maxInitialVelocityX, Path=Value}" Increment=".1" Margin="4,2" />
<xctk:SingleUpDown Grid.Row="6" Grid.Column="2" x:Name="maxInitialVelocityX" Value="{Binding Properties._MaxInitialVelocityX}" Minimum="{Binding ElementName=minInitialVelocityX, Path=Value}" Increment=".1" Margin="4,2" />

<Label Content="Initial vertical velocity" Grid.Row="7" HorizontalAlignment="Right" />
<xctk:SingleUpDown Grid.Row="7" Grid.Column="1" x:Name="minInitialVelocityY" Value="{Binding Properties._MinInitialVelocityY}" Maximum="{Binding ElementName=maxInitialVelocityY, Path=Value}" Increment=".1" Margin="4,2" />
<xctk:SingleUpDown Grid.Row="7" Grid.Column="2" x:Name="maxInitialVelocityY" Value="{Binding Properties._MaxInitialVelocityY}" Minimum="{Binding ElementName=minInitialVelocityY, Path=Value}" Increment=".1" Margin="4,2" />

<Label Content="Horizontal acceleration" Grid.Row="8" HorizontalAlignment="Right" />
<xctk:SingleUpDown Grid.Row="8" Grid.Column="1" Grid.ColumnSpan="2" Value="{Binding Properties._AccelerationX}" Increment=".1" Margin="4,2" />

<Label Content="Vertical acceleration" Grid.Row="9" HorizontalAlignment="Right" />
<xctk:SingleUpDown Grid.Row="9" Grid.Column="1" Grid.ColumnSpan="2" Value="{Binding Properties._AccelerationY}" Increment=".1" Margin="4,2" />

<Label Content="Horizontal Drag" Grid.Row="10" HorizontalAlignment="Right" />
<Slider Grid.Row="10" Grid.Column="1" Grid.ColumnSpan="2" Value="{Binding Properties._DragX}" Minimum="0" Maximum="1" Margin="4,2" />

<Label Content="Vertical Drag" Grid.Row="11" HorizontalAlignment="Right" />
<Slider Grid.Row="11" Grid.Column="1" Grid.ColumnSpan="2" Value="{Binding Properties._DragY}" Minimum="0" Maximum="1" Margin="4,2" />

<Label Content="Initial particle size" Grid.Row="12" HorizontalAlignment="Right" />
<xctk:SingleUpDown Grid.Row="12" Grid.Column="1" x:Name="minInitialSize" Value="{Binding Properties._MinSize}" Maximum="{Binding ElementName=maxInitialSize, Path=Value}" Increment="1" Margin="4,2" />
<xctk:SingleUpDown Grid.Row="12" Grid.Column="2" x:Name="maxInitialSize" Value="{Binding Properties._MaxSize}" Minimum="{Binding ElementName=minInitialSize, Path=Value}" Increment="1" Margin="4,2" />

<Label Content="Size change" Grid.Row="13" HorizontalAlignment="Right" />
<xctk:SingleUpDown Grid.Row="13" Grid.Column="1" Grid.ColumnSpan="2" Value="{Binding Properties._DeltaSize}" Increment=".5" Margin="4,2" />

<Label Content="Presets" Grid.Row="14" HorizontalAlignment="Right" />
<DockPanel Grid.Row="14" Grid.Column="1" Grid.ColumnSpan="2" Margin="4,2">
<Button Content="Apply" DockPanel.Dock="Right" Margin="4,0,0,0" Padding="6,0" Click="ApplyButton_Click" />
<ComboBox x:Name="presetsCombo" DisplayMemberPath="Text" SelectedValuePath="ApplyFunc" />
</DockPanel>

<!-- Note that we cannot set the visibility of this to Hidden or Collapsed, else the control will not be initialised (and therefore will not show the freeform region). It can just be moved really far offscreen instead. -->
<controls:KeySequence Margin="100000,100000,0,0" Title="Spawn region" Sequence="{Binding Properties._Sequence, Mode=TwoWay}" ShowOnCanvas="{Binding Properties._SpawnLocation, Mode=OneWay, Converter={StaticResource ParticleSpawnLocationsIsRegionConv}}" />

<Label Content="Preview" Grid.Column="4" />
<controls:Control_LayerPreview Grid.Column="4" Grid.Row="1" Grid.RowSpan="5" TargetLayer="{Binding .}" />
</Grid>
</UserControl>
Loading

0 comments on commit 80ac565

Please sign in to comment.