Example #1
0
    public SubPattern(Pattern pattern, DesignPatternInformation.DesignPatternPart part, bool import = false)
    {
        Bitmap = new TextureBitmap(part.Width, part.Height);
        Bitmap.Clear();
        Bitmap.Texture.filterMode = UnityEngine.FilterMode.Point;
        Pattern = pattern;
        Part    = part;
        Layers  = new List <Layer>();
        History = new History(this, 50);
        History.OnHistoryChanged = () => {
            Pattern.Editor.Tools.HistoryChanged(History);
        };
        if (!import)
        {
            var backgroundLayer = new RasterLayer(this, "Background");

            unsafe
            {
                var colors      = pattern.Bitmap.GetColors();
                var layerColors = backgroundLayer.Texture.GetColors();
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        layerColors[x + (Height - 1 - y) * Width] = colors[Part.X + x + (pattern.Height - 1 - (Part.Y + y)) * pattern.Width];
                    }
                }
                //backgroundLayer.Texture.SetPixel(x, y, pattern.Bitmap.GetPixel(Part.X + x, (pattern.Height - 1 - Part.Y) + y));
            }
            Layers.Add(backgroundLayer);
            History.AddEvent(new History.LayerCreatedAction("Opened", 0, backgroundLayer));
        }
    }
Example #2
0
    public void UpdateImage(bool updatePreview = true)
    {
        Bitmap.Clear();
        for (int j = 0; j < Layers.Count; j++)
        {
            if (Layers[j] is SmartObjectLayer sol)
            {
                Bitmap.AlphaComposite(Layers[j].Texture, new TextureBitmap.Color(255, 255, 255, 255), new TextureBitmap.Point(sol.ObjectX, sol.ObjectY));
            }
            else
            {
                Bitmap.AlphaComposite(Layers[j].Texture, new TextureBitmap.Color(255, 255, 255, 255));
            }
            if (TemporaryLayer != null && j == SelectedLayerIndex)
            {
                Bitmap.AlphaComposite(TemporaryLayer.Texture, new TextureBitmap.Color(255, 255, 255, 255));
            }
        }
        Bitmap.Apply();
        if (updatePreview)
        {
            Pattern.RegeneratePreview();
        }

        Pattern.Bitmap.Replace(this.Bitmap, new TextureBitmap.Color(255, 255, 255, 255), new TextureBitmap.Point(Part.X, Part.Y));
        //Pattern.Bitmap.Apply();
        Pattern.Editor.OnImageUpdated();
    }
Example #3
0
    public Pattern(PatternEditor editor, DesignPattern pattern)
    {
        try
        {
            Logger.Log(Logger.Level.INFO, "[PatternEditor/Pattern] Creating new pattern");
            StartPreviewThread();
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Preview generator thread started!");
            _Type = pattern.Type;
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern type: " + _Type.ToString());
            Bitmap = new TextureBitmap(pattern.Width, pattern.Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created TextureBitmap " + pattern.Width + "x" + pattern.Height);
            Bitmap.Clear();
            PreviewBitmap = new TextureBitmap(pattern.Width, pattern.Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview TextureBitmap " + pattern.Width + "x" + pattern.Height);
            PreviewBitmap.Clear();
            PreviewSprite = UnityEngine.Sprite.Create(PreviewBitmap.Texture, new UnityEngine.Rect(0, 0, PreviewBitmap.Width, PreviewBitmap.Height), new UnityEngine.Vector2(0.5f, 0.5f));
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview sprite");

            UpscaledPreviewBitmap = new TextureBitmap(pattern.Width * 4, pattern.Height * 4);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created upscaled preview TextureBitmap " + (pattern.Width * 4) + "x" + (pattern.Height * 4));
            UpscaledPreviewBitmap.Clear();

            Quantizer = Quantizers[0];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Selected Quantizer: " + Quantizer.GetType().ToString());
            ColorCache = ColorCaches[0];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Selected Color Cache: " + ColorCache.GetType().ToString());

            Editor        = editor;
            DesignPattern = pattern;
            var colors = pattern.GetPixels();

            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Parsing colors of pattern...");
            unsafe
            {
                var bitmapColors = Bitmap.GetColors();
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        var col = new TextureBitmap.Color(
                            (byte)(colors[x + y * Width].r * 255f),
                            (byte)(colors[x + y * Width].g * 255f),
                            (byte)(colors[x + y * Width].b * 255f),
                            (byte)(colors[x + y * Width].a * 255f)
                            );
                        *(bitmapColors + x + (Height - 1 - y) * Width) = col;
                    }
                }
            }
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Parsed " + (Width * Height) + " pixels.");
            Info = DesignPatternInformation.Types[pattern.Type];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern information obtained.");
        }
        catch (System.Exception e)
        {
            Logger.Log(Logger.Level.ERROR, "[PatternEditor/Pattern] Error while creating pattern: " + e.ToString());
        }
    }
 public Layer(SubPattern pattern, string name = "")
 {
     SubPattern = pattern;
     Name       = name;
     _Width     = pattern.Width;
     _Height    = pattern.Height;
     Texture    = new TextureBitmap(_Width, _Height);
     Texture.Clear();
     Sprite = Sprite.Create(Texture.Texture, new Rect(0, 0, Texture.Width, Texture.Height), new Vector2(0.5f, 0.5f));
 }
