Example #1
0
 public UnityEngine.Rect ToRect(int pos)
 {
     if (_atlas != null)
     {
         return(_atlas.ToRect(pos));
     }
     return(default(UnityEngine.Rect));
 }
Example #2
0
    private static void DrawFaceEditor(ref int face, OCAtlas atlas, ref UnityEngine.Matrix4x4 matrix)
    {
        UnityEngine.GUILayout.BeginVertical(UnityEngine.GUI.skin.box);
        UnityEngine.Texture texture = atlas.Texture;
        UnityEngine.Rect    rect    = UnityEngine.GUILayoutUtility.GetAspectRect((float)texture.width / texture.height);
        UnityEngine.GUILayout.EndVertical();

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

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

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

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

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

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

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

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

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

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

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

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

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

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