//a Custom titlebar for tasks static bool ShowTaskTitlebar(Task task, Action<Task> callback){ GUI.backgroundColor = new Color(1,1,1,0.8f); GUILayout.BeginHorizontal("box"); GUI.backgroundColor = Color.white; if (GUILayout.Button("X", GUILayout.Width(20))){ if (callback != null) callback(null); return false; } GUILayout.Label("<b>" + (task.isUnfolded? "▼ " :"► ") + task.name + "</b>" + (task.isUnfolded? "" : "\n<i><size=10>(" + task.summaryInfo + ")</size></i>") ); if (GUILayout.Button(EditorUtils.csIcon, (GUIStyle)"label", GUILayout.Width(20), GUILayout.Height(20))){ EditorUtils.OpenScriptOfType(task.GetType()); } GUILayout.EndHorizontal(); var titleRect = GUILayoutUtility.GetLastRect(); EditorGUIUtility.AddCursorRect(titleRect, MouseCursor.Link); var e = Event.current; if (e.type == EventType.ContextClick && titleRect.Contains(e.mousePosition)){ var menu = new GenericMenu(); menu.AddItem(new GUIContent("Open Script"), false, ()=> { EditorUtils.OpenScriptOfType(task.GetType()); } ); menu.AddItem(new GUIContent("Copy"), false, ()=> {Task.copiedTask = task;} ); menu.AddItem(new GUIContent("Delete"), false, ()=> { if (callback != null) callback(null); }); if (task is ActionList){ menu.AddItem(new GUIContent("Save Preset"), false, ()=> { (task as ActionList).DoSavePreset(); } ); menu.AddItem(new GUIContent("Load Preset"), false, ()=> { (task as ActionList).DoLoadPreset(); } ); } if (task is ConditionList){ menu.AddItem(new GUIContent("Save Preset"), false, ()=> { (task as ConditionList).DoSavePreset(); } ); menu.AddItem(new GUIContent("Load Preset"), false, ()=> { (task as ConditionList).DoLoadPreset(); } ); } menu.ShowAsContext(); e.Use(); } if (e.button == 0 && e.type == EventType.MouseDown && titleRect.Contains(e.mousePosition)) e.Use(); if (e.button == 0 && e.type == EventType.MouseUp && titleRect.Contains(e.mousePosition)){ task.isUnfolded = !task.isUnfolded; e.Use(); } return task.isUnfolded; }
//a Custom titlebar for tasks static bool ShowTaskTitlebar(Task task, Action <Task> callback) { GUI.backgroundColor = new Color(1, 1, 1, 0.8f); GUILayout.BeginHorizontal("box"); GUI.backgroundColor = Color.white; if (GUILayout.Button("X", GUILayout.Width(20))) { if (callback != null) { callback(null); } return(false); } GUILayout.Label("<b>" + (task.isUnfolded? "▼ " :"► ") + task.name + "</b>" + (task.isUnfolded? "" : "\n<i><size=10>(" + task.summaryInfo + ")</size></i>")); if (GUILayout.Button(EditorUtils.csIcon, (GUIStyle)"label", GUILayout.Width(20), GUILayout.Height(20))) { EditorUtils.OpenScriptOfType(task.GetType()); } GUILayout.EndHorizontal(); var titleRect = GUILayoutUtility.GetLastRect(); EditorGUIUtility.AddCursorRect(titleRect, MouseCursor.Link); var e = Event.current; if (e.type == EventType.ContextClick && titleRect.Contains(e.mousePosition)) { var menu = new GenericMenu(); menu.AddItem(new GUIContent("Open Script"), false, () => { EditorUtils.OpenScriptOfType(task.GetType()); }); menu.AddItem(new GUIContent("Copy"), false, () => { Task.copiedTask = task; }); menu.AddItem(new GUIContent("Delete"), false, () => { if (callback != null) { callback(null); } }); if (task is ActionList) { menu.AddItem(new GUIContent("Save Preset"), false, () => { (task as ActionList).DoSavePreset(); }); menu.AddItem(new GUIContent("Load Preset"), false, () => { (task as ActionList).DoLoadPreset(); }); } if (task is ConditionList) { menu.AddItem(new GUIContent("Save Preset"), false, () => { (task as ConditionList).DoSavePreset(); }); menu.AddItem(new GUIContent("Load Preset"), false, () => { (task as ConditionList).DoLoadPreset(); }); } menu.ShowAsContext(); e.Use(); } if (e.button == 0 && e.type == EventType.MouseDown && titleRect.Contains(e.mousePosition)) { e.Use(); } if (e.button == 0 && e.type == EventType.MouseUp && titleRect.Contains(e.mousePosition)) { task.isUnfolded = !task.isUnfolded; e.Use(); } return(task.isUnfolded); }
protected bool NodeToXml(Node node, XmlElement xmlParentElement) { bool bSuccess = true; var enumConnection = node.outConnections.GetEnumerator(); XmlElement xmlNode = null; if (typeof(Sequencer).IsInstanceOfType(node)) { xmlNode = xmlParentElement.OwnerDocument.CreateElement("sequencer"); xmlNode.SetAttribute("name", "sequencer" + node.ID); } else if (typeof(Selector).IsInstanceOfType(node)) { xmlNode = xmlParentElement.OwnerDocument.CreateElement("selector"); xmlNode.SetAttribute("name", "selector" + node.ID); } else if (typeof(Inverter).IsInstanceOfType(node)) { xmlNode = xmlParentElement.OwnerDocument.CreateElement("inverter"); xmlNode.SetAttribute("name", "inverter" + node.ID); } else if (typeof(ITaskAssignable).IsInstanceOfType(node)) { xmlNode = xmlParentElement.OwnerDocument.CreateElement("action"); Task task = (node as ITaskAssignable).task; if (task != null) { string typeName = task.GetType().FriendlyName(); xmlNode.SetAttribute("classname", typeName); xmlNode.SetAttribute("name", typeName); FieldInfo[] fields = task.GetType().GetFields(); foreach (FieldInfo field in fields) { object fieldVar = field.GetValue(task); if (fieldVar.GetType().IsSubclassOf(typeof(BBParameter))) { BBParameter param = fieldVar as BBParameter; string varName = field.Name; if (field.IsDefined(typeof(ExportNameAttribute), false)) { object[] attrs = field.GetCustomAttributes(typeof(ExportNameAttribute), false); if (attrs != null) { ExportNameAttribute attr = attrs.FirstOrDefault() as ExportNameAttribute; if (attr != null) { varName = attr.exportName; } } } XmlElement xmlParam = xmlParentElement.OwnerDocument.CreateElement("param"); xmlParam.SetAttribute("name", varName); xmlParam.SetAttribute("type", Variable.GetTypeName(param.varType)); if (param.useBlackboard) { xmlParam.SetAttribute("ref", param.name); } else { string valueStr = null; if (param.varType.IsEnum) { valueStr = System.Convert.ToString((int)param.value); } else { valueStr = System.Convert.ToString(param.value).ToLower(); } xmlParam.SetAttribute("value", valueStr); } xmlNode.AppendChild(xmlParam); } } } else { bSuccess = false; Debug.LogError("ActionNode without Action, Condition node without Condition! " + node.name); } } else if (typeof(SubTree).IsInstanceOfType(node)) { //todo 递归插入subtree node SubTree st = node as SubTree; st.Init(); if (st.subTree) { NodeToXml(st.subTree.primeNode, xmlParentElement); } else { Debug.LogError("can not find the subtree : id = " + node.ID); } } else if (typeof(ConditionNode).IsInstanceOfType(node)) { //xmlNode = xmlParentElement.OwnerDocument.CreateElement("condition"); bSuccess = false; Debug.LogError("Unsupported type used: " + node.name); } else { bSuccess = false; Debug.LogError("Unsupported type used: " + node.name); } if (xmlNode != null) { xmlNode.SetAttribute("id", System.Convert.ToString(node.ID)); while (enumConnection.MoveNext() && bSuccess) { bSuccess = NodeToXml(enumConnection.Current.targetNode, xmlNode); } if (bSuccess) { xmlParentElement.AppendChild(xmlNode); } } return(bSuccess); }