示例#1
0
        private bool doesAnchorMatch(AnchorType anchorType, ArrayConsList <char> currentPos, int afterLastMatchIndex)
        {
            switch (anchorType)
            {
            case AnchorType.StartOfString:
                return(currentPos.IsStartOfArray);

            case AnchorType.StartOfLine:
                return(currentPos.IsStartOfArray || currentPos.Prev == '\n');


            case AnchorType.EndOfString:
                return(currentPos.IsEmpty);

            case AnchorType.EndOfLine:
                return(currentPos.IsEmpty || currentPos.Head == '\n');

            case AnchorType.EndOfStringOrBeforeEndingNewline:
                return(currentPos.DropWhile(c => c == '\n').IsEmpty);


            case AnchorType.ContiguousMatch:
                return(currentPos.ArrayIndex == afterLastMatchIndex);

            case AnchorType.WordBoundary:
                return(isWordBoundary(currentPos));

            case AnchorType.NonWordBoundary:
                return(!isWordBoundary(currentPos));


            default:
                throw new ApplicationException(
                          string.Format("BacktrackingMatcher: illegal anchor type ({0}).",
                                        anchorType.ToString()));
            }
        }
示例#2
0
文件: Core.cs 项目: manhg/malyst
        public bool Detect(AnchorType anchorType)
        {
            int dx, dy;
            int startX, startY, endX, endY, stepX, stepY;

            dx = dy = startX = startY = endX = endY = stepX = stepY = 0;
            bool hasTry = false; // Giới hạn detect lại

            for (; !hasTry;)
            {
                switch (anchorType)
                {
                    #region Khởi tạo các giá trị dx,dy, ...
                case AnchorType.TopLeft:
                    dx     = -1;
                    dy     = -1;
                    startX = 1;
                    endX   = SearchAnchorBoxSize;
                    startY = 1;
                    endY   = SearchAnchorBoxSize;
                    stepX  = 1;
                    stepY  = 1;
                    break;

                case AnchorType.BottomLeft:
                    dx     = -1;
                    dy     = 1;
                    startX = 1;
                    endX   = SearchAnchorBoxSize;
                    startY = acq.Height - 1;
                    endY   = acq.Height - SearchAnchorBoxSize;
                    stepX  = 1;
                    stepY  = -1;
                    break;

                case AnchorType.BottomRight:
                    dx     = 1;
                    dy     = 1;
                    startX = acq.Width - 1;
                    endX   = acq.Width - SearchAnchorBoxSize;
                    startY = acq.Height - 1;
                    endY   = acq.Height - SearchAnchorBoxSize;
                    stepX  = -1;
                    stepY  = -1;
                    break;

                case AnchorType.TopRight:
                    dx     = 1;
                    dy     = -1;
                    startX = acq.Width - 1;
                    startY = 1;
                    endX   = acq.Width - SearchAnchorBoxSize;
                    endY   = SearchAnchorBoxSize;
                    stepX  = -1;
                    stepY  = 1;
                    break;
                    #endregion
                }
                for (int y = startY; (y - endY) * stepY < 0; y += stepY)
                {
                    for (int x = startX; (x - endX) * stepX < 0; x += stepX)
                    {
                        if (// Ô hiện tại và 3 ô hướng khác gần kín
                            ((acq.IsBlack(x, y) + acq.IsBlack(x + dx, y + dy)
                              + acq.IsBlack(x + dx, y) + acq.IsBlack(x, y + dy)) >= 3)
                            // Hướng ngược lại là trắng
                            && ((acq.IsBlack(x - dx, y - dy)
                                 + acq.IsBlack(x - dx, y) + acq.IsBlack(x, y - dy)) == 0)
                            // Tâm hướng chéo trên phải đậm
                            && (acq.CaculateBlackArea(x + dDiagonal * dx / 2, y + dDiagonal * dy / 2) >= 9 * 0.7)
                            // Điểm xa chéo là trống.
                            && (acq.IsBlack(x + 2 * dDiagonal * dx, y + 2 * dDiagonal * dy) == 0))

                        {
                            this.anchors[(int)anchorType].X = x;
                            this.anchors[(int)anchorType].Y = y;
                            return(true);
                        }
                    }
                }
                if (!hasTry)
                {
                    hasTry    = true;
                    dDiagonal = dDiagonal - 2;
                }
                else
                {
                    Inform(this, "{0}: không tìm thấy!!!", anchorType.ToString());
                    return(false);
                }
            }
            return(false);
        }
        private bool doesAnchorMatch(AnchorType anchorType, ArrayConsList<char> currentPos, int afterLastMatchIndex)
        {
            switch (anchorType)
            {
                case AnchorType.StartOfString:
                    return currentPos.IsStartOfArray;

                case AnchorType.StartOfLine:
                    return currentPos.IsStartOfArray || currentPos.Prev == '\n';

                case AnchorType.EndOfString:
                    return currentPos.IsEmpty;

                case AnchorType.EndOfLine:
                    return currentPos.IsEmpty || currentPos.Head == '\n';

                case AnchorType.EndOfStringOrBeforeEndingNewline:
                    return currentPos.DropWhile(c => c == '\n').IsEmpty;

                case AnchorType.ContiguousMatch:
                    return currentPos.ArrayIndex == afterLastMatchIndex;

                case AnchorType.WordBoundary:
                    return isWordBoundary(currentPos);

                case AnchorType.NonWordBoundary:
                    return !isWordBoundary(currentPos);

                default:
                    throw new ApplicationException(
                        string.Format("BacktrackingMatcher: illegal anchor type ({0}).",
                                      anchorType.ToString()));
            }
        }
    void _Process()
    {
        Texture2D texture = _texture as Texture2D;

        if (texture == null)
        {
            Debug.LogError("Texture is not Texture2D.");
            return;
        }

        string                   assetPath                = AssetDatabase.GetAssetPath(texture);
        TextureImporter          textureImporter          = (TextureImporter)TextureImporter.GetAtPath(assetPath);
        bool                     overrideTextureFormat    = false;
        bool                     overrideNPOTScale        = false;
        bool                     overrideGenerateMipmap   = false;
        bool                     overrideMaxTextureSize   = false;
        TextureImporterFormat    textureImporterFormat    = TextureImporterFormat.ARGB32;
        TextureImporterNPOTScale textureImporterNPOTScale = TextureImporterNPOTScale.None;
        int maxTextureSize = 0;

        if (textureImporter.textureFormat != TextureImporterFormat.ARGB32)
        {
            textureImporterFormat         = textureImporter.textureFormat;
            textureImporter.textureFormat = TextureImporterFormat.ARGB32;
            overrideTextureFormat         = true;
        }
        if (textureImporter.generateMipsInLinearSpace)
        {
            textureImporter.generateMipsInLinearSpace = false;
            overrideGenerateMipmap = true;
        }
        if (textureImporter.npotScale != TextureImporterNPOTScale.None)
        {
            textureImporterNPOTScale  = textureImporter.npotScale;
            textureImporter.npotScale = TextureImporterNPOTScale.None;
            overrideNPOTScale         = true;
        }
        if (textureImporter.maxTextureSize != 4096)
        {
            maxTextureSize = textureImporter.maxTextureSize;
            textureImporter.maxTextureSize = 4096;
            overrideMaxTextureSize         = true;
        }
        if (overrideTextureFormat || overrideGenerateMipmap || overrideNPOTScale || overrideMaxTextureSize)
        {
            AssetDatabase.WriteImportSettingsIfDirty(assetPath);
            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
        }

        int width  = texture.width;
        int height = texture.height;

        if (overrideTextureFormat || overrideGenerateMipmap || overrideNPOTScale || overrideMaxTextureSize)
        {
            if (overrideTextureFormat)
            {
                textureImporter.textureFormat = textureImporterFormat;
            }
            if (overrideGenerateMipmap)
            {
                textureImporter.generateMipsInLinearSpace = true;
            }
            if (overrideNPOTScale)
            {
                textureImporter.npotScale = textureImporterNPOTScale;
            }
            if (overrideMaxTextureSize)
            {
                textureImporter.maxTextureSize = maxTextureSize;
            }
            AssetDatabase.WriteImportSettingsIfDirty(assetPath);
            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
        }

        GameObject go = new GameObject(texture.name);

        go.transform.localPosition = Vector3.zero;
        go.transform.localRotation = Quaternion.identity;
        go.transform.localScale    = Vector3.one;

        if (string.IsNullOrEmpty(assetPath))
        {
            Debug.LogError("");
            return;
        }

        string directoryName = Path.GetDirectoryName(assetPath);
        string fileName      = Path.GetFileNameWithoutExtension(assetPath);
        string baseAssetPath = Path.Combine(directoryName, fileName);
        string materialPath  = baseAssetPath + ".mat";

        Material material = null;
        bool     isExistsAssetMaterial = false;

        if (_isCreateAsset)
        {
            material = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material));
            isExistsAssetMaterial = (material != null);
            if (isExistsAssetMaterial)
            {
                material.shader = Shader.Find(_GetShaderName());
            }
        }

        if (material == null)
        {
            material = new Material(Shader.Find(_GetShaderName()));
        }

        material.mainTexture = texture;

        System.Text.StringBuilder postfixMeshName = new System.Text.StringBuilder();

        postfixMeshName.Append("_");
        if (_anchorType.ToString().StartsWith("Top"))
        {
            postfixMeshName.Append("t");
        }
        else if (_anchorType.ToString().StartsWith("Bottom"))
        {
            postfixMeshName.Append("b");
        }
        else
        {
            postfixMeshName.Append("c");
        }

        if (_anchorType.ToString().StartsWith("Left"))
        {
            postfixMeshName.Append("l");
        }
        else if (_anchorType.ToString().StartsWith("Right"))
        {
            postfixMeshName.Append("r");
        }
        else
        {
            postfixMeshName.Append("c");
        }

        postfixMeshName.Append("_");
        postfixMeshName.Append(_scaleBy.ToString().Substring(0, 1).ToLower());
        postfixMeshName.Append(_size.ToString());
        string meshPath = baseAssetPath + postfixMeshName.ToString() + ".asset";

        Mesh mesh = null;
        bool isExistsAssetMesh = false;

        if (_isCreateAsset)
        {
            mesh = (Mesh)AssetDatabase.LoadAssetAtPath(meshPath, typeof(Mesh));
            isExistsAssetMesh = (mesh != null);
        }

        float w = (float)width;
        float h = (float)height;

        switch (_scaleBy)
        {
        case ScaleBy.Pixel:
            w *= _size;
            h *= _size;
            break;

        case ScaleBy.Width:
            h *= _size / w;
            w  = _size;
            break;

        case ScaleBy.Height:
            w *= _size / h;
            h  = _size;
            break;
        }
        float x = 0.0f, y = 0.0f;

        switch (_anchorType)
        {
        case AnchorType.TopLeft:
            x = 0.0f;
            y = -h;
            break;

        case AnchorType.Top:
            x = -w * 0.5f;
            y = -h;
            break;

        case AnchorType.TopRight:
            x = -w;
            y = -h;
            break;

        case AnchorType.Left:
            x = 0.0f;
            y = -h * 0.5f;
            break;

        case AnchorType.Center:
            x = -w * 0.5f;
            y = -h * 0.5f;
            break;

        case AnchorType.Right:
            x = -w;
            y = -h * 0.5f;
            break;

        case AnchorType.BottomLeft:
            x = 0.0f;
            y = 0.0f;
            break;

        case AnchorType.Bottom:
            x = -w * 0.5f;
            y = 0.0f;
            break;

        case AnchorType.BottomRight:
            x = -w;
            y = 0.0f;
            break;
        }

        if (mesh == null)
        {
            mesh          = new Mesh();
            mesh.vertices = new Vector3[4] {
                new Vector3(x, y + h, 0.0f),
                new Vector3(x + w, y + h, 0.0f),
                new Vector3(x, y, 0.0f),
                new Vector3(x + w, y, 0.0f),
            };
            mesh.uv = new Vector2[4] {
                new Vector2(0.0f, 1.0f),
                new Vector2(1.0f, 1.0f),
                new Vector2(0.0f, 0.0f),
                new Vector2(1.0f, 0.0f),
            };
            mesh.triangles = new int[6] {
                0, 1, 2, 3, 2, 1,
            };

            mesh.RecalculateBounds();
            mesh.RecalculateNormals();
        }

        MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>();

        meshRenderer.castShadows    = false;
        meshRenderer.receiveShadows = false;
        meshRenderer.sharedMaterial = material;
        MeshFilter meshFilter = go.AddComponent <MeshFilter>();

        meshFilter.sharedMesh = mesh;

        if (_isCreateAsset)
        {
            if (!isExistsAssetMaterial)
            {
                AssetDatabase.CreateAsset(material, materialPath);
            }
            else
            {
                EditorUtility.SetDirty(material);
                AssetDatabase.SaveAssets();
            }

            if (!isExistsAssetMesh)
            {
                AssetDatabase.CreateAsset(mesh, meshPath);
            }
        }
    }
示例#5
0
 public SandboxAnchorBuilder WithType(AnchorType anchorType)
 {
     _type = anchorType.ToString();
     return(this);
 }