Skip to content

Commit

Permalink
Merge pull request antonpup#2072 from Wibble199/feature/more-evaluata…
Browse files Browse the repository at this point in the history
…bles

Add boolean change and T flip-flop evaluatable
  • Loading branch information
diogotr7 authored Jun 8, 2020
2 parents 1e51d41 + 5db43f1 commit 05c5d3c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Project-Aurora/Project-Aurora/Project-Aurora.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@
<Compile Include="Settings\Layers\Control_BinaryCounterLayer.xaml.cs">
<DependentUpon>Control_BinaryCounterLayer.xaml</DependentUpon>
</Compile>
<Compile Include="Settings\Overrides\Logic\Boolean\Boolean_Latch.cs" />
<Compile Include="Settings\Overrides\Logic\Boolean\Boolean_NumericChangeDetector.cs" />
<Compile Include="Settings\Overrides\Logic\Boolean\Boolean_FlipFlop.cs" />
<Compile Include="Settings\Overrides\Logic\Boolean\Boolean_ChangeDetector.cs" />
<Compile Include="Settings\Overrides\Logic\Boolean\Boolean_PeripheralInput.cs" />
<Compile Include="Settings\Overrides\Logic\Boolean\Boolean_ProcessRunning.cs" />
<Compile Include="Settings\Overrides\Logic\Boolean\Boolean_TrueExtender.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public NumericChangeDetector(Evaluatable<double> eval, bool detectRising = true,
DetectionThreshold = threshold;
}

public Evaluatable<double> Evaluatable { get; set; } = new NumberConstant();
public Evaluatable<double> Evaluatable { get; set; } = EvaluatableDefaults.Get<double>();
public bool DetectRising { get; set; } = true;
public bool DetectFalling { get; set; } = true;
public double DetectionThreshold { get; set; } = 0;

public override Visual GetControl() => new StackPanel()
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(double) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, this, "Evaluatable", BindingMode.TwoWay))
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, this, nameof(Evaluatable), BindingMode.TwoWay))
.WithChild(new CheckBox { Content = "Trigger on increase" }
.WithBinding(CheckBox.IsCheckedProperty, this, "DetectRising"))
.WithBinding(CheckBox.IsCheckedProperty, this, nameof(DetectRising)))
.WithChild(new CheckBox { Content = "Trigger on decrease" }
.WithBinding(CheckBox.IsCheckedProperty, this, "DetectFalling"))
.WithBinding(CheckBox.IsCheckedProperty, this, nameof(DetectFalling)))
.WithChild(new DockPanel { LastChildFill = true }
.WithChild(new Label { Content = "Change required", VerticalAlignment = System.Windows.VerticalAlignment.Center }, Dock.Left)
.WithChild(new DoubleUpDown { Minimum = 0 }
.WithBinding(DoubleUpDown.ValueProperty, this, "DetectionThreshold")));
.WithBinding(DoubleUpDown.ValueProperty, this, nameof(DetectionThreshold))));

protected override bool Execute(IGameState gameState) {
var val = Evaluatable.Evaluate(gameState);
Expand All @@ -59,4 +59,45 @@ protected override bool Execute(IGameState gameState) {

public override Evaluatable<bool> Clone() => new NumericChangeDetector { Evaluatable = Evaluatable.Clone(), DetectRising = DetectRising, DetectFalling = DetectFalling, DetectionThreshold = DetectionThreshold };
}



/// <summary>
/// Evaluatable that detects when a boolean value changes.
/// </summary>
[Evaluatable("Boolean Change Detector", category: EvaluatableCategory.Logic)]
public class BooleanChangeDetector : Evaluatable<bool> {

private bool? lastValue;

public BooleanChangeDetector() { }
public BooleanChangeDetector(Evaluatable<bool> eval) : this(eval, true, true) { }
public BooleanChangeDetector(Evaluatable<bool> eval, bool detectTrue = true, bool detectFalse = true) {
Evaluatable = eval;
DetectTrue = detectTrue;
DetectFalse = detectFalse;
}

public Evaluatable<bool> Evaluatable { get; set; } = EvaluatableDefaults.Get<bool>();
public bool DetectTrue { get; set; } = true;
public bool DetectFalse { get; set; } = true;

public override Visual GetControl() => new StackPanel()
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(bool) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, this, nameof(Evaluatable), BindingMode.TwoWay))
.WithChild(new CheckBox { Content = "Trigger on become true" }
.WithBinding(CheckBox.IsCheckedProperty, this, nameof(DetectTrue)))
.WithChild(new CheckBox { Content = "Trigger on become false" }
.WithBinding(CheckBox.IsCheckedProperty, this, nameof(DetectFalse)));

