-
Notifications
You must be signed in to change notification settings - Fork 40
feat: Introduce GroupAnchorOffsetOverlay component and example fixture for displaying group anchor hover offsets. #483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/components/GroupAnchorOffsetOverlay/calculateGroupBoundingBox.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { getBoundsFromPoints } from "@tscircuit/math-utils" | ||
| import type { PcbComponent } from "circuit-json" | ||
| import type { BoundingBox } from "../../lib/util/get-primitive-bounding-box" | ||
|
|
||
| export const calculateGroupBoundingBox = ( | ||
| groupComponents: PcbComponent[], | ||
| ): BoundingBox | null => { | ||
| const points: Array<{ x: number; y: number }> = [] | ||
|
|
||
| for (const comp of groupComponents) { | ||
| if ( | ||
| !comp.center || | ||
| typeof comp.width !== "number" || | ||
| typeof comp.height !== "number" | ||
| ) { | ||
| continue | ||
| } | ||
|
|
||
| const halfWidth = comp.width / 2 | ||
| const halfHeight = comp.height / 2 | ||
|
|
||
| points.push({ x: comp.center.x - halfWidth, y: comp.center.y - halfHeight }) | ||
| points.push({ x: comp.center.x + halfWidth, y: comp.center.y + halfHeight }) | ||
| } | ||
|
|
||
| return getBoundsFromPoints(points) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| export const VISUAL_CONFIG = { | ||
| GROUP_PADDING: 1, | ||
| MIN_LINE_LENGTH_FOR_LABEL: 40, | ||
| LABEL_OFFSET_ABOVE: 2, | ||
| LABEL_OFFSET_BELOW: -18, | ||
| LABEL_OFFSET_RIGHT: 8, | ||
| LABEL_OFFSET_LEFT: -80, | ||
| LINE_STROKE_WIDTH: 1.5, | ||
| LINE_DASH_PATTERN: "4,4", | ||
| COMPONENT_MARKER_RADIUS: 3, | ||
| LABEL_FONT_SIZE: 11, | ||
| } as const | ||
|
|
||
| export const COLORS = { | ||
| OFFSET_LINE: "white", | ||
| COMPONENT_MARKER_FILL: "#66ccff", | ||
| COMPONENT_MARKER_STROKE: "white", | ||
| LABEL_TEXT: "white", | ||
| } as const |
19 changes: 19 additions & 0 deletions
19
src/components/GroupAnchorOffsetOverlay/findAnchorMarkerPosition.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { BoundingBox } from "../../lib/util/get-primitive-bounding-box" | ||
| export const findAnchorMarkerPosition = ( | ||
| anchor: { x: number; y: number }, | ||
| bounds: BoundingBox, | ||
| ): { x: number; y: number } => { | ||
| const { minX, maxX, minY, maxY } = bounds | ||
|
|
||
| const distToLeft = Math.abs(anchor.x - minX) | ||
| const distToRight = Math.abs(anchor.x - maxX) | ||
| const distToTop = Math.abs(anchor.y - maxY) | ||
| const distToBottom = Math.abs(anchor.y - minY) | ||
|
|
||
| const minDist = Math.min(distToLeft, distToRight, distToTop, distToBottom) | ||
|
|
||
| if (minDist === distToLeft) return { x: minX, y: anchor.y } | ||
| if (minDist === distToRight) return { x: maxX, y: anchor.y } | ||
| if (minDist === distToTop) return { x: anchor.x, y: maxY } | ||
| return { x: anchor.x, y: minY } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| import type { AnyCircuitElement, PcbComponent, PcbGroup } from "circuit-json" | ||
| import { applyToPoint } from "transformation-matrix" | ||
| import type { Matrix } from "transformation-matrix" | ||
| import { useGlobalStore } from "../../global-store" | ||
| import type { BoundingBox } from "../../lib/util/get-primitive-bounding-box" | ||
| import { zIndexMap } from "../../lib/util/z-index-map" | ||
| import type { HighlightedPrimitive } from "../MouseElementTracker" | ||
| import { calculateGroupBoundingBox } from "./calculateGroupBoundingBox" | ||
| import { COLORS, VISUAL_CONFIG } from "./constants" | ||
| import { findAnchorMarkerPosition } from "./findAnchorMarkerPosition" | ||
|
|
||
| type Point = { | ||
| x: number | ||
| y: number | ||
| } | ||
|
|
||
| interface Props { | ||
| elements: AnyCircuitElement[] | ||
| highlightedPrimitives: HighlightedPrimitive[] | ||
| transform: Matrix | ||
| containerWidth: number | ||
| containerHeight: number | ||
| children?: any | ||
| } | ||
|
|
||
| /** | ||
| * Overlay component that displays offset measurements from a group's anchor point | ||
| * to the hovered component. Shows dotted lines and distance labels for X and Y axes. | ||
| */ | ||
| export const GroupAnchorOffsetOverlay = ({ | ||
| elements, | ||
| highlightedPrimitives, | ||
| transform, | ||
| containerWidth, | ||
| containerHeight, | ||
| children, | ||
| }: Props) => { | ||
| const is_showing_group_anchor_offsets = useGlobalStore( | ||
| (s) => s.is_showing_group_anchor_offsets, | ||
| ) | ||
|
|
||
| if (!is_showing_group_anchor_offsets || highlightedPrimitives.length === 0) { | ||
| return null | ||
| } | ||
|
|
||
| const hoveredPrimitive = highlightedPrimitives.find( | ||
| (p) => | ||
| p._parent_pcb_component?.type === "pcb_component" || | ||
| p._element?.type === "pcb_component", | ||
| ) | ||
|
|
||
| if (!hoveredPrimitive) return null | ||
|
|
||
| const pcbComponent = (hoveredPrimitive._parent_pcb_component || | ||
| hoveredPrimitive._element) as PcbComponent | undefined | ||
|
|
||
| if (!pcbComponent?.pcb_group_id) return null | ||
|
|
||
| const parentGroup = elements | ||
| .filter((el): el is PcbGroup => el.type === "pcb_group") | ||
| .find((group) => group.pcb_group_id === pcbComponent.pcb_group_id) | ||
|
|
||
| if (!parentGroup?.anchor_position) return null | ||
|
|
||
| const targetCenter: Point = | ||
| hoveredPrimitive._element?.type === "pcb_smtpad" | ||
| ? { x: hoveredPrimitive.x, y: hoveredPrimitive.y } | ||
| : pcbComponent.center || { | ||
| x: hoveredPrimitive.x, | ||
| y: hoveredPrimitive.y, | ||
| } | ||
|
|
||
| const groupComponents = elements | ||
| .filter((el): el is PcbComponent => el.type === "pcb_component") | ||
| .filter((comp) => comp.pcb_group_id === parentGroup.pcb_group_id) | ||
|
|
||
| const boundingBox = calculateGroupBoundingBox(groupComponents) | ||
| if (!boundingBox) return null | ||
|
|
||
| const groupBounds: BoundingBox = { | ||
| minX: boundingBox.minX - VISUAL_CONFIG.GROUP_PADDING, | ||
| maxX: boundingBox.maxX + VISUAL_CONFIG.GROUP_PADDING, | ||
| minY: boundingBox.minY - VISUAL_CONFIG.GROUP_PADDING, | ||
| maxY: boundingBox.maxY + VISUAL_CONFIG.GROUP_PADDING, | ||
| } | ||
|
|
||
| const anchorMarkerPosition = findAnchorMarkerPosition( | ||
| parentGroup.anchor_position, | ||
| groupBounds, | ||
| ) | ||
|
|
||
| const offsetX = targetCenter.x - anchorMarkerPosition.x | ||
| const offsetY = targetCenter.y - anchorMarkerPosition.y | ||
|
|
||
| const anchorMarkerScreen = applyToPoint(transform, anchorMarkerPosition) | ||
| const componentScreen = applyToPoint(transform, targetCenter) | ||
|
|
||
| const xLineLength = Math.abs(componentScreen.x - anchorMarkerScreen.x) | ||
| const yLineLength = Math.abs(componentScreen.y - anchorMarkerScreen.y) | ||
|
|
||
| const isComponentAboveAnchor = componentScreen.y < anchorMarkerScreen.y | ||
| const isComponentRightOfAnchor = componentScreen.x > anchorMarkerScreen.x | ||
|
|
||
| const xLabelOffset = isComponentAboveAnchor | ||
| ? VISUAL_CONFIG.LABEL_OFFSET_ABOVE | ||
| : VISUAL_CONFIG.LABEL_OFFSET_BELOW | ||
|
|
||
| const yLabelOffset = isComponentRightOfAnchor | ||
| ? VISUAL_CONFIG.LABEL_OFFSET_RIGHT | ||
| : VISUAL_CONFIG.LABEL_OFFSET_LEFT | ||
|
|
||
| const shouldShowXLabel = xLineLength > VISUAL_CONFIG.MIN_LINE_LENGTH_FOR_LABEL | ||
| const shouldShowYLabel = yLineLength > VISUAL_CONFIG.MIN_LINE_LENGTH_FOR_LABEL | ||
|
|
||
| const labelStyle: React.CSSProperties = { | ||
| color: COLORS.LABEL_TEXT, | ||
| mixBlendMode: "difference", | ||
| pointerEvents: "none", | ||
| fontSize: VISUAL_CONFIG.LABEL_FONT_SIZE, | ||
| fontFamily: "monospace", | ||
| fontWeight: "bold", | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| style={{ | ||
| position: "absolute", | ||
| left: 0, | ||
| top: 0, | ||
| width: containerWidth, | ||
| height: containerHeight, | ||
| overflow: "hidden", | ||
| pointerEvents: "none", | ||
| zIndex: zIndexMap.dimensionOverlay, | ||
| }} | ||
| > | ||
| <svg | ||
| style={{ | ||
| position: "absolute", | ||
| left: 0, | ||
| top: 0, | ||
| pointerEvents: "none", | ||
| }} | ||
| width={containerWidth} | ||
| height={containerHeight} | ||
| > | ||
| <line | ||
| x1={anchorMarkerScreen.x} | ||
| y1={anchorMarkerScreen.y} | ||
| x2={componentScreen.x} | ||
| y2={anchorMarkerScreen.y} | ||
| stroke={COLORS.OFFSET_LINE} | ||
| strokeWidth={VISUAL_CONFIG.LINE_STROKE_WIDTH} | ||
| strokeDasharray={VISUAL_CONFIG.LINE_DASH_PATTERN} | ||
| /> | ||
|
|
||
| <line | ||
| x1={componentScreen.x} | ||
| y1={anchorMarkerScreen.y} | ||
| x2={componentScreen.x} | ||
| y2={componentScreen.y} | ||
| stroke={COLORS.OFFSET_LINE} | ||
| strokeWidth={VISUAL_CONFIG.LINE_STROKE_WIDTH} | ||
| strokeDasharray={VISUAL_CONFIG.LINE_DASH_PATTERN} | ||
| /> | ||
|
|
||
| <circle | ||
| cx={componentScreen.x} | ||
| cy={componentScreen.y} | ||
| r={VISUAL_CONFIG.COMPONENT_MARKER_RADIUS} | ||
| fill={COLORS.COMPONENT_MARKER_FILL} | ||
| stroke={COLORS.COMPONENT_MARKER_STROKE} | ||
| strokeWidth={1} | ||
| /> | ||
| </svg> | ||
|
|
||
| {shouldShowXLabel && ( | ||
| <div | ||
| style={{ | ||
| ...labelStyle, | ||
| position: "absolute", | ||
| left: Math.min(anchorMarkerScreen.x, componentScreen.x), | ||
| top: anchorMarkerScreen.y + xLabelOffset, | ||
| width: Math.abs(componentScreen.x - anchorMarkerScreen.x), | ||
| textAlign: "center", | ||
| }} | ||
| > | ||
| Δx: {offsetX.toFixed(2)}mm | ||
| </div> | ||
| )} | ||
|
|
||
| {shouldShowYLabel && ( | ||
| <div | ||
| style={{ | ||
| ...labelStyle, | ||
| position: "absolute", | ||
| left: componentScreen.x + yLabelOffset, | ||
| top: Math.min(anchorMarkerScreen.y, componentScreen.y), | ||
| height: Math.abs(componentScreen.y - anchorMarkerScreen.y), | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| justifyContent: "center", | ||
| }} | ||
| > | ||
| Δy: {offsetY.toFixed(2)}mm | ||
| </div> | ||
| )} | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this inside the MouseElement tracker?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't it be with the other overlays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marking this resolved? Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GroupAnchorOffsetOverlaysits insideMouseElementTrackerbecause it needs the tracker’s hover payload (highlighted primitives, mouse coords, container size).It’s the same pattern one line above with
ElementOverlayBox: both overlays rely on that internal hover state, so they live alongside it, while the rest of the overlays, those that only needelementsandtransform, so they stay in the outer stack. Is this not corrent ??