コード例 #1
0
        // Select any vertices
        void OnMouseUp(SceneView sceneView, Event e)
        {
            if (e.button == 0 && !CameraPanInProgress)
            {
                Transform sceneViewTransform = sceneView.camera.transform;
                Vector3   sceneViewPosition  = sceneViewTransform.position;
                if (moveInProgress)
                {
                }
                else
                {
                    if (isMarqueeSelection)                    // Marquee vertex selection
                    {
                        selectedEdges.Clear();

                        isMarqueeSelection = false;

                        marqueeEnd = e.mousePosition;

                        foreach (PrimitiveBrush brush in targetBrushes)
                        {
                            Polygon[] polygons = brush.GetPolygons();

                            for (int i = 0; i < polygons.Length; i++)
                            {
                                Polygon polygon = polygons[i];

                                for (int j = 0; j < polygon.Vertices.Length; j++)
                                {
                                    Vertex vertex = polygon.Vertices[j];

                                    Vector3 worldPosition = brush.transform.TransformPoint(vertex.Position);
                                    Vector3 screenPoint   = sceneView.camera.WorldToScreenPoint(worldPosition);

                                    // Point is contained within marquee box
                                    if (SabreMouse.MarqueeContainsPoint(marqueeStart, marqueeEnd, screenPoint))
                                    {
                                        if (EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Control))
                                        {
                                            // Only when holding control should a deselection occur from a valid point
                                            selectedVertices.Remove(vertex);
                                        }
                                        else
                                        {
                                            // Point was in marquee (and ctrl wasn't held) so select it!
                                            if (!selectedVertices.ContainsKey(vertex))
                                            {
                                                selectedVertices.Add(vertex, brush);
                                            }
                                        }
                                    }
                                    else if (!EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Control) &&
                                             !EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Shift))
                                    {
                                        selectedVertices.Remove(vertex);
                                    }
                                }
                            }
                        }
                    }
                    else                     // Clicking style vertex selection
                    {
                        Vector2 mousePosition = e.mousePosition;

                        bool clickedAnyPoints = false;
//						Vertex closestVertexFound = null;
                        Vector3 closestVertexWorldPosition = Vector3.zero;
                        float   closestDistanceSquare      = float.PositiveInfinity;

                        foreach (PrimitiveBrush brush in targetBrushes)
                        {
                            Polygon[] polygons = brush.GetPolygons();
                            for (int i = 0; i < polygons.Length; i++)
                            {
                                Polygon polygon = polygons[i];

                                for (int j = 0; j < polygon.Vertices.Length; j++)
                                {
                                    Vertex vertex = polygon.Vertices[j];

                                    Vector3 worldPosition = brush.transform.TransformPoint(vertex.Position);

                                    float vertexDistanceSquare = (sceneViewPosition - worldPosition).sqrMagnitude;

                                    if (EditorHelper.InClickZone(mousePosition, worldPosition) && vertexDistanceSquare < closestDistanceSquare)
                                    {
//										closestVertexFound = vertex;
                                        closestVertexWorldPosition = worldPosition;
                                        clickedAnyPoints           = true;
                                        closestDistanceSquare      = vertexDistanceSquare;
                                    }
                                }
                            }
                        }

                        if (!EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Control) && !EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Shift))
                        {
                            ClearSelection();
                        }

                        foreach (PrimitiveBrush brush in targetBrushes)
                        {
                            Polygon[] polygons = brush.GetPolygons();
                            for (int i = 0; i < polygons.Length; i++)
                            {
                                Polygon polygon = polygons[i];

                                for (int j = 0; j < polygon.Vertices.Length; j++)
                                {
                                    Vertex  vertex        = polygon.Vertices[j];
                                    Vector3 worldPosition = brush.transform.TransformPoint(vertex.Position);
                                    if (clickedAnyPoints && worldPosition == closestVertexWorldPosition)
                                    {
                                        if (EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Control))
                                        {
                                            if (!selectedVertices.ContainsKey(vertex))
                                            {
                                                selectedVertices.Add(vertex, brush);
                                            }
                                            else
                                            {
                                                selectedVertices.Remove(vertex);
                                            }
                                        }
                                        else
                                        {
                                            if (!selectedVertices.ContainsKey(vertex))
                                            {
                                                selectedVertices.Add(vertex, brush);
                                            }
                                        }
                                    }
                                    else if (!EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Control) &&
                                             !EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Shift))
                                    {
                                        selectedVertices.Remove(vertex);
                                    }
                                }
                            }
                        }


                        if (!clickedAnyPoints)                        // Couldn't click any directly, next try to click an edge
                        {
                            Edge    selectedEdge = null;
                            Vector3 selectedEdgeWorldPosition1 = Vector3.zero;
                            Vector3 selectedEdgeWorldPosition2 = Vector3.zero;
                            // Used to track the closest edge clicked, so if we could click through several edges with
                            // one click, then we only count the closest
                            float closestFound = float.PositiveInfinity;

                            foreach (PrimitiveBrush brush in targetBrushes)
                            {
                                Polygon[] polygons = brush.GetPolygons();
                                for (int i = 0; i < polygons.Length; i++)
                                {
                                    Polygon polygon = polygons[i];
                                    for (int j = 0; j < polygon.Vertices.Length; j++)
                                    {
                                        Vector3 worldPoint1 = brush.transform.TransformPoint(polygon.Vertices[j].Position);
                                        Vector3 worldPoint2 = brush.transform.TransformPoint(polygon.Vertices[(j + 1) % polygon.Vertices.Length].Position);

                                        // Distance from the mid point of the edge to the camera
                                        float squareDistance = (Vector3.Lerp(worldPoint1, worldPoint2, 0.5f) - Camera.current.transform.position).sqrMagnitude;

                                        float screenDistance = HandleUtility.DistanceToLine(worldPoint1, worldPoint2);
                                        if (screenDistance < EDGE_SCREEN_TOLERANCE && squareDistance < closestFound)
                                        {
                                            selectedEdgeWorldPosition1 = worldPoint1;
                                            selectedEdgeWorldPosition2 = worldPoint2;
                                            selectedEdge = new Edge(polygon.Vertices[j], polygon.Vertices[(j + 1) % polygon.Vertices.Length]);

                                            closestFound = squareDistance;
                                        }
                                    }
                                }
                            }

                            List <Vertex> newSelectedVertices = new List <Vertex>();

                            if (selectedEdge != null)
                            {
                                newSelectedVertices.Add(selectedEdge.Vertex1);
                                newSelectedVertices.Add(selectedEdge.Vertex2);

                                selectedEdges.Add(selectedEdge);

                                foreach (PrimitiveBrush brush in targetBrushes)
                                {
                                    Polygon[] polygons = brush.GetPolygons();

                                    for (int i = 0; i < polygons.Length; i++)
                                    {
                                        Polygon polygon = polygons[i];

                                        for (int j = 0; j < polygon.Vertices.Length; j++)
                                        {
                                            Vertex vertex = polygon.Vertices[j];

                                            Vector3 worldPosition = brush.transform.TransformPoint(vertex.Position);
                                            if (worldPosition == selectedEdgeWorldPosition1 ||
                                                worldPosition == selectedEdgeWorldPosition2)
                                            {
                                                if (!selectedVertices.ContainsKey(vertex))
                                                {
                                                    selectedVertices.Add(vertex, brush);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    moveInProgress = false;


                    // Repaint all scene views to show the selection change
                    SceneView.RepaintAll();
                }

                if (selectedVertices.Count > 0)
                {
                    e.Use();
                }
            }
        }
コード例 #2
0
ファイル: CSGModel.cs プロジェクト: MrSuicideFish/RuntimeZero
        public void OnSceneGUI(SceneView sceneView)
        {
            Event e = Event.current;

            //			if (e.type == EventType.Repaint)
            //			{
            //				if(CurrentSettings.GridMode == GridMode.SabreCSG)
            //				{
            //					CSGGrid.Activate();
            //				}
            //			}

            if (!EditMode)
            {
                return;
            }

            // Frame rate tracking
            if (e.type == EventType.Repaint)
            {
                currentFrameDelta     = Time.realtimeSinceStartup - currentFrameTimestamp;
                currentFrameTimestamp = Time.realtimeSinceStartup;
            }

            // Raw checks for tracking mouse events (use raw so that consumed events are not ignored)
            if (e.rawType == EventType.MouseDown)
            {
                mouseIsDragging = false;
                mouseIsHeld     = true;

                if (e.button == 0 && GUIUtility.hotControl == 0)
                {
                    GUIUtility.keyboardControl = 0;
                }
            }
            else if (e.rawType == EventType.MouseDrag)
            {
                mouseIsDragging = true;
            }
            else if (e.rawType == EventType.MouseUp)
            {
                mouseIsHeld = false;
            }

//			if (CurrentSettings.BrushesVisible)
            {
                // No idea what this line of code means, but it seems to stop normal mouse selection
                HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
            }

            if (EditMode)
            {
                // In CSG mode, prevent the normal tools, so that the user must use our tools instead
                Tools.current = UnityEditor.Tool.None;
            }

            int concaveBrushCount = 0;

            for (int i = 0; i < brushes.Count; i++)
            {
                if (brushes[i] != null && !brushes[i].IsBrushConvex)
                {
                    concaveBrushCount++;
                }
            }
            if (concaveBrushCount > 0)
            {
                Toolbar.WarningMessage = concaveBrushCount + " Concave Brush" + (concaveBrushCount > 1 ? "es" : "") + " Detected";
            }
            else
            {
                //				Toolbar.WarningMessage = "";
            }

            Toolbar.CSGModel = this;
            Toolbar.OnSceneGUI(sceneView, e);

            if (e.type == EventType.Repaint)            // || e.type == EventType.Layout)
            {
                if (tools[CurrentSettings.CurrentMode].BrushesHandleDrawing)
                {
                    SabreGraphics.GetSelectedBrushMaterial().SetPass(0);
                    // Selection
                    GL.Begin(GL.LINES);
                    Color outlineColor = Color.blue;

                    for (int brushIndex = 0; brushIndex < brushes.Count; brushIndex++)
                    {
                        Brush brush = brushes[brushIndex];
                        if (brush == null)
                        {
                            continue;
                        }
                        GameObject brushGameObject = brush.gameObject;

                        if (!brushGameObject.activeInHierarchy)
                        {
                            continue;
                        }

                        if (Selection.Contains(brushGameObject))
                        {
                            if (brushes[brushIndex].Mode == CSGMode.Add)
                            {
                                outlineColor = Color.cyan;
                            }
                            else
                            {
                                outlineColor = Color.yellow;
                            }
                        }
                        else if (CurrentSettings.BrushesVisible)
                        {
                            if (brushes[brushIndex].Mode == CSGMode.Add)
                            {
                                outlineColor = Color.blue;
                            }
                            else
                            {
                                outlineColor = new Color32(255, 130, 0, 255);
                            }
                        }
                        else
                        {
                            continue;
                        }

                        GL.Color(outlineColor);

                        Polygon[] polygons       = brush.GetPolygons();
                        Transform brushTransform = brush.transform;

                        // Brush Outline
                        for (int i = 0; i < polygons.Length; i++)
                        {
                            Polygon polygon = polygons[i];

                            for (int j = 0; j < polygon.Vertices.Length; j++)
                            {
                                Vector3 position = brushTransform.TransformPoint(polygon.Vertices[j].Position);
                                GL.Vertex(position);

                                if (j < polygon.Vertices.Length - 1)
                                {
                                    Vector3 position2 = brushTransform.TransformPoint(polygon.Vertices[j + 1].Position);
                                    GL.Vertex(position2);
                                }
                                else
                                {
                                    Vector3 position2 = brushTransform.TransformPoint(polygon.Vertices[0].Position);
                                    GL.Vertex(position2);
                                }
                            }
                        }
                    }

                    GL.End();

                    for (int i = 0; i < brushes.Count; i++)
                    {
                        if (brushes[i] is PrimitiveBrush && brushes[i] != null && brushes[i].gameObject.activeInHierarchy)
                        {
                            ((PrimitiveBrush)brushes[i]).OnRepaint(sceneView, e);
                        }
                    }
                }
            }

            if (e.type == EventType.Repaint)
            {
                Rect rect = new Rect(0, 0, Screen.width, Screen.height);
                EditorGUIUtility.AddCursorRect(rect, SabreMouse.ActiveCursor);
            }
            //

            //		int hotControl = GUIUtility.hotControl;
            //		if(hotControl != 0)
            //			Debug.Log (hotControl);
            //		Tools.viewTool = ViewTool.None;

            PrimitiveBrush primitiveBrush = null;

            if (Selection.activeGameObject != null)
            {
                primitiveBrush = Selection.activeGameObject.GetComponent <PrimitiveBrush>();
//				primitiveBrush = Selection.activeGameObject.GetComponentInChildren<PrimitiveBrush>();
            }

            List <PrimitiveBrush> primitiveBrushes = new List <PrimitiveBrush>();

            for (int i = 0; i < Selection.gameObjects.Length; i++)
            {
                PrimitiveBrush[] matchedBrushes = Selection.gameObjects[i].GetComponents <PrimitiveBrush>();
//				PrimitiveBrush[] matchedBrushes = Selection.gameObjects[i].GetComponentsInChildren<PrimitiveBrush>();
                if (matchedBrushes.Length > 0)
                {
                    primitiveBrushes.AddRange(matchedBrushes);
                }
            }

            Tool lastTool = activeTool;

            if (tools.ContainsKey(CurrentSettings.CurrentMode))
            {
                activeTool = tools[CurrentSettings.CurrentMode];
            }
            else
            {
                activeTool = null;
            }

            if (activeTool != null)
            {
                activeTool.CSGModel           = this;
                activeTool.PrimaryTargetBrush = primitiveBrush;
                activeTool.TargetBrushes      = primitiveBrushes.ToArray();
                activeTool.OnSceneGUI(sceneView, e);

                if (activeTool != lastTool)
                {
                    if (lastTool != null)
                    {
                        lastTool.Deactivated();
                    }
                    activeTool.ResetTool();
                }
            }

//			if(e.type == EventType.DragPerform)
//			{
//				Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
//
//				RaycastHit hit = new RaycastHit();
//
//				int layerMask = 1 << LayerMask.NameToLayer("CSGMesh");
//				// Invert the layer mask
//				layerMask = ~layerMask;
//
//				// Shift mode means only add what they click (clicking nothing does nothing)
//				if (Physics.Raycast(ray, out hit, float.PositiveInfinity, layerMask))
//				{
//										OnDragDrop(hit.collider.gameObject);
//				}
//			}

            if (e.type == EventType.MouseDown)
            {
            }
            else if (e.type == EventType.MouseDrag)
            {
            }
            else if (e.type == EventType.MouseUp)
            {
                OnMouseUp(sceneView, e);
                SabreMouse.ResetCursor();
            }
            else if (e.type == EventType.KeyDown || e.type == EventType.KeyUp)
            {
                OnKeyAction(sceneView, e);
            }

            if (CurrentSettings.OverrideFlyCamera)
            {
                LinearFPSCam.OnSceneGUI(sceneView);
            }
        }