Exemplo n.º 1
0
 public override void OnPreviewGUI(Rect rect, GUIStyle background)
 {
     base.OnPreviewGUI(rect, background);
     if (mSelectedSprites.Count == 1)
     {
         var atlas      = target as AtlasRaw;
         var bin        = atlas.bins[mSelectedSprites[0].bin];
         var sprite     = bin.sprites[mSelectedSprites[0].sprite];
         var borderRect = AtlasEditorUtil.DrawSpriteInRect(bin.main, bin.addition, sprite, rect, new Vector2(100, 100));
         if (mShowBorder)
         {
             OnSpriteBorderGUI(rect, borderRect, sprite);
         }
         var titleRect = new Rect(rect.x, rect.y, rect.width, 20);
         OnPreivewTitleGUI(titleRect, sprite);
         OnPreviewInfoGUI(rect, sprite);
         var controlID = GUIUtility.GetControlID(FocusType.Passive);
         var eventType = Event.current.GetTypeForControl(controlID);
         if (eventType == EventType.MouseDown &&
             rect.Contains(Event.current.mousePosition) &&
             !titleRect.Contains(Event.current.mousePosition))
         {
             GUI.FocusControl("");
             Event.current.Use();
         }
     }
     else if (mSelectedSprites.Count >= 2)
     {
         OnPreviewSpritesGUI(rect, mSelectedSprites);
     }
 }
Exemplo n.º 2
0
            public void Match(string words, out SpriteIndex[] sprites, out int[] atlases)
            {
                if (mDirty)
                {
                    mDirty = false;
                    Refresh();
                }
                var rets = new List <SpriteIndex>();

                for (int i = 0; i < mAtlases.Count; i++)
                {
                    var atlas = mAtlases[i];
                    rets.AddRange(AtlasEditorUtil.SearchSprites(atlas, i, words));
                }
                sprites = rets.ToArray();
                atlases = (from sprite in sprites
                           group sprite by sprite.atlas into spriteGroup
                           select spriteGroup.Key).ToArray();
            }
Exemplo n.º 3
0
        private void OnPreviewSpritesGUI(Rect rect, List <SpriteIndex> sprites)
        {
            var atlas   = target as AtlasRaw;
            var margin  = new Vector2(60, 60);
            var size    = 100;
            var space   = 20;
            var columns = Mathf.FloorToInt((rect.width + space - margin.x) / (size + space));

            columns = Mathf.Clamp(columns, 1, sprites.Count);
            var rows = Mathf.FloorToInt((rect.height + space - margin.y) / (size + space));

            rows = Mathf.Clamp(rows, 1, Mathf.CeilToInt((float)sprites.Count / columns));
            var width  = (rect.width - margin.x - (columns - 1) * space) / columns;
            var height = (rect.height - margin.y - (rows - 1) * space) / rows;

            for (int row = 0; row < rows; row++)
            {
                for (int column = 0; column < columns; column++)
                {
                    int index = row * columns + column;
                    if (index < sprites.Count)
                    {
                        var bin    = atlas.bins[sprites[index].bin];
                        var sprite = bin.sprites[sprites[index].sprite];
                        AtlasEditorUtil.DrawSpriteInRect(bin.main, bin.addition, sprite,
                                                         new Rect(rect.x + margin.x * 0.5f + (width + space) * column,
                                                                  rect.y + margin.y * 0.5f + (height + space) * row,
                                                                  width,
                                                                  height),
                                                         new Vector2(0, 0));
                    }
                }
            }
            var bottomInfo     = new GUIContent(string.Format("Previewing {0} of {1} objects", Mathf.Min(rows * columns, sprites.Count), sprites.Count));
            var bottomInfoSize = EditorStyles.boldLabel.CalcSize(bottomInfo);

            EditorGUI.LabelField(new Rect(rect.x + rect.width * 0.5f - bottomInfoSize.x * 0.5f, rect.yMax, bottomInfoSize.x, bottomInfoSize.y), bottomInfo, EditorStyles.boldLabel);
        }
