2019-04-12 04:19:40 -04:00
|
|
|
using System;
|
2018-11-04 04:00:06 -05:00
|
|
|
using UnityEngine;
|
|
|
|
|
2019-10-01 11:27:22 -04:00
|
|
|
namespace UnityAtoms
|
2018-11-04 04:00:06 -05:00
|
|
|
{
|
2019-10-15 16:02:44 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Static helper class for when creating Atoms a runtime (yes it is indeed possible 🤯).
|
|
|
|
/// </summary>
|
2018-11-04 04:00:06 -05:00
|
|
|
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<T, E1, E2>`.</typeparam>
|
|
|
|
/// <typeparam name="E1">The type of the `changed` Event of type `AtomEvent<T>`.</typeparam>
|
|
|
|
/// <typeparam name="E2">The type of the `changedWithHistory` Event of type `AtomEvent<T, T>`.</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)
|
2019-09-25 15:05:06 -04:00
|
|
|
where V : AtomVariable<T, E1, E2>
|
|
|
|
where E1 : AtomEvent<T> where E2 : AtomEvent<T, T>
|
2018-11-04 04:00:06 -05:00
|
|
|
{
|
|
|
|
var sov = ScriptableObject.CreateInstance<V>();
|
2019-04-12 03:41:53 -04:00
|
|
|
sov.Changed = changed;
|
|
|
|
sov.ChangedWithHistory = changedWithHistory;
|
|
|
|
sov.Value = initialValue;
|
2018-11-04 04:00:06 -05:00
|
|
|
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<T, E>`.</typeparam>
|
|
|
|
/// <typeparam name="E">The Event tyoe used for `removed` and `added` of type `AtomEvent<T>`.</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)
|
2019-09-25 15:05:06 -04:00
|
|
|
where L : AtomList<T, E>
|
|
|
|
where E : AtomEvent<T>
|
2018-11-04 04:00:06 -05:00
|
|
|
{
|
|
|
|
var sol = ScriptableObject.CreateInstance<L>();
|
2019-04-12 03:41:53 -04:00
|
|
|
sol.Added = added;
|
|
|
|
sol.Removed = removed;
|
|
|
|
sol.Cleared = cleared;
|
2018-11-04 04:00:06 -05:00
|
|
|
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<T>`.</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<T1, T2>`.</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<T1, T2, T3>`.</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)
|
2019-09-25 15:05:06 -04:00
|
|
|
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<T1, T2, T3, T4>`.</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)
|
2019-09-25 15:05:06 -04:00
|
|
|
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<T1, T2, T3, T4, T5>`.</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)
|
2019-09-25 15:05:06 -04:00
|
|
|
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<R>`.</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<R, T1>`.</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<R, T1, T2>`.</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<R, T1, T2, T3>`.</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<R, T1, T2, T3, T4>`.</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<R, T1, T2, T3, T4, T5>`.</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;
|
|
|
|
}
|
2018-11-04 04:00:06 -05:00
|
|
|
}
|
2019-04-07 05:10:09 -04:00
|
|
|
}
|