Example #1
0
    private static void DrawFaceEditor(ref int face, Atlas atlas, ref Matrix4x4 matrix)
    {
        GUILayout.BeginVertical(GUI.skin.box);
        Texture texture = atlas.GetTexture();
        Rect    rect    = GUILayoutUtility.GetAspectRect((float)texture.width / texture.height);

        GUILayout.EndVertical();

        Matrix4x4 rectMatrix    = Matrix4x4.Scale(new Vector3(rect.width, rect.height, 0)) * matrix;
        Matrix4x4 invRectMatrix = matrix.inverse * Matrix4x4.Scale(new Vector3(1 / rect.width, 1 / rect.height, 0));
        Matrix4x4 invertY       = Matrix4x4.TRS(new Vector2(0, 1), Quaternion.identity, new Vector2(1, -1));

        bool mouseInRect = rect.Contains(Event.current.mousePosition);

        GUI.BeginGroup(rect);
        {
            Vector2 mouse = invRectMatrix.MultiplyPoint(Event.current.mousePosition);             // local mouse [0..1]

            if (Event.current.type == EventType.Repaint)
            {
                Rect texturePosition = Mul(new Rect(0, 0, 1, 1), rectMatrix);
                Rect faceRet         = atlas.ToRect(face);
                faceRet = Mul(faceRet, rectMatrix * invertY);
                GUI.DrawTexture(texturePosition, texture);
                EditorGUIUtils.DrawRect(faceRet, Color.green);
            }

            if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && mouseInRect)
            {
                Vector2 invMouse = invertY.MultiplyPoint(mouse);
                if (invMouse.x >= 0 && invMouse.x <= 1 && invMouse.y >= 0 && invMouse.y <= 1)
                {
                    int posX = Mathf.FloorToInt(invMouse.x * atlas.GetWidth());
                    int posY = Mathf.FloorToInt(invMouse.y * atlas.GetHeight());
                    face = posY * atlas.GetWidth() + posX;

                    GUI.changed = true;
                    Event.current.Use();
                }
            }

            if (Event.current.type == EventType.MouseDrag && Event.current.button == 1 && mouseInRect)
            {
                Vector3 delta = Event.current.delta;
                delta.x /= rect.width;
                delta.y /= rect.height;

                Matrix4x4 offsetMatrix = Matrix4x4.TRS(delta, Quaternion.identity, Vector3.one);
                matrix = offsetMatrix * matrix;

                GUI.changed = true;
                Event.current.Use();
            }

            if (Event.current.type == EventType.ScrollWheel && mouseInRect)
            {
                float s = 0.95f;
                if (Event.current.delta.y < 0)
                {
                    s = 1.0f / s;
                }

                Matrix4x4 offsetMatrix = Matrix4x4.TRS(mouse, Quaternion.identity, Vector3.one);
                matrix *= offsetMatrix;

                Matrix4x4 scaleMatrix = Matrix4x4.Scale(Vector3.one * s);
                matrix *= scaleMatrix;

                offsetMatrix = Matrix4x4.TRS(-mouse, Quaternion.identity, Vector3.one);
                matrix      *= offsetMatrix;

                GUI.changed = true;
                Event.current.Use();
            }
        }
        GUI.EndGroup();
    }