public TextureAtlasPosition this[string textureCode]
        {
            get
            {
                if (contentTextPos == null)
                {
                    int textureSubId;

                    textureSubId = ObjectCacheUtil.GetOrCreate <int>(capi, "contenttexture-" + contentTexture.ToString() + "-" + contentTexture.Alpha, () =>
                    {
                        TextureAtlasPosition texPos;
                        int id = 0;

                        BitmapRef bmp = capi.Assets.TryGet(contentTexture.Base.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"))?.ToBitmap(capi);
                        if (bmp != null)
                        {
                            if (contentTexture.Alpha != 255)
                            {
                                bmp.MulAlpha(contentTexture.Alpha);
                            }
                            capi.BlockTextureAtlas.InsertTexture(bmp, out id, out texPos);
                            bmp.Dispose();
                        }

                        return(id);
                    });

                    contentTextPos = capi.BlockTextureAtlas.Positions[textureSubId];
                }

                return(contentTextPos);
            }
        }
示例#2
0
        private void OnDraw(Context ctx, ImageSurface surface, ElementBounds currentBounds)
        {
            BitmapRef bmp = capi.Assets.Get("alchemy:textures/hud/alchemyhud.png").ToBitmap(capi);

            if (inactive)
            {
                bmp.MulAlpha(30);
            }
            Vintagestory.API.Common.SurfaceDrawImage.Image(surface, ((Vintagestory.API.Common.BitmapExternal)bmp), (int)currentBounds.drawX, (int)currentBounds.drawY, (int)currentBounds.InnerWidth, (int)currentBounds.InnerHeight);
            bmp.Dispose();
        }
示例#3
0
        public TextureAtlasPosition this[string textureCode]
        {
            get
            {
                AssetLocation textureLoc = null;
                if (curContProps.Textures != null)
                {
                    CompositeTexture compTex;
                    if (curContProps.Textures.TryGetValue(textureCode, out compTex))
                    {
                        textureLoc = compTex.Base;
                    }
                }

                if (textureLoc == null && shapeTextures != null)
                {
                    shapeTextures.TryGetValue(textureCode, out textureLoc);
                }

                if (textureLoc != null)
                {
                    TextureAtlasPosition texPos = capi.BlockTextureAtlas[textureLoc];
                    if (texPos == null)
                    {
                        BitmapRef bmp = capi.Assets.TryGet(textureLoc.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"))?.ToBitmap(capi);
                        if (bmp != null)
                        {
                            capi.BlockTextureAtlas.InsertTextureCached(textureLoc, bmp, out _, out texPos);
                            bmp.Dispose();
                        }
                    }

                    return(texPos);
                }

                ItemStack content = GetContents();
                if (content.Class == EnumItemClass.Item)
                {
                    TextureAtlasPosition texPos;
                    textureLoc = content.Item.Textures[textureCode].Base;
                    BitmapRef bmp = capi.Assets.TryGet(textureLoc.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"))?.ToBitmap(capi);
                    if (bmp != null)
                    {
                        capi.BlockTextureAtlas.InsertTextureCached(textureLoc, bmp, out _, out texPos);
                        bmp.Dispose();
                        return(texPos);
                    }
                }

                return(contentTexSource[textureCode]);
            }
        }
示例#4
0
        bool RuntimeInsert(ICoreClientAPI capi, ITextureAtlasAPI intoAtlas, BakedCompositeTexture btex)
        {
            BitmapRef bmp = capi.Assets.Get(btex.BakedName).ToBitmap(capi);

            int textureSubId;
            TextureAtlasPosition texpos;

            if (intoAtlas.InsertTexture(bmp, out textureSubId, out texpos))
            {
                btex.TextureSubId = textureSubId;
                capi.Render.RemoveTexture(btex.BakedName);
                return(true);
            }

            bmp.Dispose();
            return(false);
        }
示例#5
0
        public TextureAtlasPosition this[string textureCode]
        {
            get
            {
                if (textureCode == "topper" && corkTextPos != null)
                {
                    return(corkTextPos);
                }
                if (textureCode == "glass" && blockTextPos != null)
                {
                    return(blockTextPos);
                }
                if (textureCode == "bracing" && bracingTextPos != null)
                {
                    return(bracingTextPos);
                }
                if (contentTextPos == null)
                {
                    int textureSubId;

                    textureSubId = ObjectCacheUtil.GetOrCreate <int>(capi, "contenttexture-" + contentTexture.ToString(), () =>
                    {
                        TextureAtlasPosition texPos;
                        int id = 0;

                        BitmapRef bmp = capi.Assets.TryGet(contentTexture.Base.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"))?.ToBitmap(capi);
                        if (bmp != null)
                        {
                            capi.BlockTextureAtlas.InsertTexture(bmp, out id, out texPos);
                            bmp.Dispose();
                        }

                        return(id);
                    });

                    contentTextPos = capi.BlockTextureAtlas.Positions[textureSubId];
                }

                return(contentTextPos);
            }
        }
        public override void Initialize(EntityProperties properties, JsonObject attributes)
        {
            base.Initialize(properties, attributes);

            skintree = entity.WatchedAttributes.GetTreeAttribute("skinConfig");
            if (skintree == null)
            {
                entity.WatchedAttributes["skinConfig"] = skintree = new TreeAttribute();
            }

            entity.WatchedAttributes.RegisterModifiedListener("skinConfig", onSkinConfigChanged);

            AvailableSkinParts = properties.Attributes["skinnableParts"].AsObject <SkinnablePart[]>();
            foreach (var val in AvailableSkinParts)
            {
                string partCode = val.Code;
                val.VariantsByCode = new Dictionary <string, SkinnablePartVariant>();

                AvailableSkinPartsByCode[val.Code] = val;

                if (val.Type == EnumSkinnableType.Texture && entity.Api.Side == EnumAppSide.Client)
                {
                    ICoreClientAPI capi = entity.Api as ICoreClientAPI;

                    LoadedTexture texture = new LoadedTexture(capi);
                    foreach (var variant in val.Variants)
                    {
                        AssetLocation textureLoc;

                        if (val.TextureTemplate != null)
                        {
                            textureLoc      = val.TextureTemplate.Clone();
                            textureLoc.Path = textureLoc.Path.Replace("{code}", variant.Code);
                        }
                        else
                        {
                            textureLoc = variant.Texture;
                        }

                        IAsset asset = capi.Assets.TryGet(textureLoc.Clone().WithPathAppendixOnce(".png").WithPathPrefixOnce("textures/"), true);

                        int   r = 0, g = 0, b = 0;
                        float c = 0;

                        BitmapRef bmp = asset.ToBitmap(capi);
                        for (int i = 0; i < 8; i++)
                        {
                            Vec2d vec  = GameMath.R2Sequence2D(i);
                            Color col2 = bmp.GetPixelRel((float)vec.X, (float)vec.Y);
                            if (col2.A > 0.5)
                            {
                                r += col2.R;
                                g += col2.G;
                                b += col2.B;
                                c++;
                            }
                        }

                        bmp.Dispose();

                        c             = Math.Max(1, c);
                        variant.Color = ColorUtil.ColorFromRgba((int)(r / c), (int)(g / c), (int)(b / c), 255);
                        val.VariantsByCode[variant.Code] = variant;
                    }
                }
                else
                {
                    foreach (var variant in val.Variants)
                    {
                        val.VariantsByCode[variant.Code] = variant;
                    }
                }
            }

            if (entity.Api.Side == EnumAppSide.Server && AppliedSkinParts.Count == 0)
            {
                foreach (var val in AvailableSkinParts)
                {
                    string partCode    = val.Code;
                    string variantCode = val.Variants[entity.World.Rand.Next(val.Variants.Length)].Code;
                    selectSkinPart(partCode, variantCode, false);
                }
            }
        }