Ejemplo n.º 1
0
 public Layer(SubPattern pattern, string name = "")
 {
     SubPattern = pattern;
     Name       = name;
     _Width     = pattern.Width;
     _Height    = pattern.Height;
 }
Ejemplo n.º 2
0
 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));
 }
Ejemplo n.º 3
0
 public SmartObjectLayer(SubPattern pattern, string name, System.Drawing.Bitmap bitmap, int x, int y, int width, int height) : base(pattern, name)
 {
     Crop          = Crops[0];
     Resampler     = Resamplers[0];
     Bitmap        = bitmap;
     _ObjectX      = x;
     _ObjectY      = y;
     _ObjectWidth  = width;
     _ObjectHeight = height;
 }
Ejemplo n.º 4
0
 public SmartObjectLayer(SubPattern pattern, string name, TextureBitmap bitmap, int x, int y, int width, int height) : base(pattern, name)
 {
     Crop          = TextureBitmap.CropMode.Scale;
     Resampler     = ResamplingFilters.Box;
     Bitmap        = bitmap;
     _ObjectX      = x;
     _ObjectY      = y;
     _ObjectWidth  = width;
     _ObjectHeight = height;
 }
Ejemplo n.º 5
0
 public RasterLayer(SubPattern pattern, string name) : base(pattern, name)
 {
 }
Ejemplo n.º 6
0
 public SubPatternColors(SubPattern subPattern)
 {
     SubPattern = subPattern;
 }
Ejemplo n.º 7
0
    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();
    }
Ejemplo n.º 8
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;
        }
    }
Ejemplo n.º 9
0
 public History(SubPattern subPattern, int maxEvents)
 {
     this.SubPattern = subPattern;
     this.MaxEvents  = maxEvents;
     this.Events     = new List <HistoryEvent>();
 }
Ejemplo n.º 10
0
        // Match Functions
        /// <summary>
        /// Match input sentence with current element and output relavent parts, by nature of design elements only match as long as it can recognize and doesn't require input sentence to be exact length
        /// bOptional should be checked by caller
        /// </summary>
        /// <param name="content">string to be matched from begining</param>
        /// <param name="consumed">actual number of characters consumed during the match</param>
        /// <returns>An element instance if match found, otherwise null; Caller might also want to remove trailing space</returns>
        /// <Debug> By design MatchELement doesn't consider English spacing, so caller must be cautious about that since consumed doesn't count ending spaces</Debug>
        public PatternElementInstance MatchElement(string content, VocabularyManager vocabulary, out int consumed)
        {
            switch (Type)
            {
            case PatternElementType.SpecificWord:
                consumed = content.IndexOf(content.TrimStart());
                if (content.TrimStart().IndexOf(Key.ToLower()) == 0)
                {
                    consumed += Key.Length;
                    return(new PatternElementInstance(Type, Key));
                }
                break;

            case PatternElementType.VarietyWord:
                consumed = content.IndexOf(content.TrimStart());
                if (vocabulary.IsPraseVaryingFormOrSynonymUndetermined(content.TrimStart(), Key, ref consumed) == true)
                {
                    return(new PatternElementInstance(Type, content.Substring(0, consumed)));
                }
                break;

            case PatternElementType.WordAttribute:
                // We do not explicitly trim for this; Handling of beginning white spaces dealt with below
                if (Key == "any")
                {
                    throw new Exception("Any should be handled outside");
                }
                else
                {
                    // Get attribtues to match; Attributes are guaranted to be valid at load time
                    bool          bInfinite  = (Key.ElementAt(0) == '*');
                    string[]      attributes = Key.Split(new char[] { '+', '*' });
                    WordAttribute attribute  = 0;
                    foreach (string a in attributes)
                    {
                        if (string.IsNullOrWhiteSpace(a))
                        {
                            continue;
                        }
                        attribute |= (WordAttribute)Enum.Parse(typeof(WordAttribute), a);
                    }
                    consumed = 0;
                    // The input must be recognziable so it's gonna be a phrase of some kind
                    Phrase phrase = vocabulary.GetPhrase(content);     // <Improvement> Could we be matching the shortest attribute? // <Warning> GetPhrase() trimmed, so phrase.Length might not equal actual consumed characters
                    while (phrase != null)
                    {
                        // Try match against attributes
                        if ((phrase.Attribute & attribute) == attribute || attribute == WordAttribute.any)
                        {
                            consumed += content.IndexOf(phrase.Key) + phrase.Key.Length;    // Use content.IndexOf(phrase.Key) first to find where in the original string our phrase is is necessary for sometimes there might be some spaces in front of it
                            phrase    = vocabulary.GetPhrase(content.Substring(consumed));  // Continue with next phrase
                        }
                        else
                        {
                            phrase = null;
                        }
                        if (!bInfinite)
                        {
                            break;
                        }
                    }
                    if (consumed != 0)
                    {
                        return(new PatternElementInstance(Type, content.Substring(0, consumed).Trim()));       // Return that many elements as one single phrase (which by itself may not exist in the library)
                        // <Development> This can be utilzied by action handlers for learning new expressions e.g. "big shinny red juicy" apple
                    }
                }
                break;

            case PatternElementType.SubPattern:
                consumed = 0;
                PatternInstance subPatternInstance = SubPattern.Match(content, vocabulary, ref consumed, false);
                if (subPatternInstance != null)
                {
                    return(new PatternElementInstance(Type, subPatternInstance));
                }
                break;

            case PatternElementType.Choice:
                // Emitting a successful choice at the first matching // <Improvement> A more accurate way would be to match all options and use the longest match, e.g. Courtesy Interrupt
                PatternElementInstance ChoiceInstance = null;
                foreach (PatternElement choiceElement in Choices)
                {
                    ChoiceInstance = choiceElement.MatchElement(content, vocabulary, out consumed);
                    if (ChoiceInstance != null)
                    {
                        return(new PatternElementInstance(Type, ChoiceInstance.ElementValue));
                    }
                }
                // Valid if we have at least one and only one choice
                break;

            case PatternElementType.Tag:
                consumed = content.IndexOf(content.TrimStart());
                string tagValue = MatchTag(Key, content.TrimStart(), vocabulary);
                if (tagValue != null)
                {
                    consumed += tagValue.Length;
                    return(new PatternElementInstance(Type, tagValue));
                }
                break;

            case PatternElementType.CategoryInclude:
            {
                consumed = content.IndexOf(content.TrimStart());
                string match = MatchCategory(Key, true, content.TrimStart(), vocabulary);
                if (match != null)
                {
                    consumed += match.Length;
                    return(new PatternElementInstance(Type, match));
                }
            }
            break;

            case PatternElementType.CategoryExclude:
            {
                consumed = content.IndexOf(content.TrimStart());
                string match = MatchCategory(Key, false, content.TrimStart(), vocabulary);
                if (match != null)
                {
                    consumed += match.Length;
                    return(new PatternElementInstance(Type, match));
                }
            }
            break;

            case PatternElementType.Punctuation:
                if (content.IndexOf(Key) == 0)
                {
                    consumed = Key.Length;
                    return(new PatternElementInstance(Type, Key));
                }
                break;

            case PatternElementType.UnknownPhrase:
                // Try extract unknown from known
                string unknownString = vocabulary.GetUnknownPhrase(content);
                if (unknownString != null)      // Commit only if we find no match
                {
                    consumed = unknownString.Length;
                    return(new PatternElementInstance(Type, unknownString));
                }
                break;

            default:
                break;
            }
            consumed = 0;
            return(null);
        }