# ui/uiwindow (vanilla.js 0.4.0)

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

A single window in the window stack. (UIWindowLayer opens and removes it)

## UIWindow extends WorldNode

A single window in the window stack. (UIWindowLayer opens and removes it)
- Dims the whole area (presses do not pass through to what is behind) and shows a panel in the center. The dim also covers the safe area (setViewBleed).
- The panel widens to fit the area's width but does not exceed maximumWidth. Its height is the value returned by layoutContent, and if it is larger than the area the whole panel is scaled down.
- The open and close motion is PopupMotion. When the close motion finishes, the window layer detaches it. (it does not receive presses in the meantime)
- Pressing outside (the dim) closes it. Turned off per window with setOutsideCloseEnabled. The window layer's closeTopWindow (back) also follows this value.
- Node structure: window (area size) ├ dim (dimNode, Paint + UIButton) └ frame (frameNode, panel center pivot, motion scale) └ panel (panelNode, a single Paint, content goes here)
- The panel look (decorations such as shadow, border, gloss) is up to the game. The game inherits this class, attaches decoration nodes to the frame in the constructor
  (setSiblingIndex(0) for things placed behind the panel), and overrides layoutPanel to fit them to the panel size. The default panel Paint is changed or turned off via getPanelPaint().
  For the motion shape (a panel rising from below, etc.), override applyMotion.
- The derived class places the content inside the panel in layoutContent(panelWidth) and returns the panel's height. (default is the setPanelHeight value)
- Usage:
    const window = new UIWindow("settings");
    window.getPanelNode().addChild(contentNode);
    windowLayer.openWindow(window);
    window.close();

Example: Showing a window subclass that lays out its content

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

class NoticeWindow extends UIWindow {
	#messageNode;

	constructor(name, messageText) {
		super(name);
		const messageNode = new WorldNode();
		messageNode.setPivot(Pivot.topLeft.clone());
		messageNode.setAnchor(Pivot.topLeft.clone());
		const messageLabel = messageNode.addComponent(UILabel);
		messageLabel.setText(messageText);
		messageLabel.setFontSize(18);
		const panelNode = this.getPanelNode();
		panelNode.addChild(messageNode);
		this.#messageNode = messageNode;
	}

	getMessageNode() {
		return this.#messageNode;
	}

	layoutContent(panelWidth) {
		const messageNode = this.getMessageNode();
		messageNode.setLocalPosition(Vector2.create(24, 24));
		messageNode.setContentSize(Vector2.create(panelWidth - 48, 64));
		return 112;
	}
}

class WindowScene 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);
		const noticeWindow = new NoticeWindow("notice", "New items arrived in the shop.");
		noticeWindow.setMaximumWidth(360);
		noticeWindow.setOutsideCloseEnabled(false);
		windowLayer.openWindow(noticeWindow);
	}
}

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

- `constructor(name = "UIWindow")` (constructor, since 0.4.0): Constructor.
  - `name` string: Window name. (found with UIWindowLayer.findWindow)
- `getPanelNode()` (method, since 0.4.0): Returns the panel node. (attach content here)
  - returns WorldNode: Panel node.
- `getPanelPaint()` (method, since 0.4.0): Returns the panel's Paint. (change its color or roundness, or turn it off with setEnable(false) and use the game's panel)
  - returns Paint: The panel's Paint.
- `getFrameNode()` (method, since 0.4.0): Returns the frame node. (where decorations that grow and shrink with the panel are attached)
  - returns WorldNode: Frame node. (pivot at the panel center)
- `getDimNode()` (method, since 0.4.0): Returns the dim node.
  - returns WorldNode: Dim node.
- `getDimPaint()` (method, since 0.4.0): Returns the dim's Paint. (to change its color)
  - returns Paint: The dim's Paint.
