Beispiel #1
0
        private void DoPivotGUI()
        {
            EditorGUI.BeginChangeCheck();
            int alignment = s_Setting.spriteAlignment;

            alignment = EditorGUILayout.Popup(s_Styles.pivotLabel, alignment, s_Styles.spriteAlignmentOptions);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RegisterCompleteObjectUndo(s_Setting, "Change Alignment");
                s_Setting.spriteAlignment = alignment;
                s_Setting.pivot           = SpriteEditorUtility.GetPivotValue((SpriteAlignment)alignment, s_Setting.pivot);
            }

            Vector2 pivot = s_Setting.pivot;

            EditorGUI.BeginChangeCheck();
            using (new EditorGUI.DisabledScope(alignment != (int)SpriteAlignment.Custom))
            {
                pivot = EditorGUILayout.Vector2Field(s_Styles.customPivotLabel, pivot);
            }
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RegisterCompleteObjectUndo(s_Setting, "Change custom pivot");

                s_Setting.pivot = pivot;
            }
        }
 private void DrawGizmos()
 {
     if (eventSystem.current.type != EventType.Repaint)
     {
         return;
     }
     for (int i = 0; i < spriteCount; i++)
     {
         List <Vector2[]> outline = GetSpriteOutlineAt(i);
         Vector2          offset  = GetSpriteRectAt(i).size * 0.5f;
         if (outline.Count > 0)
         {
             SpriteEditorUtility.BeginLines(new Color(0.75f, 0.75f, 0.75f, 0.75f));
             for (int j = 0; j < outline.Count; ++j)
             {
                 for (int k = 0, last = outline[j].Length - 1; k < outline[j].Length; last = k, ++k)
                 {
                     SpriteEditorUtility.DrawLine(outline[j][last] + offset, outline[j][k] + offset);
                 }
             }
             SpriteEditorUtility.EndLines();
         }
     }
     DrawSpriteRectGizmos();
 }
 public void SetSpritePivotAndAlignment(Vector2 pivot, SpriteAlignment alignment)
 {
     undoSystem.RegisterCompleteObjectUndo(m_RectsCache, "Change Sprite Pivot");
     spriteEditor.SetDataModified();
     selected.alignment = alignment;
     selected.pivot     = SpriteEditorUtility.GetPivotValue(alignment, pivot);
 }
 internal SpriteDataExt(SerializedProperty sp)
 {
     rect               = sp.FindPropertyRelative("m_Rect").rectValue;
     border             = sp.FindPropertyRelative("m_Border").vector4Value;
     name               = sp.FindPropertyRelative("m_Name").stringValue;
     alignment          = (SpriteAlignment)sp.FindPropertyRelative("m_Alignment").intValue;
     pivot              = SpriteEditorUtility.GetPivotValue(alignment, sp.FindPropertyRelative("m_Pivot").vector2Value);
     tessellationDetail = sp.FindPropertyRelative("m_TessellationDetail").floatValue;
     spriteID           = new GUID(sp.FindPropertyRelative("m_SpriteID").stringValue);
     internalID         = sp.FindPropertyRelative("m_InternalID").longValue;
 }
 private void DrawGizmos()
 {
     if (eventSystem.current.type == EventType.Repaint)
     {
         var selected = spriteEditorWindow.selectedSpriteRect;
         if (selected != null)
         {
             SpriteEditorUtility.BeginLines(styles.spriteBorderColor);
             SpriteEditorUtility.DrawBox(selected.rect);
             SpriteEditorUtility.EndLines();
         }
     }
 }