Example #5
0
    public Pattern(PatternEditor editor, byte[] bytes)
    {
        try
        {
            Editor = editor;

            using (MemoryStream stream = new MemoryStream(bytes))
            {
                BinaryReader reader  = new BinaryReader(stream);
                byte         version = reader.ReadByte();
                var          t       = (DesignPattern.TypeEnum)reader.ReadByte();
                if (!Editor.IsPro && t != DesignPattern.TypeEnum.SimplePattern)
                {
                    throw new ArgumentException("Simple design spot can't hold pro design.", "project");
                }
                if (Editor.IsPro && t == DesignPattern.TypeEnum.SimplePattern)
                {
                    throw new ArgumentException("Pro design spot can't hold simple design.", "project");
                }
                this._Type = t;
                Quantizer  = Quantizers[reader.ReadByte()];
                ColorCache = ColorCaches[reader.ReadByte()];

                var info = DesignPatternInformation.Types[this._Type];
                this.SubPatterns = new List <SubPattern>();
                for (int i = 0; i < info.Parts.Count; i++)
                {
                    var subPattern = new SubPattern(this, info.Parts[i], true);
                    this.SubPatterns.Add(subPattern);

                    int layerCount = reader.ReadByte();
                    for (int j = 0; j < layerCount; j++)
                    {
                        var layerType = reader.ReadByte();
                        if (layerType == 0x01)
                        {
                            int    objectX      = reader.ReadInt32();
                            int    objectY      = reader.ReadInt32();
                            int    objectW      = reader.ReadInt32();
                            int    objectH      = reader.ReadInt32();
                            byte   crop         = reader.ReadByte();
                            byte   resampling   = reader.ReadByte();
                            int    nameLength   = reader.ReadInt32();
                            string name         = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(nameLength));
                            int    bitmapWidth  = reader.ReadInt32();
                            int    bitmapHeight = reader.ReadInt32();
                            byte[] bitmapPixels = reader.ReadBytes(bitmapWidth * bitmapHeight * 4);
                            var    bitmap       = new TextureBitmap(bitmapWidth, bitmapHeight);
                            int    m            = bitmapWidth * bitmapHeight * bitmap.PixelSize;
                            unsafe
                            {
                                byte *ptr = (byte *)bitmap.Bytes.ToPointer();
                                for (int k = 0; k < m; k++)
                                {
                                    *(ptr + k) = bitmapPixels[k];
                                }
                            }
                            if (version == 0)
                            {
                                bitmap.FlipY();
                            }
                            SmartObjectLayer layer = new SmartObjectLayer(subPattern, name, bitmap, objectX, objectY, objectW, objectH);
                            subPattern.Layers.Add(layer);
                            layer.Crop      = (TextureBitmap.CropMode)crop;
                            layer.Resampler = (ResamplingFilters)resampling;
                            layer.Bitmap    = bitmap;
                            layer.UpdateColors();
                            layer.UpdateTexture();
                        }
                        else if (layerType == 0x00)
                        {
                            int         nameLength = reader.ReadInt32();
                            string      name       = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(nameLength));
                            RasterLayer layer      = new RasterLayer(subPattern, name);

                            int    m            = layer.Width * layer.Height * layer.Texture.PixelSize;
                            byte[] bitmapPixels = reader.ReadBytes(m);
                            layer.Texture = new TextureBitmap(layer.Width, layer.Height);

                            unsafe
                            {
                                byte *ptr = (byte *)layer.Texture.Bytes.ToPointer();
                                for (int k = 0; k < m; k++)
                                {
                                    *(ptr + k) = bitmapPixels[k];
                                }
                            }
                            if (version == 0)
                            {
                                layer.Texture.FlipY();
                            }

                            layer.UpdateTexture();
                            subPattern.Layers.Add(layer);
                        }
                    }
                }
            }
            GC.Collect();
            _CurrentSubPattern = 0;

            Logger.Log(Logger.Level.INFO, "[PatternEditor/Pattern] Creating new pattern");
            StartPreviewThread();
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern type: " + _Type.ToString());
            Bitmap = new TextureBitmap(Width, Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created TextureBitmap " + Width + "x" + Height);
            Bitmap.Clear();
            PreviewBitmap = new TextureBitmap(Width, Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview TextureBitmap " + Width + "x" + Height);
            PreviewBitmap.Clear();
            PreviewSprite = UnityEngine.Sprite.Create(PreviewBitmap.Texture, new UnityEngine.Rect(0, 0, PreviewBitmap.Width, PreviewBitmap.Height), new UnityEngine.Vector2(0.5f, 0.5f));
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview sprite");

            UpscaledPreviewBitmap = new TextureBitmap(Width * 4, Height * 4);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created upscaled preview TextureBitmap " + (Width * 4) + "x" + (Height * 4));
            UpscaledPreviewBitmap.Clear();

            Info = DesignPatternInformation.Types[this._Type];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern information obtained.");
        }
        catch (System.Exception e)
        {
            Logger.Log(Logger.Level.ERROR, "[PatternEditor/Pattern] Error while creating pattern: " + e.ToString());
            this.Dispose();
            throw e;
        }
    }