Skip to content

Instantly share code, notes, and snippets.

@Frooxius
Created September 12, 2018 23:48
Show Gist options
  • Save Frooxius/a8a27825049199c3dda7e57fea67b414 to your computer and use it in GitHub Desktop.
Save Frooxius/a8a27825049199c3dda7e57fea67b414 to your computer and use it in GitHub Desktop.
Cleaned up 0utsider's Latches
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FrooxEngine.LogiX.ProgramFlow
{
[Category("LogiX/Flow")]
[NodeName("Boolean Latch")]
public class BooleanToggle : LogixOperator<bool>
{
public readonly Impulse OnSet;
public readonly Impulse OnReset;
public readonly Sync<bool> State;
public override bool Content => State;
[ImpulseTarget]
public void Set()
{
State.Value = true;
OnSet.Trigger();
}
[ImpulseTarget]
public void Reset()
{
State.Value = false;
OnReset.Trigger();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseX;
namespace FrooxEngine.LogiX
{
[Category("LogiX/Actions")]
[NodeName("Write Latch")]
public class WriteLatch<T> : LogixNode
{
public readonly Input<T> SetValue;
public readonly Input<T> ResetValue;
public readonly Impulse OnSet;
public readonly Impulse OnReset;
[AsOutput]
public readonly Input<IValue<T>> Target;
[ImpulseTarget]
public void Set()
{
DoSet(SetValue);
OnSet.Trigger();
}
[ImpulseTarget]
public void Reset()
{
DoSet(ResetValue);
OnReset.Trigger();
}
void DoSet(Input<T> source)
{
IValue<T> target = Target.Evaluate(null);
if (target != null)
target.Value = source.Evaluate(default(T));
}
protected override Type FindOverload(NodeTypes connectingTypes)
{
Type type;
// Check the target first, its type takes precedence
if (connectingTypes.inputs.TryGetValue("Target", out type))
{
// Check if the type implements IValue<T>
var interfaceType = type.EnumerateInterfacesRecursively().FirstOrDefault(
t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IValue<>));
//UniLog.Log($"Checking Target input {type} for IValue interface: " + interfaceType);
if (interfaceType != null)
return typeof(WriteLatch<>).MakeGenericType(interfaceType.GetGenericArguments()[0]);
else
return null; // not possible to connect this Target
}
if (connectingTypes.inputs.TryGetValue("Value", out type))
return typeof(WriteLatch<>).MakeGenericType(type);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment