Esempio n. 1
0
        public ControlParser(string srcFileDirPath, PsdLayerExtractor.Layer layer)
        {
            var errorPreMsg     = "Parse error at '" + srcFileDirPath + "/" + layer.name + "'.";
            var name_cmdAndVals = layer.name.Split('@');

            this.srcFileDirPath = srcFileDirPath;
            this.originalName   = layer.name;
            this.name           = name_cmdAndVals[0];
            {
                for (var i = 1; i < name_cmdAndVals.Length; ++i)
                {
                    name_cmdAndVals[i] = name_cmdAndVals[i].Trim().ToLower();
                }
            }

            this.ParseCommandType(name_cmdAndVals, layer, errorPreMsg);
            if (this.type == ControlType.Script)
            {
                this.text = string.IsNullOrEmpty(layer.text) ? "" : layer.text.Trim();
            }
            else
            {
                this.fullName = this.name +
                                (string.IsNullOrEmpty(this.command) ? "" : '@' + this.command);

                var comment = this.ParseComment(name_cmdAndVals);
                if (!string.IsNullOrEmpty(comment))
                {
                    this.name = this.name + "(" + comment + ")";
                }

                this.area = new PsdLayerRect(
                    layer.psdLayer.area.left,
                    layer.psdLayer.area.top,
                    layer.psdLayer.area.width,
                    layer.psdLayer.area.height);

                this.hasBoxCollider = this.ParseCollider(name_cmdAndVals) == "box";
                this.sliceArea      = this.ParseSliceArea(name_cmdAndVals);
                this.color          = this.ParseColor(name_cmdAndVals);
                if (!string.IsNullOrEmpty(layer.text))
                {
                    this.text = layer.text.Trim();
                    var arr = this.text.Split('\n');
                    this.fontSize = Mathf.FloorToInt(this.area.height / arr.Length * 0.92f);
                }
                else
                {
                    this.text = this.ParseText(name_cmdAndVals);
                }

                this.bold   = this.IsBold(name_cmdAndVals);
                this.shadow = this.IsShadow(name_cmdAndVals);
                this.align  = this.ParseAlign(name_cmdAndVals);

                this.fps = this.ParseFps(name_cmdAndVals);
            }
        }
Esempio n. 2
0
    public static void ExtractLayersLocal(ref List <PSDLayerData> dataLayers,
                                          ref PsdLayerExtractor extractor,
                                          ref FileStream psdFileStream,
                                          PsdLayerExtractor.Layer layer,
                                          string prePath)
    {
        //exit if we can't parse the layer
        if (!layer.canLoadLayer)
        {
            return;
        }

        //recurse if the layer is a container
        if (layer.isContainer)
        {
            foreach (var l in layer.children)
            {
                ExtractLayersLocal(ref dataLayers,
                                   ref extractor,
                                   ref psdFileStream,
                                   l,
                                   prePath);
            }
        }
        else
        {
            //call the appropriate handler based on layer type
            var pa = new PsdLayerCommandParser.ControlParser(prePath, layer);
            switch (pa.type)
            {
            case PsdLayerCommandParser.ControlType.Label:
                HandleTextLayer(
                    ref dataLayers,
                    ref extractor,
                    ref psdFileStream,
                    layer,
                    prePath,
                    pa);
                break;

            case PsdLayerCommandParser.ControlType.Sprite:
                HandleImageLayer(
                    ref dataLayers,
                    ref extractor,
                    ref psdFileStream,
                    layer,
                    prePath,
                    pa);
                break;
            }
        }
    }
Esempio n. 3
0
    public static void HandleTextLayer(
        ref List <PSDLayerData> dataLayers,
        ref PsdLayerExtractor extractor,
        ref FileStream psdFileStream,
        PsdLayerExtractor.Layer layer,
        string prePath,
        PsdLayerCommandParser.ControlParser pa)
    {
        // track the dims and imported asset filepath
        var layerData = new PSDLayerData();

        layerData.type       = 1;
        layerData.dimensions = new Vector4(pa.area.left, pa.area.top, pa.area.width, pa.area.height);
        layerData.text       = layer.text;
        layerData.name       = layer.name;

        //the data to the referenced list
        dataLayers.Add(layerData);
    }
