@kayelaa/canvas 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -584,10 +584,12 @@ declare namespace KaylaInternals {
584
584
  instance: KaylaContext<any>;
585
585
  value: unknown;
586
586
  };
587
+ lastLayer?: KaylaLayerInfo;
587
588
  get childrenCount(): number;
588
589
  maxSafeChildren: number;
589
590
  dynamicChildren: KaylaElement<any>[];
590
591
  constructor(globalKayla: GlobalKayla, scene: KaylaScene, element: KaylaElement<Props>);
592
+ getCorrectMousePos(pos?: Vector2): Vector2;
591
593
  getChildrenEntities(): KaylaRectEntity[];
592
594
  pointerHook(pos: Vector2, type: KaylaClickType, action: KaylaPointerAction): void;
593
595
  resizeHook({ width, height }: {
@@ -634,6 +636,7 @@ declare namespace KaylaInternals {
634
636
  setMaxChildren(max: number): void;
635
637
  getMaxChildren(): number;
636
638
  getFiberChain(): UnsafeKaylaFiber[];
639
+ getAllChildrenNestedFlattened(): UnsafeKaylaFiber[];
637
640
  getContextChain(): KaylaFiber<any, any>["contextInfo"][];
638
641
  findContextValueFromInst(inst: KaylaContext<any>): unknown;
639
642
  getEntityChain(): KaylaRectEntity[];
@@ -845,6 +848,7 @@ declare namespace KaylaInternals {
845
848
  class KaylaScene {
846
849
  #private;
847
850
  constructor(scene: LeaSceneII);
851
+ getLayers(): Map<string, KaylaLayerInfo>;
848
852
  /**
849
853
  * Exposes all the dangerous fibers.
850
854
  */
@@ -861,6 +865,15 @@ declare namespace KaylaInternals {
861
865
  * Exposes the game object.
862
866
  */
863
867
  getGame(): KaylaGame;
868
+ getOrCreateLayer(name: string, defaultZ?: number): KaylaLayerInfo;
869
+ getSortedLayers(): KaylaLayerInfo[];
870
+ setLayerVisible(name: string, visible: boolean): void;
871
+ /**
872
+ * Draws all entities in z-order (higher z = drawn later).
873
+ *
874
+ * @param ctx - The 2D rendering context
875
+ */
876
+ protected handleDraw(ctx: CanvasRenderingContext2D): void;
864
877
  private drawHandler;
865
878
  /**
866
879
  * Attaches this scene to a game instance.
@@ -880,6 +893,7 @@ declare namespace KaylaInternals {
880
893
  * @param elem - Root Kayla element (usually JSX)
881
894
  */
882
895
  spawn(elem: KaylaElement<any>): Promise<void>;
896
+ getRoot(): UnsafeKaylaFiber;
883
897
  private createFiber;
884
898
  /**
885
899
  * Detaches this scene from its game (if attached).
@@ -1094,17 +1108,29 @@ declare const useGlobalClick: (onPointer: KaylaInternals.KaylaFiber["onGlobalPoi
1094
1108
  * Useful for global input (pause menu toggle, debug shortcuts, drag anywhere…).
1095
1109
  * Multiple `useGlobalPointer` calls **stack** and run in declaration order.
1096
1110
  *
1097
- * @param callback Global pointer handler receives world position and click type
1111
+ * **Important coordinate system depends on context:**
1112
+ * - If this component is inside a `<Layer transform={…}>`, you receive **layer-local coordinates**
1113
+ * - Otherwise you receive **world coordinates** (same as entity positions)
1114
+ *
1115
+ * Multiple handlers stack — they are called in declaration order.
1116
+ *
1117
+ * @param handler Called with:
1118
+ * - `pos` pointer position (world or layer-local — see above)
1119
+ * - `type` "left" | "right" | "middle"
1120
+ * - `action` "down" | "up" | "move" | "cancel"
1121
+ * @param config Optional filter to reduce calls
1098
1122
  *
1099
1123
  * @example
1100
- * useGlobalPointer((pos) => {
1101
- * isPaused.set(!isPaused.get());
1124
+ * // Global pause on right-click (anywhere)
1125
+ * useGlobalPointer((pos, type) => {
1126
+ * if (type === "right") isPaused.value = !isPaused.value;
1102
1127
  * }, { type: "right", action: "down" });
1103
- * useGlobalPointer((pos) => {
1104
- * // set player pos as mouse pos
1105
- * player.x = pos.x;
1106
- * player.y = pos.y;
1107
- * }, { action: "move" });
1128
+ *
1129
+ * @example
1130
+ * // Inside a zoomable map layer — pos is already in map-local space
1131
+ * useGlobalPointer((localPos) => {
1132
+ * console.log("Clicked map at:", localPos);
1133
+ * });
1108
1134
  */
1109
1135
  declare const useGlobalPointer: (onPointer: KaylaInternals.KaylaFiber["onGlobalPointer"][number]["handler"], config?: KaylaClickConfig) => void;
1110
1136
  /**
@@ -1138,17 +1164,35 @@ declare const useClick: (onPointer: KaylaInternals.KaylaFiber["onGlobalPointer"]
1138
1164
  * **This hook MUST be called at the top level of a component function — never inside loops, conditions, nested functions, or callbacks.**
1139
1165
  * The call order of all hooks is **strictly fixed** across every refresh; reordering calls will corrupt internal state.
1140
1166
  *
1167
+ * **Coordinate system note (very important since layers):**
1168
+ * - If this component lives inside a `<Layer>` with a `transform` prop (camera, zoom, scroll, rotation…),
1169
+ * then `pos` is already transformed into the **layer-local coordinate system**.
1170
+ * - The automatic hit-test rectangle check is also performed in **layer-local space**.
1171
+ * - If there is **no transform** on the containing layer → `pos` is in world coordinates.
1172
+ *
1141
1173
  * Uses the rectangle bounds from {@link useRect} / underlying entity.
1142
1174
  * Multiple `usePointer` calls **stack** and run in declaration order.
1143
1175
  *
1144
- * @param callback pointer handler — receives world position and click type ("left" | "right" | "middle")
1176
+ * @param handler Receives:
1177
+ * - `pos` pointer position in the coordinate system this entity lives in
1178
+ * - `type` button ("left"|"right"|"middle")
1179
+ * - `action` "down"|"up"|"move"|"cancel"
1180
+ * @param config Optional filters (button + action)
1181
+ *
1182
+ * @example
1183
+ * // Classic button (no layer transform)
1184
+ * usePointer((worldPos, type, action) => {
1185
+ * if (action === "down" && type === "left") {
1186
+ * onClick();
1187
+ * }
1188
+ * }, { type: "left", action: "down" });
1145
1189
  *
1146
1190
  * @example
1147
- * usePointer((pos) => {
1148
- * score.add(1);
1149
- * playSound("collect");
1150
- * // optional: spawn particle children on click
1151
- * }, { type: "left", action: "up" });
1191
+ * // Inside a zoomed/scrollable UI panel or map
1192
+ * usePointer((localPos) => {
1193
+ * // localPos is already correct for items inside this zoomed layer
1194
+ * inventory.selectSlotAt(localPos);
1195
+ * });
1152
1196
  *
1153
1197
  * @see {@link useGlobalPointer} — for pointer events anywhere (not bound to this entity)
1154
1198
  */
@@ -1705,12 +1749,116 @@ declare namespace JSX {
1705
1749
  }
1706
1750
  type ElementType = FC<any>;
1707
1751
  }
1752
+ /**
1753
+ * Configuration object for a single named rendering layer.
1754
+ * Created/managed via `<Layer>` components.
1755
+ */
1756
+ interface KaylaLayerInfo {
1757
+ /** Unique name of the layer (used as key in scene.#layers Map) */
1758
+ name: string;
1759
+ /** Z-index / sort order of the layer itself (higher = drawn later) */
1760
+ z: number;
1761
+ /** Blend mode applied to the entire layer during rendering */
1762
+ blendMode?: GlobalCompositeOperation;
1763
+ /** Whether this layer should be rendered at all */
1764
+ visible: boolean;
1765
+ /**
1766
+ * Root fiber whose subtree belongs to this layer.
1767
+ * Usually set to the fiber of the `<Layer name="..." />` component that first claimed this name.
1768
+ */
1769
+ rootFiber: UnsafeKaylaFiber | null;
1770
+ /**
1771
+ * 2D transformation matrix applied to rendering **and** pointer coordinate transformation.
1772
+ * When present, pointer events inside this layer are delivered in local coordinates.
1773
+ */
1774
+ transform?: KMatrix2D | null;
1775
+ }
1776
+ /** Props accepted by the `<Layer>` component */
1777
+ interface KaylaLayerProps extends FCProps {
1778
+ /** Required. Unique name identifying this layer */
1779
+ name: string;
1780
+ /** Layer draw order (higher = later / on top) */
1781
+ z?: number;
1782
+ /** Canvas blend mode for the whole layer */
1783
+ blend?: GlobalCompositeOperation;
1784
+ /** Toggle rendering of the entire layer */
1785
+ visible?: boolean;
1786
+ /** Children rendered inside this layer's context */
1787
+ children?: KaylaElement<any>[];
1788
+ /**
1789
+ * Transformation applied to rendering and input coordinates.
1790
+ * Use `KMatrix2D` to build cameras, zoom views, parallax layers, rotated UI panels, etc.
1791
+ */
1792
+ transform?: KMatrix2D;
1793
+ }
1794
+ /**
1795
+ * Defines a named rendering layer with optional transform, blend mode, visibility, and draw order.
1796
+ *
1797
+ * `<Layer>` is a special component that groups its children into a separate rendering pass.
1798
+ * It allows you to:
1799
+ * - Control draw order between major groups of entities (via `z`)
1800
+ * - Apply coordinate transforms (camera, zoom, scroll, rotation) to an entire subtree
1801
+ * - Change blending behavior for the whole layer
1802
+ * - Toggle visibility of large parts of the scene without unmounting components
1803
+ *
1804
+ * **Coordinate system behavior:**
1805
+ * - If `transform` is provided, **all pointer coordinates** delivered to `usePointer` / `useGlobalPointer`
1806
+ * inside this layer (and its descendants) are automatically transformed into the **layer-local coordinate system**.
1807
+ * - The automatic hit-test for `usePointer` also happens in layer-local space.
1808
+ * - Without `transform` (identity/default), coordinates remain in **world space**.
1809
+ *
1810
+ * **Important notes:**
1811
+ * - Layers are **not** nested in the traditional sense — each `<Layer>` creates an independent rendering group.
1812
+ * - Multiple `<Layer>` components with the **same `name`** will **share the same layer configuration** (last one wins for most properties).
1813
+ * - The `rootFiber` of the layer is set to the fiber of the **first** `<Layer name="..."` component encountered with that name.
1814
+ * - Children inside `<Layer>` still participate in normal component lifecycle, ticking, painting, pointer handling, etc.
1815
+ *
1816
+ * @example
1817
+ * // Simple UI overlay that draws on top
1818
+ * <Layer name="ui" z={1000} visible={true}>
1819
+ * <HUD />
1820
+ * <PauseMenu />
1821
+ * </Layer>
1822
+ *
1823
+ * @example
1824
+ * // Zoomable / scrollable map area
1825
+ * function MapView() {
1826
+ * const zoom = useSelf(() => ({ scale: 1.8, offset: new LEA.Vector2(120, 80) }));
1827
+ *
1828
+ * const matrix = new KMatrix2D();
1829
+ * matrix.translateSelf(zoom.offset.x, zoom.offset.y);
1830
+ * matrix.scaleSelf(zoom.scale, zoom.scale);
1831
+ *
1832
+ * return (
1833
+ * <Layer
1834
+ * name="world-map"
1835
+ * z={10}
1836
+ * transform={matrix}
1837
+ * blend="source-over"
1838
+ * >
1839
+ * <BackgroundTiles />
1840
+ * <Player />
1841
+ * <Enemies />
1842
+ * {// Pointer events inside here receive map-local coordinates }
1843
+ * </Layer>
1844
+ * );
1845
+ * }
1846
+ * */
1847
+ declare const Layer: FC<KaylaLayerProps>;
1848
+ declare class KMatrix2D extends DOMMatrix {
1849
+ #private;
1850
+ constructor();
1851
+ transformV(v: Vector2): Vector2;
1852
+ inverseV(v: Vector2): Vector2;
1853
+ }
1708
1854
 
1709
1855
  type Kayla_ExportsOfFC<T extends FC<any, any>> = ExportsOfFC<T>;
1710
1856
  type Kayla_FC<Props extends FCProps = FCProps, Exports extends FCExports = FCExports> = FC<Props, Exports>;
1711
1857
  type Kayla_FCExports = FCExports;
1712
1858
  type Kayla_FCProps = FCProps;
1713
1859
  declare const Kayla_JSX: typeof JSX;
1860
+ type Kayla_KMatrix2D = KMatrix2D;
1861
+ declare const Kayla_KMatrix2D: typeof KMatrix2D;
1714
1862
  type Kayla_KaylaClickConfig = KaylaClickConfig;
1715
1863
  type Kayla_KaylaClickType = KaylaClickType;
1716
1864
  type Kayla_KaylaContext<Context> = KaylaContext<Context>;
@@ -1724,12 +1872,15 @@ type Kayla_KaylaFiberControl = KaylaFiberControl;
1724
1872
  declare const Kayla_KaylaFragment: typeof KaylaFragment;
1725
1873
  type Kayla_KaylaGame = KaylaGame;
1726
1874
  declare const Kayla_KaylaInternals: typeof KaylaInternals;
1875
+ type Kayla_KaylaLayerInfo = KaylaLayerInfo;
1876
+ type Kayla_KaylaLayerProps = KaylaLayerProps;
1727
1877
  type Kayla_KaylaPointerAction = KaylaPointerAction;
1728
1878
  declare const Kayla_KaylaRect: typeof KaylaRect;
1729
1879
  type Kayla_KaylaRef<T> = KaylaRef<T>;
1730
1880
  type Kayla_KaylaRenderer = KaylaRenderer;
1731
1881
  type Kayla_KaylaScene = KaylaScene;
1732
1882
  type Kayla_KaylaState<T> = KaylaState<T>;
1883
+ declare const Kayla_Layer: typeof Layer;
1733
1884
  type Kayla_PropOfFC<T extends FC<any, any>> = PropOfFC<T>;
1734
1885
  type Kayla_Reassignable<T extends Record<any, any>> = Reassignable<T>;
1735
1886
  type Kayla_UnsafeKaylaFiber = UnsafeKaylaFiber;
@@ -1767,7 +1918,7 @@ declare const Kayla_useState: typeof useState;
1767
1918
  declare const Kayla_useTick: typeof useTick;
1768
1919
  declare const Kayla_useViewportEffect: typeof useViewportEffect;
1769
1920
  declare namespace Kayla {
1770
- export { type Kayla_ExportsOfFC as ExportsOfFC, type Kayla_FC as FC, type Kayla_FCExports as FCExports, type Kayla_FCProps as FCProps, Kayla_JSX as JSX, type Kayla_KaylaClickConfig as KaylaClickConfig, type Kayla_KaylaClickType as KaylaClickType, Kayla_KaylaContext as KaylaContext, type Kayla_KaylaCustomHookConfig as KaylaCustomHookConfig, type Kayla_KaylaElement as KaylaElement, type Kayla_KaylaElementRef as KaylaElementRef, type Kayla_KaylaExportables as KaylaExportables, type Kayla_KaylaExports as KaylaExports, type Kayla_KaylaFiberControl as KaylaFiberControl, Kayla_KaylaFragment as KaylaFragment, type Kayla_KaylaGame as KaylaGame, Kayla_KaylaInternals as KaylaInternals, type Kayla_KaylaPointerAction as KaylaPointerAction, Kayla_KaylaRect as KaylaRect, type Kayla_KaylaRef as KaylaRef, type Kayla_KaylaRenderer as KaylaRenderer, type Kayla_KaylaScene as KaylaScene, type Kayla_KaylaState as KaylaState, type Kayla_PropOfFC as PropOfFC, type Kayla_Reassignable as Reassignable, type Kayla_UnsafeKaylaFiber as UnsafeKaylaFiber, Kayla_createContext as createContext, Kayla_createElement as createElement, Kayla_createGame as createGame, Kayla_createReassignableObject as createReassignableObject, Kayla_createRenderer as createRenderer, Kayla_createScene as createScene, Kayla_createUseHook as createUseHook, selfSym as self, Kayla_setLogLevel as setLogLevel, Kayla_useClick as useClick, Kayla_useContext as useContext, Kayla_useCurrentGame as useCurrentGame, Kayla_useCurrentRenderer as useCurrentRenderer, Kayla_useCurrentScene as useCurrentScene, Kayla_useCurrentTicker as useCurrentTicker, Kayla_useDisposableRef as useDisposableRef, Kayla_useEffect as useEffect, Kayla_useEntity as useEntity, Kayla_useExports as useExports, Kayla_useFiber as useFiber, Kayla_useFiberControl as useFiberControl, Kayla_useGlobalClick as useGlobalClick, Kayla_useGlobalPointer as useGlobalPointer, Kayla_useInitialization as useInitialization, Kayla_useNextStack as useNextStack, Kayla_usePaint as usePaint, Kayla_usePointer as usePointer, Kayla_useRect as useRect, Kayla_useRef as useRef, Kayla_useSelf as useSelf, Kayla_useShouldRefresh as useShouldRefresh, Kayla_useState as useState, Kayla_useTick as useTick, Kayla_useViewportEffect as useViewportEffect };
1921
+ export { type Kayla_ExportsOfFC as ExportsOfFC, type Kayla_FC as FC, type Kayla_FCExports as FCExports, type Kayla_FCProps as FCProps, Kayla_JSX as JSX, Kayla_KMatrix2D as KMatrix2D, type Kayla_KaylaClickConfig as KaylaClickConfig, type Kayla_KaylaClickType as KaylaClickType, Kayla_KaylaContext as KaylaContext, type Kayla_KaylaCustomHookConfig as KaylaCustomHookConfig, type Kayla_KaylaElement as KaylaElement, type Kayla_KaylaElementRef as KaylaElementRef, type Kayla_KaylaExportables as KaylaExportables, type Kayla_KaylaExports as KaylaExports, type Kayla_KaylaFiberControl as KaylaFiberControl, Kayla_KaylaFragment as KaylaFragment, type Kayla_KaylaGame as KaylaGame, Kayla_KaylaInternals as KaylaInternals, type Kayla_KaylaLayerInfo as KaylaLayerInfo, type Kayla_KaylaLayerProps as KaylaLayerProps, type Kayla_KaylaPointerAction as KaylaPointerAction, Kayla_KaylaRect as KaylaRect, type Kayla_KaylaRef as KaylaRef, type Kayla_KaylaRenderer as KaylaRenderer, type Kayla_KaylaScene as KaylaScene, type Kayla_KaylaState as KaylaState, Kayla_Layer as Layer, type Kayla_PropOfFC as PropOfFC, type Kayla_Reassignable as Reassignable, type Kayla_UnsafeKaylaFiber as UnsafeKaylaFiber, Kayla_createContext as createContext, Kayla_createElement as createElement, Kayla_createGame as createGame, Kayla_createReassignableObject as createReassignableObject, Kayla_createRenderer as createRenderer, Kayla_createScene as createScene, Kayla_createUseHook as createUseHook, selfSym as self, Kayla_setLogLevel as setLogLevel, Kayla_useClick as useClick, Kayla_useContext as useContext, Kayla_useCurrentGame as useCurrentGame, Kayla_useCurrentRenderer as useCurrentRenderer, Kayla_useCurrentScene as useCurrentScene, Kayla_useCurrentTicker as useCurrentTicker, Kayla_useDisposableRef as useDisposableRef, Kayla_useEffect as useEffect, Kayla_useEntity as useEntity, Kayla_useExports as useExports, Kayla_useFiber as useFiber, Kayla_useFiberControl as useFiberControl, Kayla_useGlobalClick as useGlobalClick, Kayla_useGlobalPointer as useGlobalPointer, Kayla_useInitialization as useInitialization, Kayla_useNextStack as useNextStack, Kayla_usePaint as usePaint, Kayla_usePointer as usePointer, Kayla_useRect as useRect, Kayla_useRef as useRef, Kayla_useSelf as useSelf, Kayla_useShouldRefresh as useShouldRefresh, Kayla_useState as useState, Kayla_useTick as useTick, Kayla_useViewportEffect as useViewportEffect };
1771
1922
  }
1772
1923
 
1773
- export { usePointer as $, createUseHook as A, selfSym as B, setLogLevel as C, useClick as D, type ExportsOfFC as E, type FCProps as F, useContext as G, useCurrentGame as H, useCurrentRenderer as I, JSX as J, Kayla as K, useCurrentScene as L, useCurrentTicker as M, useDisposableRef as N, useEffect as O, type PropOfFC as P, useEntity as Q, type Reassignable as R, useExports as S, useFiber as T, type UnsafeKaylaFiber as U, useFiberControl as V, useGlobalClick as W, useGlobalPointer as X, useInitialization as Y, useNextStack as Z, usePaint as _, type FCExports as a, useRect as a0, useRef as a1, useSelf as a2, useShouldRefresh as a3, useState as a4, useTick as a5, useViewportEffect as a6, type FC as b, type KaylaClickConfig as c, type KaylaClickType as d, KaylaContext as e, type KaylaCustomHookConfig as f, type KaylaElement as g, type KaylaElementRef as h, type KaylaExportables as i, type KaylaExports as j, type KaylaFiberControl as k, KaylaFragment as l, KaylaGame as m, KaylaInternals as n, type KaylaPointerAction as o, KaylaRect as p, type KaylaRef as q, KaylaRenderer as r, KaylaScene as s, type KaylaState as t, createContext as u, createElement as v, createGame as w, createReassignableObject as x, createRenderer as y, createScene as z };
1924
+ export { useGlobalPointer as $, createReassignableObject as A, createRenderer as B, createScene as C, createUseHook as D, type ExportsOfFC as E, type FCProps as F, selfSym as G, setLogLevel as H, useClick as I, JSX as J, Kayla as K, Layer as L, useContext as M, useCurrentGame as N, useCurrentRenderer as O, type PropOfFC as P, useCurrentScene as Q, type Reassignable as R, useCurrentTicker as S, useDisposableRef as T, type UnsafeKaylaFiber as U, useEffect as V, useEntity as W, useExports as X, useFiber as Y, useFiberControl as Z, useGlobalClick as _, type FCExports as a, useInitialization as a0, useNextStack as a1, usePaint as a2, usePointer as a3, useRect as a4, useRef as a5, useSelf as a6, useShouldRefresh as a7, useState as a8, useTick as a9, useViewportEffect as aa, type FC as b, KMatrix2D as c, type KaylaClickConfig as d, type KaylaClickType as e, KaylaContext as f, type KaylaCustomHookConfig as g, type KaylaElement as h, type KaylaElementRef as i, type KaylaExportables as j, type KaylaExports as k, type KaylaFiberControl as l, KaylaFragment as m, KaylaGame as n, KaylaInternals as o, type KaylaLayerInfo as p, type KaylaLayerProps as q, type KaylaPointerAction as r, KaylaRect as s, type KaylaRef as t, KaylaRenderer as u, KaylaScene as v, type KaylaState as w, createContext as x, createElement as y, createGame as z };
@@ -584,10 +584,12 @@ declare namespace KaylaInternals {
584
584
  instance: KaylaContext<any>;
585
585
  value: unknown;
586
586
  };
587
+ lastLayer?: KaylaLayerInfo;
587
588
  get childrenCount(): number;
588
589
  maxSafeChildren: number;
589
590
  dynamicChildren: KaylaElement<any>[];
590
591
  constructor(globalKayla: GlobalKayla, scene: KaylaScene, element: KaylaElement<Props>);
592
+ getCorrectMousePos(pos?: Vector2): Vector2;
591
593
  getChildrenEntities(): KaylaRectEntity[];
592
594
  pointerHook(pos: Vector2, type: KaylaClickType, action: KaylaPointerAction): void;
593
595
  resizeHook({ width, height }: {
@@ -634,6 +636,7 @@ declare namespace KaylaInternals {
634
636
  setMaxChildren(max: number): void;
635
637
  getMaxChildren(): number;
636
638
  getFiberChain(): UnsafeKaylaFiber[];
639
+ getAllChildrenNestedFlattened(): UnsafeKaylaFiber[];
637
640
  getContextChain(): KaylaFiber<any, any>["contextInfo"][];
638
641
  findContextValueFromInst(inst: KaylaContext<any>): unknown;
639
642
  getEntityChain(): KaylaRectEntity[];
@@ -845,6 +848,7 @@ declare namespace KaylaInternals {
845
848
  class KaylaScene {
846
849
  #private;
847
850
  constructor(scene: LeaSceneII);
851
+ getLayers(): Map<string, KaylaLayerInfo>;
848
852
  /**
849
853
  * Exposes all the dangerous fibers.
850
854
  */
@@ -861,6 +865,15 @@ declare namespace KaylaInternals {
861
865
  * Exposes the game object.
862
866
  */
863
867
  getGame(): KaylaGame;
868
+ getOrCreateLayer(name: string, defaultZ?: number): KaylaLayerInfo;
869
+ getSortedLayers(): KaylaLayerInfo[];
870
+ setLayerVisible(name: string, visible: boolean): void;
871
+ /**
872
+ * Draws all entities in z-order (higher z = drawn later).
873
+ *
874
+ * @param ctx - The 2D rendering context
875
+ */
876
+ protected handleDraw(ctx: CanvasRenderingContext2D): void;
864
877
  private drawHandler;
865
878
  /**
866
879
  * Attaches this scene to a game instance.
@@ -880,6 +893,7 @@ declare namespace KaylaInternals {
880
893
  * @param elem - Root Kayla element (usually JSX)
881
894
  */
882
895
  spawn(elem: KaylaElement<any>): Promise<void>;
896
+ getRoot(): UnsafeKaylaFiber;
883
897
  private createFiber;
884
898
  /**
885
899
  * Detaches this scene from its game (if attached).
@@ -1094,17 +1108,29 @@ declare const useGlobalClick: (onPointer: KaylaInternals.KaylaFiber["onGlobalPoi
1094
1108
  * Useful for global input (pause menu toggle, debug shortcuts, drag anywhere…).
1095
1109
  * Multiple `useGlobalPointer` calls **stack** and run in declaration order.
1096
1110
  *
1097
- * @param callback Global pointer handler receives world position and click type
1111
+ * **Important coordinate system depends on context:**
1112
+ * - If this component is inside a `<Layer transform={…}>`, you receive **layer-local coordinates**
1113
+ * - Otherwise you receive **world coordinates** (same as entity positions)
1114
+ *
1115
+ * Multiple handlers stack — they are called in declaration order.
1116
+ *
1117
+ * @param handler Called with:
1118
+ * - `pos` pointer position (world or layer-local — see above)
1119
+ * - `type` "left" | "right" | "middle"
1120
+ * - `action` "down" | "up" | "move" | "cancel"
1121
+ * @param config Optional filter to reduce calls
1098
1122
  *
1099
1123
  * @example
1100
- * useGlobalPointer((pos) => {
1101
- * isPaused.set(!isPaused.get());
1124
+ * // Global pause on right-click (anywhere)
1125
+ * useGlobalPointer((pos, type) => {
1126
+ * if (type === "right") isPaused.value = !isPaused.value;
1102
1127
  * }, { type: "right", action: "down" });
1103
- * useGlobalPointer((pos) => {
1104
- * // set player pos as mouse pos
1105
- * player.x = pos.x;
1106
- * player.y = pos.y;
1107
- * }, { action: "move" });
1128
+ *
1129
+ * @example
1130
+ * // Inside a zoomable map layer — pos is already in map-local space
1131
+ * useGlobalPointer((localPos) => {
1132
+ * console.log("Clicked map at:", localPos);
1133
+ * });
1108
1134
  */
1109
1135
  declare const useGlobalPointer: (onPointer: KaylaInternals.KaylaFiber["onGlobalPointer"][number]["handler"], config?: KaylaClickConfig) => void;
1110
1136
  /**
@@ -1138,17 +1164,35 @@ declare const useClick: (onPointer: KaylaInternals.KaylaFiber["onGlobalPointer"]
1138
1164
  * **This hook MUST be called at the top level of a component function — never inside loops, conditions, nested functions, or callbacks.**
1139
1165
  * The call order of all hooks is **strictly fixed** across every refresh; reordering calls will corrupt internal state.
1140
1166
  *
1167
+ * **Coordinate system note (very important since layers):**
1168
+ * - If this component lives inside a `<Layer>` with a `transform` prop (camera, zoom, scroll, rotation…),
1169
+ * then `pos` is already transformed into the **layer-local coordinate system**.
1170
+ * - The automatic hit-test rectangle check is also performed in **layer-local space**.
1171
+ * - If there is **no transform** on the containing layer → `pos` is in world coordinates.
1172
+ *
1141
1173
  * Uses the rectangle bounds from {@link useRect} / underlying entity.
1142
1174
  * Multiple `usePointer` calls **stack** and run in declaration order.
1143
1175
  *
1144
- * @param callback pointer handler — receives world position and click type ("left" | "right" | "middle")
1176
+ * @param handler Receives:
1177
+ * - `pos` pointer position in the coordinate system this entity lives in
1178
+ * - `type` button ("left"|"right"|"middle")
1179
+ * - `action` "down"|"up"|"move"|"cancel"
1180
+ * @param config Optional filters (button + action)
1181
+ *
1182
+ * @example
1183
+ * // Classic button (no layer transform)
1184
+ * usePointer((worldPos, type, action) => {
1185
+ * if (action === "down" && type === "left") {
1186
+ * onClick();
1187
+ * }
1188
+ * }, { type: "left", action: "down" });
1145
1189
  *
1146
1190
  * @example
1147
- * usePointer((pos) => {
1148
- * score.add(1);
1149
- * playSound("collect");
1150
- * // optional: spawn particle children on click
1151
- * }, { type: "left", action: "up" });
1191
+ * // Inside a zoomed/scrollable UI panel or map
1192
+ * usePointer((localPos) => {
1193
+ * // localPos is already correct for items inside this zoomed layer
1194
+ * inventory.selectSlotAt(localPos);
1195
+ * });
1152
1196
  *
1153
1197
  * @see {@link useGlobalPointer} — for pointer events anywhere (not bound to this entity)
1154
1198
  */
@@ -1705,12 +1749,116 @@ declare namespace JSX {
1705
1749
  }
1706
1750
  type ElementType = FC<any>;
1707
1751
  }
1752
+ /**
1753
+ * Configuration object for a single named rendering layer.
1754
+ * Created/managed via `<Layer>` components.
1755
+ */
1756
+ interface KaylaLayerInfo {
1757
+ /** Unique name of the layer (used as key in scene.#layers Map) */
1758
+ name: string;
1759
+ /** Z-index / sort order of the layer itself (higher = drawn later) */
1760
+ z: number;
1761
+ /** Blend mode applied to the entire layer during rendering */
1762
+ blendMode?: GlobalCompositeOperation;
1763
+ /** Whether this layer should be rendered at all */
1764
+ visible: boolean;
1765
+ /**
1766
+ * Root fiber whose subtree belongs to this layer.
1767
+ * Usually set to the fiber of the `<Layer name="..." />` component that first claimed this name.
1768
+ */
1769
+ rootFiber: UnsafeKaylaFiber | null;
1770
+ /**
1771
+ * 2D transformation matrix applied to rendering **and** pointer coordinate transformation.
1772
+ * When present, pointer events inside this layer are delivered in local coordinates.
1773
+ */
1774
+ transform?: KMatrix2D | null;
1775
+ }
1776
+ /** Props accepted by the `<Layer>` component */
1777
+ interface KaylaLayerProps extends FCProps {
1778
+ /** Required. Unique name identifying this layer */
1779
+ name: string;
1780
+ /** Layer draw order (higher = later / on top) */
1781
+ z?: number;
1782
+ /** Canvas blend mode for the whole layer */
1783
+ blend?: GlobalCompositeOperation;
1784
+ /** Toggle rendering of the entire layer */
1785
+ visible?: boolean;
1786
+ /** Children rendered inside this layer's context */
1787
+ children?: KaylaElement<any>[];
1788
+ /**
1789
+ * Transformation applied to rendering and input coordinates.
1790
+ * Use `KMatrix2D` to build cameras, zoom views, parallax layers, rotated UI panels, etc.
1791
+ */
1792
+ transform?: KMatrix2D;
1793
+ }
1794
+ /**
1795
+ * Defines a named rendering layer with optional transform, blend mode, visibility, and draw order.
1796
+ *
1797
+ * `<Layer>` is a special component that groups its children into a separate rendering pass.
1798
+ * It allows you to:
1799
+ * - Control draw order between major groups of entities (via `z`)
1800
+ * - Apply coordinate transforms (camera, zoom, scroll, rotation) to an entire subtree
1801
+ * - Change blending behavior for the whole layer
1802
+ * - Toggle visibility of large parts of the scene without unmounting components
1803
+ *
1804
+ * **Coordinate system behavior:**
1805
+ * - If `transform` is provided, **all pointer coordinates** delivered to `usePointer` / `useGlobalPointer`
1806
+ * inside this layer (and its descendants) are automatically transformed into the **layer-local coordinate system**.
1807
+ * - The automatic hit-test for `usePointer` also happens in layer-local space.
1808
+ * - Without `transform` (identity/default), coordinates remain in **world space**.
1809
+ *
1810
+ * **Important notes:**
1811
+ * - Layers are **not** nested in the traditional sense — each `<Layer>` creates an independent rendering group.
1812
+ * - Multiple `<Layer>` components with the **same `name`** will **share the same layer configuration** (last one wins for most properties).
1813
+ * - The `rootFiber` of the layer is set to the fiber of the **first** `<Layer name="..."` component encountered with that name.
1814
+ * - Children inside `<Layer>` still participate in normal component lifecycle, ticking, painting, pointer handling, etc.
1815
+ *
1816
+ * @example
1817
+ * // Simple UI overlay that draws on top
1818
+ * <Layer name="ui" z={1000} visible={true}>
1819
+ * <HUD />
1820
+ * <PauseMenu />
1821
+ * </Layer>
1822
+ *
1823
+ * @example
1824
+ * // Zoomable / scrollable map area
1825
+ * function MapView() {
1826
+ * const zoom = useSelf(() => ({ scale: 1.8, offset: new LEA.Vector2(120, 80) }));
1827
+ *
1828
+ * const matrix = new KMatrix2D();
1829
+ * matrix.translateSelf(zoom.offset.x, zoom.offset.y);
1830
+ * matrix.scaleSelf(zoom.scale, zoom.scale);
1831
+ *
1832
+ * return (
1833
+ * <Layer
1834
+ * name="world-map"
1835
+ * z={10}
1836
+ * transform={matrix}
1837
+ * blend="source-over"
1838
+ * >
1839
+ * <BackgroundTiles />
1840
+ * <Player />
1841
+ * <Enemies />
1842
+ * {// Pointer events inside here receive map-local coordinates }
1843
+ * </Layer>
1844
+ * );
1845
+ * }
1846
+ * */
1847
+ declare const Layer: FC<KaylaLayerProps>;
1848
+ declare class KMatrix2D extends DOMMatrix {
1849
+ #private;
1850
+ constructor();
1851
+ transformV(v: Vector2): Vector2;
1852
+ inverseV(v: Vector2): Vector2;
1853
+ }
1708
1854
 
1709
1855
  type Kayla_ExportsOfFC<T extends FC<any, any>> = ExportsOfFC<T>;
1710
1856
  type Kayla_FC<Props extends FCProps = FCProps, Exports extends FCExports = FCExports> = FC<Props, Exports>;
1711
1857
  type Kayla_FCExports = FCExports;
1712
1858
  type Kayla_FCProps = FCProps;
1713
1859
  declare const Kayla_JSX: typeof JSX;
1860
+ type Kayla_KMatrix2D = KMatrix2D;
1861
+ declare const Kayla_KMatrix2D: typeof KMatrix2D;
1714
1862
  type Kayla_KaylaClickConfig = KaylaClickConfig;
1715
1863
  type Kayla_KaylaClickType = KaylaClickType;
1716
1864
  type Kayla_KaylaContext<Context> = KaylaContext<Context>;
@@ -1724,12 +1872,15 @@ type Kayla_KaylaFiberControl = KaylaFiberControl;
1724
1872
  declare const Kayla_KaylaFragment: typeof KaylaFragment;
1725
1873
  type Kayla_KaylaGame = KaylaGame;
1726
1874
  declare const Kayla_KaylaInternals: typeof KaylaInternals;
1875
+ type Kayla_KaylaLayerInfo = KaylaLayerInfo;
1876
+ type Kayla_KaylaLayerProps = KaylaLayerProps;
1727
1877
  type Kayla_KaylaPointerAction = KaylaPointerAction;
1728
1878
  declare const Kayla_KaylaRect: typeof KaylaRect;
1729
1879
  type Kayla_KaylaRef<T> = KaylaRef<T>;
1730
1880
  type Kayla_KaylaRenderer = KaylaRenderer;
1731
1881
  type Kayla_KaylaScene = KaylaScene;
1732
1882
  type Kayla_KaylaState<T> = KaylaState<T>;
1883
+ declare const Kayla_Layer: typeof Layer;
1733
1884
  type Kayla_PropOfFC<T extends FC<any, any>> = PropOfFC<T>;
1734
1885
  type Kayla_Reassignable<T extends Record<any, any>> = Reassignable<T>;
1735
1886
  type Kayla_UnsafeKaylaFiber = UnsafeKaylaFiber;
@@ -1767,7 +1918,7 @@ declare const Kayla_useState: typeof useState;
1767
1918
  declare const Kayla_useTick: typeof useTick;
1768
1919
  declare const Kayla_useViewportEffect: typeof useViewportEffect;
1769
1920
  declare namespace Kayla {
1770
- export { type Kayla_ExportsOfFC as ExportsOfFC, type Kayla_FC as FC, type Kayla_FCExports as FCExports, type Kayla_FCProps as FCProps, Kayla_JSX as JSX, type Kayla_KaylaClickConfig as KaylaClickConfig, type Kayla_KaylaClickType as KaylaClickType, Kayla_KaylaContext as KaylaContext, type Kayla_KaylaCustomHookConfig as KaylaCustomHookConfig, type Kayla_KaylaElement as KaylaElement, type Kayla_KaylaElementRef as KaylaElementRef, type Kayla_KaylaExportables as KaylaExportables, type Kayla_KaylaExports as KaylaExports, type Kayla_KaylaFiberControl as KaylaFiberControl, Kayla_KaylaFragment as KaylaFragment, type Kayla_KaylaGame as KaylaGame, Kayla_KaylaInternals as KaylaInternals, type Kayla_KaylaPointerAction as KaylaPointerAction, Kayla_KaylaRect as KaylaRect, type Kayla_KaylaRef as KaylaRef, type Kayla_KaylaRenderer as KaylaRenderer, type Kayla_KaylaScene as KaylaScene, type Kayla_KaylaState as KaylaState, type Kayla_PropOfFC as PropOfFC, type Kayla_Reassignable as Reassignable, type Kayla_UnsafeKaylaFiber as UnsafeKaylaFiber, Kayla_createContext as createContext, Kayla_createElement as createElement, Kayla_createGame as createGame, Kayla_createReassignableObject as createReassignableObject, Kayla_createRenderer as createRenderer, Kayla_createScene as createScene, Kayla_createUseHook as createUseHook, selfSym as self, Kayla_setLogLevel as setLogLevel, Kayla_useClick as useClick, Kayla_useContext as useContext, Kayla_useCurrentGame as useCurrentGame, Kayla_useCurrentRenderer as useCurrentRenderer, Kayla_useCurrentScene as useCurrentScene, Kayla_useCurrentTicker as useCurrentTicker, Kayla_useDisposableRef as useDisposableRef, Kayla_useEffect as useEffect, Kayla_useEntity as useEntity, Kayla_useExports as useExports, Kayla_useFiber as useFiber, Kayla_useFiberControl as useFiberControl, Kayla_useGlobalClick as useGlobalClick, Kayla_useGlobalPointer as useGlobalPointer, Kayla_useInitialization as useInitialization, Kayla_useNextStack as useNextStack, Kayla_usePaint as usePaint, Kayla_usePointer as usePointer, Kayla_useRect as useRect, Kayla_useRef as useRef, Kayla_useSelf as useSelf, Kayla_useShouldRefresh as useShouldRefresh, Kayla_useState as useState, Kayla_useTick as useTick, Kayla_useViewportEffect as useViewportEffect };
1921
+ export { type Kayla_ExportsOfFC as ExportsOfFC, type Kayla_FC as FC, type Kayla_FCExports as FCExports, type Kayla_FCProps as FCProps, Kayla_JSX as JSX, Kayla_KMatrix2D as KMatrix2D, type Kayla_KaylaClickConfig as KaylaClickConfig, type Kayla_KaylaClickType as KaylaClickType, Kayla_KaylaContext as KaylaContext, type Kayla_KaylaCustomHookConfig as KaylaCustomHookConfig, type Kayla_KaylaElement as KaylaElement, type Kayla_KaylaElementRef as KaylaElementRef, type Kayla_KaylaExportables as KaylaExportables, type Kayla_KaylaExports as KaylaExports, type Kayla_KaylaFiberControl as KaylaFiberControl, Kayla_KaylaFragment as KaylaFragment, type Kayla_KaylaGame as KaylaGame, Kayla_KaylaInternals as KaylaInternals, type Kayla_KaylaLayerInfo as KaylaLayerInfo, type Kayla_KaylaLayerProps as KaylaLayerProps, type Kayla_KaylaPointerAction as KaylaPointerAction, Kayla_KaylaRect as KaylaRect, type Kayla_KaylaRef as KaylaRef, type Kayla_KaylaRenderer as KaylaRenderer, type Kayla_KaylaScene as KaylaScene, type Kayla_KaylaState as KaylaState, Kayla_Layer as Layer, type Kayla_PropOfFC as PropOfFC, type Kayla_Reassignable as Reassignable, type Kayla_UnsafeKaylaFiber as UnsafeKaylaFiber, Kayla_createContext as createContext, Kayla_createElement as createElement, Kayla_createGame as createGame, Kayla_createReassignableObject as createReassignableObject, Kayla_createRenderer as createRenderer, Kayla_createScene as createScene, Kayla_createUseHook as createUseHook, selfSym as self, Kayla_setLogLevel as setLogLevel, Kayla_useClick as useClick, Kayla_useContext as useContext, Kayla_useCurrentGame as useCurrentGame, Kayla_useCurrentRenderer as useCurrentRenderer, Kayla_useCurrentScene as useCurrentScene, Kayla_useCurrentTicker as useCurrentTicker, Kayla_useDisposableRef as useDisposableRef, Kayla_useEffect as useEffect, Kayla_useEntity as useEntity, Kayla_useExports as useExports, Kayla_useFiber as useFiber, Kayla_useFiberControl as useFiberControl, Kayla_useGlobalClick as useGlobalClick, Kayla_useGlobalPointer as useGlobalPointer, Kayla_useInitialization as useInitialization, Kayla_useNextStack as useNextStack, Kayla_usePaint as usePaint, Kayla_usePointer as usePointer, Kayla_useRect as useRect, Kayla_useRef as useRef, Kayla_useSelf as useSelf, Kayla_useShouldRefresh as useShouldRefresh, Kayla_useState as useState, Kayla_useTick as useTick, Kayla_useViewportEffect as useViewportEffect };
1771
1922
  }
1772
1923
 
1773
- export { usePointer as $, createUseHook as A, selfSym as B, setLogLevel as C, useClick as D, type ExportsOfFC as E, type FCProps as F, useContext as G, useCurrentGame as H, useCurrentRenderer as I, JSX as J, Kayla as K, useCurrentScene as L, useCurrentTicker as M, useDisposableRef as N, useEffect as O, type PropOfFC as P, useEntity as Q, type Reassignable as R, useExports as S, useFiber as T, type UnsafeKaylaFiber as U, useFiberControl as V, useGlobalClick as W, useGlobalPointer as X, useInitialization as Y, useNextStack as Z, usePaint as _, type FCExports as a, useRect as a0, useRef as a1, useSelf as a2, useShouldRefresh as a3, useState as a4, useTick as a5, useViewportEffect as a6, type FC as b, type KaylaClickConfig as c, type KaylaClickType as d, KaylaContext as e, type KaylaCustomHookConfig as f, type KaylaElement as g, type KaylaElementRef as h, type KaylaExportables as i, type KaylaExports as j, type KaylaFiberControl as k, KaylaFragment as l, KaylaGame as m, KaylaInternals as n, type KaylaPointerAction as o, KaylaRect as p, type KaylaRef as q, KaylaRenderer as r, KaylaScene as s, type KaylaState as t, createContext as u, createElement as v, createGame as w, createReassignableObject as x, createRenderer as y, createScene as z };
1924
+ export { useGlobalPointer as $, createReassignableObject as A, createRenderer as B, createScene as C, createUseHook as D, type ExportsOfFC as E, type FCProps as F, selfSym as G, setLogLevel as H, useClick as I, JSX as J, Kayla as K, Layer as L, useContext as M, useCurrentGame as N, useCurrentRenderer as O, type PropOfFC as P, useCurrentScene as Q, type Reassignable as R, useCurrentTicker as S, useDisposableRef as T, type UnsafeKaylaFiber as U, useEffect as V, useEntity as W, useExports as X, useFiber as Y, useFiberControl as Z, useGlobalClick as _, type FCExports as a, useInitialization as a0, useNextStack as a1, usePaint as a2, usePointer as a3, useRect as a4, useRef as a5, useSelf as a6, useShouldRefresh as a7, useState as a8, useTick as a9, useViewportEffect as aa, type FC as b, KMatrix2D as c, type KaylaClickConfig as d, type KaylaClickType as e, KaylaContext as f, type KaylaCustomHookConfig as g, type KaylaElement as h, type KaylaElementRef as i, type KaylaExportables as j, type KaylaExports as k, type KaylaFiberControl as l, KaylaFragment as m, KaylaGame as n, KaylaInternals as o, type KaylaLayerInfo as p, type KaylaLayerProps as q, type KaylaPointerAction as r, KaylaRect as s, type KaylaRef as t, KaylaRenderer as u, KaylaScene as v, type KaylaState as w, createContext as x, createElement as y, createGame as z };