void DoExistingPointsGUI() { Transform trs = polygon.transform; int len = polygon.m_Points.Count; Vector3 up = polygon.transform.up; Vector3 right = polygon.transform.right; Vector3 forward = polygon.transform.forward; Vector3 center = Vector3.zero; Event evt = Event.current; bool used = evt.type == EventType.Used; if (!used && (evt.type == EventType.MouseDown && evt.button == 0 && !EditorHandleUtility.IsAppendModifier(evt.modifiers))) { m_SelectedIndex = -1; Repaint(); } if (polygon.polyEditMode == PolyShape.PolyEditMode.Height) { if (!used && evt.type == EventType.MouseUp && evt.button == 0 && !EditorHandleUtility.IsAppendModifier(evt.modifiers)) { evt.Use(); SetPolyEditMode(PolyShape.PolyEditMode.Edit); } bool sceneInUse = EditorHandleUtility.SceneViewInUse(evt); Ray r = HandleUtility.GUIPointToWorldRay(evt.mousePosition); Vector3 origin = polygon.transform.TransformPoint(Math.Average(polygon.m_Points)); float extrude = polygon.extrude; if (evt.type == EventType.MouseMove && !sceneInUse) { Vector3 p = Math.GetNearestPointRayRay(origin, up, r.origin, r.direction); extrude = ProGridsInterface.ProGridsSnap(s_HeightMouseOffset + Vector3.Distance(origin, p) * Mathf.Sign(Vector3.Dot(p - origin, up))); } Vector3 extrudePoint = origin + (extrude * up); Handles.color = k_HandleColor; Handles.DotHandleCap(-1, origin, Quaternion.identity, HandleUtility.GetHandleSize(origin) * k_HandleSize, evt.type); Handles.color = k_HandleColorGreen; Handles.DrawLine(origin, extrudePoint); Handles.DotHandleCap(-1, extrudePoint, Quaternion.identity, HandleUtility.GetHandleSize(extrudePoint) * k_HandleSize, evt.type); Handles.color = Color.white; if (!sceneInUse && polygon.extrude != extrude) { OnBeginVertexMovement(); polygon.extrude = extrude; RebuildPolyShapeMesh(false); } } else { // vertex dots for (int ii = 0; ii < len; ii++) { Vector3 point = trs.TransformPoint(polygon.m_Points[ii]); center.x += point.x; center.y += point.y; center.z += point.z; float size = HandleUtility.GetHandleSize(point) * k_HandleSize; Handles.color = ii == m_SelectedIndex ? k_HandleSelectedColor : k_HandleColor; EditorGUI.BeginChangeCheck(); point = Handles.Slider2D(point, up, right, forward, size, Handles.DotHandleCap, Vector2.zero, true); if (EditorGUI.EndChangeCheck()) { UndoUtility.RecordObject(polygon, "Move Polygon Shape Point"); Vector3 snapMask = Snapping.GetSnappingMaskBasedOnNormalVector(m_Plane.normal); polygon.m_Points[ii] = ProGridsInterface.ProGridsSnap(trs.InverseTransformPoint(point), snapMask); OnBeginVertexMovement(); RebuildPolyShapeMesh(false); } // "clicked" a button if (!used && evt.type == EventType.Used) { if (ii == 0 && polygon.m_Points.Count > 2 && polygon.polyEditMode == PolyShape.PolyEditMode.Path) { m_NextMouseUpAdvancesMode = true; return; } else { used = true; m_SelectedIndex = ii; } } } Handles.color = Color.white; // height setting if (polygon.polyEditMode != PolyShape.PolyEditMode.Path && polygon.m_Points.Count > 2) { center.x /= (float)len; center.y /= (float)len; center.z /= (float)len; Vector3 extrude = center + (up * polygon.extrude); m_DistanceFromHeightHandle = Vector2.Distance(HandleUtility.WorldToGUIPoint(extrude), evt.mousePosition); EditorGUI.BeginChangeCheck(); Handles.color = k_HandleColor; Handles.DotHandleCap(-1, center, Quaternion.identity, HandleUtility.GetHandleSize(center) * k_HandleSize, evt.type); Handles.DrawLine(center, extrude); Handles.color = k_HandleColorGreen; extrude = Handles.Slider(extrude, up, HandleUtility.GetHandleSize(extrude) * k_HandleSize, Handles.DotHandleCap, 0f); Handles.color = Color.white; if (EditorGUI.EndChangeCheck()) { UndoUtility.RecordObject(polygon, "Set Polygon Shape Height"); polygon.extrude = ProGridsInterface.ProGridsSnap(Vector3.Distance(extrude, center) * Mathf.Sign(Vector3.Dot(up, extrude - center))); OnBeginVertexMovement(); RebuildPolyShapeMesh(false); } } } }
public static void DoMouseDrag(Rect mouseDragRect, SelectMode selectionMode, ScenePickerPreferences scenePickerPreferences) { var pickingOptions = new PickerOptions() { depthTest = scenePickerPreferences.cullMode == CullingMode.Back, rectSelectMode = scenePickerPreferences.rectSelectMode }; UndoUtility.RecordSelection("Drag Select"); bool isAppendModifier = EditorHandleUtility.IsAppendModifier(Event.current.modifiers); if (!isAppendModifier) { MeshSelection.ClearElementSelection(); } bool elementsInDragRect = false; switch (selectionMode) { case SelectMode.Vertex: case SelectMode.TextureVertex: { Dictionary <ProBuilderMesh, HashSet <int> > selected = SelectionPicker.PickVerticesInRect( SceneView.lastActiveSceneView.camera, mouseDragRect, MeshSelection.topInternal, pickingOptions, EditorGUIUtility.pixelsPerPoint); foreach (var kvp in selected) { var mesh = kvp.Key; SharedVertex[] sharedIndexes = mesh.sharedVerticesInternal; HashSet <int> common; if (isAppendModifier) { common = mesh.GetSharedVertexHandles(mesh.selectedIndexesInternal); if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Add) { common.UnionWith(kvp.Value); } else if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Subtract) { common.RemoveWhere(x => kvp.Value.Contains(x)); } else if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Difference) { common.SymmetricExceptWith(kvp.Value); } } else { common = kvp.Value; } elementsInDragRect |= kvp.Value.Any(); mesh.SetSelectedVertices(common.SelectMany(x => sharedIndexes[x])); } break; } case SelectMode.Face: case SelectMode.TextureFace: { Dictionary <ProBuilderMesh, HashSet <Face> > selected = SelectionPicker.PickFacesInRect( SceneView.lastActiveSceneView.camera, mouseDragRect, MeshSelection.topInternal, pickingOptions, EditorGUIUtility.pixelsPerPoint); foreach (var kvp in selected) { HashSet <Face> current; if (isAppendModifier) { current = new HashSet <Face>(kvp.Key.selectedFacesInternal); if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Add) { current.UnionWith(kvp.Value); } else if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Subtract) { current.RemoveWhere(x => kvp.Value.Contains(x)); } else if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Difference) { current.SymmetricExceptWith(kvp.Value); } } else { current = kvp.Value; } elementsInDragRect |= kvp.Value.Any(); kvp.Key.SetSelectedFaces(current); } break; } case SelectMode.Edge: case SelectMode.TextureEdge: { var selected = SelectionPicker.PickEdgesInRect( SceneView.lastActiveSceneView.camera, mouseDragRect, MeshSelection.topInternal, pickingOptions, EditorGUIUtility.pixelsPerPoint); foreach (var kvp in selected) { ProBuilderMesh mesh = kvp.Key; Dictionary <int, int> common = mesh.sharedVertexLookup; HashSet <EdgeLookup> selectedEdges = EdgeLookup.GetEdgeLookupHashSet(kvp.Value, common); HashSet <EdgeLookup> current; if (isAppendModifier) { current = EdgeLookup.GetEdgeLookupHashSet(mesh.selectedEdges, common); if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Add) { current.UnionWith(selectedEdges); } else if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Subtract) { current.RemoveWhere(x => selectedEdges.Contains(x)); } else if (scenePickerPreferences.selectionModifierBehavior == SelectionModifierBehavior.Difference) { current.SymmetricExceptWith(selectedEdges); } } else { current = selectedEdges; } elementsInDragRect |= kvp.Value.Any(); mesh.SetSelectedEdges(current.Select(x => x.local)); } break; } } // if nothing was selected in the drag rect, clear the object selection too if (!elementsInDragRect && !isAppendModifier) { MeshSelection.ClearElementAndObjectSelection(); } ProBuilderEditor.Refresh(); SceneView.RepaintAll(); }
public static ProBuilderMesh DoMouseClick(Event evt, SelectMode selectionMode, ScenePickerPreferences pickerPreferences) { bool appendModifier = EditorHandleUtility.IsAppendModifier(evt.modifiers); if (!appendModifier) { MeshSelection.SetSelection((GameObject)null); } float pickedElementDistance = Mathf.Infinity; if (selectionMode.ContainsFlag(SelectMode.Edge | SelectMode.TextureEdge)) { pickedElementDistance = EdgeRaycast(evt.mousePosition, pickerPreferences, true, s_Selection); } else if (selectionMode.ContainsFlag(SelectMode.Vertex | SelectMode.TextureVertex)) { pickedElementDistance = VertexRaycast(evt.mousePosition, pickerPreferences, true, s_Selection); } else { pickedElementDistance = FaceRaycast(evt.mousePosition, pickerPreferences, true, s_Selection, evt.clickCount > 1 ? -1 : 0, false); } evt.Use(); if (pickedElementDistance > pickerPreferences.maxPointerDistance) { if (appendModifier && Selection.gameObjects.Contains(s_Selection.gameObject)) { MeshSelection.RemoveFromSelection(s_Selection.gameObject); } else { MeshSelection.AddToSelection(s_Selection.gameObject); } return(null); } MeshSelection.AddToSelection(s_Selection.gameObject); if (s_Selection.mesh != null) { var mesh = s_Selection.mesh; if (s_Selection.face != null) { // Check for other editor mouse shortcuts first (todo proper event handling for mouse shortcuts) MaterialEditor matEditor = MaterialEditor.instance; if (matEditor != null && matEditor.ClickShortcutCheck(Event.current.modifiers, mesh, s_Selection.face)) { return(null); } UVEditor uvEditor = UVEditor.instance; if (uvEditor != null && uvEditor.ClickShortcutCheck(mesh, s_Selection.face)) { return(null); } var faces = mesh.faces as Face[] ?? mesh.faces.ToArray(); var ind = Array.IndexOf <Face>(faces, s_Selection.face); var sel = mesh.selectedFaceIndexes.IndexOf(ind); UndoUtility.RecordSelection(mesh, "Select Face"); if (sel > -1) { mesh.RemoveFromFaceSelectionAtIndex(sel); } else { mesh.AddToFaceSelection(ind); } } else if (s_Selection.edge != Edge.Empty) { int ind = mesh.IndexOf(mesh.selectedEdges, s_Selection.edge); UndoUtility.RecordSelection(mesh, "Select Edge"); if (ind > -1) { mesh.SetSelectedEdges(mesh.selectedEdges.ToArray().RemoveAt(ind)); } else { mesh.SetSelectedEdges(mesh.selectedEdges.ToArray().Add(s_Selection.edge)); } } else if (s_Selection.vertex > -1) { int ind = Array.IndexOf(mesh.selectedIndexesInternal, s_Selection.vertex); UndoUtility.RecordSelection(mesh, "Select Vertex"); if (ind > -1) { mesh.SetSelectedVertices(mesh.selectedIndexesInternal.RemoveAt(ind)); } else { mesh.SetSelectedVertices(mesh.selectedIndexesInternal.Add(s_Selection.vertex)); } } return(mesh); } return(null); }
public static ProBuilderMesh DoMouseClick(Event evt, SelectMode selectionMode, ScenePickerPreferences pickerPreferences) { bool appendModifier = EditorHandleUtility.IsAppendModifier(evt.modifiers); bool addToSelectionModifier = EditorHandleUtility.IsSelectionAddModifier(evt.modifiers); bool addOrRemoveIfPresentFromSelectionModifier = EditorHandleUtility.IsSelectionAppendOrRemoveIfPresentModifier(evt.modifiers); bool pathSelectionModifier = EditorHandleUtility.IsSelectionPathModifier(evt.modifiers); float pickedElementDistance; if (selectionMode.ContainsFlag(SelectMode.Edge | SelectMode.TextureEdge)) { pickedElementDistance = EdgeRaycast(evt.mousePosition, pickerPreferences, k_AllowUnselected, s_Selection); } else if (selectionMode.ContainsFlag(SelectMode.Vertex | SelectMode.TextureVertex)) { pickedElementDistance = VertexRaycast(evt.mousePosition, pickerPreferences, k_AllowUnselected, s_Selection); } else { pickedElementDistance = FaceRaycast(evt.mousePosition, pickerPreferences, k_AllowUnselected, s_Selection, evt.clickCount > 1 ? -1 : 0, false); } evt.Use(); if (!appendModifier) { if (s_Selection.mesh != null) { s_Selection.mesh.ClearSelection(); } MeshSelection.SetSelection((GameObject)null); } if (pickedElementDistance > ScenePickerPreferences.maxPointerDistance) { if (appendModifier && Selection.gameObjects.Contains(s_Selection.gameObject)) { MeshSelection.RemoveFromSelection(s_Selection.gameObject); } else { MeshSelection.AddToSelection(s_Selection.gameObject); } return(null); } GameObject candidateNewActiveObject = s_Selection.gameObject; bool activeObjectSelectionChanged = Selection.gameObjects.Contains(s_Selection.gameObject) && s_Selection.gameObject != Selection.activeGameObject; if (s_Selection.mesh != null) { var mesh = s_Selection.mesh; foreach (var face in s_Selection.faces) { // Check for other editor mouse shortcuts first (todo proper event handling for mouse shortcuts) MaterialEditor matEditor = MaterialEditor.instance; if (matEditor != null && matEditor.ClickShortcutCheck(Event.current.modifiers, mesh, face)) { return(null); } UVEditor uvEditor = UVEditor.instance; if (uvEditor != null && uvEditor.ClickShortcutCheck(mesh, face)) { return(null); } var faces = mesh.faces as Face[] ?? mesh.faces.ToArray(); var ind = Array.IndexOf <Face>(faces, face); var sel = mesh.selectedFaceIndexes.IndexOf(ind); UndoUtility.RecordSelection(mesh, "Select Face"); if (sel > -1) { if (!appendModifier || addOrRemoveIfPresentFromSelectionModifier || (addToSelectionModifier && face == mesh.GetActiveFace() && !activeObjectSelectionChanged)) { mesh.RemoveFromFaceSelectionAtIndex(sel); if (addOrRemoveIfPresentFromSelectionModifier && activeObjectSelectionChanged) { candidateNewActiveObject = Selection.activeGameObject; } else if (mesh.selectedFaceCount == 0) { for (var i = MeshSelection.topInternal.Count - 1; i >= 0; i--) { if (MeshSelection.topInternal[i].selectedFaceCount > 0) { candidateNewActiveObject = MeshSelection.topInternal[i].gameObject; activeObjectSelectionChanged = true; break; } } } } else { mesh.selectedFaceIndicesInternal = mesh.selectedFaceIndicesInternal.Remove(ind); mesh.SetSelectedFaces(mesh.selectedFaceIndicesInternal.Add(ind)); } } else if (pathSelectionModifier && mesh.GetActiveFace() != null) { var pathFaces = SelectPathFaces.GetPath(mesh, Array.IndexOf <Face>(faces, mesh.GetActiveFace()), Array.IndexOf <Face>(faces, face)); foreach (var pathFace in pathFaces) { mesh.AddToFaceSelection(pathFace); } } else { mesh.AddToFaceSelection(ind); } } foreach (var edge in s_Selection.edges) { int ind = mesh.IndexOf(mesh.selectedEdges, edge); UndoUtility.RecordSelection(mesh, "Select Edge"); if (ind > -1) { if (!appendModifier || addOrRemoveIfPresentFromSelectionModifier || (addToSelectionModifier && edge == mesh.GetActiveEdge() && !activeObjectSelectionChanged)) { mesh.SetSelectedEdges(mesh.selectedEdges.ToArray().RemoveAt(ind)); if (addOrRemoveIfPresentFromSelectionModifier && activeObjectSelectionChanged) { candidateNewActiveObject = Selection.activeGameObject; } else if (mesh.selectedEdgeCount == 0) { for (var i = MeshSelection.topInternal.Count - 1; i >= 0; i--) { if (MeshSelection.topInternal[i].selectedEdgeCount > 0) { candidateNewActiveObject = MeshSelection.topInternal[i].gameObject; activeObjectSelectionChanged = true; break; } } } } else { mesh.selectedEdgesInternal = mesh.selectedEdgesInternal.Remove(edge); mesh.SetSelectedEdges(mesh.selectedEdgesInternal.Add(edge)); } } else { mesh.SetSelectedEdges(mesh.selectedEdges.ToArray().Add(edge)); } } foreach (var vertex in s_Selection.vertexes) { int ind = Array.IndexOf(mesh.selectedIndexesInternal, vertex); UndoUtility.RecordSelection(mesh, "Select Vertex"); if (ind > -1) { var sharedIndex = mesh.sharedVertexLookup[vertex]; var sharedVertex = mesh.sharedVerticesInternal[sharedIndex]; s_IndexBuffer.Clear(); foreach (var sVertex in sharedVertex) { var index = Array.IndexOf(mesh.selectedIndexesInternal, sVertex); if (index < 0) { continue; } s_IndexBuffer.Add(index); } s_IndexBuffer.Sort(); if (!appendModifier || addOrRemoveIfPresentFromSelectionModifier || (addToSelectionModifier && vertex == mesh.GetActiveVertex() && !activeObjectSelectionChanged)) { mesh.selectedIndexesInternal = mesh.selectedIndexesInternal.SortedRemoveAt(s_IndexBuffer); mesh.SetSelectedVertices(mesh.selectedIndexesInternal); if (addOrRemoveIfPresentFromSelectionModifier && activeObjectSelectionChanged) { candidateNewActiveObject = Selection.activeGameObject; } else if (mesh.selectedIndexesInternal.Length == 0) { for (var i = MeshSelection.topInternal.Count - 1; i >= 0; i--) { if (MeshSelection.topInternal[i].selectedIndexesInternal.Length > 0) { candidateNewActiveObject = MeshSelection.topInternal[i].gameObject; activeObjectSelectionChanged = true; break; } } } } else { mesh.selectedIndexesInternal = mesh.selectedIndexesInternal.SortedRemoveAt(s_IndexBuffer); mesh.SetSelectedVertices(mesh.selectedIndexesInternal.Add(vertex)); } } else { mesh.SetSelectedVertices(mesh.selectedIndexesInternal.Add(vertex)); } } if (activeObjectSelectionChanged) { MeshSelection.MakeActiveObject(candidateNewActiveObject); } else { MeshSelection.AddToSelection(candidateNewActiveObject); } return(mesh); } return(null); }