// Create a default ImageTarget that can be added to the default dataset. public static ConfigData.ImageTargetData CreateDefaultImageTarget() { ConfigData.ImageTargetData it = new ConfigData.ImageTargetData(); // Apply default values it.size = new Vector2(200.0f, 200.0f); it.virtualButtons = new List<ConfigData.VirtualButtonData>(); return it; }
// This method creates a single data set from the trackables provided. // The method ignores the data set property in TrackableBehaviour and // adds all Trackables to a single file. // Default Trackables are not added to the data set. private ConfigData CreateDataSetFromTrackables(TrackableBehaviour[] trackables) { // Sanity check. if (trackables == null) { return null; } ConfigData sceneData = new ConfigData(); foreach (TrackableBehaviour tb in trackables) { // Ignore non-data set trackables. if (!(tb is DataSetTrackableBehaviour)) { continue; } IEditorDataSetTrackableBehaviour trackable = (DataSetTrackableBehaviour)tb; string dataSetName = trackable.DataSetName; string trackableName = trackable.TrackableName; // We ignore default Trackables or undefined Trackables. if (dataSetName == QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME || dataSetName == "" || trackableName == QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME || trackableName == "") { Debug.LogWarning("Ignoring default Trackable for export"); continue; } if (trackable.GetType() == typeof(ImageTargetBehaviour)) { ImageTargetBehaviour it = (ImageTargetBehaviour)trackable; IEditorImageTargetBehaviour editorIt = it; ConfigData.ImageTargetData itConfig = new ConfigData.ImageTargetData(); itConfig.size = editorIt.GetSize(); // Process Virtual Button list. VirtualButtonBehaviour[] vbs = it.GetComponentsInChildren<VirtualButtonBehaviour>(); itConfig.virtualButtons = new List<ConfigData.VirtualButtonData>(vbs.Length); foreach (VirtualButtonBehaviour vb in vbs) { Vector2 leftTop; Vector2 rightBottom; if (!vb.CalculateButtonArea(out leftTop, out rightBottom)) { // Invalid Button continue; } ConfigData.VirtualButtonData vbConfig = new ConfigData.VirtualButtonData(); IEditorVirtualButtonBehaviour editorVB = vb; vbConfig.name = editorVB.VirtualButtonName; vbConfig.enabled = editorVB.enabled; vbConfig.rectangle = new Vector4(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y); vbConfig.sensitivity = editorVB.SensitivitySetting; itConfig.virtualButtons.Add(vbConfig); } sceneData.SetImageTarget(itConfig, editorIt.TrackableName); } else if (trackable.GetType() == typeof(MultiTargetBehaviour)) { Debug.Log("Multi Targets not exported."); } else if (trackable.GetType() == typeof(CylinderTargetBehaviour)) { Debug.Log("Cylinder Targets not exported."); } } return sceneData; }
public bool fileToStruct(string configXMLPath, ConfigData configData) { if (!File.Exists(configXMLPath)) { return(false); } List <ConfigData.CylinderTargetData> list = new List <ConfigData.CylinderTargetData>(); using (XmlTextReader reader = new XmlTextReader(configXMLPath)) { while (reader.Read()) { string str; Vector2 vector; string str2; Vector4 vector2; bool flag; string str6; string str7; string str8; string str9; Vector3 vector3; Quaternion quaternion; if (reader.NodeType == XmlNodeType.Element) { str = ""; switch (reader.Name) { case "ImageTarget": str = reader.GetAttribute("name"); if (str != null) { goto Label_00F1; } Debug.LogWarning("Found ImageTarget without name attribute in config.xml. Image Target will be ignored."); break; case "VirtualButton": str2 = reader.GetAttribute("name"); if (str2 != null) { goto Label_01AD; } Debug.LogWarning("Found VirtualButton without name attribute in config.xml. Virtual Button will be ignored."); break; case "MultiTarget": str6 = reader.GetAttribute("name"); if (str6 != null) { goto Label_0387; } Debug.LogWarning("Found Multi Target without name attribute in config.xml. Multi Target will be ignored."); break; case "CylinderTarget": str7 = reader.GetAttribute("name"); if (str7 != null) { goto Label_03D1; } Debug.LogWarning("Found Cylinder Target without name attribute in config.xml. Cylinder Target will be ignored."); break; case "Part": str9 = reader.GetAttribute("name"); if (str9 != null) { goto Label_0445; } Debug.LogWarning("Found Multi Target Part without name attribute in config.xml. Part will be ignored."); break; case "RigidBodyTarget": { string name = reader.GetAttribute("name"); ConfigData.RigidBodyTargetData data6 = new ConfigData.RigidBodyTargetData { name = name }; configData.SetRigidBodyTarget(data6, name); break; } } } continue; Label_00F1: vector = Vector2.zero; string[] valuesToParse = reader.GetAttribute("size").Split(new char[] { ' ' }); if (valuesToParse != null) { if (QCARUtilities.SizeFromStringArray(out vector, valuesToParse)) { goto Label_015C; } Debug.LogWarning("Found illegal itSize attribute for Image Target " + str + " in config.xml. Image Target will be ignored."); } else { Debug.LogWarning("Image Target " + str + " is missing a itSize attribut in config.xml. Image Target will be ignored."); } continue; Label_015C: reader.MoveToElement(); ConfigData.ImageTargetData item = new ConfigData.ImageTargetData { size = vector, virtualButtons = new List <ConfigData.VirtualButtonData>() }; configData.SetImageTarget(item, str); continue; Label_01AD: vector2 = Vector4.zero; string[] strArray2 = reader.GetAttribute("rectangle").Split(new char[] { ' ' }); if (strArray2 != null) { if (QCARUtilities.RectangleFromStringArray(out vector2, strArray2)) { goto Label_021B; } Debug.LogWarning("Found invalid rectangle attribute for Virtual Button " + str2 + " in config.xml. Virtual Button will be ignored."); } else { Debug.LogWarning("Virtual Button " + str2 + " has no rectangle attribute in config.xml. Virtual Button will be ignored."); } continue; Label_021B: flag = true; string attribute = reader.GetAttribute("enabled"); if (attribute != null) { if (string.Compare(attribute, "true", true) == 0) { flag = true; } else if (string.Compare(attribute, "false", true) == 0) { flag = false; } else { Debug.LogWarning("Found invalid enabled attribute for Virtual Button " + str2 + " in config.xml. Default setting will be used."); } } VirtualButton.Sensitivity lOW = VirtualButton.Sensitivity.LOW; string strA = reader.GetAttribute("sensitivity"); if (strA != null) { if (string.Compare(strA, "low", true) == 0) { lOW = VirtualButton.Sensitivity.LOW; } else if (string.Compare(strA, "medium", true) == 0) { lOW = VirtualButton.Sensitivity.MEDIUM; } else if (string.Compare(strA, "high", true) == 0) { lOW = VirtualButton.Sensitivity.HIGH; } else { Debug.LogWarning("Found illegal sensitivity attribute for Virtual Button " + str2 + " in config.xml. Default setting will be used."); } } reader.MoveToElement(); ConfigData.VirtualButtonData data2 = new ConfigData.VirtualButtonData(); string latestITName = GetLatestITName(configData); data2.name = str2; data2.rectangle = vector2; data2.enabled = flag; data2.sensitivity = lOW; if (configData.ImageTargetExists(latestITName)) { configData.AddVirtualButton(data2, latestITName); } else { Debug.LogWarning("Image Target with name " + latestITName + " could not be found. Virtual Button " + str2 + "will not be added."); } continue; Label_0387: reader.MoveToElement(); ConfigData.MultiTargetData data3 = new ConfigData.MultiTargetData { parts = new List <ConfigData.MultiTargetPartData>() }; configData.SetMultiTarget(data3, str6); continue; Label_03D1: str8 = reader.GetAttribute("sideLength"); float num = -1f; if (str8 != null) { num = float.Parse(str8, CultureInfo.InvariantCulture); } reader.MoveToElement(); ConfigData.CylinderTargetData data4 = new ConfigData.CylinderTargetData { name = str7, sideLength = num }; list.Add(data4); continue; Label_0445: vector3 = Vector3.zero; string[] strArray3 = reader.GetAttribute("translation").Split(new char[] { ' ' }); if (strArray3 != null) { if (QCARUtilities.TransformFromStringArray(out vector3, strArray3)) { goto Label_04B3; } Debug.LogWarning("Found illegal transform attribute for Part " + str9 + " in config.xml. Part will be ignored."); } else { Debug.LogWarning("Multi Target Part " + str9 + " has no translation attribute in config.xml. Part will be ignored."); } continue; Label_04B3: quaternion = Quaternion.identity; string[] strArray4 = reader.GetAttribute("rotation").Split(new char[] { ' ' }); if (strArray4 != null) { if (QCARUtilities.OrientationFromStringArray(out quaternion, strArray4)) { goto Label_0521; } Debug.LogWarning("Found illegal rotation attribute for Part " + str9 + " in config.xml. Part will be ignored."); } else { Debug.LogWarning("Multi Target Part " + str9 + " has no rotation attribute in config.xml. Part will be ignored."); } continue; Label_0521: reader.MoveToElement(); ConfigData.MultiTargetPartData data5 = new ConfigData.MultiTargetPartData(); string latestMTName = GetLatestMTName(configData); data5.name = str9; data5.rotation = quaternion; data5.translation = vector3; if (configData.MultiTargetExists(latestMTName)) { configData.AddMultiTargetPart(data5, latestMTName); } else { Debug.LogWarning("Multi Target with name " + latestMTName + " could not be found. Multi Target Part " + str9 + "will not be added."); continue; } } } if (list.Count > 0) { string datFile = configXMLPath.Substring(0, configXMLPath.Length - 3) + "dat"; ConfigData.CylinderTargetData[] targetData = list.ToArray(); CylinderDatasetReader.Read(datFile, targetData); foreach (ConfigData.CylinderTargetData data7 in targetData) { configData.SetCylinderTarget(data7, data7.name); } } return(true); }
// This method reads a config.xml file at the given path and fills the // ConfigData object with the data. public bool fileToStruct(string configXMLPath, ConfigData configData) { if (!File.Exists(configXMLPath)) return false; using (XmlTextReader configReader = new XmlTextReader(configXMLPath)) { while (configReader.Read()) { if (configReader.NodeType == XmlNodeType.Element) { // "Global" Attributes string itNameAttr = ""; switch (configReader.Name) { case "ImageTarget": // Parse name from config file itNameAttr = configReader.GetAttribute("name"); if (itNameAttr == null) { Debug.LogWarning("Found ImageTarget without " + "name attribute in " + "config.xml. Image Target " + "will be ignored."); continue; } // Parse itSize from config file Vector2 itSize = Vector2.zero; string[] itSizeAttr = configReader.GetAttribute("size").Split(' '); if (itSizeAttr != null) { if (!QCARUtilities.SizeFromStringArray( out itSize, itSizeAttr)) { Debug.LogWarning("Found illegal itSize " + "attribute for Image " + "Target " + itNameAttr + " in config.xml. " + "Image Target will be " + "ignored."); continue; } } else { Debug.LogWarning("Image Target " + itNameAttr + " is missing a itSize " + "attribut in config.xml. " + "Image Target will be " + "ignored."); continue; } configReader.MoveToElement(); ConfigData.ImageTargetData imageTarget = new ConfigData.ImageTargetData(); imageTarget.size = itSize; imageTarget.virtualButtons = new List<ConfigData.VirtualButtonData>(); configData.SetImageTarget(imageTarget, itNameAttr); break; case "VirtualButton": // Parse name from config file string vbNameAttr = configReader.GetAttribute("name"); if (vbNameAttr == null) { Debug.LogWarning("Found VirtualButton " + "without name attribute in " + "config.xml. Virtual Button " + "will be ignored."); continue; } // Parse rectangle from config file Vector4 vbRectangle = Vector4.zero; string[] vbRectangleAttr = configReader.GetAttribute("rectangle").Split(' '); if (vbRectangleAttr != null) { if (!QCARUtilities.RectangleFromStringArray( out vbRectangle, vbRectangleAttr)) { Debug.LogWarning("Found invalid " + "rectangle attribute " + "for Virtual Button " + vbNameAttr + " in config.xml. " + "Virtual Button will " + "be ignored."); continue; } } else { Debug.LogWarning("Virtual Button " + vbNameAttr + " has no rectangle " + "attribute in config.xml. " + "Virtual Button will be " + "ignored."); continue; } // Parse enabled boolean from config file bool vbEnabled = true; string enabledAttr = configReader.GetAttribute("enabled"); if (enabledAttr != null) { if (string.Compare(enabledAttr, "true", true) == 0) { vbEnabled = true; } else if (string.Compare(enabledAttr, "false", true) == 0) { vbEnabled = false; } else { Debug.LogWarning("Found invalid enabled " + "attribute for Virtual " + "Button " + vbNameAttr + " in config.xml. " + "Default setting will " + "be used."); } } // Parse sensitivity from config file VirtualButton.Sensitivity vbSensitivity = VirtualButton.DEFAULT_SENSITIVITY; string vbSensitivityAttr = configReader.GetAttribute("sensitivity"); if (vbSensitivityAttr != null) { if (string.Compare(vbSensitivityAttr, "low", true) == 0) { vbSensitivity = VirtualButton.Sensitivity.LOW; } else if (string.Compare(vbSensitivityAttr, "medium", true) == 0) { vbSensitivity = VirtualButton.Sensitivity.MEDIUM; } else if (string.Compare(vbSensitivityAttr, "high", true) == 0) { vbSensitivity = VirtualButton.Sensitivity.HIGH; } else { Debug.LogWarning("Found illegal " + "sensitivity attribute " + "for Virtual Button " + vbNameAttr + " in config.xml. " + "Default setting will " + "be used."); } } configReader.MoveToElement(); ConfigData.VirtualButtonData virtualButton = new ConfigData.VirtualButtonData(); string latestITName = GetLatestITName(configData); virtualButton.name = vbNameAttr; virtualButton.rectangle = vbRectangle; virtualButton.enabled = vbEnabled; virtualButton.sensitivity = vbSensitivity; // Since the XML Reader runs top down we can assume // that the Virtual Button that has been found is // part of the latest Image Target. if (configData.ImageTargetExists(latestITName)) { configData.AddVirtualButton(virtualButton, latestITName); } else { Debug.LogWarning("Image Target with name " + latestITName + " could not be found. " + "Virtual Button " + vbNameAttr + "will not be added."); } break; case "MultiTarget": // Parse name from config file string mtNameAttr = configReader.GetAttribute("name"); if (mtNameAttr == null) { Debug.LogWarning("Found Multi Target without " + "name attribute in " + "config.xml. Multi Target " + "will be ignored."); continue; } configReader.MoveToElement(); ConfigData.MultiTargetData multiTarget = new ConfigData.MultiTargetData(); multiTarget.parts = new List<ConfigData.MultiTargetPartData>(); configData.SetMultiTarget(multiTarget, mtNameAttr); break; case "Part": // Parse name from config file string prtNameAttr = configReader.GetAttribute("name"); if (prtNameAttr == null) { Debug.LogWarning("Found Multi Target Part " + "without name attribute in " + "config.xml. Part will be " + "ignored."); continue; } // Parse translations from config file Vector3 prtTranslation = Vector3.zero; string[] prtTranslationAttr = configReader.GetAttribute("translation").Split(' '); if (prtTranslationAttr != null) { if (!QCARUtilities.TransformFromStringArray( out prtTranslation, prtTranslationAttr)) { Debug.LogWarning("Found illegal " + "transform attribute " + "for Part " + prtNameAttr + " in config.xml. Part " + "will be ignored."); continue; } } else { Debug.LogWarning("Multi Target Part " + prtNameAttr + " has no " + "translation attribute in " + "config.xml. Part will be " + "ignored."); continue; } // Parse rotations from config file Quaternion prtRotation = Quaternion.identity; string[] prtRotationAttr = configReader.GetAttribute("rotation").Split(' '); if (prtRotationAttr != null) { if (!QCARUtilities.OrientationFromStringArray( out prtRotation, prtRotationAttr)) { Debug.LogWarning("Found illegal rotation " + "attribute for Part " + prtNameAttr + " in config.xml. Part " + "will be ignored."); continue; } } else { Debug.LogWarning("Multi Target Part " + prtNameAttr + " has no " + "rotation attribute in " + "config.xml. Part will be " + "ignored."); continue; } configReader.MoveToElement(); ConfigData.MultiTargetPartData multiTargetPart = new ConfigData.MultiTargetPartData(); string latestMTName = GetLatestMTName(configData); multiTargetPart.name = prtNameAttr; multiTargetPart.rotation = prtRotation; multiTargetPart.translation = prtTranslation; // Since the XML Reader runs top down we can assume // that the Virtual Button that has been found is // part of the latest Image Target. if (configData.MultiTargetExists(latestMTName)) { configData.AddMultiTargetPart(multiTargetPart, latestMTName); } else { Debug.LogWarning("Multi Target with name " + latestMTName + " could not be found. " + "Multi Target Part " + prtNameAttr + "will not be added."); } break; default: break; } } } } return true; }
// This method creates a single data set from the trackables provided. // The method ignores the data set property in TrackableBehaviour and // adds all Trackables to a single file. // Default Trackables are not added to the data set. private ConfigData CreateDataSetFromTrackables(TrackableBehaviour[] trackables) { // Sanity check. if (trackables == null) { return(null); } ConfigData sceneData = new ConfigData(); foreach (TrackableBehaviour tb in trackables) { // Ignore non-data set trackables. if (!(tb is DataSetTrackableBehaviour)) { continue; } IEditorDataSetTrackableBehaviour trackable = (DataSetTrackableBehaviour)tb; string dataSetName = trackable.DataSetName; string trackableName = trackable.TrackableName; // We ignore default Trackables or undefined Trackables. if (dataSetName == QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME || dataSetName == "" || trackableName == QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME || trackableName == "") { Debug.LogWarning("Ignoring default Trackable for export"); continue; } if (trackable.GetType() == typeof(ImageTargetBehaviour)) { ImageTargetBehaviour it = (ImageTargetBehaviour)trackable; IEditorImageTargetBehaviour editorIt = it; ConfigData.ImageTargetData itConfig = new ConfigData.ImageTargetData(); itConfig.size = editorIt.GetSize(); // Process Virtual Button list. VirtualButtonBehaviour[] vbs = it.GetComponentsInChildren <VirtualButtonBehaviour>(); itConfig.virtualButtons = new List <ConfigData.VirtualButtonData>(vbs.Length); foreach (VirtualButtonBehaviour vb in vbs) { Vector2 leftTop; Vector2 rightBottom; if (!vb.CalculateButtonArea(out leftTop, out rightBottom)) { // Invalid Button continue; } ConfigData.VirtualButtonData vbConfig = new ConfigData.VirtualButtonData(); IEditorVirtualButtonBehaviour editorVB = vb; vbConfig.name = editorVB.VirtualButtonName; vbConfig.enabled = editorVB.enabled; vbConfig.rectangle = new Vector4(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y); vbConfig.sensitivity = editorVB.SensitivitySetting; itConfig.virtualButtons.Add(vbConfig); } sceneData.SetImageTarget(itConfig, editorIt.TrackableName); } else if (trackable.GetType() == typeof(MultiTargetBehaviour)) { Debug.Log("Multi Targets not exported."); } } return(sceneData); }