Пример #1
0
        private void RenderTextureAssetPreview(string assetName)
        {
            if (!SetPreviews(PreviewType.Default, assetName))
            {
                return;
            }
            if (assetName == null)
            {
                return;
            }

            var filePath = ContentProjectManager.GetTexturePath(assetName);

            if (!string.IsNullOrWhiteSpace(filePath))
            {
                // We got it. Set as the new image.
                try
                {
                    using (var img = Image.FromFile(filePath))
                    {
                        using (var g = System.Drawing.Graphics.FromImage(pbPreview.Image))
                        {
                            g.DrawImage(img, (pbPreview.Image.Width - img.Width) / 2f, (pbPreview.Image.Height - img.Height) / 2f, img.Width, img.Height);
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                }
            }
        }
Пример #2
0
        private void EffectsAfterSelect(object sender, TreeViewEventArgs e)
        {
            // Disable OK button until we have a valid image.
            btnOK.Enabled = false;
            SelectedAsset = null;

            // Clear preview.
            _preview.Effect = null;

            // Do we have something new?
            if (e.Node == null)
            {
                return;
            }

            // See if the "none" entry is selected.
            if (e.Node.Name.Equals(""))
            {
                btnOK.Enabled = true;
                return;
            }

            // See if the asset is valid (full path).
            var assetPath = ContentProjectManager.GetEffectPath(e.Node.FullPath);

            if (assetPath == null)
            {
                return;
            }

            // OK, try to load it.
            _preview.Effect = e.Node.FullPath;
            btnOK.Enabled   = true;
            SelectedAsset   = e.Node.FullPath;
        }
Пример #3
0
        private void TexturesAfterSelect(object sender, TreeViewEventArgs e)
        {
            // Disable OK button until we have a valid image.
            btnOK.Enabled = false;
            SelectedAsset = null;

            // Clear preview.
            var oldImage = pbPreview.Image;

            pbPreview.Image = null;
            if (oldImage != null)
            {
                oldImage.Dispose();
            }

            // Do we have something new?
            if (e.Node == null)
            {
                return;
            }

            // See if the "none" entry is selected.
            if (e.Node.Name.Equals(""))
            {
                btnOK.Enabled = true;
                return;
            }

            // See if the asset is valid (full path).
            var assetPath = ContentProjectManager.GetTexturePath(e.Node.FullPath);

            if (assetPath == null)
            {
                return;
            }

            // OK, try to load it.
            try
            {
                pbPreview.Image = Image.FromFile(assetPath);
                btnOK.Enabled   = true;
                SelectedAsset   = e.Node.FullPath;
            }
            catch (FileNotFoundException)
            {
            }
        }
Пример #4
0
        public override T Load <T>(string assetName)
        {
            var g = (IGraphicsDeviceService)ServiceProvider.GetService(typeof(IGraphicsDeviceService));

            if (typeof(T) == typeof(Texture2D))
            {
                if (_textures.ContainsKey(assetName))
                {
                    return((T)(object)_textures[assetName]);
                }

                using (var img = Image.FromFile(ContentProjectManager.GetTexturePath(assetName)))
                {
                    var bmp  = new Bitmap(img);
                    var data = new uint[bmp.Width * bmp.Height];
                    for (var y = 0; y < bmp.Height; ++y)
                    {
                        for (var x = 0; x < bmp.Width; ++x)
                        {
                            var pixel = bmp.GetPixel(x, y);
                            data[x + y * bmp.Width] =
                                Microsoft.Xna.Framework.Color.FromNonPremultiplied(pixel.R, pixel.G, pixel.B, pixel.A).
                                PackedValue;
                        }
                    }
                    if (g != null)
                    {
                        var t = new Texture2D(g.GraphicsDevice, img.Width, img.Height);
                        t.SetData(data);
                        _textures.Add(assetName, t);
                        return((T)(object)t);
                    }
                    else
                    {
                        throw new InvalidOperationException("Must wait with loading until graphics device is initialized.");
                    }
                }
            }
            else if (typeof(T) == typeof(Effect))
            {
                if (_shaders.ContainsKey(assetName))
                {
                    return((T)(object)_shaders[assetName]);
                }

                var shaderPath = ContentProjectManager.GetShaderPath(assetName);
                if (shaderPath != null)
                {
                    using (var file = File.OpenText(shaderPath))
                    {
                        var sourceCode = file.ReadToEnd();

                        var effectSource = new EffectContent
                        {
                            EffectCode = sourceCode,
                            Identity   = new ContentIdentity {
                                SourceFilename = assetName
                            }
                        };
                        var processor      = new EffectProcessor();
                        var compiledEffect = processor.Process(effectSource, new DummyProcessorContext());
                        var effect         = new Effect(g.GraphicsDevice, compiledEffect.GetEffectCode());
                        _shaders.Add(assetName, effect);
                        return((T)(object)effect);
                    }
                }
            }
            else if (typeof(T) == typeof(ParticleEffect))
            {
                using (var xmlReader = XmlReader.Create(ContentProjectManager.GetEffectPath(assetName)))
                {
                    var effect = IntermediateSerializer.Deserialize <ParticleEffect>(xmlReader, null);
                    effect.Initialise();
                    effect.LoadContent(this);
                    return((T)(object)effect);
                }
            }
            else if (typeof(T) == typeof(IFactory[]))
            {
                return((T)(object)FactoryManager.GetFactoriesFromFile(assetName).ToArray());
            }
            else if (typeof(T) == typeof(ItemPool[]))
            {
                return((T)(object)ItemPoolManager.GetItemPools().ToArray());
            }
            else if (typeof(T) == typeof(AttributePool[]))
            {
                return((T)(object)AttributePoolManager.GetAttributePools().ToArray());
            }
            return(default(T));
        }