Beispiel #6
0
        static private Rect GetCurrentRect(bool screenSpace, Rect clampArea, Vector2 startPoint, Vector2 endPoint)
        {
            Rect r = EditorGUIExt.FromToRect(Handles.inverseMatrix.MultiplyPoint(startPoint), Handles.inverseMatrix.MultiplyPoint(endPoint));

            r = SpriteEditorUtility.ClampedRect(SpriteEditorUtility.RoundToInt(r), clampArea, false);

            if (screenSpace)
            {
                Vector2 topleft     = Handles.matrix.MultiplyPoint(new Vector2(r.xMin, r.yMin));
                Vector2 bottomright = Handles.matrix.MultiplyPoint(new Vector2(r.xMax, r.yMax));

                r = new Rect(topleft.x, topleft.y, bottomright.x - topleft.x, bottomright.y - topleft.y);
            }
            return(r);
        }
        protected void DrawTexturespaceBackground()
        {
            float   size   = Mathf.Max(maxRect.width, maxRect.height);
            Vector2 offset = new Vector2(maxRect.xMin, maxRect.yMin);

            float halfSize = size * .5f;
            float alpha    = EditorGUIUtility.isProSkin ? 0.15f : 0.08f;
            float gridSize = 8f;

            SpriteEditorUtility.BeginLines(new Color(0f, 0f, 0f, alpha));
            for (float v = 0; v <= size; v += gridSize)
            {
                SpriteEditorUtility.DrawLine(new Vector2(-halfSize + v, halfSize + v) + offset, new Vector2(halfSize + v, -halfSize + v) + offset);
            }
            SpriteEditorUtility.EndLines();
        }
        private void HandleDragging()
        {
            if (hasSelected && !MouseOnTopOfInspector())
            {
                Rect textureBounds = new Rect(0, 0, textureActualWidth, textureActualHeight);
                EditorGUI.BeginChangeCheck();

                Rect oldRect = selectedSpriteRect;
                Rect newRect = SpriteEditorUtility.ClampedRect(SpriteEditorUtility.RoundedRect(SpriteEditorHandles.SliderRect(oldRect)), textureBounds, true);

                if (EditorGUI.EndChangeCheck())
                {
                    selectedSpriteRect = newRect;
                }
            }
        }
        protected void DrawRectGizmos(IEnumerable <Rect> rects, Color color)
        {
            if (eventSystem.current.type != EventType.Repaint)
            {
                return;
            }

            SpriteEditorUtility.BeginLines(color);
            foreach (var rect in rects)
            {
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMin), new Vector3(rect.xMin, rect.yMax));
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMax, rect.yMin), new Vector3(rect.xMax, rect.yMax));

                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMin), new Vector3(rect.xMax, rect.yMin));
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMax), new Vector3(rect.xMax, rect.yMax));
            }
            SpriteEditorUtility.EndLines();
        }
        protected void DrawSpriteRectGizmos()
        {
            if (eventSystem.current.type != EventType.Repaint)
            {
                return;
            }

            SpriteEditorUtility.BeginLines(new Color(0f, 1f, 0f, 0.7f));
            int currentSelectedSpriteIndex = CurrentSelectedSpriteIndex();

            for (int i = 0; i < spriteCount; i++)
            {
                Vector4 border = GetSpriteBorderAt(i);
                if (currentSelectedSpriteIndex != i && m_GizmoMode != GizmoMode.BorderEditing)
                {
                    if (Mathf.Approximately(border.sqrMagnitude, 0))
                    {
                        continue;
                    }
                }


                var rect = GetSpriteRectAt(i);
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin + border.x, rect.yMin), new Vector3(rect.xMin + border.x, rect.yMax));
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMax - border.z, rect.yMin), new Vector3(rect.xMax - border.z, rect.yMax));

                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMin + border.y), new Vector3(rect.xMax, rect.yMin + border.y));
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMax - border.w), new Vector3(rect.xMax, rect.yMax - border.w));
            }
            SpriteEditorUtility.EndLines();

            if (ShouldShowRectScaling())
            {
                Rect r = selectedSpriteRect;
                SpriteEditorUtility.BeginLines(new Color(0f, 0.1f, 0.3f, 0.25f));
                SpriteEditorUtility.DrawBox(new Rect(r.xMin + 1f / m_Zoom, r.yMin + 1f / m_Zoom, r.width, r.height));
                SpriteEditorUtility.EndLines();
                SpriteEditorUtility.BeginLines(new Color(0.25f, 0.5f, 1f, 0.75f));
                SpriteEditorUtility.DrawBox(r);
                SpriteEditorUtility.EndLines();
            }
        }
        protected void DrawSpriteRectGizmos()
        {
            if (eventSystem.current.type != EventType.Repaint)
            {
                return;
            }

            SpriteEditorUtility.BeginLines(new Color(0f, 1f, 0f, 0.7f));
            var selectedGUID = selected != null ? selected.spriteID : new GUID();

            for (int i = 0; i < spriteCount; i++)
            {
                Vector4 border = GetSpriteBorderAt(i);
                if (m_GizmoMode != GizmoMode.BorderEditing && (m_RectsCache != null && m_RectsCache.spriteRects[i].spriteID != selectedGUID))
                {
                    // border does not contain negative values
                    if (border.sqrMagnitude < Mathf.Epsilon * 8)
                    {
                        continue;
                    }
                }

                var rect = GetSpriteRectAt(i);
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin + border.x, rect.yMin), new Vector3(rect.xMin + border.x, rect.yMax));
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMax - border.z, rect.yMin), new Vector3(rect.xMax - border.z, rect.yMax));

                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMin + border.y), new Vector3(rect.xMax, rect.yMin + border.y));
                SpriteEditorUtility.DrawLine(new Vector3(rect.xMin, rect.yMax - border.w), new Vector3(rect.xMax, rect.yMax - border.w));
            }
            SpriteEditorUtility.EndLines();

            if (ShouldShowRectScaling())
            {
                Rect r = selectedSpriteRect;
                SpriteEditorUtility.BeginLines(new Color(0f, 0.1f, 0.3f, 0.25f));
                SpriteEditorUtility.DrawBox(new Rect(r.xMin + 1f / m_Zoom, r.yMin + 1f / m_Zoom, r.width, r.height));
                SpriteEditorUtility.EndLines();
                SpriteEditorUtility.BeginLines(new Color(0.25f, 0.5f, 1f, 0.75f));
                SpriteEditorUtility.DrawBox(r);
                SpriteEditorUtility.EndLines();
            }
        }
