Esempio n. 1
0
    // This method takes a configData object and creates a config.xml file at
    // the given path out of it.
    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.ImageTarget imageTarget =
                                new ConfigData.ImageTarget();

                            imageTarget.size = itSize;
                            imageTarget.virtualButtons =
                                new List<ConfigData.VirtualButton>();

                            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
                            VirtualButtonBehaviour.Sensitivity vbSensitivity =
                                VirtualButtonBehaviour.DEFAULT_SENSITIVITY;
                            string vbSensitivityAttr =
                                configReader.GetAttribute("sensitivity");
                            if (vbSensitivityAttr != null)
                            {
                                if (string.Compare(vbSensitivityAttr,
                                    "low", true) == 0)
                                {
                                    vbSensitivity =
                                    VirtualButtonBehaviour.Sensitivity.LOW;
                                }
                                else if (string.Compare(vbSensitivityAttr,
                                    "medium", true) == 0)
                                {
                                    vbSensitivity =
                                    VirtualButtonBehaviour.Sensitivity.MEDIUM;
                                }
                                else if (string.Compare(vbSensitivityAttr,
                                    "high", true) == 0)
                                {
                                    vbSensitivity =
                                    VirtualButtonBehaviour.Sensitivity.HIGH;
                                }
                                else
                                {
                                    Debug.LogWarning("Found illegal " +
                                                     "sensitivity attribute " +
                                                     "for Virtual Button " +
                                                     vbNameAttr +
                                                     " in config.xml. " +
                                                     "Default setting will " +
                                                     "be used.");
                                }
                            }

                            configReader.MoveToElement();

                            ConfigData.VirtualButton virtualButton =
                                new ConfigData.VirtualButton();

                            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.AddVirtualButton(virtualButton,
                                                             latestITName))
                            {
                                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.MultiTarget multiTarget =
                                new ConfigData.MultiTarget();

                            multiTarget.parts =
                                new List<ConfigData.MultiTargetPart>();

                            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.MultiTargetPart multiTargetPart =
                                new ConfigData.MultiTargetPart();

                            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.AddMultiTargetPart(multiTargetPart,
                                                               latestMTName))
                            {
                                Debug.LogWarning("Multi Target with name " +
                                                 latestMTName +
                                                 " could not be found. " +
                                                 "Multi Target Part " +
                                                 prtNameAttr +
                                                 "will not be added.");
                            }
                            break;

                        case "Marker":

                            // Parse name from config file
                            string fmNameAttr =
                                configReader.GetAttribute("name");
                            if (fmNameAttr == null)
                            {
                                Debug.LogWarning("Found Marker without name " +
                                                 "attribute in config.xml. " +
                                                 "Marker will be ignored.");
                                continue;
                            }

                            // Parse size from config file
                            Vector2 fmSize = Vector2.zero;
                            string[] fmSizeAttr =
                                configReader.GetAttribute("size").Split(' ');
                            if (fmSizeAttr != null)
                            {
                                if (!QCARUtilities.SizeFromStringArray(
                                    out fmSize, fmSizeAttr))
                                {
                                    Debug.LogWarning("Found illegal size " +
                                                     "attribute for Marker " +
                                                     fmNameAttr +
                                                     " in config.xml. Marker " +
                                                     "will be ignored.");
                                    continue;
                                }
                            }
                            else
                            {
                                Debug.LogWarning("Marker " + fmNameAttr +
                                                 " has no size attribute in " +
                                                 "config.xml. Marker will be " +
                                                 "ignored.");
                                continue;
                            }

                            // Parse id from config file
                            string fmIDAttr = configReader.GetAttribute("id");
                            if (fmIDAttr == null)
                            {
                                Debug.LogWarning("Marker " + fmNameAttr +
                                                 " has no id attribute in " +
                                                 "config.xml. Marker will be " +
                                                 "ignored.");
                                continue;
                            }
                            configReader.MoveToElement();

                            int fmID = int.Parse(fmIDAttr);
                            if (fmID < 0 ||
                                fmID >= QCARUtilities.GlobalVars.MAX_NUM_FRAME_MARKERS)
                            {
                                Debug.LogWarning("Found Marker " + fmNameAttr +
                                                 " with invalid id in " +
                                                 "config.xml. Marker will be " +
                                                 "ignored.");
                                continue;
                            }

                            ConfigData.FrameMarker fm =
                                new ConfigData.FrameMarker();

                            fm.name = fmNameAttr;
                            fm.size = fmSize;

                            configData.SetFrameMarker(fm, fmID);

                            break;

                        default:
                            break;
                    }
                }
            }
        }

        return true;
    }