# ui/uiwindowlayer (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uiwindowlayer/

Window layer. (stacks UIWindow over the area)

## UIWindowLayer extends WorldNode

Window layer. (stacks UIWindow over the area)
- Opening a window stacks it on top, and only the top window is visible. When the upper window closes, the window below is visible again.
- Closing removes it from the list immediately so presses and covering are released at once, and it is detached after the close motion (PopupMotion) finishes.
  Closing a window that was covered detaches it immediately without motion.
- This layer is enlarged by the UI scale (UIScale.read), and windows are notified of the area size and safe area width divided by that amount. (so text is readable even on small screens)
  With setUIScaleOptions(null) it does not enlarge.
- While even one window is open, play is suspended. It notifies when windows open and close (setWindowChangeEvent), so the game pauses and resumes play.
- The top window receives the wheel first (UIWindow.handleWheel), and if it does not handle it, the scroll view at that position. GameScene passes wheel input pointing inside the window layer to scrollWindowAt.
- Usage: (place it as the topmost child of the area node, and notify it whenever the area size changes)
    const windowLayer = new UIWindowLayer();
    playAreaNode.addChild(windowLayer);
    windowLayer.setViewSize(playAreaWidth, playAreaHeight);
    windowLayer.setViewBleed(insets.top, insets.right, insets.bottom, insets.left);
    windowLayer.setWindowChangeEvent(() => { game.setPaused(windowLayer.hasOpenWindow()); });
    windowLayer.openWindow(new UIConfirmWindow("quit", { title: "Quit", message: "Do you really want to quit?" }));

Example: Stacking two windows and closing the top one

```js
import { Engine, EngineConfiguration, UIScene, UIWindow, UIWindowLayer } from "./build/vanilla.js";

class WindowLayerScene extends UIScene {
	initialize(engine) {
		super.initialize(engine);
		const root = this.getRoot();
		const rootSize = root.getContentSize();
		const windowLayer = new UIWindowLayer();
		root.addChild(windowLayer);
		windowLayer.setViewSize(rootSize.x, rootSize.y);
		windowLayer.setWindowChangeEvent(() => {
			const windowCount = windowLayer.getWindowCount();
			console.log(windowCount);
		});

		const settingsWindow = new UIWindow("settings");
		settingsWindow.setPanelHeight(220);
		windowLayer.openWindow(settingsWindow);
		const helpWindow = new UIWindow("help");
		helpWindow.setPanelHeight(140);
		helpWindow.setMaximumWidth(300);
		windowLayer.openWindow(helpWindow);

		const isClosed = windowLayer.closeTopWindow();
		const topWindow = windowLayer.getTopWindow();
		const topWindowName = topWindow.getName();
		console.log(isClosed, topWindowName);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new WindowLayerScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.4.0): Constructor.
- `setWindowChangeEvent(windowChangeEvent)` (method, since 0.4.0): Sets the notification for when a window opens or closes.
  - `windowChangeEvent` Function | null: () => void
- `setUIScaleOptions(uiScaleOptions)` (method, since 0.4.0): Sets the UI scale. (options of UIScale.read, null means no enlargement, default is {} for the UIScale defaults)
  - `uiScaleOptions` object | null: Options to pass to UIScale.read. (scale 1 if null, re-lays out right away)
- `getUIScale()` (method, since 0.4.0): Current UI scale.
  - returns number: UI scale applied to this layer. (re-read on setViewSize)
- `hasOpenWindow()` (method, since 0.4.0): Whether at least one window is open. (windows in the close motion are not counted)
  - returns boolean: True if any window is open.
- `getWindowCount()` (method, since 0.4.0): Number of open windows.
  - returns number: Number of open windows. (excluding windows in their closing motion)
- `getTopWindow()` (method, since 0.4.0): Top window. (null if none)
  - returns UIWindow | null: Top window. (null if none)
- `findWindow(windowName)` (method, since 0.4.0): Window with that name. (from the top among the open ones, null if none)
  - `windowName` string: Window name. (the name given to the UIWindow constructor)
  - returns UIWindow | null: The found window. (null if none)
- `setViewSize(viewWidth, viewHeight)` (method, since 0.4.0): Notifies the area size. (rereads the UI scale and lays out all open windows again)
  - `viewWidth` number: Parent units.
  - `viewHeight` number: Area height. (parent units, 1 if less than 1)
- `setViewBleed(top, right, bottom, left)` (method, since 0.4.0): Notifies the extra width covered outside the area. (safe area, parent units)
  - `top` number: Extra width to cover above. (parent units, 0 if negative)
  - `right` number: Extra width to cover on the right. (parent units, 0 if negative)
  - `bottom` number: Extra width to cover below. (parent units, 0 if negative)
  - `left` number: Extra width to cover on the left. (parent units, 0 if negative)
- `placeAllWindows()` (method, since 0.4.0): Lays out all open windows and closing windows again.
- `placeWindow(window)` (method, since 0.4.0): Lays out one window. (notifies the area size divided by the UI scale and the covering width)
  - `window` UIWindow: The window to place.
- `openWindow(window)` (method, since 0.4.0): Opens a window. (stacks it on top, brings it to the top if already open)
  - `window` UIWindow: The window to show. (reopened if it was in its closing motion)
- `closeWindow(window)` (method, since 0.4.0): Closes a window. (removes it from the list and starts the close motion, does nothing if not open)
  - `window` UIWindow: The window to close.
- `closeAllWindows()` (method, since 0.4.0): Closes all windows. (from the top)
- `closeTopWindow()` (method, since 0.4.0): Closes the top window. (for the device's back button, does not close a window with outside close turned off, true if closed)
  - returns boolean: True if closed. (false if there is no window or the window has outside close turned off)
- `removeWindowNode(window)` (method, since 0.4.0): Detaches a window node.
  - `window` UIWindow: The window to detach. (sets its window layer to null, calls dispose and removes it from the children)
- `refreshWindowCovers()` (method, since 0.4.0): Covers windows. (only the top window is visible, the windows below are hidden)
- `notifyWindowChange()` (method, since 0.4.0): Notifies that a window opened or closed.
- `tick(timeDelta)` (method, since 0.4.0): Every frame. (after the windows move, detaches windows whose close motion has finished)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `scrollWindowAt(viewInputPosition, wheelDelta)` (method, since 0.4.0): Passes the mouse wheel to the top window. (if the window does not handle it, the scroll view at that position, true if handled)
  - `viewInputPosition` Vector2: Input position. (view coordinates)
  - `wheelDelta` Vector2: Accumulated wheel delta.
  - returns boolean: True if the window or a scroll view handled it. (false if there is no window)
- `static findScrollViewAt(node, viewInputPosition)` (static, since 0.4.0): Finds the frontmost (drawn last) scroll view covering that position. (enabled nodes only, null if none)
  - To avoid importing UIScrollView, like TouchRaycaster it treats a component that has wheel and getScrollContentSize as one.
  - `node` WorldNode: The node to start searching from. (children are checked first, from the last)
  - `viewInputPosition` Vector2: Input position. (view coordinates)
  - returns *: An enabled component that has wheel and getScrollContentSize. (null if none)
- `static findWindowLayerOf(node)` (static, since 0.4.0): Finds the window layer containing the node. (from itself toward the parents, null if none)
  - `node` *: The node to start searching from.
  - returns UIWindowLayer | null: The closest window layer. (can be the node itself, null if none)
