public void Load(string file_name) { string path = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, file_name)); XmlDocument xml = new XmlDocument();//初始化一个xml实例 xml.Load(path);//导入指定xml文件 this.root_node = dfs (xml.DocumentElement); }
private void ContextCallback(object obj) { //Debug.Log ("the obj is " + obj); Vector2 pos = (Vector2)obj; BaseNode n = new BaseNode (pos.x,pos.y); nodes.Add (n); //n.windowRect = GUILayout.Window(0, nodes[i].windowRect, DoWindow, "Hi There"); }
private void dfs(BaseNode p,Dictionary<BaseNode,int> depth) { if (p.parent == null) depth[p] = 1; else depth [p] = depth [p.parent] + 1; for (int i=0; i<p.neighbors.Count; i++) { dfs(p.neighbors[i],depth); } }
private void WriteNode(BaseNode tree_root,string file_name) { string s = tree_root.NodeToString (); using (System.IO.StreamWriter file = new System.IO.StreamWriter(System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, file_name)),true)) { file.Write(s); file.Close(); } }
private void SetSelected(BaseNode n) { n.is_selected = true; foreach (BaseNode c in n.neighbors) { SetSelected(c); } }
private void paint_dfs(BaseNode root) { if (root == null) return; root.is_selected = true; Repaint (); foreach (BaseNode n in root.neighbors) { paint_dfs(n); } }
void OnGUI() { Event e = Event.current; //判断是否在某个节点内部 int in_node_region_index = GetMousePositionInRegion (e.mousePosition); if (e.button == 1&&e.type==EventType.mouseUp) { GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("新加一个节点"),false,ContextCallback,e.mousePosition); menu.AddItem(new GUIContent("a/b/c"),false,null,null); if(in_node_region_index>=0) { this.transition_from_node = nodes[in_node_region_index]; menu.AddItem(new GUIContent("新加一条边"),false,AddTransitionCallback,this.transition_from_node); } else { menu.AddDisabledItem(new GUIContent("新加一条边")); } menu.AddItem(new GUIContent("保存所选节点"),false,SaveNodeCallback,null); menu.AddItem(new GUIContent("加载文件"),false,LoadNodeCallback,null); menu.ShowAsContext(); } if (e.button == 0 &&( e.type == EventType.mouseDown ||e.type == EventType.mouseUp)) { if(this.is_draw_curve) LinkTarget(e.mousePosition); this.is_draw_curve = false; if (in_node_region_index < 0 && e.button == 0 && e.type == EventType.mouseDown) { this.is_selected_state = true; this.pre_button_position = new Vector3 (e.mousePosition.x, e.mousePosition.y, 0); } else if (in_node_region_index < 0 && e.button == 0 && e.type == EventType.mouseUp) { this.is_selected_state = false; this.pre_button_position = Vector3.zero; } } Handles.BeginGUI(); if (this.is_selected_state) { verts[0] = this.pre_button_position; verts[1] = new Vector3(this.pre_button_position.x,e.mousePosition.y,0); verts[2] = new Vector3(e.mousePosition.x,e.mousePosition.y,0); verts[3] = new Vector3(e.mousePosition.x,this.pre_button_position.y,0); Handles.DrawSolidRectangleWithOutline(verts, new Color(1,1,1,0.2f),new Color(0,0,0,1)); CheckNodeIsSelected(verts[0],verts[2]); Repaint(); } for (int i=0; i<nodes.Count; i++) { nodes[i].DrawCurve(); } if (this.transition_from_node != null&&this.is_draw_curve) { Vector3 s = this.transition_from_node.GetCenterPosition(); Vector3 end = new Vector3(e.mousePosition.x,e.mousePosition.y,0); DrawBezierCurve(s,end); if(e.button==0&&e.type==EventType.mouseUp){ LinkTarget(e.mousePosition); } Repaint (); } Handles.EndGUI(); BeginWindows(); for (int i=0; i<nodes.Count; i++) { nodes[i].DrawNode(i,DoWindow); } EndWindows(); }
private void LinkTarget(Vector2 mousePosition) { int to_region_index = GetMousePositionInRegion(mousePosition); if(to_region_index>=0) { this.transition_from_node.AddTarget(nodes[to_region_index]); } this.transition_from_node = null; }
private BaseNode GetLCA(BaseNode a,BaseNode b,Dictionary<BaseNode,int> depth) { if (depth [a] < depth [b]) return GetLCA (b, a, depth); if (b.parent == null) return b; if (a == b) return b; return GetLCA (a.parent, b, depth); }
private Dictionary<BaseNode, int> GetDepth(BaseNode root) { Dictionary<BaseNode,int> depth = new Dictionary<BaseNode, int> (); dfs (root, depth); return depth; }
public void AddTarget(BaseNode target) { this.neighbors.Add (target); target.parent = this; }
private BaseNode dfs(XmlElement node) { if(node.Name=="SubRootNode") { XmlElement e = (XmlElement)node; string sub_file_name = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, "data/"+e.GetAttribute("file"))); RootNode sub_root = new RootNode(Convert.ToSingle(node.GetAttribute("pos_x"))+this.start_x,this.start_y+Convert.ToSingle(node.GetAttribute("pos_y"))); sub_root.Load(sub_file_name); this.nodes.AddRange(sub_root.nodes); return sub_root.root_node; } BaseNode current = new BaseNode(Convert.ToSingle(node.GetAttribute("pos_x"))+this.start_x,this.start_y+Convert.ToSingle(node.GetAttribute("pos_y"))); foreach (XmlNode n in node.ChildNodes) { if(n.NodeType==XmlNodeType.Element) { BaseNode c = dfs((XmlElement)n); if(c!=null) current.AddTarget(c); } } nodes.Add (current); return current; }