Beispiel #12
0
        protected override void DoTextureGUIExtras()
        {
            HandleFrameSelected();

            if (m_EventSystem.current.type == EventType.Repaint)
            {
                SpriteEditorUtility.BeginLines(new Color(1f, 1f, 1f, 0.5f));
                var selectedRect = selectedSpriteRect != null ? selectedSpriteRect.spriteID : new GUID();
                for (int i = 0; i < m_RectsCache.Count; i++)
                {
                    if (m_RectsCache[i].spriteID != selectedRect)
                    {
                        SpriteEditorUtility.DrawBox(m_RectsCache[i].rect);
                    }
                }
                SpriteEditorUtility.EndLines();
            }

            m_CurrentModule.DoMainGUI();
        }
Beispiel #13
0
        internal SpriteDataExt(SerializedObject so)
        {
            var ti = so.targetObject as TextureImporter;

            name               = Path.GetFileNameWithoutExtension(ti.assetPath);
            alignment          = (SpriteAlignment)so.FindProperty("m_Alignment").intValue;
            border             = ti.spriteBorder;
            pivot              = SpriteEditorUtility.GetPivotValue(alignment, ti.spritePivot);
            tessellationDetail = so.FindProperty("m_SpriteTessellationDetail").floatValue;

            int width = 0, height = 0;

            ti.GetWidthAndHeight(ref width, ref height);
            rect = new Rect(0, 0, width, height);

            var guidSP = so.FindProperty("m_SpriteSheet.m_SpriteID");

            spriteID = new GUID(guidSP.stringValue);

            internalID = so.FindProperty("m_SpriteSheet.m_InternalID").longValue;
        }
        protected static Rect ClampSpriteRect(Rect rect, float maxX, float maxY)
        {
            // Clamp rect to width height
            rect = FlipNegativeRect(rect);
            Rect newRect = new Rect();

            newRect.xMin = Mathf.Clamp(rect.xMin, 0, maxX - 1);
            newRect.yMin = Mathf.Clamp(rect.yMin, 0, maxY - 1);
            newRect.xMax = Mathf.Clamp(rect.xMax, 1, maxX);
            newRect.yMax = Mathf.Clamp(rect.yMax, 1, maxY);

            // Prevent width and height to be 0 value after clamping.
            if (Mathf.RoundToInt(newRect.width) == 0)
            {
                newRect.width = 1;
            }
            if (Mathf.RoundToInt(newRect.height) == 0)
            {
                newRect.height = 1;
            }

            return(SpriteEditorUtility.RoundedRect(newRect));
        }
Beispiel #15
0
        static internal Rect RectCreator(Rect textureArea, GUIStyle rectStyle)
        {
            var     evt      = UnityEvent.current;
            Vector2 mousePos = evt.mousePosition;
            int     id       = s_RectSelectionID;
            Rect    result   = new Rect();

            switch (evt.GetTypeForControl(id))
            {
            case EventType.MouseDown:
                if (evt.button == 0)
                {
                    GUIUtility.hotControl = id;

                    // Make sure that the starting position is clamped to inside texture area
                    Vector2 point = Handles.inverseMatrix.MultiplyPoint(mousePos);

                    point.x = Mathf.Min(Mathf.Max(point.x, textureArea.xMin), textureArea.xMax);
                    point.y = Mathf.Min(Mathf.Max(point.y, textureArea.yMin), textureArea.yMax);

                    // Save clamped starting position for later use
                    s_DragStartScreenPosition = Handles.matrix.MultiplyPoint(point);

                    // Actual position
                    s_CurrentMousePosition = mousePos;

                    evt.Use();
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    s_CurrentMousePosition = new Vector2(mousePos.x, mousePos.y);
                    evt.Use();
                }
                break;

            case EventType.Repaint:
                if (GUIUtility.hotControl == id && ValidRect(s_DragStartScreenPosition, s_CurrentMousePosition))
                {
                    // TODO: use rectstyle
                    //rectStyle.Draw (GetCurrentRect (true, textureWidth, textureHeight, s_DragStartScreenPosition, s_CurrentMousePosition), GUIContent.none, false, false, false, false);
                    SpriteEditorUtility.BeginLines(Color.green * 1.5f);
                    SpriteEditorUtility.DrawBox(GetCurrentRect(false, textureArea, s_DragStartScreenPosition, s_CurrentMousePosition));
                    SpriteEditorUtility.EndLines();
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == id && evt.button == 0)
                {
                    if (ValidRect(s_DragStartScreenPosition, s_CurrentMousePosition))
                    {
                        result      = GetCurrentRect(false, textureArea, s_DragStartScreenPosition, s_CurrentMousePosition);
                        GUI.changed = true;
                        evt.Use();
                    }

                    GUIUtility.hotControl = 0;
                }
                break;

            case EventType.KeyDown:
                if (GUIUtility.hotControl == id)
                {
                    if (evt.keyCode == KeyCode.Escape)
                    {
                        GUIUtility.hotControl = 0;
                        GUI.changed           = true;
                        evt.Use();
                    }
                }
                break;
            }
            return(result);
        }