Beispiel #1
0
    static void Load()
    {
        int l = LayerMask.NameToLayer("UI");

        if (l == -1)
        {
            l = LayerMask.NameToLayer("GUI");
        }
        if (l == -1)
        {
            l = 31;
        }

        mLoaded        = true;
        mPartial       = EditorPrefs.GetString("NGUI Partial");
        mFontName      = EditorPrefs.GetString("NGUI Font Name");
        mAtlasName     = EditorPrefs.GetString("NGUI Atlas Name");
        mFontData      = GetObject("NGUI Font Asset") as TextAsset;
        mFontTexture   = GetObject("NGUI Font Texture") as Texture2D;
        mFont          = GetObject("NGUI Font") as NGUIFont;
        mAtlas         = GetObject("NGUI Atlas") as NGUIAtlas;
        mAtlasPadding  = EditorPrefs.GetInt("NGUI Atlas Padding", 1);
        mAtlasTrimming = EditorPrefs.GetBool("NGUI Atlas Trimming", true);
        mUnityPacking  = EditorPrefs.GetBool("NGUI Unity Packing", true);
        mForceSquare   = EditorPrefs.GetBool("NGUI Force Square Atlas", true);
        mPivot         = (NGUIWidget.Pivot)EditorPrefs.GetInt("NGUI Pivot", (int)mPivot);
        mLayer         = EditorPrefs.GetInt("NGUI Layer", l);
        mDynFont       = GetObject("NGUI DynFont") as Font;
        mDynFontSize   = EditorPrefs.GetInt("NGUI DynFontSize", 16);
        mDynFontStyle  = (FontStyle)EditorPrefs.GetInt("NGUI DynFontStyle", (int)FontStyle.Normal);

        LoadColor();
    }
    /// <summary>
    /// Draw a toggle button for the pivot point.
    /// </summary>

    void Toggle(string text, string style, NGUIWidget.Pivot pivot, bool isHorizontal)
    {
        bool isActive = false;

        switch (pivot)
        {
        case NGUIWidget.Pivot.Left:
            isActive = IsLeft(mWidget.pivot);
            break;

        case NGUIWidget.Pivot.Right:
            isActive = IsRight(mWidget.pivot);
            break;

        case NGUIWidget.Pivot.Top:
            isActive = IsTop(mWidget.pivot);
            break;

        case NGUIWidget.Pivot.Bottom:
            isActive = IsBottom(mWidget.pivot);
            break;

        case NGUIWidget.Pivot.Center:
            isActive = isHorizontal ? pivot == GetHorizontal(mWidget.pivot) : pivot == GetVertical(mWidget.pivot);
            break;
        }

        if (GUILayout.Toggle(isActive, text, style) != isActive)
        {
            SetPivot(pivot, isHorizontal);
        }
    }
