|
| 1 | +## UINode |
| 2 | + |
| 3 | +A **UINode** is an [ActorComponent](../actor/actor-component.md) |
| 4 | +that positions an actor in screen space using an **anchor**, |
| 5 | +**offset**, and **pivot** system. It is the base positioning layer |
| 6 | +for all 2D UI elements — [UISprite](./ui-sprite.md) extends it to |
| 7 | +add visuals and interaction. |
| 8 | + |
| 9 | +When a [UIRenderer](./ui-renderer.md) exists in the game instance, |
| 10 | +every new `UINode` automatically registers itself with the renderer. |
| 11 | +Otherwise it falls back to listening to renderer `resize` events |
| 12 | +directly. |
| 13 | + |
| 14 | +```ts |
| 15 | +import { Actor, UINode } from "@jolly-pixel/engine"; |
| 16 | + |
| 17 | +const hud = new Actor(gameInstance, { |
| 18 | + name: "hud", |
| 19 | + parent: camera2D |
| 20 | +}) |
| 21 | + .registerComponent(UINode, { |
| 22 | + anchor: { x: "right", y: "top" }, |
| 23 | + offset: { x: -16, y: -16 }, |
| 24 | + size: { width: 100, height: 40 } |
| 25 | + }); |
| 26 | +``` |
| 27 | + |
| 28 | +### Anchor |
| 29 | + |
| 30 | +The **anchor** determines which screen edge the element aligns to. |
| 31 | +Anchors are independent on each axis. |
| 32 | + |
| 33 | +| Axis | Values | Description | |
| 34 | +| ---- | ------ | ----------- | |
| 35 | +| `x` | `"left"`, `"center"`, `"right"` | Horizontal screen edge | |
| 36 | +| `y` | `"top"`, `"center"`, `"bottom"` | Vertical screen edge | |
| 37 | + |
| 38 | +When an anchor is set to `"left"`, the element is pushed against the |
| 39 | +left edge of the screen. Combined with an offset you can create |
| 40 | +consistent margins that survive window resizes. |
| 41 | + |
| 42 | +### Offset |
| 43 | + |
| 44 | +The **offset** shifts the element away from its anchor in |
| 45 | +world units. Positive X moves right, positive Y moves up. |
| 46 | + |
| 47 | +```ts |
| 48 | +// 20 units from the left edge, 10 units below the top |
| 49 | +{ |
| 50 | + anchor: { x: "left", y: "top" }, |
| 51 | + offset: { x: 20, y: -10 } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Pivot |
| 56 | + |
| 57 | +The **pivot** is a normalized origin point from `0` to `1` that |
| 58 | +controls which part of the element sits at the computed position. |
| 59 | + |
| 60 | +| Pivot | Meaning | |
| 61 | +| ----- | ------- | |
| 62 | +| `{ x: 0, y: 0 }` | Bottom-left corner | |
| 63 | +| `{ x: 0.5, y: 0.5 }` | Center (default) | |
| 64 | +| `{ x: 1, y: 1 }` | Top-right corner | |
| 65 | + |
| 66 | +Setting the pivot to `{ x: 0, y: 1 }` makes the top-left corner |
| 67 | +the origin — useful for left-aligned HUD panels anchored to the |
| 68 | +top of the screen. |
| 69 | + |
| 70 | +### Size |
| 71 | + |
| 72 | +The **size** defines the width and height of the element in world |
| 73 | +units. It is used by `UISprite` to create the mesh geometry and by |
| 74 | +the anchoring logic to keep the element fully on-screen. |
| 75 | + |
| 76 | +```ts |
| 77 | +{ |
| 78 | + size: { width: 200, height: 60 } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Constructor |
| 83 | + |
| 84 | +```ts |
| 85 | +interface UINodeOptions { |
| 86 | + anchor?: { |
| 87 | + x?: "left" | "center" | "right"; |
| 88 | + y?: "top" | "center" | "bottom"; |
| 89 | + }; |
| 90 | + offset?: { |
| 91 | + x?: number; |
| 92 | + y?: number; |
| 93 | + }; |
| 94 | + size?: { |
| 95 | + width?: number; |
| 96 | + height?: number; |
| 97 | + }; |
| 98 | + pivot?: { |
| 99 | + x?: number; |
| 100 | + y?: number; |
| 101 | + }; |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +| Option | Default | Description | |
| 106 | +| ------ | ------- | ----------- | |
| 107 | +| `anchor` | `{ x: "center", y: "center" }` | Screen-edge alignment | |
| 108 | +| `offset` | `{ x: 0, y: 0 }` | Offset from the anchor in world units | |
| 109 | +| `size` | `{ width: 0, height: 0 }` | Element dimensions in world units | |
| 110 | +| `pivot` | `{ x: 0.5, y: 0.5 }` | Normalized origin point (0 – 1) | |
| 111 | + |
| 112 | +### Positioning examples |
| 113 | + |
| 114 | +**Centered element:** |
| 115 | + |
| 116 | +```ts |
| 117 | +// Defaults — centered on screen |
| 118 | +{ anchor: { x: "center", y: "center" } } |
| 119 | +``` |
| 120 | + |
| 121 | +**Top-right corner with margin:** |
| 122 | + |
| 123 | +```ts |
| 124 | +{ |
| 125 | + anchor: { x: "right", y: "top" }, |
| 126 | + offset: { x: -16, y: -16 }, |
| 127 | + pivot: { x: 1, y: 1 } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +**Bottom-left health bar:** |
| 132 | + |
| 133 | +```ts |
| 134 | +{ |
| 135 | + anchor: { x: "left", y: "bottom" }, |
| 136 | + offset: { x: 20, y: 20 }, |
| 137 | + size: { width: 300, height: 24 }, |
| 138 | + pivot: { x: 0, y: 0 } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### API |
| 143 | + |
| 144 | +```ts |
| 145 | +class UINode extends ActorComponent { |
| 146 | + get size(): { width: number; height: number }; |
| 147 | + get pivot(): { x: number; y: number }; |
| 148 | + |
| 149 | + addChildren(object: THREE.Object3D): void; |
| 150 | + updateToWorldPosition(): void; |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### See also |
| 155 | + |
| 156 | +- [UIRenderer](./ui-renderer.md) — the orthographic overlay system |
| 157 | +- [UISprite](./ui-sprite.md) — visual + interactive UI element |
| 158 | +- [ActorComponent](../actor/actor-component.md) — component base type |
0 commit comments