2019-04-12 04:19:40 -04:00
|
|
|
using System;
|
2020-02-16 06:44:46 -05:00
|
|
|
using System.Collections.Generic;
|
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>
|
2020-02-16 06:44:46 -05:00
|
|
|
/// <param name="preChangeTransformers">List of pre change transformers of the type `List<F>`.</param>
|
2019-10-15 16:02:44 -04:00
|
|
|
/// <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>
|
2020-02-16 06:44:46 -05:00
|
|
|
/// <typeparam name="F">The type of the `preChangeTransformers` Functions of type `AtomFunction<T, T>`.</typeparam>
|
2019-10-15 16:02:44 -04:00
|
|
|
/// <returns>The Variable created.</returns>
|
2020-02-16 06:44:46 -05:00
|
|
|
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>
|
2019-09-25 15:05:06 -04:00
|
|
|
where E1 : AtomEvent<T> where E2 : AtomEvent<T, T>
|
2020-02-16 06:44:46 -05:00
|
|
|
where F : AtomFunction<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;
|
2020-02-16 06:44:46 -05:00
|
|
|
sov.PreChangeTransformers = preChangeTransformers;
|
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)
|
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>
|
2019-09-25 15:05:06 -04:00
|
|
|
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
|
|
|
}
|