- `setDimColor(dimColor)` (method, since 0.4.0): Sets the dim color. (changes the dim's Paint color)
  - `dimColor` Color | string: Dim color. (default black with alpha 0.55, the motion multiplies the alpha by 0 ~ 1)
- `setPanelColor(panelColor)` (method, since 0.4.0): Sets the panel color. (changes the panel's Paint color)
  - `panelColor` Color | string: Panel color.
- `getPopupMotion()` (method, since 0.4.0): Returns the motion. (setInstantEnabled opens and closes without motion)
  - returns PopupMotion: This window's motion.
- `setOutsideCloseEnabled(isOutsideCloseEnabled)` (method, since 0.4.0): Sets whether pressing outside (and the window layer's closeTopWindow) closes it. (default true)
  - `isOutsideCloseEnabled` boolean: If true, it closes by pressing outside or by closeTopWindow.
- `isOutsideCloseEnabled()` (method, since 0.4.0): Whether pressing outside closes it.
  - returns boolean: True if pressing outside closes it.
- `setCloseEvent(closeEvent)` (method, since 0.4.0): Sets the notification for when it is removed from the window layer. (close button, pressing outside, closeAllWindows, all of them)
  - `closeEvent` Function | null: () => void
- `setMaximumWidth(maximumWidth)` (method, since 0.4.0): Sets the window's width limit. (narrower than that if the area is narrow)
  - `maximumWidth` number: Panel width limit. (UI layer units, 1 if less than 1, default 480)
- `getMaximumWidth()` (method, since 0.4.0): Returns the window's width limit.
  - returns number: Panel width limit. (UI layer units)
- `setMargin(margin)` (method, since 0.4.0): Sets the distance between the area edge and the panel.
  - `margin` number: Distance between the area edge and the panel. (UI layer units, 0 if negative, default 16)
- `getMargin()` (method, since 0.4.0): Returns the distance between the area edge and the panel.
  - returns number: Distance between the area edge and the panel. (UI layer units)
- `setPanelHeight(panelHeight)` (method, since 0.4.0): Sets the panel's height. (used by windows that do not override layoutContent)
  - `panelHeight` number: Panel height. (UI layer units, 1 if less than 1, default 240)
- `getPanelHeight()` (method, since 0.4.0): Returns the panel's height.
  - returns number: Panel height set with setPanelHeight. (UI layer units)
- `getViewWidth()` (method, since 0.4.0): Width of the last received area. (UI layer units)
  - returns number: Area width. (UI layer units, 1 before it is received)
- `getViewHeight()` (method, since 0.4.0): Height of the last received area. (UI layer units, used by windows whose content fills the height)
  - returns number: Area height. (UI layer units, 1 before it is received)
- `getViewBleed()` (method, since 0.4.0): Returns the extra width covered outside the area. { top, right, bottom, left }
  - returns object: Extra cover widths. ({ top, right, bottom, left }, UI layer units, the original, not a copy)
- `getPanelRect()` (method, since 0.4.0): Rect of the last placed panel. (window coordinates, before motion, size multiplied by the shrink scale)
  - returns Rect: Panel position. (window coordinates)
- `getPanelScale()` (method, since 0.4.0): Scale by which the panel was shrunk because it was larger than the area. (1 or less)
  - returns number: Shrink scale. (greater than 0, at most 1)
- `getWindowLayer()` (method, since 0.4.0): Returns the window layer it is opened in. (null before opening or after removal)
  - returns *: The UIWindowLayer showing it. (null if none)
- `setWindowLayer(windowLayer)` (method, since 0.4.0): Sets the window layer it is opened in. (called by the window layer)
  - `windowLayer` *: The UIWindowLayer showing it. (null when taken down)
- `close()` (method, since 0.4.0): Closes. (removes it from the window layer, detaches it when the close motion finishes, does nothing if not opened)
- `handleClosed()` (method, since 0.4.0): Removed from the window layer. (called by the window layer, derived classes that override this call super)
- `setViewBleed(top, right, bottom, left)` (method, since 0.4.0): Notifies the extra width covered outside the area. (safe area, UI layer units, the dim covers underneath it too)
  - `top` number: Extra width to cover above. (UI layer units, 0 if negative)
  - `right` number: Extra width to cover on the right. (UI layer units, 0 if negative)
  - `bottom` number: Extra width to cover below. (UI layer units, 0 if negative)
  - `left` number: Extra width to cover on the left. (UI layer units, 0 if negative)
- `placeDimNode()` (method, since 0.4.0): Places the dim. (the area and the safe area beyond it)
- `setViewSize(viewWidth, viewHeight)` (method, since 0.4.0): Notifies the area size. (covers it with the dim, sets the panel width, lays out the content, then centers it)
  - `viewWidth` number: UI layer units.
  - `viewHeight` number: Area height. (UI layer units, 1 if less than 1)
- `refreshLayout()` (method, since 0.4.0): Lays out again. (when the content changes and the panel height differs, with the last area size)
- `layoutContent(panelWidth)` (method, since 0.4.0): Lays out the content. (derived classes override this to place content inside the panel and return the panel's height)
  - `panelWidth` number: Panel width. (UI layer units, the smaller of the area width minus margins and the width limit)
  - returns number: Panel height. (UI layer units, getPanelHeight by default)
- `layoutPanel(panelWidth, panelHeight)` (method, since 0.4.0): Lays out the panel. (sets the panel size, derived classes with decorations override this, call super, then place the decorations)
  - `panelWidth` number: Panel width. (UI layer units, before shrinking)
  - `panelHeight` number: Panel height. (UI layer units, before shrinking)
- `applyMotion()` (method, since 0.4.0): Applies the motion to the frame and the dim. (frame scale = shrink scale x motion scale, dim strength = motion ratio, derived classes override this to change the look)
- `startOpenMotion()` (method, since 0.4.0): Starts the open motion. (called by the window layer when opening)
- `startCloseMotion()` (method, since 0.4.0): Starts the close motion. (called by the window layer when removing, it does not receive presses in the meantime)
- `isCloseMotionFinished()` (method, since 0.4.0): Whether the close motion has finished. (the window layer checks when to detach)
  - returns boolean: True if the motion is in the closed state.
- `tick(timeDelta)` (method, since 0.4.0): Every frame. (motion after children)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `setCovered(isCovered)` (method, since 0.4.0): Notifies whether it is covered by another window. (only the top window is visible, by default it hides when covered and shows again when uncovered)
  - If something is attached outside the canvas, such as DOM input, derived classes override this to put it away as well.
  - `isCovered` boolean: If true, it is covered by another window.
- `handleWheel(viewInputPosition, wheelDelta)` (method, since 0.4.0): Mouse wheel. (if it is the top window the window layer gives it first, true if handled, overridden by derived classes)
  - `viewInputPosition` Vector2: Input position. (view coordinates)
  - `wheelDelta` Vector2: Accumulated wheel delta.
  - returns boolean: True if handled. (always false by default)
- `dispose()` (method, since 0.4.0): On detach. (cleans up things attached outside the engine such as DOM input, overridden by derived classes)
