unity-atoms/Packages/Core/Runtime/Utils/DynamicAtoms.cs

245 lines
12 KiB
C#
Raw Normal View History

2019-04-12 04:19:40 -04:00
using System;
using UnityEngine;
2019-10-01 11:27:22 -04:00
namespace UnityAtoms
{
2019-10-15 16:02:44 -04:00
/// <summary>
/// Static helper class for when creating Atoms a runtime (yes it is indeed possible 🤯).
/// </summary>
public static class DynamicAtoms
{
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create a Variable at runtime.
/// </summary>
/// <param name="initialValue">Inital value of the Variable created.</param>
/// <param name="changed">Changed Event of type `E1`.</param>
/// <param name="changedWithHistory">Changed with history Event of type `E2`.</param>
/// <typeparam name="T">The Variable value type.</typeparam>
/// <typeparam name="V">The Variable type AtomVariable&lt;T, E1, E2&gt;`.</typeparam>
/// <typeparam name="E1">The type of the `changed` Event of type `AtomEvent&lt;T&gt;`.</typeparam>
/// <typeparam name="E2">The type of the `changedWithHistory` Event of type `AtomEvent&lt;T, T&gt;`.</typeparam>
/// <returns>The Variable created.</returns>
2019-04-12 03:41:53 -04:00
public static V CreateVariable<T, V, E1, E2>(T initialValue, E1 changed = null, E2 changedWithHistory = null)
where V : AtomVariable<T, E1, E2>
where E1 : AtomEvent<T> where E2 : AtomEvent<T, T>
{
var sov = ScriptableObject.CreateInstance<V>();
2019-04-12 03:41:53 -04:00
sov.Changed = changed;
sov.ChangedWithHistory = changedWithHistory;
sov.Value = initialValue;
return sov;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create a List at runtime.
/// </summary>
/// <param name="added">Added Event of type `E`.</param>
/// <param name="removed">Removed Event of type `E`.</param>
/// <param name="cleared">Cleared Event of type `Void`.</param>
/// <typeparam name="T">The list item type.</typeparam>
/// <typeparam name="L">The List type to create of type `AtomList&lt;T, E&gt;`.</typeparam>
/// <typeparam name="E">The Event tyoe used for `removed` and `added` of type `AtomEvent&lt;T&gt;`.</typeparam>
/// <returns>The List created.</returns>
2019-04-12 03:41:53 -04:00
public static L CreateList<T, L, E>(E added = null, E removed = null, VoidEvent cleared = null)
where L : AtomList<T, E>
where E : AtomEvent<T>
{
var sol = ScriptableObject.CreateInstance<L>();
2019-04-12 03:41:53 -04:00
sol.Added = added;
sol.Removed = removed;
sol.Cleared = cleared;
return sol;
}
2019-04-12 04:19:40 -04:00
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create an Action at runtime.
/// </summary>
/// <param name="action">The action.</param>
/// <typeparam name="A">The Action created of type `AtomAction&lt;T&gt;`.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Action.</typeparam>
/// <returns>The Action created</returns>
public static A CreateAction<A, T1>(Action<T1> action)
where A : AtomAction<T1>
2019-04-12 04:19:40 -04:00
{
2019-10-15 16:02:44 -04:00
var ga = ScriptableObject.CreateInstance<A>();
2019-04-12 04:19:40 -04:00
ga.Action = action;
return ga;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create an Action at runtime.
/// </summary>
/// <param name="action">The action.</param>
/// <typeparam name="A">The Action created of type `AtomAction&lt;T1, T2&gt;`.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Action.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the Action.</typeparam>
/// <returns>The Action created</returns>
public static A CreateAction<A, T1, T2>(Action<T1, T2> action)
where A : AtomAction<T1, T2>
2019-04-12 04:19:40 -04:00
{
2019-10-15 16:02:44 -04:00
var ga = ScriptableObject.CreateInstance<A>();
2019-04-12 04:19:40 -04:00
ga.Action = action;
return ga;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create an Action at runtime.
/// </summary>
/// <param name="action">The action.</param>
/// <typeparam name="A">The Action created of type `AtomAction&lt;T1, T2, T3&gt;`.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Action.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the Action.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the Action.</typeparam>
/// <returns>The Action created</returns>
2019-04-12 04:19:40 -04:00
public static GA CreateAction<GA, T1, T2, T3>(Action<T1, T2, T3> action)
where GA : AtomAction<T1, T2, T3>
2019-04-12 04:19:40 -04:00
{
var ga = ScriptableObject.CreateInstance<GA>();
ga.Action = action;
return ga;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create an Action at runtime.
/// </summary>
/// <param name="action">The action.</param>
/// <typeparam name="A">The Action created of type `AtomAction&lt;T1, T2, T3, T4&gt;`.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Action.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the Action.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the Action.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the Action.</typeparam>
/// <returns>The Action created</returns>
2019-04-12 04:19:40 -04:00
public static GA CreateAction<GA, T1, T2, T3, T4>(Action<T1, T2, T3, T4> action)
where GA : AtomAction<T1, T2, T3, T4>
2019-04-12 04:19:40 -04:00
{
var ga = ScriptableObject.CreateInstance<GA>();
ga.Action = action;
return ga;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create an Action at runtime.
/// </summary>
/// <param name="action">The action.</param>
/// <typeparam name="A">The Action created of type `AtomAction&lt;T1, T2, T3, T4, T5&gt;`.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Action.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the Action.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the Action.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the Action.</typeparam>
/// <typeparam name="T5">The type of the fifth parameter of the Action.</typeparam>
/// <returns>The Action created</returns>
2019-04-12 04:19:40 -04:00
public static GA CreateAction<GA, T1, T2, T3, T4, T5>(Action<T1, T2, T3, T4, T5> action)
where GA : AtomAction<T1, T2, T3, T4, T5>
2019-04-12 04:19:40 -04:00
{
var ga = ScriptableObject.CreateInstance<GA>();
ga.Action = action;
return ga;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create a Function at runtime.
/// </summary>
/// <param name="func">The function.</param>
/// <typeparam name="F">The Function created of type `AtomFunction&lt;R&gt;`.</typeparam>
/// <typeparam name="R">The return type.</typeparam>
/// <returns>The Function crated.</returns>
public static F CreateFunction<F, R>(Func<R> func)
where F : AtomFunction<R>
2019-04-12 04:19:40 -04:00
{
2019-10-15 16:02:44 -04:00
var gf = ScriptableObject.CreateInstance<F>();
2019-04-12 04:19:40 -04:00
gf.Func = func;
return gf;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create a Function at runtime.
/// </summary>
/// <param name="func">The function.</param>
/// <typeparam name="F">The Function created of type `AtomFunction&lt;R, T1&gt;`.</typeparam>
/// <typeparam name="R">The return type.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Function.</typeparam>
/// <returns>The Function crated.</returns>
public static F CreateFunction<F, R, T1>(Func<T1, R> func)
where F : AtomFunction<R, T1>
2019-04-12 04:19:40 -04:00
{
2019-10-15 16:02:44 -04:00
var gf = ScriptableObject.CreateInstance<F>();
2019-04-12 04:19:40 -04:00
gf.Func = func;
return gf;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create a Function at runtime.
/// </summary>
/// <param name="func">The function.</param>
/// <typeparam name="F">The Function created of type `AtomFunction&lt;R, T1, T2&gt;`.</typeparam>
/// <typeparam name="R">The return type.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Function.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the Function.</typeparam>
/// <returns>The Function crated.</returns>
public static F CreateFunction<F, R, T1, T2>(Func<T1, T2, R> func)
where F : AtomFunction<R, T1, T2>
2019-04-12 04:19:40 -04:00
{
2019-10-15 16:02:44 -04:00
var gf = ScriptableObject.CreateInstance<F>();
2019-04-12 04:19:40 -04:00
gf.Func = func;
return gf;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create a Function at runtime.
/// </summary>
/// <param name="func">The function.</param>
/// <typeparam name="F">The Function created of type `AtomFunction&lt;R, T1, T2, T3&gt;`.</typeparam>
/// <typeparam name="R">The return type.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Function.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the Function.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the Function.</typeparam>
/// <returns>The Function crated.</returns>
public static F CreateFunction<F, R, T1, T2, T3>(Func<T1, T2, T3, R> func)
where F : AtomFunction<R, T1, T2, T3>
2019-04-12 04:19:40 -04:00
{
2019-10-15 16:02:44 -04:00
var gf = ScriptableObject.CreateInstance<F>();
2019-04-12 04:19:40 -04:00
gf.Func = func;
return gf;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create a Function at runtime.
/// </summary>
/// <param name="func">The function.</param>
/// <typeparam name="F">The Function created of type `AtomFunction&lt;R, T1, T2, T3, T4&gt;`.</typeparam>
/// <typeparam name="R">The return type.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Function.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the Function.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the Function.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the Function.</typeparam>
/// <returns>The Function crated.</returns>
public static F CreateFunction<F, R, T1, T2, T3, T4>(Func<T1, T2, T3, T4, R> func)
where F : AtomFunction<R, T1, T2, T3, T4>
2019-04-12 04:19:40 -04:00
{
2019-10-15 16:02:44 -04:00
var gf = ScriptableObject.CreateInstance<F>();
2019-04-12 04:19:40 -04:00
gf.Func = func;
return gf;
}
2019-10-15 16:02:44 -04:00
/// <summary>
/// Create a Function at runtime.
/// </summary>
/// <param name="func">The function.</param>
/// <typeparam name="F">The Function created of type `AtomFunction&lt;R, T1, T2, T3, T4, T5&gt;`.</typeparam>
/// <typeparam name="R">The return type.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the Function.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the Function.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the Function.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the Function.</typeparam>
/// <typeparam name="T5">The type of the fifth parameter of the Function.</typeparam>
/// <returns>The Function crated.</returns>
public static F CreateFunction<F, R, T1, T2, T3, T4, T5>(Func<T1, T2, T3, T4, T5, R> func)
where F : AtomFunction<R, T1, T2, T3, T4, T5>
2019-04-12 04:19:40 -04:00
{
2019-10-15 16:02:44 -04:00
var gf = ScriptableObject.CreateInstance<F>();
2019-04-12 04:19:40 -04:00
gf.Func = func;
return gf;
}
}
}