Exemplo n.º 4
0
        private void DrawAtlasList()
        {
            var rect      = new Rect(0, 20, mVSplit - 5, position.height - 20);
            var titleRect = new Rect(rect.x, rect.y, rect.width + 5, EditorStyles.toolbar.fixedHeight);

            AtlasEditorUtil.TitleBar(titleRect, new GUIContent("Atlas"));
            var itemHeight  = 20;
            var itemSpace   = 2;
            var viewRect    = new Rect(0, 0, titleRect.width, position.height - titleRect.y - titleRect.height);
            var contentRect = new Rect(rect.x, titleRect.y + titleRect.height, viewRect.width, Mathf.Max((itemHeight + itemSpace) * mAtlases.Length, viewRect.height));

            mAtlasScrPos = GUI.BeginScrollView(contentRect, mAtlasScrPos, viewRect);
            var controlId = GUIUtility.GetControlID(FocusType.Passive);
            var eventType = Event.current.GetTypeForControl(controlId);
            var index     = 0;

            foreach (var atlasIndex in mAtlases)
            {
                AtlasRaw atlas;
                if (mCache.Fetch(atlasIndex, out atlas))
                {
                    var itemRect = new Rect(0, index * (itemHeight + itemSpace), contentRect.width, itemHeight);
                    EditorGUI.DrawRect(itemRect, mSelectedAtlas == atlasIndex ? ColorSelected : ColorAtlasItem);
                    EditorGUI.LabelField(new Rect(itemRect.x + 5, itemRect.y + 2, itemRect.width, itemRect.height), new GUIContent(atlas.name));
                    var clickRect = new Rect(itemRect.x, itemRect.y, rect.width, itemHeight);
                    if (eventType == EventType.MouseDown &&
                        clickRect.Contains(Event.current.mousePosition))
                    {
                        FilterAtlas(atlasIndex);
                        Event.current.Use();
                    }
                    index += 1;
                }
            }
            GUI.EndScrollView();
        }
Exemplo n.º 5
0
        public void OnGUI(Rect rect, AtlasRaw atlas, BinRaw bin, List <SpriteIndex> selected)
        {
            GUI.BeginClip(rect);
            var localRect      = new Rect(0, 0, rect.width, rect.height);
            var texDragOffset  = mOffset;
            var texScale       = mScale;
            var texScaleOffset = new Vector2(
                localRect.width * (texScale - 1) * 0.5f,
                localRect.height * (texScale - 1) * 0.5f);
            var texOffset = texDragOffset - texScaleOffset;
            var texSize   = localRect.size * texScale;

            AtlasEditorUtil.DrawGrid(localRect);
            AtlasEditorUtil.DrawSprite(localRect, bin.main, bin.addition,
                                       new Rect(-texOffset.x / texSize.x, (texDragOffset.y + texScaleOffset.y) / texSize.y, localRect.width / texSize.x, localRect.height / texSize.y));
            int controlID = GUIUtility.GetControlID(FocusType.Passive);
            var eventType = Event.current.GetTypeForControl(controlID);

            switch (eventType)
            {
            case EventType.ScrollWheel:
            {
                if (localRect.Contains(Event.current.mousePosition))
                {
                    var scaleDelta = 15;
                    texScale      -= Event.current.delta.y / scaleDelta;
                    texScale       = Mathf.Max(texScale, 1);
                    texScaleOffset = new Vector2(localRect.width * (texScale - 1) * 0.5f,
                                                 localRect.height * (texScale - 1) * 0.5f);
                    texDragOffset.x = Mathf.Clamp(texDragOffset.x, -texScaleOffset.x, texScaleOffset.x);
                    texDragOffset.y = Mathf.Clamp(texDragOffset.y, -texScaleOffset.y, texScaleOffset.y);
                    Event.current.Use();
                }
                break;
            }

            case EventType.MouseDown:
            {
                var pos = GUIUtility.GetStateObject(typeof(List <int>), controlID) as List <int>;
                pos.Clear();
                pos.Add((int)Event.current.mousePosition.x);
                pos.Add((int)Event.current.mousePosition.y);
                GUIUtility.hotControl = controlID;
                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl == controlID)
                {
                    var pos = GUIUtility.QueryStateObject(typeof(List <int>), controlID) as List <int>;
                    if (pos[0] <= localRect.width && pos[1] <= localRect.height)
                    {
                        texDragOffset.x += Event.current.delta.x;
                        texDragOffset.y += Event.current.delta.y;
                        texDragOffset.x  = Mathf.Clamp(texDragOffset.x, -texScaleOffset.x, texScaleOffset.x);
                        texDragOffset.y  = Mathf.Clamp(texDragOffset.y, -texScaleOffset.y, texScaleOffset.y);
                        Event.current.Use();
                    }
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == controlID)
                {
                    var pos = GUIUtility.QueryStateObject(typeof(List <int>), controlID) as List <int>;
                    if (pos != null &&
                        Mathf.Abs(pos[0] - (int)Event.current.mousePosition.x) <= 1 &&
                        Mathf.Abs(pos[1] - (int)Event.current.mousePosition.y) <= 1)
                    {
                        var rPos = new Vector2(
                            (pos[0] - texOffset.x) / texSize.x,
                            (pos[1] - texOffset.y) / texSize.y
                            );
                        for (var i = 0; i < bin.sprites.Length; i++)
                        {
                            var sprite     = bin.sprites[i];
                            var spriteRect = sprite.rect;
                            var rRect      = new Rect(
                                spriteRect.x / bin.main.width,
                                (bin.main.height - spriteRect.y - spriteRect.height) / bin.main.height,
                                spriteRect.width / bin.main.width,
                                spriteRect.height / bin.main.height
                                );
                            if (rRect.Contains(rPos))
                            {
                                var index = new SpriteIndex(0, sprite.bin, i);
                                if (Event.current.button == 0)
                                {
                                    if (!mMultiSelectEnabled ||
                                        !Event.current.control)
                                    {
                                        selected.Clear();
                                        selected.Add(index);
                                    }
                                    else
                                    {
                                        if (selected.Any(s => s.bin == index.bin && s.sprite == index.sprite))
                                        {
                                            selected.RemoveAll(s => s.bin == index.bin && s.sprite == index.sprite);
                                        }
                                        else
                                        {
                                            selected.Add(index);
                                        }
                                    }
                                    GUI.FocusControl("");
                                    Event.current.Use();
                                }
                                break;
                            }
                        }
                    }
                    GUIUtility.hotControl = 0;
                }
                break;
            }
            }
            foreach (var index in selected)
            {
                if (atlas.bins[index.bin] == bin)
                {
                    var sprite     = bin.sprites[index.sprite];
                    var spriteRect = sprite.rect;
                    var rRect      = new Rect(
                        spriteRect.x / bin.main.width,
                        (bin.main.height - spriteRect.y - spriteRect.height) / bin.main.height,
                        spriteRect.width / bin.main.width,
                        spriteRect.height / bin.main.height
                        );
                    var tRect = new Rect(
                        rRect.x * texSize.x + texOffset.x,
                        rRect.y * texSize.y + texOffset.y,
                        rRect.width * texSize.x,
                        rRect.height * texSize.y
                        );
                    Handles.color = Color.green;
                    Handles.DrawPolyLine(new Vector3[] {
                        new Vector3(tRect.x, tRect.y, 0),
                        new Vector3(tRect.xMax, tRect.y, 0),
                        new Vector3(tRect.xMax, tRect.yMax, 0),
                        new Vector3(tRect.x, tRect.yMax, 0),
                        new Vector3(tRect.x, tRect.y, 0)
                    });
                    Handles.color = Color.white;
                }
            }
            GUI.EndClip();
            mOffset = texDragOffset;
            mScale  = texScale;
        }
