public UiBlackBoardNode(string key, T value, UiBlackBoardNode <T> parent)
 {
     _key                  = key;
     _value                = value;
     _parent               = parent;
     _children             = new List <UiBlackBoardNode <T> >();
     _valueChangeDelegates = new List <ValueUpdatedDelegate <T> >();
 }
        void InvokeValueChangeDelegates <T>(UiBlackBoardNode <T> node)
        {
            if (node == null)
            {
                Debug.LogError("Null node!");
                return;
            }

            for (int i = 0; i < node.ValueChangeDelegates.Count; ++i)
            {
                var valueChangeDelegate = node.ValueChangeDelegates[i];
                // TODO: Do we provide the paths as well???
                valueChangeDelegate.Invoke(node.Value);
            }
        }
        public void AddIntDelegate(string nodePath, ValueUpdatedDelegate <int> newDelegate)
        {
            List <string> tokenizedNodePath = new List <string>(TokenizeNodePath(nodePath));
            var           firstNode         = _intBlackboard.Find(x => x.Key == tokenizedNodePath[0]);

            if (firstNode == null)
            {
                firstNode = new UiBlackBoardNode <int>(tokenizedNodePath[0], default(int), null);
                _intBlackboard.Add(firstNode);
            }

            var foundNode = FindNode <int>(firstNode, tokenizedNodePath, true);

            foundNode.ValueChangeDelegates.Add(newDelegate);
        }
        public void SetIntValue(string nodePath, int value)
        {
            List <string> tokenizedNodePath = new List <string>(TokenizeNodePath(nodePath));
            var           firstNode         = _intBlackboard.Find(x => x.Key == tokenizedNodePath[0]);

            if (firstNode == null)
            {
                firstNode = new UiBlackBoardNode <int>(tokenizedNodePath[0], default(int), null);
                _intBlackboard.Add(firstNode);
            }

            var foundNode = FindNode <int>(firstNode, tokenizedNodePath, true);

            foundNode.Value = value;
            InvokeValueChangeDelegates <int>(foundNode);
        }
        public int GetIntValue(string nodePath)
        {
            List <string> tokenizedNodePath = new List <string>(TokenizeNodePath(nodePath));
            var           firstNode         = _intBlackboard.Find(x => x.Key == tokenizedNodePath[0]);

            if (firstNode == null)
            {
                firstNode = new UiBlackBoardNode <int>(tokenizedNodePath[0], default(int), null);
                _intBlackboard.Add(firstNode);
            }

            var foundNode = FindNode <int>(firstNode, tokenizedNodePath, false);

            if (foundNode != null)
            {
                return(foundNode.Value);
            }
            return(0);
        }
        UiBlackBoardNode <T> FindNode <T>(UiBlackBoardNode <T> startingNode, List <string> nodePath, bool shouldAddMissingNode = false)
        {
            if (nodePath.Count == 0)
            {
                return(null);
            }

            if (nodePath.Count == 1)
            {
                string lastKey = nodePath[0];
                if (startingNode.Key == lastKey)
                {
                    return(startingNode);
                }

                var lastChildNode = startingNode.Children.Find(x => x.Key == lastKey);
                if (lastChildNode == null)
                {
                    if (shouldAddMissingNode)
                    {
                        var newChild = new UiBlackBoardNode <T>(lastKey, default(T), startingNode);
                        startingNode.Children.Add(newChild);
                        return(newChild);
                    }
                }
                else
                {
                    return(lastChildNode);
                }
            }

            string nextChildNodeKey = nodePath[0];

            nodePath.RemoveAt(0);

            UiBlackBoardNode <T> nextChildNode = null;

            // Find the next node in the starting node or its children
            if (startingNode.Key == nextChildNodeKey)
            {
                nextChildNode = startingNode;
            }
            else
            {
                nextChildNode = startingNode.Children.Find(x => x.Key == nextChildNodeKey);
            }

            if (nextChildNode == null)
            {
                // Next node could not be found
                if (shouldAddMissingNode)
                {
                    // Start the creation of a new branch
                    var newChild = new UiBlackBoardNode <T>(nextChildNodeKey, default(T), startingNode);
                    startingNode.Children.Add(newChild);
                    return(FindNode <T>(newChild, nodePath, shouldAddMissingNode));
                }
                else
                {
                    // Nothing more we can do!
                    return(null);
                }
            }
            else
            {
                // Continue traversal down the child nodes
                return(FindNode <T>(nextChildNode, nodePath, shouldAddMissingNode));
            }
        }