Exemplo n.º 1
0
        ChiselBrushMaterial[] AddSelectedSurfaces(ChiselBrushMaterial[] surfaces)
        {
            if (surfaces == null ||
                surfaces.Length != 1)
            {
                return(surfaces);
            }

            if (ChiselUVMoveTool.IsActive() ||
                ChiselUVRotateTool.IsActive() ||
                ChiselUVScaleTool.IsActive())
            {
                return(surfaces);
            }

            // TODO: implement the ability to query this from the edit mode
            if (!ChiselSurfaceSelectionManager.IsSelected(surfaces[0]))
            {
                return(surfaces);
            }

            var surfaceHashSet = new HashSet <ChiselBrushMaterial>();

            surfaceHashSet.AddRange(ChiselSurfaceSelectionManager.SelectedBrushMaterials);
            surfaceHashSet.AddRange(surfaces);
            return(surfaceHashSet.ToArray());
        }
Exemplo n.º 2
0
        private static bool SurfaceToolBase(int id, SelectionType selectionType, Rect dragArea)
        {
            // we only do tools when we do not use a modifier (shift, control etc.)
            if (selectionType != SelectionType.Replace)
            {
                return(false);
            }

            var evt = Event.current;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.Layout:
            {
                // Unless something else is closer, make sure our tool is selected
                HandleUtility.AddControl(id, kMaxControlDistance);
                break;
            }

            case EventType.ValidateCommand:
            {
                if (IsToolEnabled(id))
                {
                    if (evt.keyCode == KeyCode.Escape)
                    {
                        evt.Use();
                        break;
                    }
                }
                if (!EditorGUIUtility.editingTextField)
                {
                    if (evt.keyCode == KeyCode.V)
                    {
                        evt.Use();
                        break;
                    }
                }
                break;
            }

            case EventType.KeyDown:
            {
                if (IsToolEnabled(id))
                {
                    if (evt.keyCode == KeyCode.Escape)
                    {
                        evt.Use();
                        break;
                    }
                }
                if (!EditorGUIUtility.editingTextField)
                {
                    if (evt.keyCode == KeyCode.V)
                    {
                        forceVertexSnapping = true;
                        evt.Use();
                        break;
                    }
                }
                break;
            }

            case EventType.KeyUp:
            {
                if (IsToolEnabled(id))
                {
                    if (evt.keyCode == KeyCode.Escape)
                    {
                        CancelTool();
                        break;
                    }
                }
                if (forceVertexSnapping && evt.keyCode == KeyCode.V)
                {
                    forceVertexSnapping = false;
                    if (!EditorGUIUtility.editingTextField)
                    {
                        evt.Use();
                    }
                    break;
                }
                break;
            }

            case EventType.MouseMove:
            {
                // In case we somehow missed a MouseUp event, we reset this bool
                MouseIsDown = false;
                break;
            }

            case EventType.MouseDown:
            {
                // We can only use a tool when the mouse cursor is inside the draggable scene area
                if (!dragArea.Contains(evt.mousePosition))
                {
                    return(false);
                }

                // We can only use a tool when we're hovering over a surfaces
                if (hoverSurfaces == null || hoverSurfaces.Count == 0)
                {
                    return(false);
                }

                if (!CanEnableTool(id))
                {
                    break;
                }

                // We want to be able to tell the difference between dragging and clicking,
                // so we keep track if we dragged or not. In this case we haven't started dragging yet.
                ToolIsDragging = false;
                MouseIsDown    = true;

                EnableTool(id);
                break;
            }

            case EventType.MouseDrag:
            {
                if (!IsToolEnabled(id))
                {
                    break;
                }

                if (!ToolIsDragging)
                {
                    // If we haven't dragged the tool yet, check if the surface underneath
                    // the mouse is selected or not, if it isn't: select it exclusively
                    if (!ChiselSurfaceSelectionManager.IsAnySelected(hoverSurfaces))
                    {
                        ClickSelection(dragArea, selectionType);
                    }
                }

                // In the tool specific code, calling StartToolDragging will set ToolIsDragging to true,
                // which will allow us to tell the difference between clicking and dragging.

                break;
            }

            case EventType.MouseUp:
            {
                if (!IsToolEnabled(id))
                {
                    break;
                }

                MouseIsDown = false;

                // We want to be able to tell the difference between clicking and dragging,
                // so we use ToolIsDragging here to determine if we clicked.
                if (!ToolIsDragging)
                {
                    // If we clicked on the surface, instead of dragged it, just click select it
                    ClickSelection(dragArea, selectionType);
                }

                ToolIsDragging = false;

                DisableTool();
                ResetSelection();
                break;
            }

            case EventType.Repaint:
            {
                RenderIntersection();
                break;
            }
            }
            return(true);
        }
