Esempio n. 1
0
        /// <summary>
        /// Constructs a UIAnimation object.
        /// </summary>
        public UIAnimation(XElement animationFileRoot)
        {
            this.phaseEndpoints   = new List <int>();
            this.phaseSprites     = new List <UISprite>();
            this.isLooped         = false;
            this.isPlaying        = false;
            this.currentTimepoint = 0;

            int currentTimepoint = 0;

            foreach (XElement animPhaseElem in animationFileRoot.Elements(ANIMATION_PHASE_ELEM))
            {
                XAttribute durationAttr = animPhaseElem.Attribute(ANIMATION_PHASE_DURATION_ATTR);
                XAttribute spriteAttr   = animPhaseElem.Attribute(ANIMATION_PHASE_SPRITE_ATTR);
                if (durationAttr != null && spriteAttr != null)
                {
                    int duration = XmlHelper.LoadInt(durationAttr.Value);
                    if (duration > 0)
                    {
                        /// TODO: check whether the sprites of the animation has the same size.
                        UISprite phaseSprite = UIResourceManager.GetResource <UISprite>(spriteAttr.Value);
                        currentTimepoint += duration;
                        this.phaseEndpoints.Add(currentTimepoint);
                        this.phaseSprites.Add(phaseSprite);
                    }
                    else
                    {
                        throw new ConfigurationException("Duration of animation phase must be positive!");
                    }
                }
                else
                {
                    throw new ConfigurationException("No duration and sprite has been defined for animation phase!");
                }
            }
        }
        /// <summary>
        /// Loads a resource definition from the given XML-element.
        /// </summary>
        /// <param name="resGroupName">The name of the group of the resource.</param>
        /// <param name="resourceElem">The XML-element to load from.</param>
        private void LoadResourceDef(string resGroupName, XElement resourceElem)
        {
            XAttribute namespaceAttr = resourceElem.Attribute(RESOURCE_NAMESPACE_ATTR);
            XAttribute nameAttr      = resourceElem.Attribute(RESOURCE_NAME_ATTR);
            XAttribute loaderAttr    = resourceElem.Attribute(RESOURCE_LOADER_ATTR);

            if (namespaceAttr != null && nameAttr != null && loaderAttr != null)
            {
                if (!this.loaderAssemblies.ContainsKey(loaderAttr.Value))
                {
                    throw new ConfigurationException(string.Format("Loader with name '{0}' doesn't exist"));
                }

                string resourceName = string.Format("{0}.{1}", namespaceAttr.Value, nameAttr.Value);

                /// Load the defined paths from the XML
                Dictionary <string, FileInfo> paths = new Dictionary <string, FileInfo>();
                foreach (XElement pathElem in resourceElem.Elements(PATH_ELEM))
                {
                    this.LoadPathElement(pathElem, ref paths);
                }

                /// Load the defined parameters from the XML
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                foreach (XElement paramElem in resourceElem.Elements(PARAMETER_ELEM))
                {
                    this.LoadParameterElement(paramElem, ref parameters);
                }

                /// Load the assembly that contains the resource loader class and create an instance of that class.
                Assembly asm = Assembly.Load(this.loaderAssemblies[loaderAttr.Value]);
                if (asm != null)
                {
                    Type resLoaderType = asm.GetType(this.loaderClasses[loaderAttr.Value]);
                    if (resLoaderType != null)
                    {
                        UIResourceLoader resLoader = Activator.CreateInstance(resLoaderType, new object[2] {
                            paths, parameters
                        }) as UIResourceLoader;
                        if (resLoader != null)
                        {
                            /// Register the created trace object to the trace manager.
                            UIResourceManager.RegisterResource(resGroupName, resourceName, resLoader);
                        }
                        else
                        {
                            throw new ConfigurationException(string.Format("Type {0} doesn't implement abstract class RC.UI.UIResourceLoader!", this.loaderClasses[loaderAttr.Value]));
                        }
                    }
                    else
                    {
                        throw new ConfigurationException(string.Format("Unable to load type {0} from assembly {1}!", this.loaderClasses[loaderAttr.Value], this.loaderAssemblies[loaderAttr.Value]));
                    }
                }
                else
                {
                    throw new ConfigurationException(string.Format("Unable to load assembly {0}!", this.loaderAssemblies[loaderAttr.Value]));
                }
            }
            else
            {
                /// Error: no namespace, name or loader defined
                throw new ConfigurationException("No namespace, name or loader defined for resource!");
            }
        }