diff options
Diffstat (limited to 'Other/NodeEditorExamples/Assets/UNEB/Utility')
12 files changed, 703 insertions, 0 deletions
diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/ColorExtensions.cs b/Other/NodeEditorExamples/Assets/UNEB/Utility/ColorExtensions.cs new file mode 100644 index 00000000..5a2de0b0 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/ColorExtensions.cs @@ -0,0 +1,13 @@ + +using UnityEngine; + +namespace UNEB.Utility +{ + public static class ColorExtensions + { + public static Color From255(byte r, byte g, byte b) + { + return new Color(r / 255f, g / 255f, b / 255f); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/ColorExtensions.cs.meta b/Other/NodeEditorExamples/Assets/UNEB/Utility/ColorExtensions.cs.meta new file mode 100644 index 00000000..9aa744fb --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/ColorExtensions.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: fe88d4f484d88f541adb9ee3c76532e9 +timeCreated: 1502671454 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs b/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs new file mode 100644 index 00000000..2faac0f1 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs @@ -0,0 +1,78 @@ + +using System; +using System.Collections; +using System.Collections.Generic; + +namespace UNEB.Utility +{ + /// <summary> + /// A simple stack with a limited capacity. + /// In order to make more room, the first element (not the top) in the stack is removed. + /// </summary> + /// <typeparam name="T"></typeparam> + public class FiniteStack<T> : IEnumerable<T> + { + private LinkedList<T> _container; + private int _capacity; + + /// <summary> + /// Called when the stack runs out of space and removes + /// the first item (bottom of stack) to make room. + /// </summary> + public event Action<T> OnRemoveBottomItem; + + public FiniteStack(int capacity) + { + _container = new LinkedList<T>(); + _capacity = capacity; + } + + public void Push(T value) + { + _container.AddLast(value); + + // Out of room, remove the first element in the stack. + if (_container.Count == _capacity) { + + T first = _container.First.Value; + _container.RemoveFirst(); + + if (OnRemoveBottomItem != null) + OnRemoveBottomItem(first); + } + } + + public T Peek() + { + return _container.Last.Value; + } + + public T Pop() + { + var lastVal = _container.Last.Value; + _container.RemoveLast(); + + return lastVal; + } + + public void Clear() + { + _container.Clear(); + } + + public int Count + { + get { return _container.Count; } + } + + public IEnumerator<T> GetEnumerator() + { + return _container.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _container.GetEnumerator(); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs.meta b/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs.meta new file mode 100644 index 00000000..33af9b97 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 8b4b8b87bda6573489817470ee03e595 +timeCreated: 1503072950 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/Pair.cs b/Other/NodeEditorExamples/Assets/UNEB/Utility/Pair.cs new file mode 100644 index 00000000..dfff1249 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/Pair.cs @@ -0,0 +1,20 @@ + +namespace UNEB.Utility +{ + /// <summary> + /// Simple pair tuple. + /// </summary> + /// <typeparam name="T1"></typeparam> + /// <typeparam name="T2"></typeparam> + public class Pair<T1, T2> + { + public readonly T1 item1; + public readonly T2 item2; + + public Pair(T1 item1, T2 item2) + { + this.item1 = item1; + this.item2 = item2; + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/Pair.cs.meta b/Other/NodeEditorExamples/Assets/UNEB/Utility/Pair.cs.meta new file mode 100644 index 00000000..ac58e80c --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/Pair.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 4c74f38f4b664f749bd0e508ea0ccc2d +timeCreated: 1502603700 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/StateMachine.cs b/Other/NodeEditorExamples/Assets/UNEB/Utility/StateMachine.cs new file mode 100644 index 00000000..9c3a8992 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/StateMachine.cs @@ -0,0 +1,300 @@ +using UnityEngine; +using System; +using System.Collections.Generic; + +namespace Bonsai.Utility +{ + /// <summary> + /// A finite state machine. + /// </summary> + /// <typeparam name="T">The date type to be stored by the machine states.</typeparam> + public class StateMachine<T> + { + /// <summary> + /// An event that fires when the machine finishes transitioning to another state. + /// </summary> + public event Action OnStateChangedEvent = delegate { }; + + protected Dictionary<T, State> _states = new Dictionary<T, State>(); + + /// <summary> + /// Get all the state data. + /// </summary> + /// <returns></returns> + public IEnumerable<T> Data() + { + return _states.Keys; + } + + protected State _currentState; + + public State CurrentState + { + get { return _currentState; } + } + + /// <summary> + /// Adds a state to the machine. + /// </summary> + /// <param name="data"></param> + public void AddState(T data) + { + var s = new State(data); + _states.Add(data, s); ; + } + + public void AddState(State s) + { + _states.Add(s.Value, s); + } + + public void AddTransition(State start, State end, Func<bool> condition, Func<bool> onMakingTransition) + { + var t = new Transition(condition); + t.onMakingTransition = onMakingTransition; + + AddTransition(start, end, t); + } + + public void AddTransition(State start, State end, Transition t) + { + start.Add(t); + t.SetNextState(end); + } + + /// <summary> + /// Add a transition that goes from start to end state. + /// </summary> + /// <param name="start"></param> + /// <param name="end"></param> + /// <param name="t"></param> + public void AddTransition(T start, T end, Transition t) + { + var startST = GetState(start); + var endST = GetState(end); + + if (startST == null || endST == null) { + Debug.LogError("State(s) are not in the state machine"); + return; + } + + AddTransition(startST, endST, t); + } + + /// <summary> + /// Add two transitions. + /// One from start to end. + /// Another from end to start. + /// </summary> + /// <param name="start"></param> + /// <param name="end"></param> + /// <param name="startToEnd"></param> + /// <param name="endToStart"></param> + public void AddBiTransition(T start, T end, Transition startToEnd, Transition endToStart) + { + var startST = GetState(start); + var endST = GetState(end); + + if (startST == null || endST == null) { + Debug.LogError("State(s) are not in the state machine"); + return; + } + + AddTransition(startST, endST, startToEnd); + AddTransition(endST, startST, endToStart); + } + + /// <summary> + /// Gets the state associated with the data. + /// </summary> + /// <param name="data"></param> + /// <returns></returns> + public State GetState(T data) + { + if (_states.ContainsKey(data)) { + return _states[data]; + } + + return null; + } + + /// <summary> + /// Sets the current active state of the machine. + /// </summary> + /// <param name="data"></param> + public void SetCurrentState(T data) + { + + var state = GetState(data); + + if (state == null) { + Debug.LogError(data + " is not in the state machine."); + } + + else { + _currentState = state; + } + } + + /// <summary> + /// Handles moving to next state when conditions are met. + /// </summary> + public void Update() + { + if (_currentState == null) { + return; + } + + Transition validTrans = null; + + // Pick the next state if the transition conditions are met. + for (int i = 0; i < _currentState.Transitions.Count; i++) { + + if (_currentState.Transitions[i].AllConditionsMet()) { + validTrans = _currentState.Transitions[i]; + break; + } + } + + if (validTrans != null) { + + // Call on making transition. + if (validTrans.onMakingTransition()) { + + // Call on state exit. + if (_currentState.onStateExit != null) + _currentState.onStateExit(); + + // Change the state to the next one. + _currentState = validTrans.NextState; + + // Call on state enter. + if (_currentState.onStateEnter != null) + _currentState.onStateEnter(); + + OnStateChangedEvent(); + } + } + } + + /// <summary> + /// A transition between two states than only occurs if all + /// its conditions are satisfied. + /// </summary> + public class Transition + { + private State _nextState = null; + private List<Func<bool>> _conditions = new List<Func<bool>>(); + + /// <summary> + /// Called after the 'from' state exits and before the 'to' state enters. + /// If this fails, then it goes back to the starting state. + /// </summary> + public Func<bool> onMakingTransition = () => { return true; }; + + public Transition() + { + + } + + /// <summary> + /// Pass in initial conditions + /// </summary> + /// <param name="?"></param> + public Transition(params Func<bool>[] conditions) + { + foreach (var c in conditions) { + AddCondition(c); + } + } + + /// <summary> + /// Adds a condition that must be satisfied in order to do the transition. + /// </summary> + /// <param name="cond"></param> + public void AddCondition(Func<bool> cond) + { + _conditions.Add(cond); + } + + /// <summary> + /// Tests if all the conditions of the transition are satisfied. + /// </summary> + /// <returns></returns> + public bool AllConditionsMet() + { + for (int i = 0; i < _conditions.Count; i++) { + + if (!_conditions[i]()) return false; + } + + // All conditions returned true. + return true; + } + + /// <summary> + /// Set the state that transition goes to. + /// </summary> + /// <param name="next"></param> + public void SetNextState(State next) + { + _nextState = next; + } + + /// <summary> + /// The state that transition goes to. + /// </summary> + public State NextState + { + get { return _nextState; } + } + } + + /// <summary> + /// A state of the machine. + /// </summary> + public class State + { + private T _data; + private List<Transition> _transitions = new List<Transition>(); + + /// <summary> + /// Executes when the machine transitions into this state. + /// </summary> + public Action onStateEnter; + + /// <summary> + /// Executes when the machine transitions out of this state. + /// </summary> + public Action onStateExit; + + /// <summary> + /// Construct a state with its data. + /// </summary> + /// <param name="data"></param> + public State(T data) + { + _data = data; + } + + /// <summary> + /// Adds a transition to the state. + /// </summary> + /// <param name="t"></param> + public void Add(Transition t) + { + _transitions.Add(t); + } + + /// <summary> + /// The data held by the state. + /// </summary> + public T Value { get { return _data; } } + + /// <summary> + /// The transitions connected to the state. + /// </summary> + public List<Transition> Transitions { get { return _transitions; } } + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/StateMachine.cs.meta b/Other/NodeEditorExamples/Assets/UNEB/Utility/StateMachine.cs.meta new file mode 100644 index 00000000..e84d2aad --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/StateMachine.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 5431af42bc33d804eb9504ebe195cc41 +timeCreated: 1503072950 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/StringExtensions.cs b/Other/NodeEditorExamples/Assets/UNEB/Utility/StringExtensions.cs new file mode 100644 index 00000000..33c0d960 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/StringExtensions.cs @@ -0,0 +1,29 @@ + +namespace UNEB +{ + public static class StringExtensions + { + + /// <summary> + /// Merges the parent and child paths with the '/' character. + /// </summary> + /// <param name="parentDir"></param> + /// <param name="childDir"></param> + /// <returns></returns> + public static string Dir(this string parentDir, string childDir) + { + return parentDir + '/' + childDir; + } + + /// <summary> + /// Appends the extension to the file name with '.' + /// </summary> + /// <param name="file"></param> + /// <param name="extension"></param> + /// <returns></returns> + public static string Ext(this string file, string extension) + { + return file + '.' + extension; + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/StringExtensions.cs.meta b/Other/NodeEditorExamples/Assets/UNEB/Utility/StringExtensions.cs.meta new file mode 100644 index 00000000..2d33c4b7 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/StringExtensions.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: ae4ec85959c405246890eb2d46b5c0e5 +timeCreated: 1503086644 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/TextureLib.cs b/Other/NodeEditorExamples/Assets/UNEB/Utility/TextureLib.cs new file mode 100644 index 00000000..3849326a --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/TextureLib.cs @@ -0,0 +1,191 @@ + +using System.Collections.Generic; +using System.IO; +using System.Linq; + +using UnityEngine; +using UnityEditor; + +namespace UNEB.Utility +{ + /// <summary> + /// A static library that loads and stores references to textures via name. + /// </summary> + public class TextureLib + { + public const string kStandardTexturesFolder = "UNEB Textures"; + public enum TexType { PNG, JPEG, BMP }; + + private static Dictionary<string, Texture2D> _textures = new Dictionary<string, Texture2D>(); + private static Dictionary<TintedTextureKey, Texture2D> _tintedTextures = new Dictionary<TintedTextureKey, Texture2D>(); + + public static void LoadStandardTextures() + { + _textures.Clear(); + _tintedTextures.Clear(); + + LoadTexture("Grid"); + LoadTexture("Circle"); + LoadTexture("Square"); + } + + public static void LoadTexture(string name, TexType type = TexType.PNG) + { + string ext = GetTexTypeExtension(type); + string filename = name.Ext(ext); + string path = GetTextureFolderPath().Dir(filename); + + var tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path); + + if (tex != null) { + _textures.Add(name, tex); + } + + else { + Debug.LogError("The texture: " + path + " could not be found."); + } + } + + public static string GetTexTypeExtension(TexType type) + { + switch (type) { + case TexType.PNG: return "png"; + case TexType.JPEG: return "jpg"; + case TexType.BMP: return "bmp"; + } + + return ""; + } + + public static Texture2D GetTexture(string name) + { + if (_textures.ContainsKey(name)) { + return _textures[name]; + } + + Debug.LogError(name + " is not loaded in the texture library."); + return null; + } + + /// <summary> + /// Gets the texture with a tint. + /// It must be already loaded in the library. + /// </summary> + /// <param name="name"></param> + /// <param name="color"></param> + /// <returns></returns> + public static Texture2D GetTintTex(string name, Color color) + { + var key = new TintedTextureKey(color, name); + + // Check if it already exsists in the tint dictionary. + if (_tintedTextures.ContainsKey(key)) { + + if (_tintedTextures[key]) { + return _tintedTextures[key]; + } + + // Rebuild texture. + else { + _tintedTextures[key] = tintCopy(GetTexture(name), color); + return _tintedTextures[key]; + } + } + + // Make a new tint from the pre-loaded texture. + Texture2D tex = GetTexture(name); + + // Tint the tex and add to tinted tex dictionary. + if (tex) { + + var tintedTex = tintCopy(tex, color); + _tintedTextures.Add(key, tintedTex); + + return tintedTex; + } + + return null; + } + + private static Texture2D tintCopy(Texture2D tex, Color color) + { + int pixCount = tex.width * tex.height; + + var tintedTex = new Texture2D(tex.width, tex.height); + tintedTex.alphaIsTransparency = true; + + var newPixels = new Color[pixCount]; + var pixels = tex.GetPixels(); + + for (int i = 0; i < pixCount; ++i) { + newPixels[i] = color; + newPixels[i].a = pixels[i].a; + } + + tintedTex.SetPixels(newPixels); + tintedTex.Apply(); + + return tintedTex; + } + + public static string GetTextureFolderPath() + { + string fullpath = getFullPath(Application.dataPath, kStandardTexturesFolder); + + if (!string.IsNullOrEmpty(fullpath)) { + + // Return the texture folder path relative to Unity's Asset folder. + int index = fullpath.IndexOf("Assets"); + string localPath = fullpath.Substring(index); + + return localPath; + } + + Debug.LogError("Could not find folder: " + kStandardTexturesFolder); + return ""; + } + + private static string getFullPath(string root, string targetFolderName) + { + string[] dirs = Directory.GetDirectories(root, targetFolderName, SearchOption.AllDirectories); + + // Return first occurance containing targetFolderName. + if (dirs.Length != 0) { + return dirs[0]; + } + + // Could not find anything. + return ""; + } + + private struct TintedTextureKey + { + public readonly Color color; + public readonly string texName; + + public TintedTextureKey(Color c, string name) + { + color = c; + texName = name; + } + + public override int GetHashCode() + { + return color.GetHashCode() * texName.GetHashCode(); + } + + public override bool Equals(object obj) + { + if (obj is TintedTextureKey) { + + var key = (TintedTextureKey)obj; + return key.color == color && key.texName == texName; + } + + else { + return false; + } + } + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/TextureLib.cs.meta b/Other/NodeEditorExamples/Assets/UNEB/Utility/TextureLib.cs.meta new file mode 100644 index 00000000..180ea446 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/TextureLib.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 620aee55f886f914e825c6b891b8bd9b +timeCreated: 1501784046 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |