Exemplo n.º 1
0
        private void  除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }

            if (treeView1.SelectedNode.Tag == null)
            {
                return;
            }

            if (!(treeView1.SelectedNode.Tag is NodeItem))
            {
                return;
            }

            NodeItem  nodeItem  = treeView1.SelectedNode.Tag as NodeItem;
            NodeClass nodeClass = nodeItem.NodeClass;

            if (MessageBox.Show(string.Format("是否删除节点{0}?", nodeClass.ClassType), "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                m_Nodes.Remove(nodeClass);
                m_NodeTreeViewManager.RemoveNodeClass(nodeClass);
            }
        }
        public bool RemoveNodeClass(NodeClass nodeClass)
        {
            NodeItem nodeItem = FindNodeItem(nodeClass);

            if (nodeItem != null)
            {
                nodeItem.TreeNode.Remove();
            }

            if (nodeItem.CategoryItem != null)
            {
                TreeNode treeNode = nodeItem.CategoryItem.TreeNode;
                while (treeNode.Tag is CategoryItem)
                {
                    TreeNode parentNode = treeNode.Parent;
                    if (treeNode.Nodes.Count == 0)
                    {
                        treeNode.Remove();
                        treeNode = parentNode;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        private void 编辑类ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }

            if (treeView1.SelectedNode.Tag == null)
            {
                return;
            }

            if (!(treeView1.SelectedNode.Tag is NodeItem))
            {
                return;
            }

            NodeItem  nodeItem  = treeView1.SelectedNode.Tag as NodeItem;
            NodeClass nodeClass = nodeItem.NodeClass;

            EditClassForm editClassForm = new EditClassForm(this, nodeClass, delegate()
            {
                nodeItem = m_NodeTreeViewManager.BindNodeClass(nodeClass);
                treeView1.SelectedNode = nodeItem.TreeNode;
            });

            editClassForm.ShowDialog();
        }
Exemplo n.º 4
0
        /// <summary>
        /// 检验节点类ClassType
        /// </summary>
        /// <returns></returns>
        public VerifyInfo VerifyClassType()
        {
            //校验ClassType是否为空
            for (int i = 0; i < m_Nodes.Count; i++)
            {
                NodeClass nodeClass = m_Nodes[i];
                if (string.IsNullOrEmpty(nodeClass.ClassType))
                {
                    return(new VerifyInfo("存在空的ClassType"));
                }
            }

            //检验ClassType是否相同
            for (int i = 0; i < m_Nodes.Count; i++)
            {
                NodeClass classType_i = m_Nodes[i];
                if (classType_i != null)
                {
                    for (int ii = i + 1; ii < m_Nodes.Count; ii++)
                    {
                        NodeClass classtType_ii = m_Nodes[ii];
                        if (classType_i.ClassType == classtType_ii.ClassType)
                        {
                            return(new VerifyInfo(string.Format("行为树存在相同ClassType:{0}", classType_i.ClassType)));
                        }
                    }
                }
            }

            return(VerifyInfo.DefaultVerifyInfo);
        }
Exemplo n.º 5
0
        private void CopyClass()
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }

            if (treeView1.SelectedNode.Tag == null)
            {
                return;
            }

            if (!(treeView1.SelectedNode.Tag is NodeItem))
            {
                return;
            }

            NodeItem  nodeItem  = treeView1.SelectedNode.Tag as NodeItem;
            NodeClass nodeClass = nodeItem.NodeClass;

            NodeClassListContent content = new NodeClassListContent();

            content.DataList.Add(nodeClass);

            if (content.DataList.Count > 0)
            {
                Clipboard.SetText(XmlUtility.ObjectToString(content));
            }

            MainForm.Instance.ShowInfo("您复制了" + content.DataList.Count.ToString() + "个节点类!!!");
        }
Exemplo n.º 6
0
        /// <summary>
        /// 添加节点类
        /// </summary>
        /// <param name="nodeClass"></param>
        public bool AddClass(NodeClass nodeClass)
        {
            if (nodeClass == null)
            {
                return(false);
            }

            for (int i = 0; i < m_Nodes.Count; i++)
            {
                NodeClass tmp = m_Nodes[i];
                if (tmp.ClassType == nodeClass.ClassType)
                {
                    MainForm.Instance.ShowMessage(string.Format("已存在{0},请换一个类名", nodeClass.ClassType), "警告");
                    return(false);
                }
            }

            m_Nodes.Add(nodeClass);

            m_Nodes.Sort(delegate(NodeClass a, NodeClass b)
            {
                return(a.NodeType.CompareTo(b.NodeType));
            });

            return(true);
        }
Exemplo n.º 7
0
        private void PasteClass()
        {
            try
            {
                NodeClassListContent content = XmlUtility.StringToObject <NodeClassListContent>(Clipboard.GetText());

                NodeClass nodeClass = null;
                for (int i = 0; i < content.DataList.Count; i++)
                {
                    nodeClass = content.DataList[i];
                    string classType = nodeClass.ClassType;
                    do
                    {
                        classType += "_New";
                    }while (m_Nodes.ExistClassType(classType));

                    nodeClass.ClassType = classType;
                    m_Nodes.AddClass(nodeClass);
                }

                m_NodeTreeViewManager.BindNodeClass(nodeClass);

                MainForm.Instance.ShowInfo("您粘贴了" + content.DataList.Count + "个节点类!!!");
            }
            catch (Exception ex)
            {
                MainForm.Instance.ShowInfo("无法进行粘贴,错误信息:" + ex.Message);
                MainForm.Instance.ShowMessage("无法进行粘贴,错误信息:" + ex.Message, "警告");
            }
        }
Exemplo n.º 8
0
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NodeClass            nodeClass = new NodeClass();
            InputValueDialogForm testForm  = new InputValueDialogForm("编辑", nodeClass);

            testForm.ShowDialog();
        }
Exemplo n.º 9
0
        /// <summary>
        /// 更新NodeClass的内容
        /// </summary>
        /// <param name="nodeClass"></param>
        public void UpdateNodeClass(NodeClass nodeClass)
        {
            if (nodeClass == null)
            {
                return;
            }

            if (nodeClass == this)
            {
                return;
            }

            //改名
            if (m_ClassType != nodeClass.ClassType)
            {
                MainForm.Instance.TreeData.UpdateClassType(m_ClassType, nodeClass.ClassType);
                ContentUserControl.Instance.ClearAllSelected();
            }

            m_ClassType   = nodeClass.ClassType;
            m_Label       = nodeClass.Label;
            m_Category    = nodeClass.Category;
            m_NodeType    = nodeClass.NodeType;
            m_Describe    = nodeClass.Describe;
            m_ShowContent = nodeClass.ShowContent;

            m_Fields.Clear();
            m_Fields.AddRange(nodeClass.Fields);
        }
Exemplo n.º 10
0
 /// <summary>
 /// 移除未定义的枚举字段
 /// </summary>
 public void RemoveUnDefineEnumField()
 {
     for (int i = 0; i < m_Nodes.Count; i++)
     {
         NodeClass nodeClass = m_Nodes[i];
         nodeClass.RemoveUnDefineEnumField();
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// 删除节点类
 /// </summary>
 /// <param name="nodeClass">节点类对象</param>
 /// <returns></returns>
 public bool Remove(NodeClass nodeClass)
 {
     if (nodeClass == null)
     {
         return(false);
     }
     return(m_Nodes.Remove(nodeClass));
 }
Exemplo n.º 12
0
 public EditClassForm(ClassForm classForm, NodeClass nodeClass, Action editCallback)
 {
     m_ClassForm     = classForm;
     m_NodeClass     = nodeClass;
     m_Content       = XmlUtility.ObjectToString(m_NodeClass);
     m_EditNodeClass = XmlUtility.StringToObject <NodeClass>(m_Content);
     m_EditCallback  = editCallback;
     InitializeComponent();
 }
        public void BindNodeClasses()
        {
            for (int i = 0; i < m_NodeClasses.Nodes.Count; i++)
            {
                NodeClass nodeClass = m_NodeClasses.Nodes[i];
                BindNodeClass(nodeClass);
            }

            m_TreeView.ExpandAll();
            m_TreeView.SelectedNode = m_TreeView.Nodes[0];
            m_TreeView.Nodes[0].EnsureVisible();
        }
Exemplo n.º 14
0
        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }

            for (int i = 0; i < contextMenuStrip1.Items.Count; i++)
            {
                contextMenuStrip1.Items[i].Visible = false;
            }

            if (treeView1.SelectedNode.Tag is NodeTypeItem || treeView1.SelectedNode.Tag is CategoryItem)
            {
                contextMenuStrip1.Items[0].Visible = true;
                contextMenuStrip1.Items[3].Visible = true;
            }
            else if (treeView1.SelectedNode.Tag is NodeItem)
            {
                contextMenuStrip1.Items[1].Visible = true;
                contextMenuStrip1.Items[2].Visible = true;
            }

            if (e.Clicks == 2)
            {
                if (treeView1.SelectedNode == null)
                {
                    return;
                }

                if (treeView1.SelectedNode.Tag == null)
                {
                    return;
                }

                if (!(treeView1.SelectedNode.Tag is NodeItem))
                {
                    return;
                }

                NodeItem  nodeItem  = treeView1.SelectedNode.Tag as NodeItem;
                NodeClass nodeClass = nodeItem.NodeClass;


                EditClassForm editClassForm = new EditClassForm(this, nodeClass, delegate()
                {
                    nodeItem = m_NodeTreeViewManager.BindNodeClass(nodeClass);
                    treeView1.SelectedNode = nodeItem.TreeNode;
                });
                editClassForm.ShowDialog();
            }
        }
        public NodeItem FindNodeItem(NodeClass nodeClass)
        {
            for (int i = 0; i < m_TreeView.Nodes.Count; i++)
            {
                TreeNode treeNode = m_TreeView.Nodes[i];
                NodeItem nodeItem = FindNodeItem(treeNode, nodeClass);
                if (nodeItem != null)
                {
                    return(nodeItem);
                }
            }

            return(null);
        }
Exemplo n.º 16
0
        /// <summary>
        /// 移除未定义的节点
        /// </summary>
        public bool RemoveUnDefineNode()
        {
            bool remove = false;

            for (int i = m_Nodes.Count - 1; i >= 0; i--)
            {
                NodeDesigner node      = m_Nodes[i];
                NodeClass    nodeClass = MainForm.Instance.NodeClasses.FindNode(node.ClassType);
                if (nodeClass == null)
                {
                    RemoveNode(node);
                    remove = true;
                }
            }

            return(remove);
        }
Exemplo n.º 17
0
        /// <summary>
        /// 获取指定类型所有组合节点类
        /// </summary>
        /// <param name="nodeType">节点类型</param>
        /// <returns></returns>
        public List <NodeClass> GetClasses(NodeType nodeType)
        {
            List <NodeClass> nodeList = new List <NodeClass>();

            for (int i = 0; i < m_Nodes.Count; i++)
            {
                NodeClass nodeClass = m_Nodes[i];
                if (nodeClass == null)
                {
                    continue;
                }
                if (nodeClass.NodeType == nodeType)
                {
                    nodeList.Add(nodeClass);
                }
            }
            return(nodeList);
        }
Exemplo n.º 18
0
        /// <summary>
        /// 通过ClassType查找节点
        /// </summary>
        /// <param name="classType"></param>
        /// <returns></returns>
        public NodeClass FindNode(string classType)
        {
            if (string.IsNullOrEmpty(classType))
            {
                return(null);
            }

            for (int i = 0; i < m_Nodes.Count; i++)
            {
                NodeClass nodeClass = m_Nodes[i];
                if (nodeClass != null && nodeClass.ClassType == classType)
                {
                    return(nodeClass);
                }
            }

            return(null);
        }
Exemplo n.º 19
0
        //刷新ListView
        private void BindField(NodeClass m_NodeClass)
        {
            if (m_NodeClass == null)
            {
                return;
            }

            listViewFields.Items.Clear();
            foreach (NodeField field in m_NodeClass.Fields)
            {
                ListViewItem listViewItem = listViewFields.Items.Add(field.FieldName);
                listViewItem.Tag = field;
                listViewItem.SubItems.Add(field.Label);
                listViewItem.SubItems.Add(EditorUtility.GetFieldTypeName(field.FieldType));
                listViewItem.SubItems.Add(field.DefaultValue != null ? field.DefaultValue.ToString() : string.Empty);
                listViewItem.SubItems.Add(field.Describe);
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// 校节点类数据
        /// </summary>
        /// <returns></returns>
        public VerifyInfo VerifyNodeClass()
        {
            VerifyInfo verifyClassType = VerifyClassType();

            if (verifyClassType.HasError)
            {
                return(verifyClassType);
            }

            for (int i = 0; i < m_Nodes.Count; i++)
            {
                NodeClass  nodeClass       = m_Nodes[i];
                VerifyInfo verifyNodeClass = nodeClass.VerifyNodeClass();
                if (verifyNodeClass.HasError)
                {
                    return(verifyNodeClass);
                }
            }

            return(VerifyInfo.DefaultVerifyInfo);
        }
Exemplo n.º 21
0
        public bool ExistClassType(string classType)
        {
            if (string.IsNullOrEmpty(classType))
            {
                throw new System.Exception("NodeClasses.ExistClassType() classType类型为空");
            }

            for (int i = 0; i < m_Nodes.Count; i++)
            {
                NodeClass nodeClass = m_Nodes[i];
                if (nodeClass == null)
                {
                    continue;
                }
                if (nodeClass.ClassType == classType)
                {
                    return(true);
                }
            }

            return(false);
        }
        private NodeItem FindNodeItem(TreeNode treeNode, NodeClass nodeClass)
        {
            if (treeNode.Tag is NodeItem)
            {
                NodeItem nodeItem = treeNode.Tag as NodeItem;
                if (nodeItem.NodeClass.ClassType == nodeClass.ClassType || nodeItem.NodeClass == nodeClass)
                {
                    return(nodeItem);
                }
            }

            for (int i = 0; i < treeNode.Nodes.Count; i++)
            {
                TreeNode tempTreeNode = treeNode.Nodes[i];
                NodeItem nodeItem     = FindNodeItem(tempTreeNode, nodeClass);
                if (nodeItem != null)
                {
                    return(nodeItem);
                }
            }

            return(null);
        }
Exemplo n.º 23
0
        /// <summary>
        /// 添加Agent
        /// </summary>
        private void AddAgent()
        {
            if (WorkSpaceData == null)
            {
                ShowMessage("当前没有工作空间,请新建或者打开工作空间!");
                return;
            }

            if (TreeData == null)
            {
                return;
            }

            string group = string.Empty;

            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Tag is GroupItem)
                {
                    GroupItem groupItem = treeView1.SelectedNode.Tag as GroupItem;
                    group = groupItem.Group.GroupName;
                }
                else if (treeView1.SelectedNode.Tag is AgentItem)
                {
                    AgentItem agentItem = treeView1.SelectedNode.Tag as AgentItem;
                    if (agentItem.GroupItem != null)
                    {
                        group = agentItem.GroupItem.Group.GroupName;
                    }
                }
            }

            AgentDesigner agent = new AgentDesigner();

            agent.GroupName = group;
            string agentID = "NewAgent_" + DateTime.Now.Ticks;

            do
            {
                agentID = "NewAgent_" + DateTime.Now.Ticks;
            } while (TreeData.ExistAgent(agentID));

            agent.AgentID = agentID;

            //创建开始节点
            NodeDesigner startNode = null;
            NodeClass    nodeClass = NodeClasses.FindNode("Sequence");

            if (nodeClass != null)
            {
                Rect rect = new Rect(EditorUtility.Center.x, EditorUtility.Center.y, EditorUtility.NodeWidth,
                                     EditorUtility.NodeHeight);
                startNode           = new NodeDesigner(nodeClass.Label, nodeClass.ClassType, rect);
                startNode.ID        = agent.GenNodeID();
                startNode.StartNode = true;
                startNode.NodeType  = nodeClass.NodeType;
                startNode.Describe  = nodeClass.Describe;

                //创建字段
                for (int i = 0; i < nodeClass.Fields.Count; i++)
                {
                    NodeField     nodeField = nodeClass.Fields[i];
                    FieldDesigner field     = EditorUtility.CreateFieldByNodeField(nodeField);
                    if (field == null)
                    {
                        continue;
                    }
                    startNode.Fields.Add(field);
                }

                agent.AddNode(startNode);
            }

            //创建空操作节点
            NodeClass noopClass = NodeClasses.FindNode("Noop");

            if (startNode != null && noopClass != null)
            {
                Rect rect = new Rect(EditorUtility.Center.x + 250, EditorUtility.Center.y, EditorUtility.NodeWidth,
                                     EditorUtility.NodeHeight);
                NodeDesigner noopNode = new NodeDesigner(noopClass.Label, noopClass.ClassType, rect);
                noopNode.ID       = agent.GenNodeID();
                noopNode.NodeType = noopClass.NodeType;
                noopNode.Describe = noopClass.Describe;
                agent.AddNode(noopNode);

                startNode.AddChildNode(noopNode);
            }

            TreeViewManager.AddAgent(agent);
        }
Exemplo n.º 24
0
        /// <summary>
        /// 修正数据
        /// </summary>
        public bool AjustData()
        {
            bool ajust = false;

            for (int i = 0; i < m_Nodes.Count; i++)
            {
                NodeDesigner node      = m_Nodes[i];
                NodeClass    nodeClass = MainForm.Instance.NodeClasses.FindNode(node.ClassType);

                //修正节点标签
                if (node.Label != nodeClass.Label)
                {
                    node.Label = nodeClass.Label;
                    ajust      = true;
                }

                //移除模板中没有的字段
                for (int ii = node.Fields.Count - 1; ii >= 0; ii--)
                {
                    FieldDesigner field = node.Fields[ii];
                    if (!nodeClass.ExistFieldName(field.FieldName))
                    {
                        ajust = true;
                        node.Fields.RemoveAt(ii);
                    }
                }

                //修正类型不匹配的(节点字段和模板字段类型不匹配)
                for (int ii = 0; ii < node.Fields.Count; ii++)
                {
                    FieldDesigner field     = node.Fields[ii];
                    NodeField     nodeField = nodeClass.FindField(field.FieldName);
                    if (field.FieldType != nodeField.FieldType)
                    {
                        //重新给默认值
                        node.Fields[ii] = EditorUtility.CreateFieldByNodeField(nodeField);
                        ajust           = true;
                    }
                }

                //添加不存的字段
                for (int ii = nodeClass.Fields.Count - 1; ii >= 0; ii--)
                {
                    NodeField     nodeField = nodeClass.Fields[ii];
                    FieldDesigner field     = node.FindFieldByName(nodeField.FieldName);
                    //不存在的字段要添加
                    if (field == null)
                    {
                        FieldDesigner newField = EditorUtility.CreateFieldByNodeField(nodeField);
                        node.AddField(newField);
                        ajust = true;
                    }
                }

                //排序字段(要和模板中一致)
                for (int ii = 0; ii < nodeClass.Fields.Count; ii++)
                {
                    NodeField nodeField = nodeClass.Fields[ii];
                    int       index     = node.GetFieldIndex(nodeField.FieldName);
                    if (index != ii)
                    {
                        //交换
                        FieldDesigner tempField_ii = node.Fields[ii];
                        node.Fields[ii]    = node.Fields[index];
                        node.Fields[index] = tempField_ii;
                        ajust = true;
                    }
                }

                //修正Label
                for (int ii = 0; ii < node.Fields.Count; ii++)
                {
                    FieldDesigner field     = node.Fields[ii];
                    NodeField     nodeField = nodeClass.Fields[ii];
                    if (field.Label != nodeField.Label)
                    {
                        field.Label = nodeField.Label;
                        ajust       = true;
                    }
                }
            }

            return(ajust);
        }
Exemplo n.º 25
0
 public void AddClass(NodeClass nodeClass)
 {
     m_NodeTreeViewManager.BindNodeClass(nodeClass);
     MainForm.Instance.ShowInfo("成功添加:" + nodeClass.ClassType + ",时间:" + DateTime.Now);
 }
