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

250 lines
12 KiB
C#
Raw Normal View History

2019-04-12 04:19:40 -04:00
using System;
using System.Collections.Generic;
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>
/// <param name="preChangeTransformers">List of pre change transformers of the type `List&lt;F&gt;`.</param>
2019-10-15 16:02:44 -04:00
/// <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>
/// <typeparam name="F">The type of the `preChangeTransformers` Functions of type `AtomFunction&lt;T, T&gt;`.</typeparam>
2019-10-15 16:02:44 -04:00
/// <returns>The Variable created.</returns>
public static V CreateVariable<T, V, E1, E2, F>(T initialValue, E1 changed = null, E2 changedWithHistory = null, List<F> preChangeTransformers = null)
where V : AtomVariable<T, E1, E2, F>
where E1 : AtomEvent<T> where E2 : AtomEvent<T, T>
where F : AtomFunction<T, T>
{
var sov = ScriptableObject.CreateInstance<V>();
2019-04-12 03:41:53 -04:00
sov.Changed = changed;
sov.ChangedWithHistory = changedWithHistory;
sov.Value = initialValue;
sov.PreChangeTransformers = preChangeTransformers;
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)
Added Variable Instancer, Event Reference, Atom Collection and Atom List (old Atom List renamed to Atom Value List) (#110) AtomVariableInstancer - Added AtomVariableInstancer as an option to AtomReference. - Added AtomVariableInstancer to generator. - Added editor icon for AtomVariableInstancer. AtomEventReference - Added an AtomEventReference class (and AtomEventX2Reference). It’s similar to an AtomReference, but for Events. Let’s you pick between an Event, Variable (will select the Changed event) and a VariableInstancer (see above). - Added AtomEventReference and AtomEventX2Reference to generator. - Added a drawer for AtomEventReference. - Listeners are now using AtomEventReference instead of AtomEvent. - Refactoring of VoidHooks since Listeners are now using AtomEventReference. AtomCollection - Created an AtomCollection - a collection of Atoms associated with key strings (AtomReferences). - Added new editor icon for collections. - Created a SerializableDictionary class, which AtomCollection is using. - Custom property drawer for SerializableDictionary. - SerializableDictionary supports nested structures meaning that a AtomCollection can have a KVP that is pointing to another AtomCollection. - AtomCollections have 3 events: Added, Removed, Cleared. - Added an option to sync an InstanceVariable to collection - adding it to the collection when created (using gameObject’s instance id as key) and removing it from the collection when destroyed. AtomList - Renamed old AtomList to AtomValueList - Added AtomList, like Collection, but a list - Added new icon for AtomList - Created a AtomBaseVariableList class, which AtomList is using. - Custom property drawer for AtomBaseVariableList. - AtomLists have 3 events: Added, Removed, Cleared. - Added an option to sync an InstanceVariable to list - adding it to the list when created and removing it from the list when destroyed.
2020-02-22 20:39:43 -05:00
where L : AtomValueList<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;
}
}
}