Exemplo n.º 6
0
        private void DrawSpriteList()
        {
            var rect      = new Rect(mVSplit + 5, 20, position.width - mVSplit - 5, position.height - 20);
            var titleRect = new Rect(rect.x - 5, rect.y, rect.width + 5, EditorStyles.toolbar.fixedHeight);

            AtlasEditorUtil.TitleBar(titleRect, new GUIContent("Sprite"));
            var padding     = new Vector2(20, 20);
            var itemSize    = new Vector2(100, 120);
            var itemSpace   = new Vector2(20, 20);
            var columns     = Mathf.Max(Mathf.FloorToInt((titleRect.width - padding.x) / (itemSize.x + itemSpace.x)), 1);
            var rows        = Mathf.CeilToInt((float)(mFilter.Length + 1) / columns);
            var viewRect    = new Rect(0, 0, titleRect.width, position.height - titleRect.y - titleRect.height);
            var contentRect = new Rect(rect.x, titleRect.y + titleRect.height, viewRect.width, padding.y + Mathf.Max((itemSize.y + itemSpace.y) * rows, viewRect.height));

            GUI.BeginGroup(contentRect);
            var controlId = GUIUtility.GetControlID(FocusType.Passive);
            var eventType = Event.current.GetTypeForControl(controlId);
            var index     = 0;
            var count     = Mathf.CeilToInt(viewRect.height / (itemSize.y + itemSpace.y)) * columns;

            count = Mathf.Min(mFilter.Length + 1, count);
            for (int i = 0; i < count; i++)
            {
                var clicked = false;
                if (i == 0)
                {
                    var previewRect = new Rect(padding.x, padding.y, itemSize.x, 100);
                    AtlasEditorUtil.DrawGrid(previewRect);
                    var selectedRect = new Rect(previewRect.x, previewRect.y + previewRect.height + 2, previewRect.width, 18);
                    if (selectedSprite == null)
                    {
                        EditorGUI.DrawRect(selectedRect, ColorSelected);
                    }
                    var labelRect = new Rect(selectedRect.x, selectedRect.y, selectedRect.width, selectedRect.height);
                    EditorGUI.LabelField(labelRect, "None", GetSpritItemLabelStyle());
                    var clickRect = new Rect(previewRect.x, previewRect.y, previewRect.width, itemSize.y);
                    if (eventType == EventType.MouseDown &&
                        clickRect.Contains(Event.current.mousePosition))
                    {
                        selectedSprite = null;
                        clicked        = true;
                        Event.current.Use();
                    }
                    index += 1;
                }
                else
                {
                    var       spriteIndex = mFilter[i - 1];
                    AtlasRaw  atlas;
                    BinRaw    bin;
                    SpriteRaw sprite;
                    if (mCache.Fetch(spriteIndex, out atlas, out bin, out sprite))
                    {
                        var row         = index / columns;
                        var column      = index % columns;
                        var previewRect = new Rect(
                            padding.x + column * (itemSize.x + itemSpace.x),
                            padding.y + row * (itemSize.y + itemSpace.y),
                            itemSize.x, 100);
                        AtlasEditorUtil.DrawSpriteInRect(bin.main, bin.addition, sprite, previewRect, Vector2.zero);
                        var selectedRect = new Rect(previewRect.x, previewRect.y + previewRect.height + 2, previewRect.width, 18);
                        if (spriteIndex.Equals(selectedSprite))
                        {
                            EditorGUI.DrawRect(selectedRect, ColorSelected);
                        }
                        var labelRect = new Rect(selectedRect.x, selectedRect.y, selectedRect.width, selectedRect.height);
                        EditorGUI.LabelField(labelRect, sprite.name, GetSpritItemLabelStyle());
                        var clickRect = new Rect(previewRect.x, previewRect.y, previewRect.width, itemSize.y);
                        if (eventType == EventType.MouseDown &&
                            clickRect.Contains(Event.current.mousePosition))
                        {
                            selectedSprite = new SpriteIndex(spriteIndex.atlas, spriteIndex.bin, spriteIndex.sprite);
                            clicked        = true;
                            Event.current.Use();
                        }
                        index += 1;
                    }
                }
                if (clicked && Event.current.clickCount == 2)
                {
                    MarkCloseWindow();
                }
            }
            var countLabel = new GUIContent((count - 1) + "/" + mFilter.Length);
            var countSize  = EditorStyles.miniLabel.CalcSize(countLabel);
            var countRect  = new Rect(viewRect.xMax - countSize.x - 10, viewRect.yMax - countSize.y, countSize.x, countSize.y);

            GUI.Label(countRect, countLabel, EditorStyles.miniLabel);
            GUI.EndGroup();
        }
