/// <summary> /// 移除一个子节点 /// </summary> /// <param name="child">子节点</param> /// <returns></returns> public void RemovNextPointID(IDialoguePointID child) { if (childIDList.Contains(child)) { childIDList.RemoveAll(temp => temp.Equals(child)); } }
/// <summary> /// 删除节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 除节点ToolStripMenuItem_Click(object sender, EventArgs e) { if (iSelectedControlNow != null) { IDialoguePointID iDialoguePointID = iSelectedControlNow as IDialoguePointID; Control parentControl = iSelectedControlNow.GetParent(); if (iDialoguePointID.GetType().Equals(typeof(DialogueConditionControl))) { FlowLayoutPanel_Main.Controls.Remove(parentControl); iSelectedControlNow = null; } else { DialogueConditionControl dialogueConditionControl = parentControl.Controls.OfType <DialogueConditionControl>().FirstOrDefault(); if (dialogueConditionControl != null) { IDialoguePointID parentIDialoguePointID = GetParentDialoguePointID(iDialoguePointID, dialogueConditionControl); if (parentIDialoguePointID != null) { parentIDialoguePointID.RemovNextPointID(iDialoguePointID); RemoveDialoguePointIDControl(parentControl, iDialoguePointID); List <Control> tagControlList = (parentIDialoguePointID as Control).Tag as List <Control>; if (tagControlList != null) { tagControlList.Remove(iDialoguePointID as Control); } //移除所有子节点 iSelectedControlNow = null; ResetControlRect(parentControl); } } } } }
/// <summary> /// 添加一个子节点 /// </summary> /// <param name="child">子节点</param> /// <returns></returns> public void AddNextPointID(IDialoguePointID child) { if (!childIDList.Contains(child)) { childIDList.Add(child); } }
/// <summary> /// 移除指定节点的控件 /// </summary> /// <param name="control">父控件</param> /// <param name="iDialoguePointID">要移除的节点</param> private void RemoveDialoguePointIDControl(Control control, IDialoguePointID iDialoguePointID) { IDialoguePointID[] childs = iDialoguePointID.GetDialogueNextPointID; foreach (IDialoguePointID child in childs) { RemoveDialoguePointIDControl(control, child); } control.Controls.Remove(iDialoguePointID as Control); }
/// <summary> /// 根据现有数据创建控件 /// </summary> /// <param name="parent">父控件</param> /// <param name="dialoguePoints">节点关系对象</param> private void CreateControlByData(IDialoguePointID parent, params DialoguePoint[] dialoguePoints) { foreach (DialoguePoint dialoguePoint in dialoguePoints) { DialogueValue dialogueValue = dialogueAnalysisData.GetDialoguePointByID(dialoguePoint.dialogueID); if (dialogueValue == null) { continue; } DialoguePointControl dialoguePointControl = AddNewChildNode(parent as ISelectedControl, dialogueValue, false);//添加一个子节点 if (dialoguePoint.childDialoguePoints != null && dialoguePoint.childDialoguePoints.Length > 0) { CreateControlByData(dialoguePointControl, dialoguePoint.childDialoguePoints); } } }
/// <summary> /// 查找指定子节点的父节点 /// </summary> /// <param name="target"></param> /// <param name="parents"></param> /// <returns></returns> private IDialoguePointID GetParentDialoguePointID(IDialoguePointID target, params IDialoguePointID[] parents) { foreach (IDialoguePointID item in parents) { if (item.GetDialogueNextPointID.Contains(target)) { return(item); } IDialoguePointID result = GetParentDialoguePointID(target, item.GetDialogueNextPointID); if (result != null) { return(result); } } return(null); }
/// <summary> /// 添加一个子节点 /// </summary> /// <param name="iSelectedControlNow">要添加子节点的节点</param> private DialoguePointControl AddNewChildNode(ISelectedControl iSelectedControlNow, DialogueValue dialogueValue = null, bool updateControl = true) { IDialoguePointID iDialoguePointID = iSelectedControlNow as IDialoguePointID; if (iDialoguePointID.GetType().Equals(typeof(DialogueConditionControl)))//顶层节点(关系关系)只能添加一个子节点(数据节点) { if (iDialoguePointID.GetDialogueNextPointID.Length > 0) { return(null); } } if (iDialoguePointID != null) { DialoguePointControl dialoguePointControl = new DialoguePointControl(dialogueValue); dialoguePointControl.Size = dialoguePointControl.StopSize; dialoguePointControl.SetListenControlSelected(ListenControlSelected); dialoguePointControl.SetLiestenOpenStop(AddListenOpenStop); dialoguePointControl.Tag = new List <Control>(); Control iSelectedControl = iSelectedControlNow as Control; List <Control> childControl = iSelectedControl.Tag as List <Control>; childControl.Add(dialoguePointControl); Control parentControl = iSelectedControlNow.GetParent(); parentControl.Controls.Add(dialoguePointControl); iDialoguePointID.AddNextPointID(dialoguePointControl); if (updateControl) { ResetControlRect(parentControl); } if (searchNodeForm != null) { searchNodeForm.NodeChanged = true; } return(dialoguePointControl); } return(null); }
/// <summary> /// 重新设置控件尺寸 /// </summary> /// <param name="parent"></param> private void ResetControlRect(Control parent) { if (parent == null) { return; } try { DialogueConditionControl topControl = parent.Controls.OfType <DialogueConditionControl>().FirstOrDefault(); #region 构建一个深度结构并计算 //初步构建 List <IDialoguePointID> nextChilds = new List <IDialoguePointID>(); List <List <DeepControlStruct> > deepControlStructLists = new List <List <DeepControlStruct> >(); int tempDeep = 0; nextChilds.Add(topControl); while (nextChilds.Count > 0) { IDialoguePointID[] tempChilds = nextChilds.ToArray(); nextChilds.Clear(); if (deepControlStructLists.Count == tempDeep) { List <DeepControlStruct> deepControlStructList = new List <DeepControlStruct>(); deepControlStructLists.Add(deepControlStructList); deepControlStructList.AddRange(tempChilds.Select(temp => new DeepControlStruct() { deep = tempDeep, iOpenStop = temp as IOpenStop, childs = new List <DeepControlStruct>() })); } foreach (IDialoguePointID tempChild in tempChilds) { IOpenStop iOpenStop = tempChild as IOpenStop; if (iOpenStop != null && iOpenStop.OpenStopState) //如果该节点是展开节点并且该节点时展开的 { nextChilds.AddRange(tempChild.GetDialogueNextPointID); //用于下次计算 } } tempDeep++; } //计算父子关系 for (int i = 0; i < deepControlStructLists.Count - 1; i++) { foreach (DeepControlStruct deepControlStruct in deepControlStructLists[i]) { IDialoguePointID iDialoguePointID = deepControlStruct.iOpenStop as IDialoguePointID; IDialoguePointID[] childIDialoguePointIDs = iDialoguePointID.GetDialogueNextPointID; DeepControlStruct[] childs = deepControlStructLists[i + 1].Where(temp => childIDialoguePointIDs.Contains(temp.iOpenStop as IDialoguePointID)).ToArray(); foreach (DeepControlStruct child in childs) { child.parent = deepControlStruct; deepControlStruct.childs.Add(child); } } } //初始化对象的BoundY foreach (DeepControlStruct deepControlStruct in deepControlStructLists[0]) { deepControlStruct.InitBoundY(); } //计算每个节点的x轴坐标 int tempLocationX = 0; for (int i = 0; i < deepControlStructLists.Count; i++) { int maxLocationX = 0; foreach (DeepControlStruct deepControlStruct in deepControlStructLists[i]) { deepControlStruct.location_x = tempLocationX; int thisWidth = deepControlStruct.iOpenStop.OpenStopState ? deepControlStruct.iOpenStop.OpenSize.Width : deepControlStruct.iOpenStop.StopSize.Width; maxLocationX = maxLocationX > thisWidth ? maxLocationX : thisWidth; } tempLocationX += maxLocationX + 50; } //计算每个节点的y轴坐标 foreach (List <DeepControlStruct> deepControlStructList in deepControlStructLists) { int tempY = 0; DeepControlStruct parentDeep = null; foreach (DeepControlStruct deepControlStruct in deepControlStructList) { if (deepControlStruct.parent == null) { deepControlStruct.location_y = tempY; tempY += deepControlStruct.boundY; } else { if (parentDeep != deepControlStruct.parent) { tempY = 0; parentDeep = deepControlStruct.parent; } deepControlStruct.location_y = parentDeep.location_y + tempY; tempY += deepControlStruct.boundY; } } } //设置父控件大小 List <int> deepWidthList = new List <int>(); deepWidthList = deepControlStructLists.Select(temp => temp.Select(deepControl => deepControl.iOpenStop). Select(iOpenStop => iOpenStop.OpenStopState ? iOpenStop.OpenSize.Width : iOpenStop.StopSize.Width). Max() + 50 ).ToList(); int panelWidth = deepWidthList.Sum(); int panelHeight = deepControlStructLists[0].Select(temp => temp.boundY).Sum(); parent.Size = new Size(panelWidth, panelHeight); //设置每个节点的大小以及隐藏节点的位置与大小 Action <IDialoguePointID> HideChild = null;//隐藏子节点 HideChild = (target) => { IDialoguePointID[] childs = target.GetDialogueNextPointID; foreach (IDialoguePointID child in childs) { (child as Control).Location = new Point(-1000, -1000); HideChild(child); } }; foreach (List <DeepControlStruct> deepControlStructList in deepControlStructLists) { foreach (DeepControlStruct deepControlStruct in deepControlStructList) { Control thisControl = deepControlStruct.iOpenStop as Control; thisControl.Location = new Point(deepControlStruct.location_x, deepControlStruct.location_y); thisControl.Size = deepControlStruct.iOpenStop.OpenStopState ? deepControlStruct.iOpenStop.OpenSize : deepControlStruct.iOpenStop.StopSize; if (!deepControlStruct.iOpenStop.OpenStopState) { HideChild(deepControlStruct.iOpenStop as IDialoguePointID); } } } //重置背景控件大小 ResetBackRect(); #endregion } catch { } }