Esempio n. 1
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void DrawSceneView(Rect _rect)
    {
        Rect oldViewport  = new Rect(0, 0, Screen.width, Screen.height);
        Rect viewportRect = new Rect(_rect.x,
                                     position.height - _rect.yMax,
                                     _rect.width,
                                     _rect.height);

        GL.PushMatrix();

        //
        GL.Viewport(viewportRect);
        GL.LoadPixelMatrix(0.0f, _rect.width, 0.0f, _rect.height);

        // background
        float     half_w  = _rect.width / 2.0f;
        float     half_h  = _rect.height / 2.0f;
        Texture2D checker = exEditorUtility.textureCheckerboard;
        Vector2   center  = new Vector2(half_w, half_h);
        Vector2   size    = new Vector2(_rect.width, _rect.height);

        exEditorUtility.GL_DrawTexture(center,
                                       size,
                                       checker,
                                       new Rect((-half_w / scale + editCamera.transform.position.x) / checker.width,
                                                (-half_h / scale + editCamera.transform.position.y) / checker.height,
                                                _rect.width / (checker.width * scale),
                                                _rect.height / (checker.height * scale)),
                                       background);


        // center line
        float center_x = half_w - editCamera.transform.position.x * scale;
        float center_y = half_h - editCamera.transform.position.y * scale;

        exEditorUtility.GL_DrawLine(0.0f,
                                    center_y,
                                    _rect.width,
                                    center_y,
                                    new Color(0.6f, 0.6f, 0.6f));
        exEditorUtility.GL_DrawLine(center_x,
                                    0.0f,
                                    center_x,
                                    _rect.height,
                                    new Color(0.6f, 0.6f, 0.6f));

        //
        GL.LoadPixelMatrix(editCamera.transform.position.x - (_rect.width * 0.5f) / scale,
                           editCamera.transform.position.x + (_rect.width * 0.5f) / scale,
                           editCamera.transform.position.y - (_rect.height * 0.5f) / scale,
                           editCamera.transform.position.y + (_rect.height * 0.5f) / scale);

        // draw texture info
        Texture2D rawTexture = exEditorUtility.LoadAssetFromGUID <Texture2D>(curEdit.rawTextureGUID);

        if (rawTexture != null)
        {
            exEditorUtility.GL_DrawTexture(Vector2.zero, new Vector2(rawTexture.width, rawTexture.height), rawTexture, new Rect(0.0f, 0.0f, 1.0f, 1.0f), Color.white);
        }

        float half_width  = curEdit.rawWidth * 0.5f;
        float half_height = curEdit.rawHeight * 0.5f;
        int   trim_left   = curEdit.trim_x;
        int   trim_right  = curEdit.rawWidth - curEdit.trim_x - curEdit.width;
        int   trim_top    = curEdit.rawHeight - curEdit.trim_y - curEdit.height;
        int   trim_bot    = curEdit.trim_y;

        // draw pixel grid
        if (showPixelGrid)
        {
            Vector2[] points = new Vector2[(curEdit.rawWidth + curEdit.rawHeight + 2) * 2];
            int       idx    = 0;
            for (int x = 0; x < curEdit.rawWidth + 1; ++x)
            {
                points[idx]     = new Vector2(-half_width + x, -half_height);
                points[idx + 1] = new Vector2(-half_width + x, half_height);
                idx            += 2;
            }
            for (int y = 0; y < curEdit.rawHeight + 1; ++y)
            {
                points[idx]     = new Vector2(-half_width, -half_height + y);
                points[idx + 1] = new Vector2(half_width, -half_height + y);
                idx            += 2;
            }

            exEditorUtility.GL_DrawLines(points, pixelGridColor);
        }

        // draw raw-texture bounding
        if (showRawRect)
        {
            exEditorUtility.GL_DrawRectLine(new Vector3[] {
                new Vector3(-half_width, -half_height, 0.0f),
                new Vector3(-half_width, half_height, 0.0f),
                new Vector3(half_width, half_height, 0.0f),
                new Vector3(half_width, -half_height, 0.0f),
            },
                                            rawRectColor);
        }

        float trim_start_x = -half_width + trim_left;
        float trim_start_y = half_height - trim_top;
        float trim_end_x   = half_width - trim_right;
        float trim_end_y   = -half_height + trim_bot;

        // draw trimed bounding
        if (showTrimRect)
        {
            exEditorUtility.GL_DrawRectLine(new Vector3[] {
                new Vector3(trim_start_x, trim_end_y, 0.0f),
                new Vector3(trim_start_x, trim_start_y, 0.0f),
                new Vector3(trim_end_x, trim_start_y, 0.0f),
                new Vector3(trim_end_x, trim_end_y, 0.0f),
            },
                                            trimRectColor);
        }

        // draw attach points
        if (editModeIndex == 0)
        {
            for (int i = 0; i < curEdit.attachPoints.Count; ++i)
            {
                exTextureInfo.AttachInfo attachInfo = curEdit.attachPoints[i];

                float start_x = attachInfo.pos.x - 1.0f;
                float start_y = attachInfo.pos.y - 1.0f;
                float end_x   = attachInfo.pos.x + 1.0f;
                float end_y   = attachInfo.pos.y + 1.0f;

                exEditorUtility.GL_DrawRectLine(new Vector3[] {
                    new Vector3(start_x, start_y, 0.0f),
                    new Vector3(start_x, end_y, 0.0f),
                    new Vector3(end_x, end_y, 0.0f),
                    new Vector3(end_x, start_y, 0.0f),
                }, Color.green);
            }
        }

        // draw sliced line
        if (editModeIndex == 2)
        {
            float left   = trim_start_x + curEdit.borderLeft;
            float right  = trim_end_x - curEdit.borderRight;
            float top    = trim_start_y - curEdit.borderTop;
            float bottom = trim_end_y + curEdit.borderBottom;

            exEditorUtility.GL_DrawLine(left, trim_start_y, left, trim_end_y, slicedColor);
            exEditorUtility.GL_DrawLine(right, trim_start_y, right, trim_end_y, slicedColor);
            exEditorUtility.GL_DrawLine(trim_start_x, top, trim_end_x, top, slicedColor);
            exEditorUtility.GL_DrawLine(trim_start_x, bottom, trim_end_x, bottom, slicedColor);
        }

        // draw diced line
        if (editModeIndex == 3)
        {
            int xCount = curEdit.editorDiceXCount - 1;
            if (xCount > 0)
            {
                Vector2[] points = new Vector2[xCount * 2];
                int       idx    = 0;
                for (int x = 1; x <= xCount; ++x)
                {
                    points[idx]     = new Vector2(trim_start_x + x * curEdit.editorDiceUnitWidth, trim_start_y);
                    points[idx + 1] = new Vector2(trim_start_x + x * curEdit.editorDiceUnitWidth, trim_end_y);
                    idx            += 2;
                }
                exEditorUtility.GL_DrawLines(points, dicedColor);
            }
            int yCount = curEdit.editorDiceYCount - 1;
            if (yCount > 0)
            {
                Vector2[] points = new Vector2[yCount * 2];
                int       idx    = 0;
                for (int y = 1; y <= yCount; ++y)
                {
                    points[idx]     = new Vector2(trim_start_x, trim_end_y + y * curEdit.editorDiceUnitHeight);
                    points[idx + 1] = new Vector2(trim_end_x, trim_end_y + y * curEdit.editorDiceUnitHeight);
                    idx            += 2;
                }
                exEditorUtility.GL_DrawLines(points, dicedColor);
            }
        }
        GL.PopMatrix();

        GL.Viewport(oldViewport);
    }
