void HighlightHoveringLine() { if (lastHoverLine != null && lastHoverLine != selectedLine) { lastHoverLine.SetSelectState(InteractiveLine.SelectState.NONE); } Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); Vector3 worldMousePos = ray.origin; InteractiveLine closestLine = ApproximateBinarySearch(horizontalLinesByY, worldMousePos.y); if (closestLine == null) { closestLine = ApproximateBinarySearch(verticalLinesByX, worldMousePos.x); } if (closestLine != null && closestLine.selectState == InteractiveLine.SelectState.IMMOBILE) { closestLine = null; } if (closestLine != null && closestLine != selectedLine) { closestLine.SetSelectState(InteractiveLine.SelectState.HOVER); } lastHoverLine = closestLine; }
// Adds a cell to the grid where other lines cannot pass inside of // Paramter cellCorners is array of 4 Vector3 representing corners of cell in order bottom left, top left, top right, bottom right public void AddIsolatedCell(Vector3[] cellCorners) { isolatedCellMinMaxs.Add(new Vector4(cellCorners[0].x, cellCorners[2].x, cellCorners[0].y, cellCorners[2].y)); // Remove any non-immobile lines inside the new isolated cell area float[] xCoordinates = verticalLinesByX.Keys.ToArray(); float[] yCoordinates = horizontalLinesByY.Keys.ToArray(); bool exactMatchFound; int lowerXBoundIndex = InterpretArrayBinarySearchIndex(Array.BinarySearch(xCoordinates, cellCorners[0].x), out exactMatchFound); int upperXBoundIndex = InterpretArrayBinarySearchIndex(Array.BinarySearch(xCoordinates, cellCorners[2].x), out exactMatchFound); int lowerYBoundIndex = InterpretArrayBinarySearchIndex(Array.BinarySearch(yCoordinates, cellCorners[0].y), out exactMatchFound); int upperYBoundIndex = InterpretArrayBinarySearchIndex(Array.BinarySearch(yCoordinates, cellCorners[2].y), out exactMatchFound); for (; lowerXBoundIndex < upperXBoundIndex; ++lowerXBoundIndex) { if (lowerXBoundIndex < xCoordinates.Length && verticalLinesByX[xCoordinates[lowerXBoundIndex]].selectState != InteractiveLine.SelectState.IMMOBILE) { DestroyImmediate(verticalLinesByX[xCoordinates[lowerXBoundIndex]]); verticalLinesByX.Remove(xCoordinates[lowerXBoundIndex]); } } for (; lowerYBoundIndex < upperYBoundIndex; ++lowerYBoundIndex) { if (lowerYBoundIndex < yCoordinates.Length && horizontalLinesByY[yCoordinates[lowerYBoundIndex]].selectState != InteractiveLine.SelectState.IMMOBILE) { DestroyImmediate(horizontalLinesByY[yCoordinates[lowerYBoundIndex]]); horizontalLinesByY.Remove(yCoordinates[lowerYBoundIndex]); } } InteractiveLine line = AddLine(cellCorners[0], cellCorners[1]); line.SetSelectState(InteractiveLine.SelectState.IMMOBILE); line = AddLine(cellCorners[1], cellCorners[2]); line.SetSelectState(InteractiveLine.SelectState.IMMOBILE); line = AddLine(cellCorners[3], cellCorners[2]); line.SetSelectState(InteractiveLine.SelectState.IMMOBILE); line = AddLine(cellCorners[0], cellCorners[3]); line.SetSelectState(InteractiveLine.SelectState.IMMOBILE); }
public void OnSceneGUI() { if (drawGrid) { int controlID = EditorGUIUtility.GetControlID(FocusType.Passive); switch (Event.current.GetTypeForControl(controlID)) { // On Layout event (the first event for initialization) record grid as being 0 units away if mouse within grid bounds // else declare infinity away to steal clicks and avoid deselection case EventType.Layout: if (selectedLine == null) { Vector3 worldMousePos = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin; if (worldMousePos.x >= minX && worldMousePos.x <= maxX && worldMousePos.y >= minY && worldMousePos.y <= maxY) { HandleUtility.AddControl(controlID, 0f); } else { HandleUtility.AddControl(controlID, Mathf.Infinity); } } else { HandleUtility.AddDefaultControl(controlID); } break; // All other events perform logic and draw like normal default: HighlightHoveringLine(); if (Event.current.type == EventType.MouseUp) { if (selectedLine != null) { selectedLine.SetSelectState(InteractiveLine.SelectState.NONE); } selectedLine = lastHoverLine; if (selectedLine != null) { selectedLine.SetSelectState(InteractiveLine.SelectState.SELECTED); Event.current.Use(); } } if (selectedLine == null) { ShowAddLineButtons(); } break; } } }