Exemplo n.º 7
0
        public override void OnInspectorGUI()
        {
            OnValidateSelected();
            var atlas = target as AtlasRaw;

            EditorGUILayout.GetControlRect(false, 2);
            int clickIndex = AtlasEditorUtil.TitleBar(new GUIContent[]
            {
                new GUIContent("+File"),
                new GUIContent("+Folder"),
            });

            if (clickIndex == 0)
            {
                DisplayImportMenu(atlas, false);
            }
            else if (clickIndex == 1)
            {
                DisplayImportMenu(atlas, true);
            }
            mRepackFold = AtlasEditorUtil.ToggleBar(new GUIContent("Settings"), mRepackFold);
            if (mRepackFold)
            {
                if (!mPackDataInit)
                {
                    mPackDataInit = true;
                    mSetting      = new PackSetting(atlas.maxSize, atlas.padding, atlas.isPOT, atlas.forceSquare);
                }
                EditorGUI.indentLevel += 1;
                mSetting.maxAtlasSize  = EditorGUILayout.IntPopup("Max Size", mSetting.maxAtlasSize, Array.ConvertAll(PackConst.AtlasSizeList, value => value.ToString()), PackConst.AtlasSizeList);
                mSetting.padding       = EditorGUILayout.IntField("Padding", mSetting.padding);
                mSetting.isPOT         = EditorGUILayout.Toggle("Power Of 2", mSetting.isPOT);
                GUI.enabled            = mSetting.isPOT;
                if (!mSetting.isPOT)
                {
                    mSetting.forceSquare = false;
                }
                mSetting.forceSquare = EditorGUILayout.Toggle("Force Square", mSetting.forceSquare);
                GUI.enabled          = true;
                var rect = EditorGUILayout.GetControlRect(false, 20);
                if (GUI.Button(new Rect(rect.center.x - 75, rect.y, 150, rect.height), "Repack"))
                {
                    AtlasPacker.Repack(atlas, null, mSetting);
                }
                EditorGUI.indentLevel -= 1;
                EditorGUILayout.Space();
            }
            EditorGUI.BeginChangeCheck();
            mSearchPattern = AtlasEditorUtil.SearchBar(new GUIContent("Search Sprite"), mSearchPattern);
            if (EditorGUI.EndChangeCheck() || mAtlasDirty)
            {
                mSearchResults.Clear();
                if (!string.IsNullOrEmpty(mSearchPattern))
                {
                    mSearchResults.AddRange(AtlasEditorUtil.SearchSprites(atlas, 0, mSearchPattern));
                }
            }
            if (mAtlasDirty)
            {
                mAtlasDirty  = false;
                mSelectedBin = Mathf.Clamp(mSelectedBin, 0, atlas.bins.Length - 1);
                mSelectedSprites.Clear();
            }
            EditorGUI.indentLevel += 1;
            if (!string.IsNullOrEmpty(mSearchPattern))
            {
                EditorGUILayout.LabelField(string.Format("{0} results", mSearchResults.Count));
            }
            if (mSearchResults != null && mSearchResults.Count > 0)
            {
                OnValidateResult();
                OnSearchResultGUI();
            }
            EditorGUI.indentLevel -= 1;
            if (atlas.bins.Length > 0)
            {
                var titleRect = EditorGUILayout.GetControlRect(false, EditorStyles.toolbar.fixedHeight);
                var controlId = GUIUtility.GetControlID(FocusType.Passive);
                var eventType = Event.current.GetTypeForControl(controlId);
                if (eventType == EventType.Repaint)
                {
                    EditorStyles.toolbar.Draw(titleRect, GUIContent.none, controlId);
                }
                var binNames   = Array.ConvertAll(atlas.bins, i => PackUtil.GetDisplayName(i));
                var binIndexes = new int[binNames.Length];
                for (int i = 0; i < binNames.Length; i++)
                {
                    binIndexes[i] = i;
                }
                mSelectedBin = EditorGUI.IntPopup(new Rect(10, titleRect.y, titleRect.width - 10, titleRect.height), "Preview Bin", mSelectedBin, binNames, binIndexes, EditorStyles.toolbarPopup);
                mSelectedBin = Mathf.Clamp(mSelectedBin, 0, atlas.bins.Length - 1);
                EditorGUILayout.Space();
                var bin         = atlas.bins[mSelectedBin];
                var previewRect = EditorGUILayout.GetControlRect(false, 512);
                previewRect.width  = Mathf.Min(previewRect.width, (float)bin.main.width / bin.main.height * previewRect.height);
                previewRect.height = (float)bin.main.height / bin.main.width * previewRect.width;
                OnPreviewBin(previewRect);
            }
        }