Skip to content

Commit

Permalink
#17 partially reworked app styles \ There is a dark side! :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Art-Stea1th committed May 9, 2017
1 parent bdbf989 commit aad8eb8
Show file tree
Hide file tree
Showing 29 changed files with 1,574 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<Compile Include="Controllers\GenerationStateMachine.cs" />
<Compile Include="BindingExtensions\SettingsExtension.cs" />
<Compile Include="DataProviders\UniformRandomDataProvider.cs" />
<Compile Include="Interfaces\IMPSGenerator.cs" />
<Compile Include="Interfaces\IEPSGenerator.cs" />
<Compile Include="Interfaces\IEvolutionAlgorithm.cs" />
<Compile Include="Interfaces\IGenerationController.cs" />
<Compile Include="Interfaces\IApplicationFacade.cs" />
Expand All @@ -97,7 +97,7 @@
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="SeedGenerators\UniformRandom.cs" />
<Compile Include="Services\MPSGenerationService.cs" />
<Compile Include="Services\EPSGenerationService.cs" />
<Compile Include="Services\ApplicationFacade.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public byte NextByte() {

public uint NextUint() {
var next = new byte[4];
crypto.GetBytes(next);
return (uint)next[0] << 24 | (uint)next[1] << 16 | (uint)next[2] << 8 | next[3];
crypto.GetBytes(next);
return BitConverter.ToUInt32(next, 0);
}