Beispiel #3
0
    /// <summary>
    /// Labels used for input shouldn't support color encoding.
    /// </summary>

    protected void Init()
    {
        if (mDoInit)
        {
            mDoInit = false;
            if (label == null)
            {
                label = GetComponentInChildren <NGUILabel>();
            }

            if (label != null)
            {
                if (useLabelTextAtStart)
                {
                    mText = label.text;
                }
                mDefaultText          = label.text;
                mDefaultColor         = label.color;
                label.supportEncoding = false;
                label.password        = isPassword;
                mPivot    = label.pivot;
                mPosition = label.cachedTransform.localPosition.x;
            }
            else
            {
                enabled = false;
            }
        }
    }
    static NGUIWidget.Pivot Combine(NGUIWidget.Pivot horizontal, NGUIWidget.Pivot vertical)
    {
        if (horizontal == NGUIWidget.Pivot.Left)
        {
            if (vertical == NGUIWidget.Pivot.Top)
            {
                return(NGUIWidget.Pivot.TopLeft);
            }
            if (vertical == NGUIWidget.Pivot.Bottom)
            {
                return(NGUIWidget.Pivot.BottomLeft);
            }
            return(NGUIWidget.Pivot.Left);
        }

        if (horizontal == NGUIWidget.Pivot.Right)
        {
            if (vertical == NGUIWidget.Pivot.Top)
            {
                return(NGUIWidget.Pivot.TopRight);
            }
            if (vertical == NGUIWidget.Pivot.Bottom)
            {
                return(NGUIWidget.Pivot.BottomRight);
            }
            return(NGUIWidget.Pivot.Right);
        }
        return(vertical);
    }
 static NGUIWidget.Pivot GetHorizontal(NGUIWidget.Pivot pivot)
 {
     if (IsLeft(pivot))
     {
         return(NGUIWidget.Pivot.Left);
     }
     if (IsRight(pivot))
     {
         return(NGUIWidget.Pivot.Right);
     }
     return(NGUIWidget.Pivot.Center);
 }
 static NGUIWidget.Pivot GetVertical(NGUIWidget.Pivot pivot)
 {
     if (IsTop(pivot))
     {
         return(NGUIWidget.Pivot.Top);
     }
     if (IsBottom(pivot))
     {
         return(NGUIWidget.Pivot.Bottom);
     }
     return(NGUIWidget.Pivot.Center);
 }
    void SetPivot(NGUIWidget.Pivot pivot, bool isHorizontal)
    {
        NGUIWidget.Pivot horizontal = GetHorizontal(mWidget.pivot);
        NGUIWidget.Pivot vertical   = GetVertical(mWidget.pivot);

        pivot = isHorizontal ? Combine(pivot, vertical) : Combine(horizontal, pivot);

        if (mWidget.pivot != pivot)
        {
            NGUIEditorTools.RegisterUndo("Pivot change", mWidget);
            mWidget.pivot = pivot;
        }
    }
    /// <summary>
    /// All widgets have depth, color and make pixel-perfect options
    /// </summary>

    protected void DrawCommonProperties()
    {
#if UNITY_3_4
        PrefabType type = EditorUtility.GetPrefabType(mWidget.gameObject);
#else
        PrefabType type = PrefabUtility.GetPrefabType(mWidget.gameObject);
#endif

        NGUIEditorTools.DrawSeparator();

#if UNITY_3_5
        // Pivot point -- old school drop-down style
        NGUIWidget.Pivot pivot = (NGUIWidget.Pivot)EditorGUILayout.EnumPopup("Pivot", mWidget.pivot);

        if (mWidget.pivot != pivot)
        {
            NGUIEditorTools.RegisterUndo("Pivot Change", mWidget);
            mWidget.pivot = pivot;
        }
#else
        // Pivot point -- the new, more visual style
        GUILayout.BeginHorizontal();
        GUILayout.Label("Pivot", GUILayout.Width(76f));
        Toggle("◄", "ButtonLeft", NGUIWidget.Pivot.Left, true);
        Toggle("▬", "ButtonMid", NGUIWidget.Pivot.Center, true);
        Toggle("►", "ButtonRight", NGUIWidget.Pivot.Right, true);
        Toggle("▲", "ButtonLeft", NGUIWidget.Pivot.Top, false);
        Toggle("▌", "ButtonMid", NGUIWidget.Pivot.Center, false);
        Toggle("▼", "ButtonRight", NGUIWidget.Pivot.Bottom, false);
        GUILayout.EndHorizontal();
#endif

        // Depth navigation
        if (type != PrefabType.Prefab)
        {
            GUILayout.Space(2f);
            GUILayout.BeginHorizontal();
            {
                EditorGUILayout.PrefixLabel("Depth");

                int depth = mWidget.depth;
                if (GUILayout.Button("Back", GUILayout.Width(60f)))
                {
                    --depth;
                }
                depth = EditorGUILayout.IntField(depth);
                if (GUILayout.Button("Forward", GUILayout.Width(60f)))
                {
                    ++depth;
                }

                if (mWidget.depth != depth)
                {
                    NGUIEditorTools.RegisterUndo("Depth Change", mWidget);
                    mWidget.depth = depth;
                    mDepthCheck   = true;
                }
            }
            GUILayout.EndHorizontal();

            NGUIPanel panel = mWidget.panel;

            if (panel != null)
            {
                int count = 0;

                for (int i = 0; i < panel.widgets.size; ++i)
                {
                    NGUIWidget w = panel.widgets[i];
                    if (w != null && w.depth == mWidget.depth && w.material == mWidget.material)
                    {
                        ++count;
                    }
                }

                if (count > 1)
                {
                    EditorGUILayout.HelpBox(count + " widgets are using the depth value of " + mWidget.depth +
                                            ". It may not be clear what should be in front of what.", MessageType.Warning);
                }

                if (mDepthCheck)
                {
                    if (panel.drawCalls.size > 1)
                    {
                        EditorGUILayout.HelpBox("The widgets underneath this panel are using more than one atlas. You may need to adjust transform position's Z value instead. When adjusting the Z, lower value means closer to the camera.", MessageType.Warning);
                    }
                }
            }
        }

        // Pixel-correctness
        if (type != PrefabType.Prefab)
        {
            GUILayout.BeginHorizontal();
            {
                EditorGUILayout.PrefixLabel("Correction");

                if (GUILayout.Button("Make Pixel-Perfect"))
                {
                    NGUIEditorTools.RegisterUndo("Make Pixel-Perfect", mWidget.transform);
                    mWidget.MakePixelPerfect();
                }
            }
            GUILayout.EndHorizontal();
        }

        //NGUIEditorTools.DrawSeparator();
        EditorGUILayout.Space();

        // Color tint
        GUILayout.BeginHorizontal();
        Color color = EditorGUILayout.ColorField("Color Tint", mWidget.color);
        if (GUILayout.Button("Copy", GUILayout.Width(50f)))
        {
            NGUISettings.color = color;
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        NGUISettings.color = EditorGUILayout.ColorField("Clipboard", NGUISettings.color);
        if (GUILayout.Button("Paste", GUILayout.Width(50f)))
        {
            color = NGUISettings.color;
        }
        GUILayout.EndHorizontal();

        if (mWidget.color != color)
        {
            NGUIEditorTools.RegisterUndo("Color Change", mWidget);
            mWidget.color = color;
        }
    }
    /// <summary>
    /// Adjust the widget's rectangle based on the specified modifier values.
    /// </summary>

    static void AdjustWidget(NGUIWidget w, Vector3 pos, Vector3 scale, float left, float top, float right, float bottom, bool makeSquare)
    {
        Vector2 offset  = w.pivotOffset;
        Vector4 padding = w.relativePadding;
        Vector2 size    = w.relativeSize;

        offset.x -= padding.x;
        offset.y -= padding.y;
        size.x   += padding.x + padding.z;
        size.y   += padding.y + padding.w;

        scale.Scale(size);

        offset.y = -offset.y;

        Transform  t   = w.cachedTransform;
        Quaternion rot = t.localRotation;

        NGUIWidget.Pivot pivot = w.pivot;

        Vector2 rotatedTL = new Vector2(left, top);
        Vector2 rotatedTR = new Vector2(right, top);
        Vector2 rotatedBL = new Vector2(left, bottom);
        Vector2 rotatedBR = new Vector2(right, bottom);
        Vector2 rotatedL  = new Vector2(left, 0f);
        Vector2 rotatedR  = new Vector2(right, 0f);
        Vector2 rotatedT  = new Vector2(0f, top);
        Vector2 rotatedB  = new Vector2(0f, bottom);

        rotatedTL = rot * rotatedTL;
        rotatedTR = rot * rotatedTR;
        rotatedBL = rot * rotatedBL;
        rotatedBR = rot * rotatedBR;
        rotatedL  = rot * rotatedL;
        rotatedR  = rot * rotatedR;
        rotatedT  = rot * rotatedT;
        rotatedB  = rot * rotatedB;

        switch (pivot)
        {
        case NGUIWidget.Pivot.TopLeft:
            pos.x += rotatedTL.x;
            pos.y += rotatedTL.y;
            break;

        case NGUIWidget.Pivot.BottomRight:
            pos.x += rotatedBR.x;
            pos.y += rotatedBR.y;
            break;

        case NGUIWidget.Pivot.BottomLeft:
            pos.x += rotatedBL.x;
            pos.y += rotatedBL.y;
            break;

        case NGUIWidget.Pivot.TopRight:
            pos.x += rotatedTR.x;
            pos.y += rotatedTR.y;
            break;

        case NGUIWidget.Pivot.Left:
            pos.x += rotatedL.x + (rotatedT.x + rotatedB.x) * 0.5f;
            pos.y += rotatedL.y + (rotatedT.y + rotatedB.y) * 0.5f;
            break;

        case NGUIWidget.Pivot.Right:
            pos.x += rotatedR.x + (rotatedT.x + rotatedB.x) * 0.5f;
            pos.y += rotatedR.y + (rotatedT.y + rotatedB.y) * 0.5f;
            break;

        case NGUIWidget.Pivot.Top:
            pos.x += rotatedT.x + (rotatedL.x + rotatedR.x) * 0.5f;
            pos.y += rotatedT.y + (rotatedL.y + rotatedR.y) * 0.5f;
            break;

        case NGUIWidget.Pivot.Bottom:
            pos.x += rotatedB.x + (rotatedL.x + rotatedR.x) * 0.5f;
            pos.y += rotatedB.y + (rotatedL.y + rotatedR.y) * 0.5f;
            break;

        case NGUIWidget.Pivot.Center:
            pos.x += (rotatedL.x + rotatedR.x + rotatedT.x + rotatedB.x) * 0.5f;
            pos.y += (rotatedT.y + rotatedB.y + rotatedL.y + rotatedR.y) * 0.5f;
            break;
        }

        scale.x -= left - right;
        scale.y += top - bottom;

        scale.x /= size.x;
        scale.y /= size.y;

        Vector4 border = w.border;
        float   minx   = Mathf.Max(2f, padding.x + padding.z + border.x + border.z);
        float   miny   = Mathf.Max(2f, padding.y + padding.w + border.y + border.w);

        if (scale.x < minx)
        {
            scale.x = minx;
        }
        if (scale.y < miny)
        {
            scale.y = miny;
        }

        // NOTE: This will only work correctly when dragging the corner opposite of the pivot point
        if (makeSquare)
        {
            scale.x = Mathf.Min(scale.x, scale.y);
            scale.y = scale.x;
        }

        t.localPosition = pos;
        t.localScale    = scale;
    }
    /// <summary>
    /// Adjust the transform's position and scale.
    /// </summary>

    static void AdjustPosAndScale(NGUIWidget w, Vector3 startLocalPos, Vector3 startLocalScale, Vector3 worldDelta, NGUIWidget.Pivot dragPivot)
    {
        Transform  t             = w.cachedTransform;
        Transform  parent        = t.parent;
        Matrix4x4  parentToLocal = (parent != null) ? t.parent.worldToLocalMatrix : Matrix4x4.identity;
        Matrix4x4  worldToLocal  = parentToLocal;
        Quaternion invRot        = Quaternion.Inverse(t.localRotation);

        worldToLocal = worldToLocal * Matrix4x4.TRS(Vector3.zero, invRot, Vector3.one);
        Vector3 localDelta = worldToLocal.MultiplyVector(worldDelta);

        bool  canBeSquare = false;
        float left        = 0f;
        float right       = 0f;
        float top         = 0f;
        float bottom      = 0f;

        switch (dragPivot)
        {
        case NGUIWidget.Pivot.TopLeft:
            canBeSquare = (w.pivot == NGUIWidget.Pivot.BottomRight);
            left        = localDelta.x;
            top         = localDelta.y;
            break;

        case NGUIWidget.Pivot.Left:
            left = localDelta.x;
            break;

        case NGUIWidget.Pivot.BottomLeft:
            canBeSquare = (w.pivot == NGUIWidget.Pivot.TopRight);
            left        = localDelta.x;
            bottom      = localDelta.y;
            break;

        case NGUIWidget.Pivot.Top:
            top = localDelta.y;
            break;

        case NGUIWidget.Pivot.Bottom:
            bottom = localDelta.y;
            break;

        case NGUIWidget.Pivot.TopRight:
            canBeSquare = (w.pivot == NGUIWidget.Pivot.BottomLeft);
            right       = localDelta.x;
            top         = localDelta.y;
            break;

        case NGUIWidget.Pivot.Right:
            right = localDelta.x;
            break;

        case NGUIWidget.Pivot.BottomRight:
            canBeSquare = (w.pivot == NGUIWidget.Pivot.TopLeft);
            right       = localDelta.x;
            bottom      = localDelta.y;
            break;
        }

        AdjustWidget(w, startLocalPos, startLocalScale, left, top, right, bottom, canBeSquare && Event.current.modifiers == EventModifiers.Shift);
    }
    /// <summary>
    /// Draw the on-screen selection, knobs, and handle all interaction logic.
    /// </summary>

    public void OnSceneGUI()
    {
        if (!EditorPrefs.GetBool("New GUI", true))
        {
            return;
        }
        if (Tools.current != Tool.View)
        {
            return;
        }

        mWidget = target as NGUIWidget;

        Handles.color = mOutlineColor;
        Transform t = mWidget.cachedTransform;

        Event     e    = Event.current;
        int       id   = GUIUtility.GetControlID(s_Hash, FocusType.Passive);
        EventType type = e.GetTypeForControl(id);

        Vector3[] corners = NGUIMath.CalculateWidgetCorners(mWidget);
        Handles.DrawLine(corners[0], corners[1]);
        Handles.DrawLine(corners[1], corners[2]);
        Handles.DrawLine(corners[2], corners[3]);
        Handles.DrawLine(corners[0], corners[3]);

        Vector3[] worldPos = new Vector3[8];

        worldPos[0] = corners[0];
        worldPos[1] = corners[1];
        worldPos[2] = corners[2];
        worldPos[3] = corners[3];

        worldPos[4] = (corners[0] + corners[1]) * 0.5f;
        worldPos[5] = (corners[1] + corners[2]) * 0.5f;
        worldPos[6] = (corners[2] + corners[3]) * 0.5f;
        worldPos[7] = (corners[0] + corners[3]) * 0.5f;

        Vector2[] screenPos = new Vector2[8];
        for (int i = 0; i < 8; ++i)
        {
            screenPos[i] = HandleUtility.WorldToGUIPoint(worldPos[i]);
        }

        Bounds b = new Bounds(screenPos[0], Vector3.zero);

        for (int i = 1; i < 8; ++i)
        {
            b.Encapsulate(screenPos[i]);
        }

        // Time to figure out what kind of action is underneath the mouse
        Action actionUnderMouse = mAction;

        NGUIWidget.Pivot pivotUnderMouse = NGUIWidget.Pivot.Center;

        if (actionUnderMouse == Action.None)
        {
            int   index = 0;
            float dist  = GetScreenDistance(worldPos, e.mousePosition, out index);

            if (dist < 10f)
            {
                pivotUnderMouse  = mPivots[index];
                actionUnderMouse = Action.Scale;
            }
            else if (e.modifiers == 0 && NGUIEditorTools.DistanceToRectangle(corners, e.mousePosition) == 0f)
            {
                actionUnderMouse = Action.Move;
            }
            else if (dist < 30f)
            {
                actionUnderMouse = Action.Rotate;
            }
        }

        // Change the mouse cursor to a more appropriate one
#if !UNITY_3_5
        {
            Vector2 min = b.min;
            Vector2 max = b.max;

            min.x -= 30f;
            max.x += 30f;
            min.y -= 30f;
            max.y += 30f;

            Rect rect = new Rect(min.x, min.y, max.x - min.x, max.y - min.y);

            if (actionUnderMouse == Action.Rotate)
            {
                SetCursorRect(rect, MouseCursor.RotateArrow);
            }
            else if (actionUnderMouse == Action.Move)
            {
                SetCursorRect(rect, MouseCursor.MoveArrow);
            }
            else if (actionUnderMouse == Action.Scale)
            {
                SetCursorRect(rect, MouseCursor.ScaleArrow);
            }
            else
            {
                SetCursorRect(rect, MouseCursor.Arrow);
            }
        }
#endif

        switch (type)
        {
        case EventType.Repaint:
        {
            Handles.BeginGUI();
            {
                for (int i = 0; i < 8; ++i)
                {
                    DrawKnob(worldPos[i], mWidget.pivot == mPivots[i], id);
                }
            }
            Handles.EndGUI();
        }
        break;

        case EventType.MouseDown:
        {
            mStartMouse     = e.mousePosition;
            mAllowSelection = true;

            if (e.button == 1)
            {
                if (e.modifiers == 0)
                {
                    GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                    e.Use();
                }
            }
            else if (e.button == 0 && actionUnderMouse != Action.None && Raycast(corners, out mStartDrag))
            {
                mStartPos             = t.position;
                mStartRot             = t.localRotation.eulerAngles;
                mStartDir             = mStartDrag - t.position;
                mStartScale           = t.localScale;
                mDragPivot            = pivotUnderMouse;
                mActionUnderMouse     = actionUnderMouse;
                GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                e.Use();
            }
        }
        break;

        case EventType.MouseDrag:
        {
            // Prevent selection once the drag operation begins
            bool dragStarted = (e.mousePosition - mStartMouse).magnitude > 3f;
            if (dragStarted)
            {
                mAllowSelection = false;
            }

            if (GUIUtility.hotControl == id)
            {
                e.Use();

                if (mAction != Action.None || mActionUnderMouse != Action.None)
                {
                    Vector3 pos;

                    if (Raycast(corners, out pos))
                    {
                        if (mAction == Action.None && mActionUnderMouse != Action.None)
                        {
                            // Wait until the mouse moves by more than a few pixels
                            if (dragStarted)
                            {
                                if (mActionUnderMouse == Action.Move)
                                {
                                    mStartPos = t.position;
                                    NGUIEditorTools.RegisterUndo("Move widget", t);
                                }
                                else if (mActionUnderMouse == Action.Rotate)
                                {
                                    mStartRot = t.localRotation.eulerAngles;
                                    mStartDir = mStartDrag - t.position;
                                    NGUIEditorTools.RegisterUndo("Rotate widget", t);
                                }
                                else if (mActionUnderMouse == Action.Scale)
                                {
                                    mStartPos   = t.localPosition;
                                    mStartScale = t.localScale;
                                    mDragPivot  = pivotUnderMouse;
                                    NGUIEditorTools.RegisterUndo("Scale widget", t);
                                }
                                mAction = actionUnderMouse;
                            }
                        }

                        if (mAction != Action.None)
                        {
                            if (mAction == Action.Move)
                            {
                                t.position      = mStartPos + (pos - mStartDrag);
                                pos             = t.localPosition;
                                pos.x           = Mathf.RoundToInt(pos.x);
                                pos.y           = Mathf.RoundToInt(pos.y);
                                t.localPosition = pos;
                            }
                            else if (mAction == Action.Rotate)
                            {
                                Vector3 dir   = pos - t.position;
                                float   angle = Vector3.Angle(mStartDir, dir);

                                if (angle > 0f)
                                {
                                    float dot = Vector3.Dot(Vector3.Cross(mStartDir, dir), t.forward);
                                    if (dot < 0f)
                                    {
                                        angle = -angle;
                                    }
                                    angle = mStartRot.z + angle;
                                    if (e.modifiers != EventModifiers.Shift)
                                    {
                                        angle = Mathf.Round(angle / 15f) * 15f;
                                    }
                                    else
                                    {
                                        angle = Mathf.Round(angle);
                                    }
                                    t.localRotation = Quaternion.Euler(mStartRot.x, mStartRot.y, angle);
                                }
                            }
                            else if (mAction == Action.Scale)
                            {
                                // World-space delta since the drag started
                                Vector3 delta = pos - mStartDrag;

                                // Adjust the widget's position and scale based on the delta, restricted by the pivot
                                AdjustPosAndScale(mWidget, mStartPos, mStartScale, delta, mDragPivot);
                            }
                        }
                    }
                }
            }
        }
        break;

        case EventType.MouseUp:
        {
            if (GUIUtility.hotControl == id)
            {
                GUIUtility.hotControl      = 0;
                GUIUtility.keyboardControl = 0;

                if (e.button < 2)
                {
                    bool handled = false;

                    if (e.button == 1)
                    {
                        // Right-click: Select the widget below
                        NGUIWidget   last    = null;
                        NGUIWidget[] widgets = Raycast(mWidget, e.mousePosition);

                        for (int i = widgets.Length; i > 0;)
                        {
                            NGUIWidget w = widgets[--i];
                            if (w == mWidget)
                            {
                                break;
                            }
                            last = w;
                        }

                        if (last != null)
                        {
                            Selection.activeGameObject = last.gameObject;
                            handled = true;
                        }
                    }
                    else if (mAction == Action.None)
                    {
                        if (mAllowSelection)
                        {
                            // Left-click: Select the widget above
                            NGUIWidget   last    = null;
                            NGUIWidget[] widgets = Raycast(mWidget, e.mousePosition);

                            if (widgets.Length > 0)
                            {
                                for (int i = 0; i < widgets.Length; ++i)
                                {
                                    NGUIWidget w = widgets[i];

                                    if (w == mWidget)
                                    {
                                        if (last != null)
                                        {
                                            Selection.activeGameObject = last.gameObject;
                                        }
                                        handled = true;
                                        break;
                                    }
                                    last = w;
                                }

                                if (!handled)
                                {
                                    Selection.activeGameObject = widgets[0].gameObject;
                                    handled = true;
                                }
                            }
                        }
                    }
                    else
                    {
                        // Finished dragging something
                        mAction           = Action.None;
                        mActionUnderMouse = Action.None;
                        Vector3 pos   = t.localPosition;
                        Vector3 scale = t.localScale;

                        if (mWidget.pixelPerfectAfterResize)
                        {
                            t.localPosition = pos;
                            t.localScale    = scale;

                            mWidget.MakePixelPerfect();
                        }
                        else
                        {
                            pos.x   = Mathf.Round(pos.x);
                            pos.y   = Mathf.Round(pos.y);
                            scale.x = Mathf.Round(scale.x);
                            scale.y = Mathf.Round(scale.y);

                            t.localPosition = pos;
                            t.localScale    = scale;
                        }
                        handled = true;
                    }

                    if (handled)
                    {
                        mActionUnderMouse = Action.None;
                        mAction           = Action.None;
                        e.Use();
                    }
                }
            }
            else if (mAllowSelection)
            {
                NGUIWidget[] widgets = Raycast(mWidget, e.mousePosition);
                if (widgets.Length > 0)
                {
                    Selection.activeGameObject = widgets[0].gameObject;
                }
            }
            mAllowSelection = true;
        }
        break;

        case EventType.KeyDown:
        {
            if (e.keyCode == KeyCode.UpArrow)
            {
                Vector3 pos = t.localPosition;
                pos.y          += 1f;
                t.localPosition = pos;
                e.Use();
            }
            else if (e.keyCode == KeyCode.DownArrow)
            {
                Vector3 pos = t.localPosition;
                pos.y          -= 1f;
                t.localPosition = pos;
                e.Use();
            }
            else if (e.keyCode == KeyCode.LeftArrow)
            {
                Vector3 pos = t.localPosition;
                pos.x          -= 1f;
                t.localPosition = pos;
                e.Use();
            }
            else if (e.keyCode == KeyCode.RightArrow)
            {
                Vector3 pos = t.localPosition;
                pos.x          += 1f;
                t.localPosition = pos;
                e.Use();
            }
            else if (e.keyCode == KeyCode.Escape)
            {
                if (GUIUtility.hotControl == id)
                {
                    if (mAction != Action.None)
                    {
                        if (mAction == Action.Move)
                        {
                            t.position = mStartPos;
                        }
                        else if (mAction == Action.Rotate)
                        {
                            t.localRotation = Quaternion.Euler(mStartRot);
                        }
                        else if (mAction == Action.Scale)
                        {
                            t.position   = mStartPos;
                            t.localScale = mStartScale;
                        }
                    }

                    GUIUtility.hotControl      = 0;
                    GUIUtility.keyboardControl = 0;

                    mActionUnderMouse = Action.None;
                    mAction           = Action.None;
                    e.Use();
                }
                else
                {
                    Selection.activeGameObject = null;
                    Tools.current = Tool.Move;
                }
            }
        }
        break;
        }
    }
    /// <summary>
    /// Draw the on-screen selection, knobs, and handle all interaction logic.
    /// </summary>
    public void OnSceneGUI()
    {
        if (!EditorPrefs.GetBool("New GUI", true)) return;
        if (Tools.current != Tool.View) return;

        mWidget = target as NGUIWidget;

        Handles.color = mOutlineColor;
        Transform t = mWidget.cachedTransform;

        Event e = Event.current;
        int id = GUIUtility.GetControlID(s_Hash, FocusType.Passive);
        EventType type = e.GetTypeForControl(id);

        Vector3[] corners = NGUIMath.CalculateWidgetCorners(mWidget);
        Handles.DrawLine(corners[0], corners[1]);
        Handles.DrawLine(corners[1], corners[2]);
        Handles.DrawLine(corners[2], corners[3]);
        Handles.DrawLine(corners[0], corners[3]);

        Vector3[] worldPos = new Vector3[8];

        worldPos[0] = corners[0];
        worldPos[1] = corners[1];
        worldPos[2] = corners[2];
        worldPos[3] = corners[3];

        worldPos[4] = (corners[0] + corners[1]) * 0.5f;
        worldPos[5] = (corners[1] + corners[2]) * 0.5f;
        worldPos[6] = (corners[2] + corners[3]) * 0.5f;
        worldPos[7] = (corners[0] + corners[3]) * 0.5f;

        Vector2[] screenPos = new Vector2[8];
        for (int i = 0; i < 8; ++i) screenPos[i] = HandleUtility.WorldToGUIPoint(worldPos[i]);

        Bounds b = new Bounds(screenPos[0], Vector3.zero);
        for (int i = 1; i < 8; ++i) b.Encapsulate(screenPos[i]);

        // Time to figure out what kind of action is underneath the mouse
        Action actionUnderMouse = mAction;
        NGUIWidget.Pivot pivotUnderMouse = NGUIWidget.Pivot.Center;

        if (actionUnderMouse == Action.None)
        {
            int index = 0;
            float dist = GetScreenDistance(worldPos, e.mousePosition, out index);

            if (dist < 10f)
            {
                pivotUnderMouse = mPivots[index];
                actionUnderMouse = Action.Scale;
            }
            else if (e.modifiers == 0 && NGUIEditorTools.DistanceToRectangle(corners, e.mousePosition) == 0f)
            {
                actionUnderMouse = Action.Move;
            }
            else if (dist < 30f)
            {
                actionUnderMouse = Action.Rotate;
            }
        }

        // Change the mouse cursor to a more appropriate one
        #if !UNITY_3_5
        {
            Vector2 min = b.min;
            Vector2 max = b.max;

            min.x -= 30f;
            max.x += 30f;
            min.y -= 30f;
            max.y += 30f;

            Rect rect = new Rect(min.x, min.y, max.x - min.x, max.y - min.y);

            if (actionUnderMouse == Action.Rotate)
            {
                SetCursorRect(rect, MouseCursor.RotateArrow);
            }
            else if (actionUnderMouse == Action.Move)
            {
                SetCursorRect(rect, MouseCursor.MoveArrow);
            }
            else if (actionUnderMouse == Action.Scale)
            {
                SetCursorRect(rect, MouseCursor.ScaleArrow);
            }
            else SetCursorRect(rect, MouseCursor.Arrow);
        }
        #endif

        switch (type)
        {
            case EventType.Repaint:
            {
                Handles.BeginGUI();
                {
                    for (int i = 0; i < 8; ++i)
                    {
                        DrawKnob(worldPos[i], mWidget.pivot == mPivots[i], id);
                    }
                }
                Handles.EndGUI();
            }
            break;

            case EventType.MouseDown:
            {
                mStartMouse = e.mousePosition;
                mAllowSelection = true;

                if (e.button == 1)
                {
                    if (e.modifiers == 0)
                    {
                        GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                        e.Use();
                    }
                }
                else if (e.button == 0 && actionUnderMouse != Action.None && Raycast(corners, out mStartDrag))
                {
                    mStartPos = t.position;
                    mStartRot = t.localRotation.eulerAngles;
                    mStartDir = mStartDrag - t.position;
                    mStartScale = t.localScale;
                    mDragPivot = pivotUnderMouse;
                    mActionUnderMouse = actionUnderMouse;
                    GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                    e.Use();
                }
            }
            break;

            case EventType.MouseDrag:
            {
                // Prevent selection once the drag operation begins
                bool dragStarted = (e.mousePosition - mStartMouse).magnitude > 3f;
                if (dragStarted) mAllowSelection = false;

                if (GUIUtility.hotControl == id)
                {
                    e.Use();

                    if (mAction != Action.None || mActionUnderMouse != Action.None)
                    {
                        Vector3 pos;

                        if (Raycast(corners, out pos))
                        {
                            if (mAction == Action.None && mActionUnderMouse != Action.None)
                            {
                                // Wait until the mouse moves by more than a few pixels
                                if (dragStarted)
                                {
                                    if (mActionUnderMouse == Action.Move)
                                    {
                                        mStartPos = t.position;
                                        NGUIEditorTools.RegisterUndo("Move widget", t);
                                    }
                                    else if (mActionUnderMouse == Action.Rotate)
                                    {
                                        mStartRot = t.localRotation.eulerAngles;
                                        mStartDir = mStartDrag - t.position;
                                        NGUIEditorTools.RegisterUndo("Rotate widget", t);
                                    }
                                    else if (mActionUnderMouse == Action.Scale)
                                    {
                                        mStartPos = t.localPosition;
                                        mStartScale = t.localScale;
                                        mDragPivot = pivotUnderMouse;
                                        NGUIEditorTools.RegisterUndo("Scale widget", t);
                                    }
                                    mAction = actionUnderMouse;
                                }
                            }

                            if (mAction != Action.None)
                            {
                                if (mAction == Action.Move)
                                {
                                    t.position = mStartPos + (pos - mStartDrag);
                                    pos = t.localPosition;
                                    pos.x = Mathf.RoundToInt(pos.x);
                                    pos.y = Mathf.RoundToInt(pos.y);
                                    t.localPosition = pos;
                                }
                                else if (mAction == Action.Rotate)
                                {
                                    Vector3 dir = pos - t.position;
                                    float angle = Vector3.Angle(mStartDir, dir);

                                    if (angle > 0f)
                                    {
                                        float dot = Vector3.Dot(Vector3.Cross(mStartDir, dir), t.forward);
                                        if (dot < 0f) angle = -angle;
                                        angle = mStartRot.z + angle;
                                        if (e.modifiers != EventModifiers.Shift) angle = Mathf.Round(angle / 15f) * 15f;
                                        else angle = Mathf.Round(angle);
                                        t.localRotation = Quaternion.Euler(mStartRot.x, mStartRot.y, angle);
                                    }
                                }
                                else if (mAction == Action.Scale)
                                {
                                    // World-space delta since the drag started
                                    Vector3 delta = pos - mStartDrag;

                                    // Adjust the widget's position and scale based on the delta, restricted by the pivot
                                    AdjustPosAndScale(mWidget, mStartPos, mStartScale, delta, mDragPivot);
                                }
                            }
                        }
                    }
                }
            }
            break;

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == id)
                {
                    GUIUtility.hotControl = 0;
                    GUIUtility.keyboardControl = 0;

                    if (e.button < 2)
                    {
                        bool handled = false;

                        if (e.button == 1)
                        {
                            // Right-click: Select the widget below
                            NGUIWidget last = null;
                            NGUIWidget[] widgets = Raycast(mWidget, e.mousePosition);

                            for (int i = widgets.Length; i > 0; )
                            {
                                NGUIWidget w = widgets[--i];
                                if (w == mWidget) break;
                                last = w;
                            }

                            if (last != null)
                            {
                                Selection.activeGameObject = last.gameObject;
                                handled = true;
                            }
                        }
                        else if (mAction == Action.None)
                        {
                            if (mAllowSelection)
                            {
                                // Left-click: Select the widget above
                                NGUIWidget last = null;
                                NGUIWidget[] widgets = Raycast(mWidget, e.mousePosition);

                                if (widgets.Length > 0)
                                {
                                    for (int i = 0; i < widgets.Length; ++i)
                                    {
                                        NGUIWidget w = widgets[i];

                                        if (w == mWidget)
                                        {
                                            if (last != null) Selection.activeGameObject = last.gameObject;
                                            handled = true;
                                            break;
                                        }
                                        last = w;
                                    }

                                    if (!handled)
                                    {
                                        Selection.activeGameObject = widgets[0].gameObject;
                                        handled = true;
                                    }
                                }
                            }
                        }
                        else
                        {
                            // Finished dragging something
                            mAction = Action.None;
                            mActionUnderMouse = Action.None;
                            Vector3 pos = t.localPosition;
                            Vector3 scale = t.localScale;

                            if (mWidget.pixelPerfectAfterResize)
                            {
                                t.localPosition = pos;
                                t.localScale = scale;

                                mWidget.MakePixelPerfect();
                            }
                            else
                            {
                                pos.x = Mathf.Round(pos.x);
                                pos.y = Mathf.Round(pos.y);
                                scale.x = Mathf.Round(scale.x);
                                scale.y = Mathf.Round(scale.y);

                                t.localPosition = pos;
                                t.localScale = scale;
                            }
                            handled = true;
                        }

                        if (handled)
                        {
                            mActionUnderMouse = Action.None;
                            mAction = Action.None;
                            e.Use();
                        }
                    }
                }
                else if (mAllowSelection)
                {
                    NGUIWidget[] widgets = Raycast(mWidget, e.mousePosition);
                    if (widgets.Length > 0) Selection.activeGameObject = widgets[0].gameObject;
                }
                mAllowSelection = true;
            }
            break;

            case EventType.KeyDown:
            {
                if (e.keyCode == KeyCode.UpArrow)
                {
                    Vector3 pos = t.localPosition;
                    pos.y += 1f;
                    t.localPosition = pos;
                    e.Use();
                }
                else if (e.keyCode == KeyCode.DownArrow)
                {
                    Vector3 pos = t.localPosition;
                    pos.y -= 1f;
                    t.localPosition = pos;
                    e.Use();
                }
                else if (e.keyCode == KeyCode.LeftArrow)
                {
                    Vector3 pos = t.localPosition;
                    pos.x -= 1f;
                    t.localPosition = pos;
                    e.Use();
                }
                else if (e.keyCode == KeyCode.RightArrow)
                {
                    Vector3 pos = t.localPosition;
                    pos.x += 1f;
                    t.localPosition = pos;
                    e.Use();
                }
                else if (e.keyCode == KeyCode.Escape)
                {
                    if (GUIUtility.hotControl == id)
                    {
                        if (mAction != Action.None)
                        {
                            if (mAction == Action.Move)
                            {
                                t.position = mStartPos;
                            }
                            else if (mAction == Action.Rotate)
                            {
                                t.localRotation = Quaternion.Euler(mStartRot);
                            }
                            else if (mAction == Action.Scale)
                            {
                                t.position = mStartPos;
                                t.localScale = mStartScale;
                            }
                        }

                        GUIUtility.hotControl = 0;
                        GUIUtility.keyboardControl = 0;

                        mActionUnderMouse = Action.None;
                        mAction = Action.None;
                        e.Use();
                    }
                    else
                    {
                        Selection.activeGameObject = null;
                        Tools.current = Tool.Move;
                    }
                }
            }
            break;
        }
    }
 static bool IsBottom(NGUIWidget.Pivot pivot)
 {
     return(pivot == NGUIWidget.Pivot.Bottom ||
            pivot == NGUIWidget.Pivot.BottomLeft ||
            pivot == NGUIWidget.Pivot.BottomRight);
 }
 static bool IsTop(NGUIWidget.Pivot pivot)
 {
     return(pivot == NGUIWidget.Pivot.Top ||
            pivot == NGUIWidget.Pivot.TopLeft ||
            pivot == NGUIWidget.Pivot.TopRight);
 }
 static bool IsRight(NGUIWidget.Pivot pivot)
 {
     return(pivot == NGUIWidget.Pivot.Right ||
            pivot == NGUIWidget.Pivot.TopRight ||
            pivot == NGUIWidget.Pivot.BottomRight);
 }
 static bool IsLeft(NGUIWidget.Pivot pivot)
 {
     return(pivot == NGUIWidget.Pivot.Left ||
            pivot == NGUIWidget.Pivot.TopLeft ||
            pivot == NGUIWidget.Pivot.BottomLeft);
 }