Esempio n. 4
0
        public void ParseCommandType(string[] name_cmdAndVals,
                                     PsdLayerExtractor.Layer layer, string errorPreMsg)
        {
            this.command = "";
            this.type    = ControlType.Sprite;

            for (var n = 1; n < name_cmdAndVals.Length; ++n)
            {
                this.command = name_cmdAndVals[n];

                if (this.command == "script")
                {
                    this.type = ControlType.Script;
                }

                else if (this.command == "panel")
                {
                    this.type = ControlType.Panel;
                }

                else if (this.command == "scrollview" || this.command.StartsWith("scrollview."))
                {
                    this.type = ControlType.ScrollView;
                }

                else if (this.command == "virtualview" || this.command.StartsWith("virtualview."))
                {
                    this.type = ControlType.VirtualView;
                }

                else if (this.command == "button" || this.command.StartsWith("button."))
                {
                    if (layer.isTextLayer && this.command != "button.label")
                    {
                        this.type = ControlType.LabelButton;
                    }
                    else
                    {
                        this.type = ControlType.Button;
                    }
                }

                else if (this.command == "checkbox" || this.command.StartsWith("checkbox.") ||
                         this.command == "toggle" || this.command.StartsWith("toggle."))
                {
                    this.type = ControlType.Toggle;
                }

                else if (this.command == "combobox" || this.command.StartsWith("combobox."))
                {
                    this.type = ControlType.ComboBox;
                }

                else if (this.command == "input" || this.command.StartsWith("input.") ||
                         this.command == "editbox" || this.command.StartsWith("editbox."))
                {
                    this.type = ControlType.Input;
                }

                else if (this.command == "password" || this.command.StartsWith("password"))
                {
                    this.type = ControlType.Password;
                }

                else if (this.command == "vscrollbar" || this.command.StartsWith("vscrollbar."))
                {
                    this.type = ControlType.VScrollBar;
                }

                else if (this.command == "hscrollbar" || this.command.StartsWith("hscrollbar."))
                {
                    this.type = ControlType.HScrollBar;
                }

                else if (this.command == "spritefont")
                {
                    this.type = ControlType.SpriteFont;
                }

                else if (this.command == "animation" || this.command == "ani")
                {
                    this.type = ControlType.SpriteAnimation;
                }

                else if (this.command == "texture")
                {
                    this.type = ControlType.Texture;
                }

                if (this.type != ControlType.Sprite)
                {
                    return;                     // type setted
                }
            }

            if (layer.isContainer)
            {
                this.type = ControlType.Container;
            }
            else if (layer.isTextLayer)
            {
                this.type = ControlType.Label;
            }
            else
            {
                if (!string.IsNullOrEmpty(this.command) &&
                    !this.command.StartsWith("comment") &&
                    !this.command.StartsWith("color") &&
                    !this.command.StartsWith("text") &&
                    !this.command.StartsWith("bold") &&
                    !this.command.StartsWith("shadow") &&
                    !this.command.StartsWith("collider") &&
                    !this.command.StartsWith("slice") &&
                    !this.command.StartsWith("align") &&
                    !this.command.StartsWith("fps") &&
                    !this.command.StartsWith("ignore"))
                {
                    Debug.LogError(errorPreMsg + "'" + this.command + "'" + " is wrong command or attribute");
                }
                this.command = "sprite";
                this.type    = ControlType.Sprite;
            }
        }
Esempio n. 5
0
    public static void HandleImageLayer(
        ref List <PSDLayerData> dataLayers,
        ref PsdLayerExtractor extractor,
        ref FileStream psdFileStream,
        PsdLayerExtractor.Layer layer,
        string prePath,
        PsdLayerCommandParser.ControlParser pa)
    {
        var fileName = pa.fullName;

        //exit if file name is bad
        if (extractor.HasUnacceptibleChar(fileName))
        {
            Debug.LogError(fileName + " Contains wrong character '\\ / : * ? \" < > |' not allowed");
            return;
        }

        //read the image from the file stream
        var filePath = prePath + "/" + fileName + ".png";

        PsdLayerExtractor.ImageFilePath newImageFilePath = null;
        {
            try
            {
                psdFileStream.Position = 0;
                var br = new BinaryReader(psdFileStream);
                {
                    layer.LoadData(br, extractor.Psd.headerInfo.bpp);
                    newImageFilePath = new PsdLayerExtractor.ImageFilePath(filePath, "pass");
                }
            }
            catch (System.Exception e)
            {
                Debug.LogError(e.Message);
            }
        }

        //merge channels and check if successful
        var data = layer.psdLayer.mergeChannels();

        if (data == null)
        {
            return;
        }

        //create the texture
        Texture tex = null;

        if (pa.sliceArea != null)
        {
            tex = extractor.MakeSlicedSprites(ref data, layer, pa.sliceArea);
        }
        else
        {
            tex = extractor.MakeTexture(ref data, layer);
        }

        if (tex != null)
        {
            if (!System.IO.Directory.Exists(prePath))
            {
                System.IO.Directory.CreateDirectory(prePath);
            }

            //import the texture to the asset database
            System.IO.File.WriteAllBytes(filePath, data);
            AssetDatabase.Refresh();
            TextureImporter importer = AssetImporter.GetAtPath(filePath) as TextureImporter;
            ProcessTexture2D(importer, ResolutionOptions.NEXT_SMALLEST, true, false, Platform.iPhone_Android);

            // track the dims and imported asset filepath
            var layerData = new PSDLayerData();
            layerData.type         = 0;
            layerData.dimensions   = new Vector4(pa.area.left, pa.area.top, pa.area.width, pa.area.height);
            layerData.texture_path = filePath;
            layerData.name         = layer.name;

            //the data to the referenced list
            dataLayers.Add(layerData);

            //destroy the temp texture
            Texture2D.DestroyImmediate(tex);
        }
    }