Esempio n. 1
0
 private void RecursiveQuickSelect(ImportLayerData layer, bool inSelection)
 {
     SetQuickSelect(layer.indexId, inSelection);
     layer.Iterate(
         childLayer =>
     {
         if (layer.Childs == null)
         {
             return;
         }
         if (childLayer.import)
         {
             SetQuickSelect(childLayer.indexId, inSelection);
         }
     }
         );
     GetSelectCount();
 }
Esempio n. 2
0
        /// <summary>
        /// Resolve setting differences from stored data and built data
        /// </summary>
        /// <param name="storedData"></param>
        /// <param name="builtData"></param>
        /// <returns></returns>
        private ImportLayerData ResolveData(ImportLayerData storedData, ImportLayerData builtData)
        {
            // Nothing was stored, used built data
            if (storedData == null)
            {
                return(builtData);
            }

            // Flatten out stored to a dictionary, using the path as keys
            Dictionary <string, ImportLayerData> storedIndex = new Dictionary <string, ImportLayerData>();

            storedData.Iterate(
                layerCallback: layer =>
            {
                if (storedIndex.ContainsKey(layer.path) == false)
                {
                    storedIndex.Add(layer.path, layer);
                }
                else
                {
                    storedIndex[layer.path] = layer;
                }
            }
                );

            // Iterate through the built data now, checking for settings from storedIndex
            builtData.Iterate(
                layerCallback: layer =>
            {
                ImportLayerData existingSettings;
                if (storedIndex.TryGetValue(layer.path, out existingSettings))
                {
                    layer.useDefaults = existingSettings.useDefaults;
                    layer.Alignment   = existingSettings.Alignment;
                    layer.Pivot       = existingSettings.Pivot;
                    layer.ScaleFactor = existingSettings.ScaleFactor;
                    layer.import      = existingSettings.import;
                }
            }
                );

            return(builtData);
        }
Esempio n. 3
0
        private static ReconstructData GetReconstructData(PsdDocument psdDoc, string psdPath, Vector2 documentPivot,
                                                          ImportUserData importSettings, ImportLayerData reconstructRoot)
        {
            // Get the texture import setting of the PSD
            TextureImporter         psdUnitySettings = (TextureImporter)AssetImporter.GetAtPath(psdPath);
            TextureImporterSettings psdUnityImport   = new TextureImporterSettings();

            psdUnitySettings.ReadTextureSettings(psdUnityImport);

            Vector2         docSize = new Vector2(psdDoc.Width, psdDoc.Height);
            ReconstructData data    = new ReconstructData(docSize, documentPivot, psdUnitySettings.spritePixelsPerUnit);

            reconstructRoot.Iterate(
                layerCallback: layer =>
            {
                if (layer.import == false)
                {
                    return;
                }

                var psdLayer = GetPsdLayerByIndex(psdDoc, layer.indexId);

                Rect layerBounds = new Rect()
                {
                    xMin = psdLayer.Left,
                    xMax = psdLayer.Right,
                    yMin = psdDoc.Height - psdLayer.Bottom,
                    yMax = psdDoc.Height - psdLayer.Top
                };
                data.layerBoundsIndex.Add(layer.indexId, layerBounds);

                string layerDir;
                string layerPath = GetFilePath(psdLayer, importSettings, "png", out layerDir);

                string layerMeshDir;
                string layerMeshPath = GetFilePath(psdLayer, importSettings, "asset", out layerMeshDir);

                Sprite layerSprite         = AssetDatabase.LoadAssetAtPath <Sprite>(layerPath);
                SpriteMesh layerSpriteMesh = AssetDatabase.LoadAssetAtPath <SpriteMesh>(layerMeshPath);

                if (layerSprite == null)
                {
                    layerSprite = ImportLayer(psdDoc, importSettings, layer, psdUnityImport);
                }

                if (layerSpriteMesh == null && importSettings.GenerateSpriteMesh)
                {
                    layerSpriteMesh = GenerateSpriteMesh(layerSprite);
                }

                Vector2 spriteAnchor = Vector2.zero;

                if (layerSprite != null)
                {
                    TextureImporter layerImporter         = (TextureImporter)AssetImporter.GetAtPath(layerPath);
                    TextureImporterSettings layerSettings = new TextureImporterSettings();
                    layerImporter.ReadTextureSettings(layerSettings);

                    if (layerSettings.spriteAlignment == (int)SpriteAlignment.Custom)
                    {
                        spriteAnchor = layerSettings.spritePivot;
                    }
                    else
                    {
                        spriteAnchor = AlignmentToPivot((SpriteAlignment)layerSettings.spriteAlignment);
                    }
                }

                data.AddSprite(layer.indexId, layerSprite, spriteAnchor);

                if (importSettings.GenerateSpriteMesh)
                {
                    data.AddSpriteMesh(layer.indexId, layerSpriteMesh);
                }
            },
                canEnterGroup: checkGroup => checkGroup.import
                );

            return(data);
        }
Esempio n. 4
0
        private void DrawIterateLayers(ImportLayerData layerData)
        {
            layerRectLookup.Clear();
            layerEntryYMax = 0;

            layerData.Iterate(
                layerCallback: layer =>
            {
                var display = GetDisplayData(layer.indexId);
                if (display == null)
                {
                    return;
                }

                if (string.IsNullOrEmpty(searchFilter) == false)
                {
                    if (layer.name.ToLower().Contains(searchFilter.ToLower()) == false)
                    {
                        return;
                    }
                }

                DrawLayerEntry(layer, display);
            },
                canEnterGroup: layer =>
            {
                var display = GetDisplayData(layer.indexId);
                if (display == null)
                {
                    return(true);
                }
                if (string.IsNullOrEmpty(searchFilter) == false)
                {
                    return(true);
                }
                return(display.isOpen);
            },
                enterGroupCallback: data => indentLevel++,
                exitGroupCallback: data => indentLevel--
                );

            // Process mouse events

            var  evt = Event.current;
            bool willChangeSelection = false;

            int[] targetLayer = null;
            foreach (var kvp in layerRectLookup)
            {
                if (kvp.Value.Contains(evt.mousePosition))
                {
                    var checkLayer = GetLayerData(kvp.Key);
                    if (checkLayer == null || checkLayer.Childs == null)
                    {
                        continue;
                    }

                    bool layerIsGroup = checkLayer.Childs.Count > 0;

                    if (evt.type == EventType.MouseDown)
                    {
                        willChangeSelection = layerIsGroup == false;
                        bool willAddSelect = quickSelect.Contains(kvp.Key) == false;
                        if (willAddSelect)
                        {
                            lastSelectedLayer = kvp.Key;
                        }

                        if (willChangeSelection)
                        {
                            isChangingSelection  = true;
                            targetLayer          = kvp.Key;
                            selectionChangeState = willAddSelect;
                        }
                        else
                        {
                            RecursiveQuickSelect(checkLayer, willAddSelect);
                            if (willAddSelect == false)
                            {
                                lastSelectedLayer = null;
                            }
                            Repaint();
                        }
                        evt.Use();
                    }

                    if (evt.type == EventType.MouseUp)
                    {
                        isChangingSelection = false;
                    }

                    if (isChangingSelection && evt.type == EventType.MouseDrag && layerIsGroup == false)
                    {
                        willChangeSelection = true;
                        targetLayer         = kvp.Key;
                    }
                    break;
                }
            }

            if (willChangeSelection && targetLayer != null)
            {
                SetQuickSelect(targetLayer, selectionChangeState);
                GetSelectCount();
                Repaint();
            }
        }