Beispiel #17
0
    static void Load()
    {
        int l = LayerMask.NameToLayer("UI");
        if (l == -1) l = LayerMask.NameToLayer("GUI");
        if (l == -1) l = 31;

        mLoaded			= true;
        mPartial		= EditorPrefs.GetString("NGUI Partial");
        mFontName		= EditorPrefs.GetString("NGUI Font Name");
        mAtlasName		= EditorPrefs.GetString("NGUI Atlas Name");
        mFontData		= GetObject("NGUI Font Asset") as TextAsset;
        mFontTexture	= GetObject("NGUI Font Texture") as Texture2D;
        mFont			= GetObject("NGUI Font") as NGUIFont;
        mAtlas			= GetObject("NGUI Atlas") as NGUIAtlas;
        mAtlasPadding	= EditorPrefs.GetInt("NGUI Atlas Padding", 1);
        mAtlasTrimming	= EditorPrefs.GetBool("NGUI Atlas Trimming", true);
        mUnityPacking	= EditorPrefs.GetBool("NGUI Unity Packing", true);
        mForceSquare	= EditorPrefs.GetBool("NGUI Force Square Atlas", true);
        mPivot			= (NGUIWidget.Pivot)EditorPrefs.GetInt("NGUI Pivot", (int)mPivot);
        mLayer			= EditorPrefs.GetInt("NGUI Layer", l);
        mDynFont		= GetObject("NGUI DynFont") as Font;
        mDynFontSize	= EditorPrefs.GetInt("NGUI DynFontSize", 16);
        mDynFontStyle	= (FontStyle)EditorPrefs.GetInt("NGUI DynFontStyle", (int)FontStyle.Normal);

        LoadColor();
    }