Exemplo n.º 1
0
            public override void OnDrawingGizmosSelected()
            {
                var gameplayArea = Target as GameplayAreaDataControl;

                if (!gameplayArea.UsesGameplayArea)
                {
                    return;
                }

                var gameplayAreaRect = MapEditor.Current.ToRelative(MapEditor.Current.LatLonToPixels(gameplayArea.BoundingBox.ToPoints()))
                                       .ToRectD().ToRect();


                var handleRectID = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);

                EditorGUI.BeginChangeCheck();
                Handles.BeginGUI();
                var newRect = HandleUtil.HandleRect(handleRectID, gameplayAreaRect, 10, (_, __, ___) => { },
                                                    (p, hover, active) => HandleUtil.DrawSquare(p, 10, MapEditor.GetColor(Color.yellow),
                                                                                                hover ? MapEditor.GetColor(Color.red) : MapEditor.GetColor(Color.black)));

                Handles.EndGUI();
                if (EditorGUI.EndChangeCheck())
                {
                    var latLonRect = MapEditor.Current
                                     .PixelsToLatLon(MapEditor.Current.FromRelative(newRect.ToRectD().ToPoints())).ToRectD();
                    gameplayArea.BoundingBox = latLonRect;
                }
            }
            public override void OnDrawingGizmosSelected()
            {
                if (GetScenePlayerMode(SceneEditor.Current.Scene) != PlayerMode.Trajectory)
                {
                    return;
                }

                wasSelected = Target;
                var influenceArea = getInfluenceArea(Target);

                if (influenceArea == null)
                {
                    return;
                }

                var boundaries = GetElementBoundaries(Target);
                var rect       = influenceArea.ScreenRect(boundaries);

                if (!influenceArea.hasInfluenceArea())
                {
                    rect.position -= new Vector2(20, 20);
                    rect.size      = boundaries.size + new Vector2(40, 40);
                }

                rect = rect.AdjustToViewport(SceneEditor.Current.Size.x, SceneEditor.Current.Size.y, SceneEditor.Current.Viewport);

                EditorGUI.BeginChangeCheck();
                var newRect = HandleUtil.HandleRect(influenceArea.GetHashCode() + 1, rect, 10f,
                                                    (polygon, over, active) =>
                {
                    HandleUtil.DrawPolyLine(polygon, true, Color.blue);
                    HandleUtil.DrawPolygon(polygon, new Color(0, 0, 1f, 0.3f));
                },
                                                    (point, over, active) => HandleUtil.DrawSquare(point, 6.5f, Color.yellow, Color.black));

                newRect = HandleUtil.HandleRectMovement(influenceArea.GetHashCode(), newRect);

                if (EditorGUI.EndChangeCheck())
                {
                    var original = newRect.ViewportToScreen(SceneEditor.Current.Size.x, SceneEditor.Current.Size.y, SceneEditor.Current.Viewport);

                    original.position -= boundaries.position;
                    var rectFixed = FixToBoundaries(new RectInt((int)original.x, (int)original.y, (int)original.width, (int)original.height), boundaries);
                    // And then we set the values in the reference
                    influenceArea.setInfluenceArea(rectFixed.x, rectFixed.y, rectFixed.width, rectFixed.height);
                }
            }
        public override void OnDrawingGizmosSelected()
        {
            Handles.BeginGUI();

            Color lineColor     = Color.red;
            var   rectangleArea = Target as RectangleArea;


            var points = WorldToViewport(GetPoints(rectangleArea), SceneEditor.Current.Viewport, SceneEditor.Current.Size.x, SceneEditor.Current.Size.y);

            switch (Event.current.type)
            {
            case EventType.Repaint: HandleUtil.DrawPolyLine(points, true, lineColor, 5f); break;
            }

            bool pointsChanged = false;

            int index             = 1;
            int rectCornerChanged = -1;

            foreach (var point in points)
            {
                var pointControlId = GUIUtility.GetControlID("Rectangle Point".GetHashCode(), FocusType.Passive, new Rect(point, new Vector2(10, 10)));
                switch (Event.current.GetTypeForControl(pointControlId))
                {
                case EventType.Repaint: if (rectangleArea.isRectangular())
                    {
                        HandleUtil.DrawSquare(point, 10f, Color.yellow, Color.black);
                    }
                    else
                    {
                        HandleUtil.DrawPoint(point, 4.5f, Color.cyan, Color.black);
                    } break;

                case EventType.MouseDown:
                    switch (ActionSelected)
                    {
                    case 0:
                        if (GUIUtility.hotControl == 0 && (point - Event.current.mousePosition).magnitude < 10)
                        {
                            GUIUtility.hotControl = pointControlId;
                            Event.current.Use();
                        }
                        break;

                    case 2:
                        if ((point - Event.current.mousePosition).magnitude < 10)
                        {
                            rectangleArea.deletePoint(rectangleArea.getPoints()[index - 1]);
                            Event.current.Use();
                        }
                        break;
                    }
                    break;

                case EventType.MouseDrag:
                    if (GUIUtility.hotControl == pointControlId)
                    {
                        points[index - 1].x += Event.current.delta.x;
                        points[index - 1].y += Event.current.delta.y;
                        pointsChanged        = true;
                        rectCornerChanged    = index - 1;
                        Event.current.Use();
                    }
                    break;

                case EventType.MouseUp:
                    if (GUIUtility.hotControl == pointControlId)
                    {
                        GUIUtility.hotControl = 0;
                        Event.current.Use();
                    }
                    break;
                }
                index++;
            }

            var rectangleControlID = GUIUtility.GetControlID("Rectangle".GetHashCode(), FocusType.Passive);

            switch (Event.current.GetTypeForControl(rectangleControlID))
            {
            case EventType.MouseDown:
                switch (ActionSelected)
                {
                case 0:
                    if (GUIUtility.hotControl == 0 && points.Inside(Event.current.mousePosition))
                    {
                        GUIUtility.hotControl = rectangleControlID;
                        Event.current.Use();
                    }
                    break;

                case 1:
                    var point = Event.current.mousePosition;         //ViewportToWorld(Event.current.mousePosition, SceneEditor.Current.Viewport, new Vector2(800f, 600f));

                    var pointlist = points.ToList();
                    pointlist.Insert(FindInsertPos(points, point), point);
                    points = pointlist.ToArray();

                    pointsChanged = true;
                    Event.current.Use();
                    break;
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == rectangleControlID)
                {
                    points        = points.Select(p => p + Event.current.delta).ToArray();
                    pointsChanged = true;
                    Event.current.Use();
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == rectangleControlID)
                {
                    GUIUtility.hotControl = 0;
                    Event.current.Use();
                }
                break;

            case EventType.Repaint:
                if (ActionSelected == 1)
                {
                    var pos = FindInsertPos(points, Event.current.mousePosition);
                    Handles.DrawLine(points[(points.Length + pos - 1) % points.Length], Event.current.mousePosition);
                    Handles.DrawLine(Event.current.mousePosition, points[pos % points.Length]);
                }

                break;
            }


            if (pointsChanged)
            {
                this.Repaint();
                // Update points in the selection
                if (rectangleArea.isRectangular())
                {
                    switch (rectCornerChanged)
                    {
                    case 0: points[3].x += Event.current.delta.x; points[1].y += Event.current.delta.y; break;

                    case 1: points[2].x += Event.current.delta.x; points[0].y += Event.current.delta.y; break;

                    case 2: points[1].x += Event.current.delta.x; points[3].y += Event.current.delta.y; break;

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

                    var rect = ViewportToWorld(points, SceneEditor.Current.Viewport, SceneEditor.Current.Size.x, SceneEditor.Current.Size.y).ToRect();
                    rectangleArea.getRectangle().setValues(
                        Mathf.RoundToInt(rect.x),
                        Mathf.RoundToInt(rect.y),
                        Mathf.RoundToInt(rect.width),
                        Mathf.RoundToInt(rect.height));
                }
                else
                {
                    rectangleArea.getPoints().Clear();
                    rectangleArea.getPoints().AddRange(ViewportToWorld(points, SceneEditor.Current.Viewport, SceneEditor.Current.Size.x, SceneEditor.Current.Size.y));
                }
            }

            Handles.EndGUI();
        }