Esempio n. 1
0
        internal void Initialise(XmlLayoutController controller)
        {
            this.controller = controller;

            DetectCalculatedProperties();

            ObserveExistingLists();
        }
        internal void Initialise(XmlLayoutController controller, XmlLayoutViewModel viewModel, string path)
        {
            this.controller = controller;
            this.viewModel  = viewModel;
            this.path       = path;

            DetectCalculatedProperties();

            ObserveExistingLists();

            InitializeViewModelChildren();
        }
Esempio n. 3
0
        void Awake()
        {
            m_awake = true;

            if (Application.isPlaying)
            {
                if (PreloadXmlLayoutCache)
                {
                    HandlePreload();
                }

                if (ForceRebuildOnAwake)
                {
                    if (XmlFile != null && ForceReloadXmlFileOnAwake)
                    {
                        ReloadXmlFile();
                    }
                    else
                    {
                        RebuildLayout(true);
                    }
                }
            }

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                if (XmlFile != null)
                {
                    if (Xml != XmlFile.text)
                    {
                        Debug.Log("[XmlLayout] : '" + XmlFile.name + "' has changed - reloading file and rebuilding layout.");

                        ReloadXmlFile();

                        // Calling MarkSceneDirty here doesn't seem to work, but delaying the call to the end of the frame does
                        XmlLayoutTimer.AtEndOfFrame(() => UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(this.gameObject.scene), this);
                    }
                }
            }
#endif

            if (Application.isPlaying && !ForceRebuildOnAwake)
            {
                SetupElementEventHandlers();
                if (XmlLayoutController != null)
                {
                    XmlLayoutTimer.DelayedCall(0.1f, () => XmlLayoutController.LayoutRebuilt(ParseXmlResult.Changed), this);
                }
            }

            IsReady = true;
        }
Esempio n. 4
0
        /// <summary>
        /// Clear the contents of this XmlLayout and rebuild it (using the Xml value)
        /// Call this after changing the Xml value for changes to take effect
        /// </summary>
        public void RebuildLayout(bool forceEvenIfXmlUnchanged = false, bool throwExceptionIfXmlIsInvalid = false)
        {
            if (!forceEvenIfXmlUnchanged && (!this.gameObject.activeInHierarchy || !m_awake))
            {
                return;
            }
            if (rebuildInProgress)
            {
                return;
            }

            rebuildInProgress = true;

            // Clear the child collection
            this.XmlElement.childElements.Clear();

            var parseResult = ParseXml(null, true, true, forceEvenIfXmlUnchanged, throwExceptionIfXmlIsInvalid);

            // Notify the XmlLayoutController that the Layout has been rebuilt
            if (XmlLayoutController != null)
            {
                XmlLayoutController.ViewModelUpdated(false);
                XmlLayoutController.NotifyXmlElementReferencesOfLayoutRebuild();
                XmlLayoutController.PreLayoutRebuilt();
                XmlLayoutController.LayoutRebuilt(parseResult);
                XmlLayoutController.PostLayoutRebuilt();
            }

#if UNITY_EDITOR
            if (!Application.isPlaying && parseResult != ParseXmlResult.Unchanged)
            {
                UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(this.gameObject.scene);
            }
#endif

            rebuildInProgress = false;
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="xmlToParse">The xml to parse. If this argument is null, then this method will use this XmlLayout's 'Xml' property instead.</param>
        /// <param name="clearContents">Should the contents of this XmlLayout be cleared before parsing the xml?</param>
        /// <param name="loadDefaultsFiles">Should the defaults files (if any) be loaded before parsing the xml?</param>
        /// <param name="forceEvenIfXmlUnchanged">Should the xml be parsed if it hasn't changed? (only applicable if xmlToParse is null)</param>
        /// <param name="throwExceptionIfXmlIsInvalid">Should an exception be thrown if the Xml contains errors?</param>
        ParseXmlResult ParseXml(string xmlToParse                 = null,
                                bool clearContents                = true,
                                bool loadDefaultsFiles            = true,
                                bool forceEvenIfXmlUnchanged      = false,
                                bool throwExceptionIfXmlIsInvalid = false)
        {
            if (xmlToParse == null)
            {
                if (!forceEvenIfXmlUnchanged)
                {
                    // If our Xml hasn't changed, and we aren't required to force update, then don't continue
                    // Note: this only happens if xmlToParse is null (which means we are parsing the primary Xml of this XmlLayout)
                    if (previousXml.Equals(Xml))
                    {
                        return(ParseXmlResult.Unchanged);
                    }
                }

                previousXml = Xml;

                xmlToParse = Xml;
            }

            if (XmlLayoutController != null)
            {
                xmlToParse = XmlLayoutController.ProcessViewModel(xmlToParse);
            }

            xmlToParse = HandleLocalization(xmlToParse);

            var xmlDoc = new XmlDocument();

            try
            {
                xmlDoc.LoadXml(xmlToParse);
            }
            catch (XmlException e)
            {
                var message = String.Format("[XmlLayout][{0}] Error parsing XML data: {1}", this.name, e.Message);
                Debug.LogError(message);

                if (throwExceptionIfXmlIsInvalid)
                {
                    throw e;
                }

                return(ParseXmlResult.Failed);
            }

            if (clearContents)
            {
                // For some reason, especially in edit mode, iterating through the child objects of this transform doesn't always iterate through all of the objects
                // repeating the action twice seems to be sufficient
                for (var x = 0; x < 2; x++)
                {
                    ClearContents();
                }
            }

            if (loadDefaultsFiles && DefaultsFiles != null)
            {
                defaultAttributeValues.Clear();

                DefaultsFiles.ForEach(f =>
                {
                    if (f != null)
                    {
                        ParseXml(f.text, false, false, true);
                    }
                });
            }

            var rectTransform = this.transform as RectTransform;

            ParseNode(xmlDoc.DocumentElement, rectTransform, rectTransform, true);

            return(ParseXmlResult.Changed);
        }