Beispiel #1
0
    void DrawPoint(EBWorldPainterData.Point worldPoint, TextureFills textureFill)
    {
        var     point       = new Vector3(worldPoint.location.x, 0.0f, worldPoint.location.y);
        Vector2 screenPoint = cam.WorldToViewportPoint(point) * textureSize;
        Rect    pos         = Rect.MinMaxRect(screenPoint.x - 5, (textureSize - screenPoint.y) - 5, screenPoint.x + 5, (textureSize - screenPoint.y) + 5);

        GUI.DrawTexture(pos, textures[(int)textureFill], ScaleMode.StretchToFill);
    }
Beispiel #2
0
    void OnGUI()
    {
        if (!inited)
        {
            return;
        }

        const int offset      = 20;
        Rect      textureRect = Rect.MinMaxRect(offset, offset, textureSize + offset, textureSize + offset);
        Rect      texturSize  = Rect.MinMaxRect(0, 0, textureSize, textureSize);

        SetMode();

        //Deal with the mouse
        if ((Event.current != null) && Event.current.isMouse && textureRect.Contains(Event.current.mousePosition))
        {
            Vector3 worldMousePos3 = cam.ViewportToWorldPoint(new Vector3(((float)Event.current.mousePosition.x - textureRect.x) / (float)textureSize, 1.0f - ((float)Event.current.mousePosition.y - textureRect.y) / (float)textureSize, 0.0f));
            EBWorldPainterData.Point worldMousePos = new EBWorldPainterData.Point(worldMousePos3.x, worldMousePos3.z);

            UpdateClosestPoint(worldPainterData.ClosestPoint(worldMousePos));
            UpdateClosestPoints(worldPainterData.ClosestLine(worldMousePos));
            UpdateCurrentRegions(worldPainterData.RegionsPointIsInside(worldMousePos));

            switch (mode)
            {
            case (Mode.None):
                break;

            case (Mode.AddPoints):
            {
                Vector2 point0       = closestPoints[0].location;
                Vector2 point1       = closestPoints[1].location;
                float   distToPoint0 = Vector2.Distance(point0, worldMousePos.location);
                float   distToPoint1 = Vector2.Distance(point1, worldMousePos.location);
                float   t            = distToPoint0 / (distToPoint0 + distToPoint1);
                pointToAdd = new EBWorldPainterData.Point(Vector2.Lerp(point0, point1, t));

                if (Event.current.type == EventType.MouseDown)
                {
                    var result = worldPainterData.InsertPoint(pointToAdd, closestPoints[0], closestPoints[1]);
                    if (!result)
                    {
                    }
                    Repaint();
                }
                break;
            }

            case (Mode.RemovePoints):
            {
                if (Event.current.type == EventType.MouseDown)
                {
                    var result = worldPainterData.RemovePoint(closestPoint);
                    if (!result)
                    {
                    }
                    Repaint();
                }
                break;
            }

            case (Mode.MovePoints):
            {
                if (Event.current.type == EventType.MouseDown)
                {
                    lastClickedPoint = closestPoint;
                    Repaint();
                }
                else if (Event.current.type == EventType.MouseDrag && lastClickedPoint != null)
                {
                    lastClickedPoint.location = worldMousePos.location;
                    Repaint();
                }
                break;
            }

            case (Mode.SplitRegions):
            {
                if (Event.current.type == EventType.MouseDown)
                {
                    if (lastClickedPoint == null)
                    {
                        lastClickedPoint = closestPoint;
                        Repaint();
                    }
                    else
                    {
                        EBWorldPainterData.Point nextClickedPoint = worldPainterData.ClosestPoint(worldMousePos);
                        if (nextClickedPoint != lastClickedPoint)
                        {
                            EBWorldPainterData.Region[] regions = worldPainterData.RegionsContainingPoints(lastClickedPoint, nextClickedPoint);
                            if (regions.Length == 1)
                            {
                                //don't want to split if the points are in more than one region, as they must be on a boundary of some sort, so splitting wouldn't make sense
                                //TODO: don't split things that are sequential
                                worldPainterData.SplitRegionAcrossPoints(regions[0], lastClickedPoint, nextClickedPoint);
                            }
                        }

                        lastClickedPoint = null;
                        Repaint();
                    }
                }
                break;
            }

            case (Mode.CombineRegions):
            {
                if (Event.current.type == EventType.MouseDown)
                {
                    worldPainterData.RemoveLine(closestPoints[0], closestPoints[1]);
                    UpdateCurrentRegions(worldPainterData.RegionsPointIsInside(worldMousePos));
                    Repaint();
                }
                break;
            }

            case (Mode.PickVisibleBase):
            {
                if (Event.current.type == EventType.MouseDown)
                {
                    lastClickedRegion = (currentRegions.Length > 0) ? currentRegions[0] : null;
                    Repaint();
                }
                break;
            }

            case (Mode.PaintVisible):
            {
                if ((lastClickedRegion != null) && (currentRegions.Length > 0) && (Event.current.type == EventType.MouseDown))
                {
                    lastClickedRegion.ToggleSeesRegion(currentRegions[0]);
                    Repaint();
                }
                break;
            }
            }
        }

        GUILayout.BeginHorizontal();
        GUILayout.Label("Zoom", GUILayout.Width(100));
        orthographicSize = GUILayout.HorizontalSlider(orthographicSize, 100.0f, 4000.0f);
        GUILayout.EndHorizontal();

        GUI.skin = guiskin;

        GUILayout.BeginHorizontal();
        float camPosZ = GUILayout.VerticalSlider(cam.transform.position.z, worldPainterData.WorldBounds().max.z, worldPainterData.WorldBounds().min.z);

        GUI.BeginGroup(textureRect);
        GUI.Box(texturSize, rt);
        GUI.EndGroup();

        GUI.BeginGroup(textureRect);

        switch (editMode)
        {
        case EditMode.Tesselate:
            //Draw all the points
            for (int i = 0; i < worldPainterData.points.Count; ++i)
            {
                EBWorldPainterData.Point point = worldPainterData.points[i];

                TextureFills tex = TextureFills.Gray;
                switch (mode)
                {
                case (Mode.AddPoints):
                    if (point == closestPoint)
                    {
                        tex = TextureFills.Blue;
                    }
                    break;

                case (Mode.RemovePoints):
                    if (point == closestPoint)
                    {
                        tex = TextureFills.Red;
                    }
                    break;

                case (Mode.MovePoints):
                    if (point == closestPoint)
                    {
                        tex = TextureFills.Green;
                    }
                    break;

                case (Mode.SplitRegions):
                    if (point == closestPoint)
                    {
                        tex = TextureFills.Green;
                    }
                    if (point == lastClickedPoint)
                    {
                        tex = TextureFills.Blue;
                    }
                    break;
                }
                DrawPoint(point, tex);
            }

            //Draw the line we may add if we are splitting regions
            if (mode == Mode.AddPoints)
            {
                DrawPoint(pointToAdd, TextureFills.Blue);
            }

            //Draw the line we may add if we are splitting regions
            if (mode == Mode.SplitRegions && lastClickedPoint != null)
            {
                DrawLine(lastClickedPoint, closestPoint, Color.blue);
            }

            //Draw all the region outlines
            foreach (EBWorldPainterData.Region region in worldPainterData.regions)
            {
                DrawRegionOutline(region, Color.white);
            }

            //Draw the line we may remove if we are combining regions
            if (mode == Mode.CombineRegions)
            {
                DrawLine(closestPoints[0], closestPoints[1], Color.red);
            }

            break;

        case EditMode.Visibility:

            //Draw all the points
            for (int i = 0; i < worldPainterData.points.Count; ++i)
            {
                DrawPoint(worldPainterData.points[i], TextureFills.Gray);
            }

            //Draw all the region outlines
            foreach (EBWorldPainterData.Region region in worldPainterData.regions)
            {
                DrawRegionOutline(region, Color.red);
            }

            //outline all the regions our current region sees, and our current region
            if (lastClickedRegion != null)
            {
                foreach (EBWorldPainterData.Region region in worldPainterData.regions)
                {
                    if (lastClickedRegion.SeesRegion(region))
                    {
                        DrawRegionOutline(region, Color.green);
                    }
                }
                DrawRegionOutline(lastClickedRegion, new Color(0.5f, 0.5f, 1.0f));
            }

            break;

        case EditMode.Hover:

            //Draw all the region outlines
            foreach (EBWorldPainterData.Region region in worldPainterData.regions)
            {
                DrawRegionOutline(region, Color.white);
            }

            //outline the one we are hovering over
            if (currentRegions.Length > 0)
            {
                DrawRegionOutline(currentRegions[0], new Color(0.0f, 0.5f, 0.0f));
            }

            break;

        case EditMode.BoundingBoxes:

            //Draw all the region bounding boxes
            foreach (EBWorldPainterData.Region region in worldPainterData.regions)
            {
                DrawRegionBoundingBox(region, Color.white);
            }

            break;

        case EditMode.DataLayer:

            //Draw all the region outlines
            foreach (EBWorldPainterData.Region region in worldPainterData.regions)
            {
                DrawRegionOutline(region, Color.white);
                DrawDataLayer(region);
            }

            break;
        }

        GUI.EndGroup();

        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        float camPosX = GUILayout.HorizontalSlider(cam.transform.position.x, worldPainterData.WorldBounds().min.x, worldPainterData.WorldBounds().max.x);

        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        foreach (EditMode m in System.Enum.GetValues(typeof(EditMode)))
        {
            if (GUILayout.Button(m.ToString()))
            {
                SwitchEditMode(m);
            }
        }
        dataLayer = (EBWorldPainterData.eDATA_LAYER)EditorGUILayout.EnumPopup("Data Layer: ", dataLayer);
        GUILayout.EndHorizontal();

        Render(camPosX, camPosZ, orthographicSize);

        GUI.skin = null;
    }