Exemple #1
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
#if UNITY_4_3
        EditorGUIUtility.LookLikeControls(80f);
#else
        EditorGUIUtility.labelWidth = 80f;
#endif

        bool close = false;
        GUILayout.Label("Icons", "LODLevelNotifyText");
        NJGEditorTools.DrawSeparator();

        GUILayout.BeginHorizontal();
        GUILayout.Space(84f);

        string before = mSearch;
        string after  = EditorGUILayout.TextField("", before, "SearchTextField");
        mSearch = after;

        if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f)))
        {
            mSearch = "";
            GUIUtility.keyboardControl = 0;
        }
        GUILayout.Space(84f);
        GUILayout.EndHorizontal();

        Texture2D tex = mAtlas.texture as Texture2D;

        if (tex == null)
        {
            GUILayout.Label("The atlas doesn't have a texture to work with");
            return;
        }

        List <string> sprites = mAtlas.GetListOfSprites(mSearch);

        float size    = 80f;
        float padded  = size + 10f;
        int   columns = Mathf.FloorToInt(Screen.width / padded);
        if (columns < 1)
        {
            columns = 1;
        }

        int  offset = 0;
        Rect rect   = new Rect(10f, 0, size, size);

        GUILayout.Space(10f);
        mPos = GUILayout.BeginScrollView(mPos);

        while (offset < sprites.Count)
        {
            GUILayout.BeginHorizontal();
            {
                int col = 0;
                rect.x = 10f;

                for (; offset < sprites.Count; ++offset)
                {
                    NJGAtlas.Sprite sprite = mAtlas.GetSprite(sprites[offset]);
                    if (sprite == null)
                    {
                        continue;
                    }

                    // Button comes first
                    if (GUI.Button(rect, ""))
                    {
                        float delta = Time.realtimeSinceStartup - mClickTime;
                        mClickTime = Time.realtimeSinceStartup;

                        if (mSprite != sprite.name)
                        {
                            mSprite = sprite.name;
                            if (mCallback != null)
                            {
                                mCallback(mSprite);
                            }
                        }
                        else if (delta < 0.5f)
                        {
                            close = true;
                        }
                    }

                    if (Event.current.type == EventType.Repaint)
                    {
                        // On top of the button we have a checkboard grid
                        NJGEditorTools.DrawTiledTexture(rect, NJGEditorTools.backdropTexture);

                        // Calculate the texture's scale that's needed to display the sprite in the clipped area
                        float scaleX = rect.width / sprite.width;
                        float scaleY = rect.height / sprite.height;

                        // Stretch the sprite so that it will appear proper
                        float aspect   = (scaleY / scaleX) / ((float)sprite.height / sprite.width);
                        Rect  clipRect = rect;

                        if (aspect != 1f)
                        {
                            if (aspect < 1f)
                            {
                                // The sprite is taller than it is wider
                                float padding = size * (1f - aspect) * 0.5f;
                                clipRect.xMin += padding;
                                clipRect.xMax -= padding;
                            }
                            else
                            {
                                // The sprite is wider than it is taller
                                float padding = size * (1f - 1f / aspect) * 0.5f;
                                clipRect.yMin += padding;
                                clipRect.yMax -= padding;
                            }
                        }

                        GUI.DrawTextureWithTexCoords(clipRect, tex, sprite.uvs);

                        // Draw the selection
                        if (mSprite == sprite.name)
                        {
                            NJGEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f));
                        }
                    }

                    if (++col >= columns)
                    {
                        ++offset;
                        break;
                    }
                    rect.x += padded;
                }
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(padded);
            rect.y += padded;
        }
        GUILayout.EndScrollView();
        if (close)
        {
            Close();
        }
    }
Exemple #2
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
#if UNITY_4_3
        EditorGUIUtility.LookLikeControls(80f);
#else
        EditorGUIUtility.labelWidth = 80f;
