|
| 1 | +import { Tokens } from "../theme/tokens.slint"; |
| 2 | + |
| 3 | +// Compass strip — a horizontal bar showing N/E/S/W (and intermediates) with |
| 4 | +// the current heading centered. Bind `heading-deg` 0..360 (0 = north, 90 = |
| 5 | +// east, …); the strip slides so the active cardinal aligns to the center |
| 6 | +// indicator. Common in driving / flight / open-world HUDs. |
| 7 | +export component CompassStrip inherits Rectangle { |
| 8 | + // Current heading in degrees, 0..360 (0 = N, 90 = E, 180 = S, 270 = W). |
| 9 | + in property <float> heading-deg: 0; |
| 10 | + // Pixel width of the visible strip; the marks scroll inside it. |
| 11 | + in property <length> strip-width: 240px; |
| 12 | + // Pixel height of the strip. |
| 13 | + in property <length> strip-height: 32px; |
| 14 | + // Color of the marks + cardinal labels. |
| 15 | + in property <color> tint: Tokens.color-foreground; |
| 16 | + // Surface color behind the marks. |
| 17 | + in property <color> surface: Tokens.color-surface-1; |
| 18 | + |
| 19 | + background: transparent; |
| 20 | + width: root.strip-width; |
| 21 | + height: root.strip-height; |
| 22 | + |
| 23 | + // Surface |
| 24 | + Rectangle { |
| 25 | + width: 100%; |
| 26 | + height: 100%; |
| 27 | + background: root.surface; |
| 28 | + border-radius: Tokens.radius-md; |
| 29 | + border-width: 1px; |
| 30 | + border-color: Tokens.color-border-hairline; |
| 31 | + } |
| 32 | + |
| 33 | + // Cardinal labels — N at 0°, E at 90°, S at 180°, W at 270°. Each label |
| 34 | + // is positioned so its center sits at (cardinal-deg - heading-deg + 180) |
| 35 | + // * px-per-deg from the strip's left edge. px-per-deg = strip-width / 360. |
| 36 | + for cardinal[ci] in [ |
| 37 | + { label: "N", deg: 0 }, |
| 38 | + { label: "NE", deg: 45 }, |
| 39 | + { label: "E", deg: 90 }, |
| 40 | + { label: "SE", deg: 135 }, |
| 41 | + { label: "S", deg: 180 }, |
| 42 | + { label: "SW", deg: 225 }, |
| 43 | + { label: "W", deg: 270 }, |
| 44 | + { label: "NW", deg: 315 }, |
| 45 | + ]: Rectangle { |
| 46 | + property <float> rel-deg: mod(cardinal.deg - root.heading-deg + 540.0, 360.0) - 180.0; |
| 47 | + property <length> rel-x: rel-deg * (root.strip-width / 360.0); |
| 48 | + x: (parent.width / 2) + self.rel-x - self.width / 2; |
| 49 | + y: (parent.height - self.height) / 2; |
| 50 | + width: 26px; |
| 51 | + height: 22px; |
| 52 | + visible: abs(self.rel-deg) <= 90; |
| 53 | + Text { |
| 54 | + text: cardinal.label; |
| 55 | + color: root.tint; |
| 56 | + font-size: Tokens.typography-body-sm-size; |
| 57 | + font-weight: Tokens.typography-weight-semibold; |
| 58 | + horizontal-alignment: center; |
| 59 | + vertical-alignment: center; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Center indicator — small downward arrow at the top center marking the |
| 64 | + // exact heading. |
| 65 | + Rectangle { |
| 66 | + x: (parent.width - 2px) / 2; |
| 67 | + y: 2px; |
| 68 | + width: 2px; |
| 69 | + height: 6px; |
| 70 | + background: root.tint; |
| 71 | + } |
| 72 | +} |
0 commit comments