Exemplo n.º 26
0
        public void ResetNodes()
        {
            m_Nodes.Clear();
            #region 组合节点
            //并行节点
            NodeClass parallelNode = new NodeClass();
            parallelNode.ClassType = "Parallel";
            parallelNode.Label     = "并行节点";
            parallelNode.NodeType  = NodeType.Composite;
            parallelNode.Describe  = "Parallel节点在一般意义上是并行的执行其子节点,即“一边做A,一边做B”";
            //成功条件
            NodeField parallelNodeSuccessType = new NodeField()
            {
                FieldName = "SuccessType", FieldType = FieldType.EnumField, Describe = "成功条件"
            };
            EnumDefaultValue parallelNodeSuccessEnumDefaultValue = parallelNodeSuccessType.DefaultValue as EnumDefaultValue;
            parallelNodeSuccessEnumDefaultValue.EnumType     = "SUCCESS_POLICY";
            parallelNodeSuccessEnumDefaultValue.DefaultValue = "SUCCEED_ON_ALL";
            parallelNodeSuccessType.Label = "成功条件";
            parallelNode.AddField(parallelNodeSuccessType);
            //失败条件
            NodeField parallelNodeFailType = new NodeField()
            {
                FieldName = "FailType", FieldType = FieldType.EnumField, Describe = "失败条件"
            };
            EnumDefaultValue parallelNodeFailTypeEnumDefaultValue = parallelNodeFailType.DefaultValue as EnumDefaultValue;
            parallelNodeFailTypeEnumDefaultValue.EnumType     = "FAILURE_POLICY";
            parallelNodeFailTypeEnumDefaultValue.DefaultValue = "FAIL_ON_ONE";
            parallelNodeFailType.Label = "失败条件";
            parallelNode.AddField(parallelNodeFailType);
            AddClass(parallelNode);

            //顺序节点
            NodeClass sequenceNode = new NodeClass();
            sequenceNode.ClassType = "Sequence";
            sequenceNode.Label     = "顺序节点";
            sequenceNode.NodeType  = NodeType.Composite;
            sequenceNode.Describe  = "Sequence节点以给定的顺序依次执行其子节点,直到所有子节点成功返回,该节点也返回成功。只要其中某个子节点失败,那么该节点也失败。";
            AddClass(sequenceNode);

            //选择节点
            NodeClass Selector = new NodeClass();
            Selector.ClassType = "Selector";
            Selector.Label     = "选择节点";
            Selector.Category  = "";
            Selector.NodeType  = NodeType.Composite;
            Selector.Describe  = "选择节点";
            AddClass(Selector);

            //ifelse
            NodeClass IfElse = new NodeClass();
            IfElse.ClassType = "IfElse";
            IfElse.Label     = "IfElse";
            IfElse.NodeType  = NodeType.Composite;
            IfElse.Describe  = "";
            AddClass(IfElse);

            //随机节点
            NodeClass Random = new NodeClass();
            Random.ClassType = "Random";
            Random.Label     = "随机节点";
            Random.Category  = "随机";
            Random.NodeType  = NodeType.Composite;
            Random.Describe  = "随机节点";
            AddClass(Random);

            //随机选择节点
            NodeClass RandomSelector = new NodeClass();
            RandomSelector.ClassType = "RandomSelector";
            RandomSelector.Label     = "随机选择";
            RandomSelector.Category  = "随机";
            RandomSelector.NodeType  = NodeType.Composite;
            RandomSelector.Describe  = "随机选择节点";
            AddClass(RandomSelector);

            //随机序列节点
            NodeClass RandomSequence = new NodeClass();
            RandomSequence.ClassType = "RandomSequence";
            RandomSequence.Label     = "随机序列";
            RandomSequence.Category  = "随机";
            RandomSequence.NodeType  = NodeType.Composite;
            RandomSequence.Describe  = "随机序列节点";
            AddClass(RandomSequence);

            //概率选择节点
            NodeClass RateSelector = new NodeClass();
            RateSelector.ClassType = "RateSelector";
            RateSelector.Label     = "概率选择";
            RateSelector.Category  = "随机";
            RateSelector.NodeType  = NodeType.Composite;
            RateSelector.Describe  = "概率选择节点";
            RateSelector.AddField(new NodeField()
            {
                FieldName = "Priority", Label = "优先级", FieldType = FieldType.RepeatIntField, Describe = ""
            });
            AddClass(RateSelector);

            #endregion

            #region 装饰节点

            //成功节点
            NodeClass Success = new NodeClass();
            Success.ClassType = "Success";
            Success.Label     = "成功节点";
            Success.NodeType  = NodeType.Decorator;
            Success.Describe  = "成功节点";
            AddClass(Success);

            //失败节点
            NodeClass Failure = new NodeClass();
            Failure.ClassType = "Failure";
            Failure.Label     = "失败节点";
            Failure.NodeType  = NodeType.Decorator;
            Failure.Describe  = "失败节点";
            AddClass(Failure);

            //帧数节点用于在指定的帧数内,持续调用其子节点
            NodeClass Frames = new NodeClass();
            Frames.ClassType   = "Frames";
            Frames.Label       = "帧数节点";
            Frames.NodeType    = NodeType.Decorator;
            Frames.ShowContent = true;
            NodeField FramesField = new NodeField()
            {
                FieldName = "Frames", Label = "持续帧数", FieldType = FieldType.IntField, Describe = "持续帧数"
            };
            (FramesField.DefaultValue as IntDefaultValue).DefaultValue = 1;
            Frames.AddField(FramesField);
            Frames.Describe = "帧数节点用于在指定的帧数内,持续调用其子节点";
            AddClass(Frames);

            //输出Log节点
            NodeClass Log = new NodeClass();
            Log.ClassType   = "Log";
            Log.Label       = "输出节点";
            Log.NodeType    = NodeType.Decorator;
            Log.Describe    = "输出log节点";
            Log.ShowContent = true;
            Log.AddField(new NodeField()
            {
                FieldName = "Content", Label = "输出内容", FieldType = FieldType.StringField, Describe = "输出的内容"
            });
            AddClass(Log);

            //循环节点 -1无限循环
            NodeClass Loop = new NodeClass();
            Loop.ClassType   = "Loop";
            Loop.Label       = "循环节点";
            Loop.NodeType    = NodeType.Decorator;
            Loop.Describe    = "循环节点 -1无限循环";
            Loop.ShowContent = true;
            Loop.AddField(new NodeField()
            {
                FieldName = "LoopTimes", Label = "循环次数", FieldType = FieldType.IntField, Describe = "循环次数"
            });
            AddClass(Loop);

            //直到某个值达成前一直循环
            NodeClass LoopUntil = new NodeClass();
            LoopUntil.ClassType = "LoopUntil";
            LoopUntil.Label     = "循环直到";
            LoopUntil.NodeType  = NodeType.Decorator;
            LoopUntil.Describe  = "直到某个值达成前一直循环";
            AddClass(LoopUntil);

            //取反节点
            NodeClass Not = new NodeClass();
            Not.ClassType = "Not";
            Not.Label     = "取反节点";
            Not.NodeType  = NodeType.Decorator;
            Not.Describe  = "取反节点";
            AddClass(Not);

            //指定时间内运行
            NodeClass Time = new NodeClass();
            Time.ClassType = "Time";
            Time.Label     = "时间";
            Time.NodeType  = NodeType.Decorator;
            Time.Describe  = "指定时间内运行";
            NodeField TimeField = new NodeField()
            {
                FieldName = "Duration", Label = "持续时间(毫秒)", FieldType = FieldType.IntField, Describe = "持续时间(毫秒)"
            };
            (TimeField.DefaultValue as IntDefaultValue).DefaultValue = 1000;
            Time.AddField(TimeField);
            Time.ShowContent = true;
            AddClass(Time);

            //阻塞,直到子节点返回true
            NodeClass WaitUntil = new NodeClass();
            WaitUntil.ClassType = "WaitUntil";
            WaitUntil.Label     = "等待返回True";
            WaitUntil.NodeType  = NodeType.Decorator;
            WaitUntil.Describe  = "阻塞,直到子节点返回true";
            AddClass(WaitUntil);

            #endregion

            #region 条件节点

            //比较Int节点
            NodeClass CompareInt = new NodeClass();
            CompareInt.ClassType = "CompareInt";
            CompareInt.Label     = "比较Int节点";
            CompareInt.NodeType  = NodeType.Condition;
            CompareInt.Describe  = "Compare节点对左右参数进行比较";
            //左边参数类型
            NodeField CompareInt_LeftType = new NodeField()
            {
                FieldName = "LeftType", FieldType = FieldType.EnumField, Describe = ""
            };
            (CompareInt_LeftType.DefaultValue as EnumDefaultValue).EnumType = "ParameterType";
            CompareInt_LeftType.Label = "左参数类型";
            CompareInt.AddField(CompareInt_LeftType);
            //左边参数变量名
            CompareInt.AddField(new NodeField()
            {
                FieldName = "LeftParameter", Label = "左参数名", FieldType = FieldType.StringField, Describe = "左边参数变量名"
            });
            //比较符号
            NodeField CompareInt_Type = new NodeField()
            {
                FieldName = "CompareType", FieldType = FieldType.EnumField, Describe = "比较符号<、>、<=、>=、==、!="
            };
            (CompareInt_Type.DefaultValue as EnumDefaultValue).EnumType = "CompareType";
            CompareInt_Type.Label = "比较操作符";
            CompareInt.AddField(CompareInt_Type);
            //右边边参数类型
            NodeField CompareInt_RightType = new NodeField()
            {
                FieldName = "RightType", FieldType = FieldType.EnumField, Describe = ""
            };
            (CompareInt_RightType.DefaultValue as EnumDefaultValue).EnumType = "ParameterType";
            CompareInt_RightType.Label = "右参数类型";
            CompareInt.AddField(CompareInt_RightType);
            //右边参数变量名
            CompareInt.AddField(new NodeField()
            {
                FieldName = "RightParameter", Label = "右参数名", FieldType = FieldType.StringField, Describe = "右边参数变量名"
            });
            AddClass(CompareInt);

            //比较Float节点
            NodeClass CompareFloat = new NodeClass();
            CompareFloat.ClassType = "CompareFloat";
            CompareFloat.Label     = "比较Float节点";
            CompareFloat.NodeType  = NodeType.Condition;
            CompareFloat.Describe  = "Compare节点对左右参数进行比较";
            //左边参数类型
            NodeField CompareFloat_LeftType = new NodeField()
            {
                FieldName = "LeftType", FieldType = FieldType.EnumField, Describe = ""
            };
            (CompareFloat_LeftType.DefaultValue as EnumDefaultValue).EnumType = "ParameterType";
            CompareFloat_LeftType.Label = "左参数类型";
            CompareFloat.AddField(CompareFloat_LeftType);
            //左边参数变量名
            CompareFloat.AddField(new NodeField()
            {
                FieldName = "LeftParameter", Label = "左参数名", FieldType = FieldType.StringField, Describe = "左边参数变量名"
            });
            //比较符号
            NodeField CompareFloat_Type = new NodeField()
            {
                FieldName = "CompareType", FieldType = FieldType.EnumField, Describe = "比较符号<、>、<=、>=、==、!="
            };
            (CompareFloat_Type.DefaultValue as EnumDefaultValue).EnumType = "CompareType";
            CompareFloat_Type.Label = "比较操作符";
            CompareFloat.AddField(CompareFloat_Type);
            //右边边参数类型
            NodeField CompareFloat_RightType = new NodeField()
            {
                FieldName = "RightType", FieldType = FieldType.EnumField, Describe = ""
            };
            (CompareFloat_RightType.DefaultValue as EnumDefaultValue).EnumType = "ParameterType";
            CompareFloat_RightType.Label = "右参数类型";
            CompareFloat.AddField(CompareFloat_RightType);
            //右边参数变量名
            CompareFloat.AddField(new NodeField()
            {
                FieldName = "RightParameter", Label = "右参数名", FieldType = FieldType.StringField, Describe = "右边参数变量名"
            });
            AddClass(CompareFloat);

            //比较String节点
            NodeClass CompareString = new NodeClass();
            CompareString.ClassType = "CompareString";
            CompareString.Label     = "比较String节点";
            CompareString.NodeType  = NodeType.Condition;
            CompareString.Describe  = "Compare节点对左右参数进行比较";
            //左边参数类型
            NodeField CompareString_LeftType = new NodeField()
            {
                FieldName = "LeftType", FieldType = FieldType.EnumField, Describe = ""
            };
            (CompareString_LeftType.DefaultValue as EnumDefaultValue).EnumType = "ParameterType";
            CompareString_LeftType.Label = "左参数类型";
            CompareString.AddField(CompareString_LeftType);
            //左边参数变量名
            CompareString.AddField(new NodeField()
            {
                FieldName = "LeftParameter", Label = "左参数名", FieldType = FieldType.StringField, Describe = "左边参数变量名"
            });
            //比较符号
            NodeField CompareString_Type = new NodeField()
            {
                FieldName = "CompareType", FieldType = FieldType.EnumField, Describe = "比较符号<、>、<=、>=、==、!="
            };
            (CompareString_Type.DefaultValue as EnumDefaultValue).EnumType = "CompareType";
            CompareString_Type.Label = "比较操作符";
            CompareString.AddField(CompareString_Type);
            //右边边参数类型
            NodeField CompareString_RightType = new NodeField()
            {
                FieldName = "RightType", FieldType = FieldType.EnumField, Describe = ""
            };
            (CompareString_RightType.DefaultValue as EnumDefaultValue).EnumType = "ParameterType";
            CompareString_RightType.Label = "右参数类型";
            CompareString.AddField(CompareString_RightType);
            //右边参数变量名
            CompareString.AddField(new NodeField()
            {
                FieldName = "RightParameter", Label = "右参数名", FieldType = FieldType.StringField, Describe = "右边参数变量名"
            });
            AddClass(CompareString);

            #endregion

            #region 动作节点

            //赋值节点Int
            NodeClass AssignmentInt = new NodeClass();
            AssignmentInt.ClassType = "AssignmentInt";
            AssignmentInt.Label     = "赋值节点(Int)";
            AssignmentInt.NodeType  = NodeType.Action;
            AssignmentInt.Describe  = "赋值节点";
            AssignmentInt.AddField(new NodeField()
            {
                FieldName = "ParameterName", Label = "变量名", FieldType = FieldType.StringField, Describe = "参数变量名"
            });
            AssignmentInt.AddField(new NodeField()
            {
                FieldName = "Parameter", Label = "赋值Int", FieldType = FieldType.IntField, Describe = "参数值"
            });
            AssignmentInt.ShowContent = true;
            AddClass(AssignmentInt);

            //赋值节点Float
            NodeClass AssignmentFloat = new NodeClass();
            AssignmentFloat.ClassType = "AssignmentFloat";
            AssignmentFloat.Label     = "赋值节点(Float)";
            AssignmentFloat.NodeType  = NodeType.Action;
            AssignmentFloat.Describe  = "赋值节点(Float)";
            AssignmentFloat.AddField(new NodeField()
            {
                FieldName = "ParameterName", Label = "变量名", FieldType = FieldType.StringField, Describe = "参数变量名"
            });
            AssignmentFloat.AddField(new NodeField()
            {
                FieldName = "Parameter", Label = "赋值Float", FieldType = FieldType.FloatField, Describe = "参数值"
            });
            AssignmentFloat.ShowContent = true;
            AddClass(AssignmentFloat);

            //赋值节点String
            NodeClass AssignmentString = new NodeClass();
            AssignmentString.ClassType = "AssignmentString";
            AssignmentString.Label     = "赋值节点(String)";
            AssignmentString.NodeType  = NodeType.Action;
            AssignmentString.Describe  = "赋值节点(String)";
            AssignmentString.AddField(new NodeField()
            {
                FieldName = "ParameterName", Label = "变量名", FieldType = FieldType.StringField, Describe = "参数变量名"
            });
            AssignmentString.AddField(new NodeField()
            {
                FieldName = "Parameter", Label = "赋值字符串", FieldType = FieldType.StringField, Describe = "参数值"
            });
            AssignmentString.ShowContent = true;
            AddClass(AssignmentString);

            //等待节点
            NodeClass Wait = new NodeClass();
            Wait.ClassType = "Wait";
            Wait.Label     = "等待节点";
            Wait.NodeType  = NodeType.Action;
            Wait.Describe  = "等待节点";
            NodeField WaintField = new NodeField()
            {
                FieldName = "Millisecond", Label = "等待时间(毫秒)", FieldType = FieldType.IntField, Describe = "等待时间(毫秒)"
            };
            IntDefaultValue WaintFieldDefaultField = WaintField.DefaultValue as IntDefaultValue;
            WaintFieldDefaultField.DefaultValue = 1000;
            Wait.AddField(WaintField);
            Wait.ShowContent = true;
            AddClass(Wait);

            //空操作节点
            NodeClass Noop = new NodeClass();
            Noop.ClassType = "Noop";
            Noop.Label     = "空操作节点";
            Noop.NodeType  = NodeType.Action;
            Noop.Describe  = "空操作节点";
            AddClass(Noop);

            #endregion
        }
        public NodeItem BindNodeClass(NodeClass nodeClass)
        {
            NodeTypeItem nodeTypeItem = GetNodeTypeItem(nodeClass.NodeType);

            if (nodeTypeItem == null)
            {
                return(null);
            }

            NodeItem nodeItem = FindNodeItem(nodeClass);

            //刷新
            if (nodeItem != null)
            {
                if (nodeItem.OldNodeType == nodeClass.NodeType && nodeItem.OldCategory == nodeClass.Category)
                {
                    nodeItem.TreeNode.Text = nodeClass.ClassType;
                    return(nodeItem);
                }
                else
                {
                    nodeItem.TreeNode.Remove();
                }

                if (nodeItem.OldCategory != nodeClass.Category)
                {
                    CheckRemoveCategory(nodeItem.OldNodeType, nodeItem.OldCategory);
                }
            }

            if (nodeItem == null)
            {
                nodeItem = new NodeItem();
            }

            if (string.IsNullOrEmpty(nodeClass.Category))
            {
                TreeNode treeNode = nodeTypeItem.TreeNode.Nodes.Add(nodeClass.ClassType);
                treeNode.Tag          = nodeItem;
                nodeItem.TreeNode     = treeNode;
                nodeItem.CategoryItem = null;
                nodeItem.NodeTypeItem = nodeTypeItem;
                nodeItem.NodeClass    = nodeClass;

                nodeItem.OldCategory = nodeClass.Category;
                nodeItem.OldNodeType = nodeClass.NodeType;
            }
            else
            {
                CategoryItem categoryItem = FindCategoryItem(nodeClass.NodeType, nodeClass.Category);

                if (categoryItem == null)
                {
                    categoryItem = BindCategory(nodeTypeItem.TreeNode, nodeClass.Category);
                }

                TreeNode treeNode = categoryItem.TreeNode.Nodes.Add(nodeClass.ClassType);
                treeNode.Tag          = nodeItem;
                nodeItem.TreeNode     = treeNode;
                nodeItem.CategoryItem = categoryItem;
                nodeItem.NodeTypeItem = nodeTypeItem;
                nodeItem.NodeClass    = nodeClass;

                nodeItem.OldCategory = nodeClass.Category;
                nodeItem.OldNodeType = nodeClass.NodeType;
            }

            return(nodeItem);
        }
