public SubPattern(Pattern pattern, DesignPatternInformation.DesignPatternPart part, bool import = false) { Colors = new SubPatternColors(this); 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"); backgroundLayer.Colors = new UnityEngine.Color[Width * Height]; for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { backgroundLayer.Colors[x + y * Width] = Colors[x + y * Width]; } } backgroundLayer.UpdateTexture(); Layers.Add(backgroundLayer); History.AddEvent(new History.LayerCreatedAction("Opened", 0, backgroundLayer)); } }
public void CreateLayer() { var newLayer = new RasterLayer(this, "Layer " + Layers.Count); newLayer.UpdateTexture(); Layers.Insert(_SelectedLayer + 1, newLayer); SelectedLayer = newLayer; Pattern.Editor.LayersChanged(); History.AddEvent(new History.LayerCreatedAction("Created: " + newLayer.Name, _SelectedLayer, newLayer)); }
public void CreateLayer() { var newLayer = new RasterLayer(this, "Layer " + Layers.Count) { Colors = new UnityEngine.Color[Width * Height] }; for (int i = 0; i < newLayer.Colors.Length; i++) { newLayer.Colors[i] = new UnityEngine.Color(0f, 0f, 0f, 0f); } newLayer.UpdateTexture(); Layers.Insert(_SelectedLayer + 1, newLayer); SelectedLayer = newLayer; Pattern.Editor.LayersChanged(); History.AddEvent(new History.LayerCreatedAction("Created: " + newLayer.Name, _SelectedLayer, newLayer)); }
public void FromBytes(byte[] bytes) { using (MemoryStream stream = new MemoryStream(bytes)) { BinaryReader reader = new BinaryReader(stream); byte version = reader.ReadByte(); if (version == 0x00) { var t = (DesignPattern.TypeEnum)reader.ReadByte(); if (this._Type == DesignPattern.TypeEnum.SimplePattern && t != DesignPattern.TypeEnum.SimplePattern) { throw new ArgumentException("Simple design spot can't hold pro design.", "project"); } if (this._Type != DesignPattern.TypeEnum.SimplePattern && 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 System.Drawing.Bitmap(bitmapWidth, bitmapHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bitmap.FromBytes(bitmapPixels); SmartObjectLayer layer = new SmartObjectLayer(subPattern, name, bitmap, objectX, objectY, objectW, objectH); subPattern.Layers.Add(layer); layer.Crop = SmartObjectLayer.Crops[crop]; layer.Resampler = SmartObjectLayer.Resamplers[resampling]; layer.Colors = new UnityEngine.Color[layer.Width * layer.Height]; 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); layer.Colors = new UnityEngine.Color[layer.Width * layer.Height]; for (int y = 0; y < layer.Height; y++) { for (int x = 0; x < layer.Width; x++) { layer.Colors[x + y * layer.Width] = new UnityEngine.Color(reader.ReadByte() / 255f, reader.ReadByte() / 255f, reader.ReadByte() / 255f, reader.ReadByte() / 255f); } } layer.UpdateTexture(); subPattern.Layers.Add(layer); } } } } } _CurrentSubPattern = 0; Editor.SetSize(CurrentSubPattern.Width, CurrentSubPattern.Height); Editor.LayersChanged(); Editor.SubPatternChanged(CurrentSubPattern.Part); Editor.SetType(this._Type); Editor.Show(null, null, null); Editor.OnImageUpdated(); Editor.Tools.HistoryChanged(CurrentSubPattern.History); for (int i = 0; i < SubPatterns.Count; i++) { SubPatterns[i].UpdateImage(false); } this.RegeneratePreview(); }
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; } }