vanilladocs
vanillapunch.com
API / misc/focusnavigator

misc/focusnavigator

방향 포커스 내비게이터.

Directional focus navigator.

FocusNavigatorextends Object

짝Counterpart: vanilla-engine FocusNavigator

방향 포커스 내비게이터.

  • 게임패드 십자키 / 키보드 화살표로 UI 항목 사이를 옮겨 다닐 때, "이동 방향 앞쪽에 있으면서 축에서 벗어난 정도에 벌점을 준 거리" 가 가장 가까운 항목으로 포커스를 옮긴다. (콘솔 UI 내비게이션의 표준 방식)
  • 항목은 { id, rect } 로 등록한다. 렌더 방식과 무관한 순수 좌표 알고리즘이다.

Directional focus navigator.

  • When moving between UI items with the gamepad D-pad / keyboard arrows, moves focus to the item with the smallest "distance among items ahead in the move direction, penalized by how far they deviate from the axis". (the standard approach for console UI navigation)
  • Items are registered as { id, rect }. A pure coordinate algorithm independent of the rendering method.
예제Example메뉴 항목 사이를 방향으로 옮겨 다니기Move between menu items by direction
import { FocusNavigator, Rect } from "./build/vanilla.js";

const focusNavigator = new FocusNavigator({ crossAxisPenalty: 2.5 });
focusNavigator.addItem("start", Rect.create(100, 100, 200, 60));
focusNavigator.addItem("options", Rect.create(100, 180, 200, 60));
focusNavigator.addItem("credits", Rect.create(320, 180, 200, 60));
console.log(focusNavigator.moveFocus(0, 1));
console.log(focusNavigator.moveFocus(0, 1));
console.log(focusNavigator.moveFocus(1, 0));
console.log(focusNavigator.moveFocus(1, 0));
console.log(focusNavigator.getFocusedId());
생성자constructorconstructor(options = {})#0.3.0 부터since 0.3.0소스source

생성.

Constructor.

optionsobject
{ crossAxisPenalty = 2.5 }

짝Counterpart: vanilla-engine FocusNavigator.new

메서드methodaddItem(id, rect)#0.3.0 부터since 0.3.0소스source

항목 등록.

Registers an item.

idstring
항목 식별자.Item identifier.
rectRect
항목의 사각형. (가운데끼리 거리를 잰다)Item rectangle. (distances are measured between centers)

짝Counterpart: vanilla-engine FocusNavigator.add_item

메서드methodclear()#0.3.0 부터since 0.3.0소스source

항목 전부 제거.

Removes all items.

짝Counterpart: vanilla-engine FocusNavigator.clear

메서드methodsetFocusedId(id)#0.3.0 부터since 0.3.0소스source

포커스 지정.

Sets the focus.

idstring | null
포커스할 항목 식별자. (null 이면 포커스 없음)Identifier of the item to focus. (no focus if null)

짝Counterpart: vanilla-engine FocusNavigator.set_focused_id

메서드methodgetFocusedId()#0.3.0 부터since 0.3.0소스source

현재 포커스 반환.

Returns the current focus.

반환Returnsstring | null 지금 포커스된 항목 식별자. (없으면 null)Identifier of the currently focused item. (null if none)

짝Counterpart: vanilla-engine FocusNavigator.get_focused_id

메서드methodmoveFocus(directionX, directionY)#0.3.0 부터since 0.3.0소스source

방향 이동.

  • directionX / directionY 는 -1, 0, 1. (예: 오른쪽 = (1, 0))
  • 포커스가 없으면 방향과 무관하게 가장 왼쪽 위 항목을 고른다.
  • 이동할 곳이 없으면 포커스를 그대로 두고 null 을 반환한다.

Moves in a direction.

  • directionX / directionY are -1, 0, 1. (e.g. right = (1, 0))
  • If nothing is focused, picks the top-left item regardless of direction.
  • If there is nowhere to move, keeps the focus and returns null.
directionXnumber
가로 방향. (-1, 0, 1)Horizontal direction. (-1, 0, 1)
directionYnumber
세로 방향. (-1, 0, 1)Vertical direction. (-1, 0, 1)

반환Returnsstring | null 새로 포커스된 항목 id.id of the newly focused item.

짝Counterpart: vanilla-engine FocusNavigator.move_focus