#endif

        bool close = false;
        GUILayout.Label(string.IsNullOrEmpty(mTitle) ? "Choose a Key" : mTitle, "LODLevelNotifyText");
        NJGEditorTools.DrawSeparator();

        GUILayout.BeginHorizontal();
        GUILayout.Space(84f);

        string before = mSearch;
        string after  = EditorGUILayout.TextField("", before, "SearchTextField");
        mSearch = after;

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

        GUILayout.Space(84f);
        GUILayout.EndHorizontal();

        GUIStyle centered = GUI.skin.GetStyle("Label");
        centered.alignment = TextAnchor.UpperCenter;

        GUILayout.Label("Keys in use are disabled and marked as red", centered);

        centered.alignment = TextAnchor.UpperLeft;

        float w       = 120f;
        float wPad    = w + 5f;
        float h       = 30f;
        float hPad    = h + 5f;
        int   columns = Mathf.FloorToInt(Screen.width / wPad);
        if (columns < 1)
        {
            columns = 1;
        }

        int  offset = 0;
        Rect rect   = new Rect(10f, 0, w, h);

        GUILayout.Space(10f);
        mPos = GUILayout.BeginScrollView(mPos);

        if (keyCodeMods != null)
        {
            KeyCode[] keyCodes = GetKeyCodes(mSearch);

            while (offset < keyCodes.Length)
            {
                GUILayout.BeginHorizontal();
                {
                    int col = 0;
                    rect.x = 10f;

                    for (; offset < keyCodes.Length; ++offset)
                    {
                        KeyCode key     = keyCodes[offset];
                        bool    isInUse = IsInUse(key);

                        // Button comes first
                        GUI.enabled = !isInUse; // && key != KeyCode.None
                        if (key == mKeyCode)
                        {
                            GUI.enabled = true;
                        }
                        if (GUI.Button(rect, key.ToString()))
                        {
                            float delta = Time.realtimeSinceStartup - mClickTime;
                            mClickTime = Time.realtimeSinceStartup;

                            if (mKeyCode != key)
                            {
                                mKeyCode = key;
                                if (mCallbackKeyCode != null)
                                {
                                    mCallbackKeyCode(mKeyCode);
                                }
                            }
                            else if (delta < 0.5f)
                            {
                                close = true;
                            }
                        }

                        if (Event.current.type == EventType.Repaint)
                        {
                            // Draw the selection
                            if (key == mKeyCode)
                            {
                                NJGEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f));
                            }
                            else if (isInUse) // && key != KeyCode.None
                            {
                                NJGEditorTools.DrawOutline(rect, Color.red);
                            }
                        }

                        GUI.enabled = true;

                        if (++col >= columns)
                        {
                            ++offset;
                            break;
                        }
                        rect.x += wPad;
                    }
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(hPad);
                rect.y += hPad;
            }
        }
        else
        {
            string[] keys = GetKeys(mSearch);

            while (offset < keys.Length)
            {
                GUILayout.BeginHorizontal();
                {
                    int col = 0;
                    rect.x = 10f;

                    for (; offset < keys.Length; ++offset)
                    {
                        string key     = keys[offset];
                        bool   isInUse = IsInUse(key);

                        // Button comes first
                        GUI.enabled = !isInUse; //&& key != "None"
                        if (GUI.Button(rect, key))
                        {
                            float delta = Time.realtimeSinceStartup - mClickTime;
                            mClickTime = Time.realtimeSinceStartup;

                            if (mKey != key)
                            {
                                mKey = key;
                                if (mCallback != null)
                                {
                                    mCallback(mKey);
                                }
                            }
                            else if (delta < 0.5f)
                            {
                                close = true;
                            }
                        }

                        if (Event.current.type == EventType.Repaint)
                        {
                            // Draw the selection
                            if (key == mKey)
                            {
                                NJGEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f));
                            }

                            if (isInUse) // && key != "None"
                            {
                                NJGEditorTools.DrawOutline(rect, Color.red);
                            }
                        }

                        GUI.enabled = true;

                        if (++col >= columns)
                        {
                            ++offset;
                            break;
                        }
                        rect.x += wPad;
                    }
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(hPad);
                rect.y += hPad;
            }
        }
        GUILayout.EndScrollView();
        if (close)
        {
            Close();
        }
    }