protected override void RecursivelyCreatChildren(Dictionary <string, BTEditorSprite> _sprites)
 {
     if (m_node != null)
     {
         BTConditionNode node = m_node as BTConditionNode;
         if (node.Child != null)
         {
             // create link
             string lineKey = BTEditorLine.GetKey(m_node, node.Child);
             if (!_sprites.ContainsKey(lineKey))
             {
                 BTEditorLine line = new BTEditorLine(m_treeViewer);
                 line.ParentNode = m_node;
                 line.ChildNode  = node.Child;
                 _sprites.Add(line.GetKey(), line);
             }
             // do for children
             RecursivelyCreateSprites(_sprites, node.Child, m_treeViewer);
         }
     }
 }
Пример #2
0
        /**
         * @brief Set child's parent to _newParent. the function do the following things
         *  1. check whether the child and parent exist
         *  2. check whether there would be cycle structure: if the _child is _newParent's ancestor
         *  3. if the _newParent is ConditionNode, check if it has child. If it does, the action is illege
         *  4. break the old edge
         *  5. make new connection
         **/
        internal bool SetParent(string _child, string _newParent)
        {
            if (!m_sprites.ContainsKey(_child) || !m_sprites.ContainsKey(_newParent))
            {
                return(false);
            }
            BTEditorRectangle child     = m_sprites[_child] as BTEditorRectangle;
            BTEditorRectangle newParent = m_sprites[_newParent] as BTEditorRectangle;

            // check cycle
            if (child.Node.FindParent(newParent.Node) != null)
            {
                System.Windows.Forms.MessageBox.Show("Cycle structor is illegal.");
                return(false);
            }
            // check newParent
            if (newParent.Node.GetType().IsSubclassOf(typeof(BTConditionNode)))
            {
                BTNode otherChild = (newParent.Node as BTConditionNode).Child;
                if (otherChild != null)
                {
                    System.Windows.Forms.MessageBox.Show("Condition node cannot have more than one child.");
                    return(false);
                }
            }
            // destroy old edge
            BTNode oldBTParent = m_btTree.FindParent(child.Node);

            if (oldBTParent != null)
            {
                string oldEdgeKey = BTEditorLine.GetKey(oldBTParent, child.Node);
                if (m_sprites.ContainsKey(oldEdgeKey))
                {
                    m_sprites.Remove(oldEdgeKey);
                }
                if (oldBTParent.GetType().IsSubclassOf(typeof(BTCompositeNode)))
                {
                    (oldBTParent as BTCompositeNode).RemoveChild(child.Node);
                }
                else if (oldBTParent.GetType().IsSubclassOf(typeof(BTConditionNode)))
                {
                    (oldBTParent as BTConditionNode).Child = null;
                }
            }
            // create new link
            // destroy other edge if necessary
            if (newParent.Node.GetType().IsSubclassOf(typeof(BTConditionNode)))
            {
                (newParent.Node as BTConditionNode).Child = child.Node;
            }
            else if (newParent.Node.GetType().IsSubclassOf(typeof(BTCompositeNode)))
            {
                (newParent.Node as BTCompositeNode).AddChild(child.Node);
            }
            else
            {
                Debug.Assert(false, "Cannot add key to node with type: " + newParent.Node.GetType().Name);
                return(false);
            }
            BTEditorLine newLine = new BTEditorLine(this);

            newLine.ParentNode = newParent.Node;
            newLine.ChildNode  = child.Node;
            m_sprites.Add(newLine.GetKey(), newLine);

            AutoLayoutChart();
            Refresh();
            return(true);
        }