Example #1
0
    public static Rect HandleRect(int handleId, Rect rect, float maxPointDistance, Action <Vector2[]> drawPolygon, Action <Vector2> drawPoint)
    {
        var     points        = rect.ToPoints();
        int     pointChanged  = -1;
        Vector2 oldPointValue = Vector2.zero;

        if (Event.current.type == EventType.Repaint)
        {
            drawPolygon(points);
        }

        for (int i = 0; i < points.Length; i++)
        {
            EditorGUI.BeginChangeCheck();
            var aux = points[i];
            points[i] = HandleUtil.HandlePointMovement(handleId + i + 1, points[i], 10f, p => drawPoint(p));
            if (EditorGUI.EndChangeCheck())
            {
                oldPointValue = aux;
                points[i]     = Event.current.mousePosition;
                pointChanged  = i;
            }
        }

        if (pointChanged != -1)
        {
            GUI.changed = true;
            var diff = points[pointChanged] - oldPointValue;
            // Fix the change
            switch (pointChanged)
            {
            case 0: points[3].x += diff.x; points[1].y += diff.y; break;

            case 1: points[2].x += diff.x; points[0].y += diff.y; break;

            case 2: points[1].x += diff.x; points[3].y += diff.y; break;

            case 3: points[0].x += diff.x; points[2].y += diff.y; break;
            }

            //First we calculate the new original screen rect
            var original = points.ToRect();

            rect = original;
        }

        return(rect);
    }
            void OnGUI()
            {
                if (guiMap == null)
                {
                    guiMap = new GUIMap();
                }


                debugWindowRect = GUI.Window(12341234, debugWindowRect, (id) =>
                {
                    var mapRect = new Rect(2, 18, 196, 180);
                    using (new GUILayout.AreaScope(mapRect))
                    {
                        guiMap.Center = GeoExtension.Instance.Location;
                        guiMap.Zoom   = 17;
                        guiMap.DrawMap(new Rect(0, 0, 196, 180));
                        // Calculate the player pixel relative to the map
                        var playerMeters        = MapzenGo.Helpers.GM.LatLonToMeters(GeoExtension.Instance.Location);
                        var playerPixel         = MapzenGo.Helpers.GM.MetersToPixels(playerMeters, guiMap.Zoom);
                        var playerPixelRelative = playerPixel + guiMap.PATR;

                        // Do the point handling
                        var pointControl = GUIUtility.GetControlID("PlayerPosition".GetHashCode(), FocusType.Passive);
                        EditorGUI.BeginChangeCheck();
                        var newPlayerPixel = HandleUtil.HandlePointMovement(pointControl, playerPixelRelative.ToVector2(), 10,
                                                                            (point, isOver, isActive) => HandleUtil.DrawPoint(point, 4, Color.cyan,
                                                                                                                              Color.black), MouseCursor.MoveArrow);
                        if (EditorGUI.EndChangeCheck())
                        {
                            // If changed, restore the point to the geochar
                            playerPixel  = newPlayerPixel.ToVector2d() - guiMap.PATR;
                            playerMeters = MapzenGo.Helpers.GM.PixelsToMeters(playerPixel, guiMap.Zoom);
                            GeoExtension.Instance.Location = MapzenGo.Helpers.GM.MetersToLatLon(playerMeters);
                        }

                        guiMap.ProcessEvents(mapRect);
                    }
                    GUI.DragWindow();
                },
                                             "DebugWindow");
            }
Example #3
0
    public static Rect HandleRect(int handleId, Rect rect, float maxPointDistance, Action <Vector2[], bool, bool> drawPolygon, Action <Vector2, bool, bool> drawPoint)
    {
        var     points        = rect.ToPoints();
        int     pointChanged  = -1;
        Vector2 oldPointValue = Vector2.zero;

        int  overActivePoint = -1;
        bool over            = rect.Contains(Event.current.mousePosition);
        bool active          = GUIUtility.hotControl == handleId;

        for (int i = 0; i < points.Length; i++)
        {
            EditorGUI.BeginChangeCheck();
            var aux     = points[i];
            var pointId = GUIUtility.GetControlID(handleId + i + 1, FocusType.Passive);
            points[i] = HandleUtil.HandlePointMovement(pointId, points[i], 10f, (p, o, a) => {
                if (o || a)
                {
                    overActivePoint = i;
                    over            = o;
                    active          = a;
                }
            }, i % 2 == 0 ? MouseCursor.ResizeUpLeft : MouseCursor.ResizeUpRight);

            if (EditorGUI.EndChangeCheck())
            {
                oldPointValue = aux;
                points[i]     = Event.current.mousePosition;
                pointChanged  = i;
            }
        }

        if (Event.current.type == EventType.Repaint)
        {
            drawPolygon(points, overActivePoint == -1 && over, overActivePoint == -1 && active);
            for (int i = 0; i < points.Length; i++)
            {
                drawPoint(points[i], overActivePoint == i && over, overActivePoint == i && active);
            }
        }


        if (pointChanged != -1)
        {
            GUI.changed = true;
            var diff = points[pointChanged] - oldPointValue;
            // Fix the change
            switch (pointChanged)
            {
            case 0: points[3].x += diff.x; points[1].y += diff.y; break;

            case 1: points[2].x += diff.x; points[0].y += diff.y; break;

            case 2: points[1].x += diff.x; points[3].y += diff.y; break;

            case 3: points[0].x += diff.x; points[2].y += diff.y; break;
            }

            //First we calculate the new original screen rect
            var original = points.ToRect();

            rect = original;
        }

        return(rect);
    }