示例#1
0
        /// <summary>
        /// Adds a resource to the scene
        /// </summary>
        /// <typeparam name="TResourceType">The type of the resource.</typeparam>
        /// <param name="resourceFactory">The factory method which creates the resource object.</param>
        /// <param name="resourceKey">The key for the newly generated resource.</param>
        internal NamedOrGenericKey AddResource <TResourceType>(Func <EngineDevice, TResourceType> resourceFactory, NamedOrGenericKey resourceKey)
            where TResourceType : Resource
        {
            resourceFactory.EnsureNotNull(nameof(resourceFactory));

            this.InitializeResourceDictionaries();

            if (resourceKey == NamedOrGenericKey.Empty)
            {
                resourceKey = GraphicsCore.GetNextGenericResourceKey();
            }

            foreach (var actResourceDict in _registeredResourceDicts)
            {
                actResourceDict.AddResource(resourceKey, resourceFactory(actResourceDict.Device));
            }

            return(resourceKey);
        }
        /// <summary>
        /// Adds the given resource to the dictionary.
        /// </summary>
        /// <param name="resource">The resource to add.</param>
        /// <param name="resourceKey">The key of the resource.</param>
        internal TResourceType AddResource <TResourceType>(NamedOrGenericKey resourceKey, TResourceType resource)
            where TResourceType : Resource
        {
            resource.EnsureNotNull(nameof(resource));

            //Perform some checks
            if (resource.Dictionary != null)
            {
                if (resource.Dictionary == this)
                {
                    return(resource);
                }
                if (resource.Dictionary != this)
                {
                    throw new ArgumentException("Given resource belongs to another ResourceDictionary!", nameof(resource));
                }
            }

            //Check given keys
            if (!resource.IsKeyEmpty && !resourceKey.IsEmpty && resource.Key != resourceKey)
            {
                throw new ArgumentException("Unable to override existing key on given resource!");
            }

            //RemoveObject another resource with the same name
            if (!resource.IsKeyEmpty)
            {
                this.RemoveResource(resource.Key);
            }
            if (!resourceKey.IsEmpty)
            {
                this.RemoveResource(resourceKey);
            }

            //Apply a valid key on the given resource object
            if (resource.Key.IsEmpty)
            {
                if (resourceKey.IsEmpty)
                {
                    resource.Key = GraphicsCore.GetNextGenericResourceKey();
                }
                else
                {
                    resource.Key = resourceKey;
                }
            }

            //AddObject the resource
            var newResource = new ResourceInfo(resource);

            _resources[resource.Key] = newResource;

            if (newResource.RenderableResource != null)
            {
                _renderableResources.Add(newResource.RenderableResource);
            }
            if (newResource.UpdatableResource != null)
            {
                _updatableResources.Add(newResource.UpdatableResource);
            }

            //Register this dictionary on the resource
            resource.Dictionary = this;

            return(resource);
        }