Exemplo n.º 3
0
        static bool SurfaceSelection(Rect dragArea, SelectionType selectionType)
        {
            var id = GUIUtility.GetControlID(kSurfaceDragSelectionHash, FocusType.Keyboard, dragArea);

            bool repaint = false;

            var evt = Event.current;

            if (evt.type == EventType.MouseMove ||
                evt.type == EventType.MouseDown)
            {
                if (UpdateHoverSurfaces(evt.mousePosition, dragArea, selectionType, true))
                {
                    repaint = true;
                }

                if (!InEditCameraMode)
                {
                    return(repaint);
                }
            }

            if (InEditCameraMode && !ToolIsDragging)
            {
                if (MouseIsDown)
                {
                    ChiselOutlineRenderer.VisualizationMode = VisualizationMode.None;
                }
                else
                {
                    ChiselOutlineRenderer.VisualizationMode = VisualizationMode.Surface | VisualizationMode.Outline;
                }
            }
            else
            {
                ChiselOutlineRenderer.VisualizationMode = VisualizationMode.None;
            }


            switch (evt.GetTypeForControl(id))
            {
            case EventType.Layout:
            {
                // we only do drag selection when we use a modifier (shift, control etc.)
                if (selectionType == SelectionType.Replace)
                {
                    break;
                }

                if (hoverSurfaces != null && hoverSurfaces.Count > 0)
                {
                    HandleUtility.AddControl(id, 3.0f);
                }
                break;
            }

            case EventType.MouseDown:
            {
                if (GUIUtility.hotControl != 0)
                {
                    break;
                }

                // we only do drag selection when we use a modifier (shift, control etc.)
                if (selectionType == SelectionType.Replace)
                {
                    break;
                }

                if ((UnityEditor.HandleUtility.nearestControl != id || evt.button != 0) &&
                    (GUIUtility.keyboardControl != id || evt.button != 2))
                {
                    break;
                }

                GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                evt.Use();
                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl != id)
                {
                    break;
                }

                UpdateHoverSurfaces(evt.mousePosition, dragArea, selectionType, false);
                evt.Use();
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl != id || evt.button != 0)
                {
                    break;
                }

                GUIUtility.hotControl      = 0;
                GUIUtility.keyboardControl = 0;
                evt.Use();

                if (ChiselSurfaceSelectionManager.UpdateSelection(selectionType, hoverSurfaces))
                {
                    repaint = true;
                }

                if (UpdateHoverSurfaces(evt.mousePosition, dragArea, selectionType, true))
                {
                    repaint = true;
                }
                break;
            }
            }

            return(repaint);
        }
Exemplo n.º 4
0
 static bool ClickSelection(Rect dragArea, SelectionType selectionType)
 {
     return(ChiselSurfaceSelectionManager.UpdateSelection(selectionType, hoverSurfaces));
 }
Exemplo n.º 5
0
        static bool UpdateHoverSurfaces(Vector2 mousePosition, Rect dragArea, SelectionType selectionType, bool clearHovering)
        {
            try
            {
                hoverIntersection     = null;
                hoverSurfaceReference = null;

                bool modified = false;
                if (clearHovering || !InEditCameraMode)
                {
                    if (hoverSurfaces.Count != 0)
                    {
                        hoverSurfaces.Clear();
                        modified = true;
                    }
                }

                if (!dragArea.Contains(mousePosition))
                {
                    return(modified);
                }

                if (!InEditCameraMode)
                {
                    return(modified);
                }

                CSGTreeBrushIntersection intersection;
                SurfaceReference         surfaceReference;
                var foundSurfaces = ChiselClickSelectionManager.FindSurfaceReference(mousePosition, false, out intersection, out surfaceReference);
                if (foundSurfaces == null)
                {
                    modified          = (hoverSurfaces != null) || modified;
                    hoverIntersection = null;
                    return(modified);
                }

                if (!float.IsInfinity(intersection.surfaceIntersection.distance))
                {
                    intersection.surfaceIntersection.worldIntersection = SnapIntersection(intersection.surfaceIntersection.worldIntersection, surfaceReference, out pointHasSnapped);
                }
                hoverIntersection     = intersection;
                hoverSurfaceReference = surfaceReference;
                if (foundSurfaces.Length == hoverSurfaces.Count)
                {
                    modified = !hoverSurfaces.ContainsAll(foundSurfaces) || modified;
                }
                else
                {
                    modified = true;
                }

                if (foundSurfaces.Length > 0)
                {
                    hoverSurfaces.AddRange(foundSurfaces);
                }
                return(modified);
            }
            finally
            {
                ChiselSurfaceSelectionManager.SetHovering(selectionType, hoverSurfaces);
            }
        }
Exemplo n.º 6
0
        void UpdateSurfaceSelection()
        {
            surfaceOutlines.Clear();
            ChiselSurfaceSelectionManager.Clean();
            var selection = ChiselSurfaceSelectionManager.Selection;
            var hovered   = ChiselSurfaceSelectionManager.Hovered;

            if (selection.Count == 0 &&
                hovered.Count == 0)
            {
                surfaceOutlines.Clear();
                surfaceOutlineRenderer.Clear();
            }
            else
            {
                var allSurfaces = new HashSet <SurfaceReference>(selection);
                allSurfaces.AddRange(hovered);
                foreach (var outline in surfaceOutlines.Keys)
                {
                    var surface = outline.surface;
                    if (!allSurfaces.Contains(surface) ||
                        !surface.TreeBrush.Valid ||
                        surface.TreeBrush.BrushMesh == BrushMeshInstance.InvalidInstance)
                    {
                        removedSurfaces.Add(outline);
                    }
                    else
                    {
                        allSurfaces.Remove(surface);
                    }
                }

                if (removedSurfaces.Count > 0)
                {
                    foreach (var outline in removedSurfaces)
                    {
                        surfaceOutlines.Remove(outline);
                    }
                }
                removedSurfaces.Clear();

                foreach (var surface in allSurfaces)
                {
                    var transform = ChiselNodeHierarchyManager.FindModelTransformOfTransform(surface.node.hierarchyItem.Transform);
                    var outline   = new SurfaceOutline(transform, surface);
                    foundSurfaceOutlines.Add(outline);
                }

                foreach (var outline in foundSurfaceOutlines)
                {
                    if (!outline.surface.TreeBrush.Valid ||
                        outline.surface.TreeBrush.BrushMesh == BrushMeshInstance.InvalidInstance)
                    {
                        continue;
                    }

                    var wireframe = GetSurfaceWireframe(outline.surface);
                    surfaceOutlines[outline] = wireframe;
                }
            }
            foundSurfaceOutlines.Clear();
            updateSurfaceWireframe = true;
        }