public void Dispose() => crypto?.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public interface IApplicationFacade : INotifyPropertyChanged {
ICommand WriteSeed { get; }
IEvolutionAlgorithm Algorithm { get; set; }

DoubleCollection MPSCollection { get; }
double MinMPS { get; }
double MaxMPS { get; }
double MPS { get; set; }
DoubleCollection EPSCollection { get; }
double MinEPS { get; }
double MaxEPS { get; }
double EPS { get; set; }

State State { get; }
ICommand Start { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace ASD.CellUniverse.Infrastructure.Interfaces {

internal interface IMPSGenerator {
internal interface IEPSGenerator {

DoubleCollection MPSCollection { get; }
DoubleCollection EPSCollection { get; }

double MPS { get; set; }
double EPS { get; set; }

event Action NextFrameTime;
void Start();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Value Profile="(Default)">Normal</Value>
</Setting>
<Setting Name="ShellWidth" Type="System.Double" Scope="User">
<Value Profile="(Default)">1054</Value>
<Value Profile="(Default)">1044</Value>
</Setting>
<Setting Name="ShellHeight" Type="System.Double" Scope="User">
<Value Profile="(Default)">534</Value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class UniformRandom : ISeedGenerator {
using (var random = new UniformRandomDataProvider()) {
for (var x = 0; x < width; ++x) {
for (var y = 0; y < height; ++y) {
result[x, y] = random.NextByte() % 2 == 1 ? (uint)255 << 24 : 0;
result[x, y] = random.NextByte() % 4 == 1 ? (uint)255 << 24 : 0;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ApplicationFacade : BindableBase, IApplicationFacade {
private bool matrixReadyToMutate;

private IGenerationController controller;
private IMPSGenerator fpsGenerator;
private IEPSGenerator fpsGenerator;

private ISeedGenerator seedWriter;

Expand Down Expand Up @@ -76,28 +76,28 @@ public IEvolutionAlgorithm Algorithm {
set => Set(ref algorithm, value);
}

public DoubleCollection MPSCollection => fpsGenerator.MPSCollection;
public double MinMPS => fpsGenerator.MPSCollection.First();
public double MaxMPS => fpsGenerator.MPSCollection.Last();
public double MPS { get => fpsGenerator.MPS; set => fpsGenerator.MPS = value; }
public DoubleCollection EPSCollection => fpsGenerator.EPSCollection;
public double MinEPS => fpsGenerator.EPSCollection.First();
public double MaxEPS => fpsGenerator.EPSCollection.Last();
public double EPS { get => fpsGenerator.EPS; set => fpsGenerator.EPS = value; }

public State State => controller.State;
public ICommand Start => controller.Start;
public ICommand Stop => controller.Stop;

public ApplicationFacade() {
Initialize(new MPSGenerationService(), new GenerationStateMachine());
Initialize(new EPSGenerationService(), new GenerationStateMachine());
MatrixReadyToChange = State == State.Stopped;
GenerationWidth = 320;
GenerationHeight = 180;
}

private void Initialize(IMPSGenerator fpsGenerator, IGenerationController controller) {
private void Initialize(IEPSGenerator fpsGenerator, IGenerationController controller) {
ConfigureFPSGenerator(this.fpsGenerator = fpsGenerator);
ConfigureController(this.controller = controller);
}

private void ConfigureFPSGenerator(IMPSGenerator fpsGenerator) {
private void ConfigureFPSGenerator(IEPSGenerator fpsGenerator) {
fpsGenerator.NextFrameTime += () => Matrix = algorithm.Mutate(matrix);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Linq;
using System.Windows.Media;
using System.Windows.Threading;

namespace ASD.CellUniverse.Infrastructure.Services {

using Interfaces;

public class EPSGenerationService : IEPSGenerator {

private DispatcherTimer timer;

private DoubleCollection epsCollection;
private double MinEPS => epsCollection.First();
private double MaxEPS => epsCollection.Last();
private double eps;

public DoubleCollection EPSCollection => epsCollection;
public double EPS { get => eps; set { eps = ValidEps(value); UpdateTimerInterval(); } }


public event Action NextFrameTime;
public void Start() => timer.Start();
public void Stop() => timer.Stop();

internal EPSGenerationService() {
epsCollection = new DoubleCollection { 1.0, 2.0, 3.0, 5.0, 15.0, 30.0, 60.0, 120.0, 125.0 }; // Last = NoLimit
timer = new DispatcherTimer();
timer.Tick += (s, e) => NextFrameTime?.Invoke();
EPS = epsCollection.TakeWhile(f => f < 120.0).Last();
}

private double ValidEps(double eps) => eps < MinEPS ? MinEPS : eps > MaxEPS ? MaxEPS : eps;
private void UpdateTimerInterval() => timer.Interval =
eps == epsCollection.Last() // <-- if Last - No Limit
? TimeSpan.FromTicks(1)
: TimeSpan.FromMilliseconds(1000.0 / ValidEps(eps));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Controls\AcceptableTextBox.cs" />
<Compile Include="Controls\WindowBase.cs" />
<Compile Include="Controls\FormattedSlider.cs" />
<Compile Include="Controls\MatrixLED.cs" />
<Compile Include="Helpers\BitmapHelper.cs" />
Expand All @@ -55,7 +56,23 @@
</Compile>
</ItemGroup>
<ItemGroup>
<Page Include="Shared.xaml">
<Page Include="Styles\ButtonStyles.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\CheckBoxStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\ComboBoxStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\FormattedSliderStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\Shared.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
Expand All @@ -67,6 +84,19 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Styles\AcceptableTextBoxStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\GroupBoxStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\WindowBaseStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
23 changes: 9 additions & 14 deletions Application/ASD.CellUniverse.Resources/Controls/MatrixLED.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace ASD.CellUniverse.Resources.Controls {
Expand Down Expand Up @@ -82,27 +81,27 @@ protected override Size ArrangeOverride(Size arrangeSize) {

protected override void OnRender(DrawingContext dc) {

dc.DrawRectangle(Background, null, new Rect(new Point(), RenderSize));
if (Background != null) {
dc.DrawRectangle(Background, null, new Rect(new Point(), RenderSize));
}

if (ShowFade) {

if (history == null) { history = new History(historySize); }

if (history == null) {
history = new History(historySize);
}
history.Add(Source);
RepaintMask(history, ref fadeMask);

//dc.PushOpacity(0.0, new DoubleAnimation(1.0, 0.0, TimeSpan.FromMilliseconds(1000)).CreateClock());
dc.PushOpacityMask(new ImageBrush(fadeMask));
dc.DrawRectangle(Foreground, null, new Rect(new Point(), RenderSize));
dc.Pop();
//dc.Pop();
}

RepaintMask(Source, ref ledsMask);

dc.PushOpacityMask(new ImageBrush(ledsMask));
dc.DrawRectangle(Foreground, null, new Rect(new Point(), RenderSize));

}

private void RepaintMask(uint[,] buffer, ref WriteableBitmap mask) {
Expand All @@ -116,10 +115,9 @@ private void RepaintMask(uint[,] buffer, ref WriteableBitmap mask) {
}
}

private class History {
private class History { // tmp. alpha only (OnRender not using RGB)

private int maximumCount;
private int currentCount;

private Queue<uint[,]> history;
private float[,] a/*, r, g, b*/;
Expand All @@ -144,7 +142,7 @@ public void Add(uint[,] next) {
Initialize(next.GetLength(0), next.GetLength(1));
}
Enqueue(next);
if (currentCount > maximumCount) {
if (history.Count > maximumCount) {
Dequeue();
}
RecalculateResult();
Expand All @@ -157,7 +155,6 @@ public void Initialize(int width, int height) {
//r = new float[width, height];
//g = new float[width, height];
//b = new float[width, height];
currentCount = 0;
}

private void Enqueue(uint[,] next) {
Expand All @@ -170,7 +167,6 @@ private void Enqueue(uint[,] next) {
//b[x, y] += next[x, y] & 0x000000FF;
});
});
++currentCount;
}

private void Dequeue() {
Expand All @@ -183,7 +179,6 @@ private void Dequeue() {
//b[x, y] -= last[x, y] & 0x000000FF;
});
});
--currentCount;
}

private void RecalculateResult() {
Expand All @@ -198,7 +193,7 @@ private void RecalculateResult() {
});
}

private byte Limit(float value) => value < 0 ? (byte)0 : value > 255 ? (byte)255 : (byte)value;
private byte Limit(float value) => value < 0 ? (byte)0 : value > 255 ? (byte)255 : (byte)Math.Round(value);
}
}
}
Loading

0 comments on commit aad8eb8

Please sign in to comment.