Exemplo n.º 28
0
        /// <summary>
        /// 绘制节点
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="graphics">graphics</param>
        /// <param name="offset">偏移</param>
        /// <param name="on">是否选中</param>
        public static void Draw(NodeDesigner node, Graphics graphics, bool on)
        {
            Rect titleRect   = GetTitleRect(node);
            Rect contentRect = GetContentRect(node);

            //绘制开始节点
            if (node.StartNode)
            {
                DrawStartNode(graphics, node);
            }
            //画连接点
            DrawNodeLinkPoint(graphics, node);
            //画标题底框
            //graphics.DrawImage(Resources.NodeBackground_Dark, titleRect);
            graphics.FillRectangle(NodeTitleBrush, titleRect);
            //标题
            graphics.DrawString(node.Title, NodeTitleFont, NodeTitleFontBrush, titleRect.x + titleRect.width / 2.0f, titleRect.y + titleRect.height / 2.0f, EditorUtility.NameStringFormat);
            //画内容底框
            graphics.FillRectangle(NodeContentBrush, contentRect);

            //渲染内容
            NodeClass nodeClass = null;

            if (!NodeClassDic.TryGetValue(node, out nodeClass))
            {
                nodeClass = MainForm.Instance.NodeClasses.FindNode(node.ClassType);
                if (nodeClass != null)
                {
                    NodeClassDic.Add(node, nodeClass);
                }
            }

            if (nodeClass != null && nodeClass.ShowContent)
            {
                graphics.DrawString(node.ToString(), NodeContentFont, NodeContentFontBrush, contentRect.x + contentRect.width / 2.0f, contentRect.y + contentRect.height / 2.0f, EditorUtility.NameStringFormat);
            }

            //选中边框
            if (on && !DebugManager.Instance.Debugging)
            {
                graphics.DrawRectangle(EditorUtility.NodeSelectedPen, node.Rect);
            }

            //处理错误
            bool hasError   = false;
            int  errorCount = 0;

            if (node.StartNode && node.ParentNode != null)
            {
                hasError = true;
                errorCount++;
                graphics.DrawString("开始节点不能没有父节点", EditorUtility.NodeTitleFont, EditorUtility.NodeErrorBrush, contentRect.x, contentRect.yMax + (errorCount * 20));
            }

            if (!node.StartNode && node.ParentNode == null)
            {
                hasError = true;
                errorCount++;
                graphics.DrawString("没有父节点", EditorUtility.NodeTitleFont, EditorUtility.NodeErrorBrush, contentRect.x, contentRect.yMax + (errorCount * 20));
            }

            if ((node.NodeType == NodeType.Composite || node.NodeType == NodeType.Decorator) && node.Transitions.Count == 0)
            {
                hasError = true;
                errorCount++;
                graphics.DrawString("没有子节点", EditorUtility.NodeTitleFont, EditorUtility.NodeErrorBrush, contentRect.x, contentRect.yMax + (errorCount * 20));
            }

            if (node.NodeType == NodeType.Decorator && node.Transitions.Count > 1)
            {
                hasError = true;
                errorCount++;
                graphics.DrawString("装饰节点只能有一个子节点", EditorUtility.NodeTitleFont, EditorUtility.NodeErrorBrush, contentRect.x, contentRect.yMax + (errorCount * 20));
            }

            if (hasError)
            {
                graphics.DrawImage(Resources.conflict, new PointF(contentRect.center.x - Resources.conflict.Width / 2.0f, contentRect.center.y - Resources.conflict.Height / 2.0f));
            }
        }