Esempio n. 2
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    protected override void DoInspectorGUI()
    {
        base.DoInspectorGUI();

        // textureInfo
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.BeginVertical();
        GUILayout.Space(3);
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(textureInfoProp, new GUIContent("Texture Info"));
        if (EditorGUI.EndChangeCheck())
        {
            foreach (Object obj in serializedObject.targetObjects)
            {
                exSprite sp = obj as exSprite;
                if (sp)
                {
                    sp.textureInfo = textureInfoProp.objectReferenceValue as exTextureInfo;
                    if (sp.textureInfo != null)
                    {
                        if (sp.textureInfo.hasBorder)
                        {
                            sp.spriteType = exSpriteType.Sliced;
                            sp.customSize = true;
                        }
                        else if (sp.textureInfo.isDiced)
                        {
                            sp.spriteType = exSpriteType.Diced;
                        }
                        else if (sp.spriteType == exSpriteType.Sliced)
                        {
                            sp.spriteType = exSpriteType.Simple;
                            sp.customSize = false;
                        }
                        else if (sp.spriteType == exSpriteType.Diced)
                        {
                            sp.spriteType = exSpriteType.Simple;
                        }
                    }
                    EditorUtility.SetDirty(sp);
                }
            }
        }
        EditorGUILayout.EndVertical();
        if (GUILayout.Button("Refresh", GUILayout.Width(57), GUILayout.Height(16)))
        {
            foreach (Object obj in serializedObject.targetObjects)
            {
                exSprite sp = obj as exSprite;
                if (sp)
                {
                    sp.textureInfo = sp.textureInfo;
                    EditorUtility.SetDirty(sp);
                }
            }
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space();

        // draw preview rect
        if (serializedObject.isEditingMultipleObjects == false)
        {
            float preview_width  = 100.0f;
            float preview_height = 100.0f;
            Rect  lastRect       = GUILayoutUtility.GetLastRect();

            float indent_space = 20.0f;
            Rect  previewRect  = new Rect(indent_space,
                                          lastRect.yMax,
                                          preview_width,
                                          preview_height);

            // preview
            if (Event.current.type == EventType.Repaint)
            {
                exTextureInfo textureInfo = textureInfoProp.objectReferenceValue as exTextureInfo;

                // draw Checker
                Texture2D checker = exEditorUtility.textureCheckerboard;
                GUI.DrawTextureWithTexCoords(previewRect,
                                             checker,
                                             new Rect(0.0f, 0.0f, 3.0f, 3.0f));

                // draw TextureInfo
                if (textureInfo != null)
                {
                    float scale = exEditorUtility.CalculateTextureInfoScale(previewRect, textureInfo);
                    Rect  pos   = new Rect(previewRect.center.x - textureInfo.width * 0.5f * scale + 2.0f,
                                           previewRect.center.y - textureInfo.height * 0.5f * scale + 2.0f,
                                           textureInfo.width * scale - 4.0f,
                                           textureInfo.height * scale - 4.0f);
                    exEditorUtility.GUI_DrawTextureInfo(pos,
                                                        textureInfo,
                                                        Color.white);
                }

                // draw border
                exEditorUtility.GL_DrawRectLine(new Vector3 [] {
                    new Vector3(indent_space, lastRect.yMax, 0.0f),
                    new Vector3(indent_space + preview_width, lastRect.yMax, 0.0f),
                    new Vector3(indent_space + preview_width, lastRect.yMax + preview_height, 0.0f),
                    new Vector3(indent_space, lastRect.yMax + preview_height, 0.0f),
                },
                                                new Color(0.8f, 0.8f, 0.8f, 1.0f));
            }
            GUILayoutUtility.GetRect(preview_width, preview_height);

            // edit button
            Rect editBtnPos = new Rect(previewRect.xMax - 50 - 2, previewRect.yMax - 20 - 2, 50, 20);
            if (GUI.Button(editBtnPos, "Edit..."))
            {
                EditorWindow.GetWindow <exTextureInfoEditor>().Edit(textureInfoProp.objectReferenceValue as exTextureInfo);
            }
        }

        EditorGUILayout.Space();

        // useTextureOffset
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(useTextureOffsetProp, new GUIContent("Use Texture Offset"));
        if (EditorGUI.EndChangeCheck())
        {
            foreach (Object obj in serializedObject.targetObjects)
            {
                exSprite sp = obj as exSprite;
                if (sp)
                {
                    sp.useTextureOffset = useTextureOffsetProp.boolValue;
                    EditorUtility.SetDirty(sp);
                }
            }
        }

        // type
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(spriteTypeProp, new GUIContent("Sprite Type"));
        if (EditorGUI.EndChangeCheck())
        {
            foreach (Object obj in serializedObject.targetObjects)
            {
                exSprite sp = obj as exSprite;
                if (sp)
                {
                    sp.spriteType = (exSpriteType)spriteTypeProp.intValue;
                    EditorUtility.SetDirty(sp);
                }
            }
        }

        if (spriteTypeProp.enumValueIndex == (int)exSpriteType.Tiled)
        {
            customSizeProp.boolValue = true;

            ++EditorGUI.indentLevel;
            // tiled spacing
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(tiledSpacingProp, new GUIContent("Tiled Spacing"), true);
            if (EditorGUI.EndChangeCheck())
            {
                foreach (Object obj in serializedObject.targetObjects)
                {
                    exSprite sp = obj as exSprite;
                    if (sp)
                    {
                        sp.tiledSpacing = tiledSpacingProp.vector2Value;
                        if (sp.textureInfo != null)
                        {
                            sp.tiledSpacing = new Vector2(Mathf.Max(-sp.textureInfo.width + 1, sp.tiledSpacing.x),
                                                          Mathf.Max(-sp.textureInfo.height + 1, sp.tiledSpacing.y));
                        }
                        EditorUtility.SetDirty(sp);
                    }
                }
            }
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Use Raw Size", GUILayout.Width(88), GUILayout.Height(16)))
            {
                foreach (Object obj in serializedObject.targetObjects)
                {
                    exSprite sp = obj as exSprite;
                    if (sp && sp.textureInfo != null)
                    {
                        exTextureInfo ti = sp.textureInfo;
                        sp.tiledSpacing = new Vector2(ti.rawWidth - ti.width, ti.rawHeight - ti.height) / 2;
                        EditorUtility.SetDirty(sp);
                    }
                }
            }
            EditorGUILayout.EndHorizontal();
            --EditorGUI.indentLevel;
        }
        else if (spriteTypeProp.enumValueIndex == (int)exSpriteType.Sliced)
        {
            ++EditorGUI.indentLevel;
            // border only
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(borderOnlyProp, new GUIContent("Border Only"), true);
            if (EditorGUI.EndChangeCheck())
            {
                foreach (Object obj in serializedObject.targetObjects)
                {
                    exSprite sp = obj as exSprite;
                    if (sp)
                    {
                        sp.borderOnly = borderOnlyProp.boolValue;
                        EditorUtility.SetDirty(sp);
                    }
                }
            }

            // custom border size
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(customBorderSizeProp, new GUIContent("Custom Border Size"), true);
            if (EditorGUI.EndChangeCheck())
            {
                foreach (Object obj in serializedObject.targetObjects)
                {
                    exSprite sp = obj as exSprite;
                    if (sp)
                    {
                        sp.customBorderSize = customBorderSizeProp.boolValue;
                        EditorUtility.SetDirty(sp);
                    }
                }
            }

            if (customBorderSizeProp.boolValue)
            {
                ++EditorGUI.indentLevel;
                // left right top bottom
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(leftProp, new GUIContent("Left"), true);
                EditorGUILayout.PropertyField(rightProp, new GUIContent("Right"), true);
                EditorGUILayout.PropertyField(topProp, new GUIContent("Top"), true);
                EditorGUILayout.PropertyField(bottomProp, new GUIContent("Bottom"), true);
                if (EditorGUI.EndChangeCheck())
                {
                    foreach (Object obj in serializedObject.targetObjects)
                    {
                        exSprite sp = obj as exSprite;
                        if (sp)
                        {
                            sp.leftBorderSize   = leftProp.floatValue;
                            sp.rightBorderSize  = rightProp.floatValue;
                            sp.topBorderSize    = topProp.floatValue;
                            sp.bottomBorderSize = bottomProp.floatValue;
                            EditorUtility.SetDirty(sp);
                        }
                    }
                }

                // reset border size
                EditorGUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Reset", GUILayout.Width(57), GUILayout.Height(16)))
                {
                    foreach (Object obj in serializedObject.targetObjects)
                    {
                        exSprite sp = obj as exSprite;
                        if (sp)
                        {
                            sp.leftBorderSize   = sp.textureInfo.borderLeft;
                            sp.rightBorderSize  = sp.textureInfo.borderRight;
                            sp.topBorderSize    = sp.textureInfo.borderTop;
                            sp.bottomBorderSize = sp.textureInfo.borderBottom;
                            EditorUtility.SetDirty(sp);
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
                --EditorGUI.indentLevel;
            }

            --EditorGUI.indentLevel;
        }

        EditorGUILayout.Space();

        // DISABLE {
        // EditorGUILayout.Space();
        // GUILayout.BeginHorizontal();
        // GUILayout.FlexibleSpace();
        //     if ( GUILayout.Button("Edit...", GUILayout.Width(50), GUILayout.Height(20) ) ) {
        //         EditorWindow.GetWindow<exSceneEditor>();
        //     }
        // GUILayout.Space(5);
        // GUILayout.EndHorizontal();
        // } DISABLE end

        // custom border size
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(enableAttachPointsProp, new GUIContent("Custom Border Size"), true);
        if (EditorGUI.EndChangeCheck())
        {
            foreach (Object obj in serializedObject.targetObjects)
            {
                exSprite sp = obj as exSprite;
                if (sp)
                {
                    sp.enableAttachPoints = enableAttachPointsProp.boolValue;
                    EditorUtility.SetDirty(sp);
                }
            }
        }

        if (GUILayout.Button("Create/Update Attaches", GUILayout.Height(20)))
        {
            foreach (Object obj in serializedObject.targetObjects)
            {
                exSprite sp = obj as exSprite;
                if (sp && sp.textureInfo)
                {
                    for (int i = 0; i < sp.textureInfo.attachPoints.Count; ++i)
                    {
                        exTextureInfo.AttachInfo attachInfo = sp.textureInfo.attachPoints[i];
                        Transform  trans = sp.transform.Find(attachInfo.name);
                        GameObject go    = null;
                        if (trans == null)
                        {
                            go = new GameObject();
                        }
                        else
                        {
                            go = trans.gameObject;
                        }
                        go.name                    = attachInfo.name;
                        go.transform.parent        = sp.transform;
                        go.transform.localPosition = sp.CalcAttachPointLocalPosition(attachInfo.pos);
                    }
                }
            }
        }

        EditorGUILayout.Space();
    }
Esempio n. 3
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void Settings(int _width)
    {
        EditorGUILayout.BeginHorizontal(new GUILayoutOption [] {
            GUILayout.Width(_width),
            GUILayout.MinWidth(_width),
            GUILayout.MaxWidth(_width),
            GUILayout.ExpandWidth(false),
        });

        GUILayout.Space(10);

        EditorGUILayout.BeginVertical();

        showTrimRect  = EditorGUILayout.Toggle("Show Trimed Rect", showTrimRect);
        trimRectColor = EditorGUILayout.ColorField("Trimed Rect Color", trimRectColor);

        showRawRect  = EditorGUILayout.Toggle("Show Raw Rect", showRawRect);
        rawRectColor = EditorGUILayout.ColorField("Raw Rect Color", rawRectColor);

        showPixelGrid  = EditorGUILayout.Toggle("Show Pixel Grid", showPixelGrid);
        pixelGridColor = EditorGUILayout.ColorField("Pixel Grid Color", pixelGridColor);

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        switch (editModeIndex)
        {
        // attach pionts
        case 0:
            GUILayout.Label("Attach Points:");
            EditorGUI.BeginChangeCheck();
            for (int i = 0; i < curEdit.attachPoints.Count; ++i)
            {
                exTextureInfo.AttachInfo info = curEdit.attachPoints[i];
                EditorGUILayout.BeginHorizontal();
                info.name = GUILayout.TextField(info.name, GUILayout.Width(80));
                info.pos  = EditorGUILayout.Vector2Field("", info.pos, GUILayout.Width(150));
                curEdit.attachPoints[i] = info;

                if (GUILayout.Button("Delete", GUILayout.Width(50), GUILayout.Height(15)))
                {
                    curEdit.attachPoints.RemoveAt(i);
                    --i;
                }
                EditorGUILayout.EndHorizontal();
            }

            if (GUILayout.Button("Add...", GUILayout.Width(50), GUILayout.Height(20)))
            {
                curEdit.attachPoints.Add(new exTextureInfo.AttachInfo("attach", Vector2.zero));
            }

            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(curEdit);
            }

            break;

        // collision
        case 1:
            GUILayout.Label("Coming Soon...");
            break;

        // sliced
        case 2:
            slicedColor = EditorGUILayout.ColorField("Color", slicedColor);
            EditorGUI.BeginChangeCheck();
            int result = EditorGUILayout.IntField("Left", curEdit.borderLeft);
            curEdit.borderLeft = System.Math.Min(System.Math.Max(0, result), curEdit.width - curEdit.borderRight);

            result = EditorGUILayout.IntField("Right", curEdit.borderRight);
            curEdit.borderRight = System.Math.Min(System.Math.Max(0, result), curEdit.width - curEdit.borderLeft);

            result            = EditorGUILayout.IntField("Top", curEdit.borderTop);
            curEdit.borderTop = System.Math.Min(System.Math.Max(0, result), curEdit.height - curEdit.borderBottom);

            result = EditorGUILayout.IntField("Bottom", curEdit.borderBottom);
            curEdit.borderBottom = System.Math.Min(System.Math.Max(0, result), curEdit.height - curEdit.borderTop);

            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(curEdit);
            }
            break;

        // diced
        case 3:
            dicedColor = EditorGUILayout.ColorField("Color", dicedColor);
            EditorGUI.BeginChangeCheck();
            int dicedX = EditorGUILayout.IntField("Dice X", curEdit.rawEditorDiceUnitWidth);
            curEdit.rawEditorDiceUnitWidth = Mathf.Clamp(dicedX, 0, curEdit.width);

            int dicedY = EditorGUILayout.IntField("Dice Y", curEdit.rawEditorDiceUnitHeight);
            curEdit.rawEditorDiceUnitHeight = Mathf.Clamp(dicedY, 0, curEdit.height);

            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(curEdit);
            }
            break;
        }

        EditorGUILayout.EndVertical();

        EditorGUILayout.EndHorizontal();
    }