protected override bool Execute(IGameState gameState) {
var val = Evaluatable.Evaluate(gameState);
var result = (val && lastValue == false && DetectTrue) // Result is true if: the next value is true, the old value was false and we are detecting true
|| (!val && lastValue == true && DetectFalse); // Or the next value is false, the old value was true and we are detecting false
lastValue = val;
return result;
}

public override Evaluatable<bool> Clone() => new BooleanChangeDetector { Evaluatable = Evaluatable.Clone(), DetectTrue = DetectTrue, DetectFalse = DetectFalse };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Aurora.Profiles;
using Aurora.Utils;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace Aurora.Settings.Overrides.Logic.Boolean {

/// <summary>
/// A simple memory gate that can be used for storing a boolean state.
/// While the given input is true, the state of the flip-flop is toggled.
/// </summary>
[Evaluatable("Flip-flop (Toggle)", category: EvaluatableCategory.Logic)]
public class Boolean_FlipFlopT : Evaluatable<bool> {

private bool state = false;

public Evaluatable<bool> Toggle { get; set; }

public Boolean_FlipFlopT() : this(EvaluatableDefaults.Get<bool>()) { }
public Boolean_FlipFlopT(Evaluatable<bool> toggle) => Toggle = toggle;

protected override bool Execute(IGameState gameState) {
if (Toggle.Evaluate(gameState))
state = !state;
return state;
}

public override Visual GetControl() => new StackPanel()
.WithChild(new TextBlock { Text = "Flip-Flop (Toggle)", FontWeight = FontWeights.Bold })
.WithChild(new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 4, 0, 4) }
.WithChild(new Label { Content = "Toggle" })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(bool) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(Toggle)) { Source = this, Mode = BindingMode.TwoWay })));

public override Evaluatable<bool> Clone() => new Boolean_FlipFlopT(Toggle.Clone());
}


/// <summary>
/// A simple memory gate that can be used for storing a boolean state.
/// When 'Set' is true, the gate will start outputting true until 'Reset' becomes true.
/// </summary>
[Evaluatable("Flip-flop (Set-Reset)", category: EvaluatableCategory.Logic)]
public class Boolean_FlipFlopSR : Evaluatable<bool> {

private bool state = false;

public Evaluatable<bool> Set { get; set; }
public Evaluatable<bool> Reset { get; set; }

public Boolean_FlipFlopSR() : this(EvaluatableDefaults.Get<bool>(),EvaluatableDefaults.Get<bool>()) { }
public Boolean_FlipFlopSR(Evaluatable<bool> set, Evaluatable<bool> reset) { Set = set; Reset = reset; }

protected override bool Execute(IGameState gameState) {
if (Reset.Evaluate(gameState))
state = false;
if (Set.Evaluate(gameState))
state = true;
return state;
}

public override Visual GetControl() => new StackPanel()
.WithChild(new TextBlock { Text = "Flip-Flop (Set-Reset)", FontWeight = FontWeights.Bold })
.WithChild(new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 4, 0, 4) }
.WithChild(new Label { Content = "Set" })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(bool) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(Set)) { Source = this, Mode = BindingMode.TwoWay })))
.WithChild(new StackPanel { Orientation = Orientation.Horizontal }
.WithChild(new Label { Content = "Reset" })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(bool) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(Reset)) { Source = this, Mode = BindingMode.TwoWay })));

public override Evaluatable<bool> Clone() => new Boolean_FlipFlopSR(Set.Clone(), Reset.Clone());
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions Project-Aurora/Project-Aurora/Utils/JSONUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public override Type BindToType(string assemblyName, string typeName)
case "System.Collections.ObjectModel.ObservableCollection`1[[Aurora.Settings.Overrides.Logic.IEvaluatableString, Aurora]]":
case "System.Collections.ObjectModel.ObservableCollection`1[[Aurora.Settings.Overrides.Logic.IEvaluatable`1[[System.String, mscorlib]], Aurora]]":
return typeof(ObservableCollection<Evaluatable<string>>);
case "Aurora.Settings.Overrides.Logic.Boolean.Boolean_Latch":
return typeof(Settings.Overrides.Logic.Boolean.Boolean_FlipFlopSR);
default:
if (!typeName.Contains("Overlays") && new Regex(@"Aurora.Profiles.\w+.\w+Settings").IsMatch(typeName))
return base.BindToType(assemblyName, typeName.Replace("Settings", "Profile"));
Expand Down

0 comments on commit 05c5d3c

Please sign in to comment.