Ejemplo n.º 1
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        Event     currentEvent = Event.current;
        EventType type         = currentEvent.type;

        int x = cellPadding, y = cellPadding;
        int width    = Screen.width - cellPadding;
        int spacingX = cellSize + cellPadding;
        int spacingY = spacingX;

        if (mMode == Mode.DetailedMode)
        {
            spacingY += 32;
        }

        GameObject dragged         = draggedObject;
        bool       isDragging      = (dragged != null);
        int        indexUnderMouse = GetCellUnderMouse(spacingX, spacingY);
        Item       selection       = isDragging ? FindItem(dragged) : null;
        string     searchFilter    = NGUISettings.searchField;

        int newTab = mTab;

        GUILayout.BeginHorizontal();
        if (GUILayout.Toggle(newTab == 0, "1", "ButtonLeft"))
        {
            newTab = 0;
        }
        if (GUILayout.Toggle(newTab == 1, "2", "ButtonMid"))
        {
            newTab = 1;
        }
        if (GUILayout.Toggle(newTab == 2, "3", "ButtonMid"))
        {
            newTab = 2;
        }
        if (GUILayout.Toggle(newTab == 3, "4", "ButtonMid"))
        {
            newTab = 3;
        }
        if (GUILayout.Toggle(newTab == 4, "5", "ButtonRight"))
        {
            newTab = 4;
        }
        GUILayout.EndHorizontal();

        if (mTab != newTab)
        {
            Save();
            mTab   = newTab;
            mReset = true;
            NGUISettings.SetInt("NGUI Prefab Tab", mTab);
            Load();
        }

        if (mReset && type == EventType.Repaint)
        {
            mReset = false;
            foreach (Item item in mItems)
            {
                GeneratePreview(item, null);
            }
            RectivateLights();
        }

        // Search field
        GUILayout.BeginHorizontal();
        {
            string after = EditorGUILayout.TextField("", searchFilter, "SearchTextField", GUILayout.Width(Screen.width - 20f));

            if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f)))
            {
                after = "";
                GUIUtility.keyboardControl = 0;
            }

            if (searchFilter != after)
            {
                NGUISettings.searchField = after;
                searchFilter             = after;
            }
        }
        GUILayout.EndHorizontal();

        bool eligibleToDrag = (currentEvent.mousePosition.y < Screen.height - 40);

        if (type == EventType.MouseDown)
        {
            mMouseIsInside = true;
        }
        else if (type == EventType.MouseDrag)
        {
            mMouseIsInside = true;

            if (indexUnderMouse != -1 && eligibleToDrag)
            {
                // Drag operation begins
                if (draggedObjectIsOurs)
                {
                    DragAndDrop.StartDrag("Prefab Tool");
                }
                currentEvent.Use();
            }
        }
        else if (type == EventType.MouseUp)
        {
            DragAndDrop.PrepareStartDrag();
            mMouseIsInside = false;
            Repaint();
        }
        else if (type == EventType.DragUpdated)
        {
            // Something dragged into the window
            mMouseIsInside = true;
            UpdateVisual();
            currentEvent.Use();
        }
        else if (type == EventType.DragPerform)
        {
            // We've dropped a new object into the window
            if (dragged != null)
            {
                if (selection != null)
                {
                    DestroyTexture(selection);
                    mItems.Remove(selection);
                }

                AddItem(dragged, indexUnderMouse);
                draggedObject = null;
            }
            mMouseIsInside = false;
            currentEvent.Use();
        }
        else if (type == EventType.DragExited || type == EventType.Ignore)
        {
            mMouseIsInside = false;
        }

        // If the mouse is not inside the window, clear the selection and dragged object
        if (!mMouseIsInside)
        {
            selection = null;
            dragged   = null;
        }

        // Create a list of indices, inserting an entry of '-1' underneath the dragged object
        BetterList <int> indices = new BetterList <int>();

        for (int i = 0; i < mItems.size;)
        {
            if (dragged != null && indices.size == indexUnderMouse)
            {
                indices.Add(-1);
            }

            if (mItems[i] != selection)
            {
                if (string.IsNullOrEmpty(searchFilter) ||
                    mItems[i].prefab.name.IndexOf(searchFilter, System.StringComparison.CurrentCultureIgnoreCase) != -1)
                {
                    indices.Add(i);
                }
            }
            ++i;
        }

        // There must always be '-1' (Add/Move slot) present
        if (!indices.Contains(-1))
        {
            indices.Add(-1);
        }

        // We want to start dragging something from within the window
        if (eligibleToDrag && type == EventType.MouseDown && indexUnderMouse > -1)
        {
            GUIUtility.keyboardControl = 0;

            if (currentEvent.button == 0 && indexUnderMouse < indices.size)
            {
                int index = indices[indexUnderMouse];

                if (index != -1 && index < mItems.size)
                {
                    selection     = mItems[index];
                    draggedObject = selection.prefab;
                    dragged       = selection.prefab;
                    currentEvent.Use();
                }
            }
        }
        //else if (type == EventType.MouseUp && currentEvent.button == 1 && indexUnderMouse > mItems.size)
        //{
        //    NGUIContextMenu.AddItem("Reset", false, RemoveItem, index);
        //    NGUIContextMenu.Show();
        //}

        // Draw the scroll view with prefabs
        mPos = GUILayout.BeginScrollView(mPos);
        {
            Color normal = new Color(1f, 1f, 1f, 0.5f);

            for (int i = 0; i < indices.size; ++i)
            {
                int  index = indices[i];
                Item ent   = (index != -1) ? mItems[index] : selection;

                if (ent != null && ent.prefab == null)
                {
                    mItems.RemoveAt(index);
                    continue;
                }

                Rect rect  = new Rect(x, y, cellSize, cellSize);
                Rect inner = rect;
                inner.xMin += 2f;
                inner.xMax -= 2f;
                inner.yMin += 2f;
                inner.yMax -= 2f;
                rect.yMax  -= 1f;                // Button seems to be mis-shaped. It's height is larger than its width by a single pixel.

                if (!isDragging && (mMode == Mode.CompactMode || (ent == null || ent.tex != null)))
                {
                    mContent.tooltip = (ent != null) ? ent.prefab.name : "Click to add";
                }
                else
                {
                    mContent.tooltip = "";
                }

                //if (ent == selection)
                {
                    GUI.color = normal;
                    NGUIEditorTools.DrawTiledTexture(inner, NGUIEditorTools.backdropTexture);
                }

                GUI.color           = Color.white;
                GUI.backgroundColor = normal;

                if (GUI.Button(rect, mContent, "Button"))
                {
                    if (ent == null || currentEvent.button == 0)
                    {
                        string path = EditorUtility.OpenFilePanel("Add a prefab", NGUISettings.currentPath, "prefab");

                        if (!string.IsNullOrEmpty(path))
                        {
                            NGUISettings.currentPath = System.IO.Path.GetDirectoryName(path);
                            Item newEnt = CreateItemByPath(path);

                            if (newEnt != null)
                            {
                                mItems.Add(newEnt);
                                Save();
                            }
                        }
                    }
                    else if (currentEvent.button == 1)
                    {
                        NGUIContextMenu.AddItem("Delete", false, RemoveItem, index);
                        NGUIContextMenu.Show();
                    }
                }

                string caption = (ent == null) ? "" : ent.prefab.name.Replace("Control - ", "");

                if (ent != null)
                {
                    if (ent.tex != null)
                    {
                        GUI.DrawTexture(inner, ent.tex);
                    }
                    else if (mMode != Mode.DetailedMode)
                    {
                        GUI.Label(inner, caption, mStyle);
                        caption = "";
                    }
                }
                else
                {
                    GUI.Label(inner, "Add", mStyle);
                }

                if (mMode == Mode.DetailedMode)
                {
                    GUI.backgroundColor = new Color(1f, 1f, 1f, 0.5f);
                    GUI.contentColor    = new Color(1f, 1f, 1f, 0.7f);
                    GUI.Label(new Rect(rect.x, rect.y + rect.height, rect.width, 32f), caption, "ProgressBarBack");
                    GUI.contentColor    = Color.white;
                    GUI.backgroundColor = Color.white;
                }

                x += spacingX;

                if (x + spacingX > width)
                {
                    y += spacingY;
                    x  = cellPadding;
                }
            }
            GUILayout.Space(y);
        }
        GUILayout.EndScrollView();

        // Mode
        Mode modeAfter = (Mode)EditorGUILayout.EnumPopup(mMode);

        if (modeAfter != mMode)
        {
            mMode  = modeAfter;
            mReset = true;
            NGUISettings.SetEnum("NGUI Prefab Mode", mMode);
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Draw the label's properties.
    /// </summary>

    protected override bool ShouldDrawProperties()
    {
        mLabel = mWidget as UILabel;

        GUILayout.BeginHorizontal();

#if DYNAMIC_FONT
        mFontType = (FontType)EditorGUILayout.EnumPopup(mFontType, "DropDown", GUILayout.Width(74f));
        if (NGUIEditorTools.DrawPrefixButton("Font", GUILayout.Width(64f)))
#else
        mFontType = FontType.NGUI;
        if (NGUIEditorTools.DrawPrefixButton("Font", GUILayout.Width(74f)))
#endif
        {
            if (mFontType == FontType.NGUI)
            {
                ComponentSelector.Show <UIFont>(OnNGUIFont);
            }
            else
            {
                ComponentSelector.Show <Font>(OnUnityFont, new string[] { ".ttf", ".otf" });
            }
        }

        bool isValid           = false;
        SerializedProperty fnt = null;
        SerializedProperty ttf = null;

        if (mFontType == FontType.NGUI)
        {
            GUI.changed = false;
            fnt         = NGUIEditorTools.DrawProperty("", serializedObject, "mFont", GUILayout.MinWidth(40f));

            if (fnt.objectReferenceValue != null)
            {
                if (GUI.changed)
                {
                    serializedObject.FindProperty("mTrueTypeFont").objectReferenceValue = null;
                }
                NGUISettings.ambigiousFont = fnt.objectReferenceValue;
                isValid = true;
            }
        }
        else
        {
            GUI.changed = false;
            ttf         = NGUIEditorTools.DrawProperty("", serializedObject, "mTrueTypeFont", GUILayout.MinWidth(40f));

            if (ttf.objectReferenceValue != null)
            {
                if (GUI.changed)
                {
                    serializedObject.FindProperty("mFont").objectReferenceValue = null;
                }
                NGUISettings.ambigiousFont = ttf.objectReferenceValue;
                isValid = true;
            }
        }

        GUILayout.EndHorizontal();

        if (mFontType == FontType.Unity)
        {
            EditorGUILayout.HelpBox("Dynamic fonts suffer from issues in Unity itself where your characters may disappear, get garbled, or just not show at times. Use this feature at your own risk.\n\n" +
                                    "When you do run into such issues, please submit a Bug Report to Unity via Help -> Report a Bug (as this is will be a Unity bug, not an NGUI one).", MessageType.Warning);
        }

        if (GUILayout.Button("字 体 样 式"))
        {
            NGUISettings.SetInt("NGUI Prefab Tab", 1);
            EditorWindow.GetWindow <UIPrefabTool>(false, "Prefab Toolbar", true).Show();
        }

        EditorGUI.BeginDisabledGroup(!isValid);
        {
            UIFont uiFont  = (fnt != null) ? fnt.objectReferenceValue as UIFont : null;
            Font   dynFont = (ttf != null) ? ttf.objectReferenceValue as Font : null;

            if (uiFont != null && uiFont.isDynamic)
            {
                dynFont = uiFont.dynamicFont;
                uiFont  = null;
            }

            if (dynFont != null)
            {
                GUILayout.BeginHorizontal();
                {
                    EditorGUI.BeginDisabledGroup((ttf != null) ? ttf.hasMultipleDifferentValues : fnt.hasMultipleDifferentValues);

                    SerializedProperty prop = NGUIEditorTools.DrawProperty("Font Size", serializedObject, "mFontSize", GUILayout.Width(142f));
                    NGUISettings.fontSize = prop.intValue;

                    prop = NGUIEditorTools.DrawProperty("", serializedObject, "mFontStyle", GUILayout.MinWidth(40f));
                    NGUISettings.fontStyle = (FontStyle)prop.intValue;

                    NGUIEditorTools.DrawPadding();
                    EditorGUI.EndDisabledGroup();
                }
                GUILayout.EndHorizontal();

                NGUIEditorTools.DrawProperty("Material", serializedObject, "mMaterial");
            }
            else if (uiFont != null)
            {
                GUILayout.BeginHorizontal();
                SerializedProperty prop = NGUIEditorTools.DrawProperty("Font Size", serializedObject, "mFontSize", GUILayout.Width(142f));

                EditorGUI.BeginDisabledGroup(true);
                if (!serializedObject.isEditingMultipleObjects)
                {
                    if (mLabel.overflowMethod == UILabel.Overflow.ShrinkContent)
                    {
                        GUILayout.Label(" Actual: " + mLabel.finalFontSize + "/" + mLabel.defaultFontSize);
                    }
                    else
                    {
                        GUILayout.Label(" Default: " + mLabel.defaultFontSize);
                    }
                }
                EditorGUI.EndDisabledGroup();

                NGUISettings.fontSize = prop.intValue;
                GUILayout.EndHorizontal();
            }

            bool ww = GUI.skin.textField.wordWrap;
            GUI.skin.textField.wordWrap = true;
            SerializedProperty sp = serializedObject.FindProperty("mText");

            if (sp.hasMultipleDifferentValues)
            {
                NGUIEditorTools.DrawProperty("", sp, GUILayout.Height(128f));
            }
            else
            {
                GUIStyle style = new GUIStyle(EditorStyles.textField);
                style.wordWrap = true;

                float height = style.CalcHeight(new GUIContent(sp.stringValue), Screen.width - 100f);
                bool  offset = true;

                if (height > 90f)
                {
                    offset = false;
                    height = style.CalcHeight(new GUIContent(sp.stringValue), Screen.width - 20f);
                }
                else
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.BeginVertical(GUILayout.Width(76f));
                    GUILayout.Space(3f);
                    GUILayout.Label("Text");
                    GUILayout.EndVertical();
                    GUILayout.BeginVertical();
                }
                Rect rect = EditorGUILayout.GetControlRect(GUILayout.Height(height));

                GUI.changed = false;
                string text = EditorGUI.TextArea(rect, sp.stringValue, style);
                if (GUI.changed)
                {
                    sp.stringValue = text;
                }

                if (offset)
                {
                    GUILayout.EndVertical();
                    GUILayout.EndHorizontal();
                }
            }

            GUI.skin.textField.wordWrap = ww;

            NGUIEditorTools.DrawPaddedProperty("Modifier", serializedObject, "mModifier");

            SerializedProperty ov = NGUIEditorTools.DrawPaddedProperty("Overflow", serializedObject, "mOverflow");
            NGUISettings.overflowStyle = (UILabel.Overflow)ov.intValue;
            if (NGUISettings.overflowStyle == UILabel.Overflow.ClampContent)
            {
                NGUIEditorTools.DrawProperty("Use Ellipsis", serializedObject, "mOverflowEllipsis", GUILayout.Width(110f));
            }

            if (NGUISettings.overflowStyle == UILabel.Overflow.ResizeFreely)
            {
                GUILayout.BeginHorizontal();
                SerializedProperty s = NGUIEditorTools.DrawPaddedProperty("Max Width", serializedObject, "mOverflowWidth");
                if (s != null && s.intValue < 1)
                {
                    GUILayout.Label("unlimited");
                }
                GUILayout.EndHorizontal();
            }

            NGUIEditorTools.DrawPaddedProperty("Alignment", serializedObject, "mAlignment");

            if (dynFont != null)
            {
                NGUIEditorTools.DrawPaddedProperty("Keep crisp", serializedObject, "keepCrispWhenShrunk");
            }

            EditorGUI.BeginDisabledGroup(mLabel.bitmapFont != null && mLabel.bitmapFont.packedFontShader);
            GUILayout.BeginHorizontal();
            SerializedProperty gr = NGUIEditorTools.DrawProperty("Gradient", serializedObject, "mApplyGradient",
                                                                 GUILayout.Width(95f));

            EditorGUI.BeginDisabledGroup(!gr.hasMultipleDifferentValues && !gr.boolValue);
            {
                NGUIEditorTools.SetLabelWidth(30f);
                NGUIEditorTools.DrawProperty("Top", serializedObject, "mGradientTop", GUILayout.MinWidth(40f));
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                NGUIEditorTools.SetLabelWidth(50f);
                GUILayout.Space(79f);

                NGUIEditorTools.DrawProperty("Bottom", serializedObject, "mGradientBottom", GUILayout.MinWidth(40f));
                NGUIEditorTools.SetLabelWidth(80f);
            }
            EditorGUI.EndDisabledGroup();
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Effect", GUILayout.Width(76f));
            sp = NGUIEditorTools.DrawProperty("", serializedObject, "mEffectStyle", GUILayout.MinWidth(16f));

            EditorGUI.BeginDisabledGroup(!sp.hasMultipleDifferentValues && !sp.boolValue);
            {
                NGUIEditorTools.DrawProperty("", serializedObject, "mEffectColor", GUILayout.MinWidth(10f));
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                {
                    GUILayout.Label(" ", GUILayout.Width(56f));
                    NGUIEditorTools.SetLabelWidth(20f);
                    NGUIEditorTools.DrawProperty("X", serializedObject, "mEffectDistance.x", GUILayout.MinWidth(40f));
                    NGUIEditorTools.DrawProperty("Y", serializedObject, "mEffectDistance.y", GUILayout.MinWidth(40f));
                    NGUIEditorTools.DrawPadding();
                    NGUIEditorTools.SetLabelWidth(80f);
                }
            }
            EditorGUI.EndDisabledGroup();
            GUILayout.EndHorizontal();
            EditorGUI.EndDisabledGroup();

            sp = NGUIEditorTools.DrawProperty("Float spacing", serializedObject, "mUseFloatSpacing", GUILayout.Width(100f));

            if (!sp.boolValue)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Spacing", GUILayout.Width(56f));
                NGUIEditorTools.SetLabelWidth(20f);
                NGUIEditorTools.DrawProperty("X", serializedObject, "mSpacingX", GUILayout.MinWidth(40f));
                NGUIEditorTools.DrawProperty("Y", serializedObject, "mSpacingY", GUILayout.MinWidth(40f));
                NGUIEditorTools.DrawPadding();
                NGUIEditorTools.SetLabelWidth(80f);
                GUILayout.EndHorizontal();
            }
            else
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Spacing", GUILayout.Width(56f));
                NGUIEditorTools.SetLabelWidth(20f);
                NGUIEditorTools.DrawProperty("X", serializedObject, "mFloatSpacingX", GUILayout.MinWidth(40f));
                NGUIEditorTools.DrawProperty("Y", serializedObject, "mFloatSpacingY", GUILayout.MinWidth(40f));
                NGUIEditorTools.DrawPadding();
                NGUIEditorTools.SetLabelWidth(80f);
                GUILayout.EndHorizontal();
            }

            NGUIEditorTools.DrawProperty("Max Lines", serializedObject, "mMaxLineCount", GUILayout.Width(110f));

            GUILayout.BeginHorizontal();
            sp = NGUIEditorTools.DrawProperty("BBCode", serializedObject, "mEncoding", GUILayout.Width(100f));
            EditorGUI.BeginDisabledGroup(!sp.boolValue || mLabel.bitmapFont == null || !mLabel.bitmapFont.hasSymbols);
            NGUIEditorTools.SetLabelWidth(60f);
            NGUIEditorTools.DrawPaddedProperty("Symbols", serializedObject, "mSymbols");
            NGUIEditorTools.SetLabelWidth(80f);
            EditorGUI.EndDisabledGroup();
            GUILayout.EndHorizontal();
        }
        EditorGUI.EndDisabledGroup();
        return(isValid);
    }