API / misc/focusnavigator
misc/focusnavigator
방향 포커스 내비게이터.
Directional focus navigator.
FocusNavigatorextends Object
방향 포커스 내비게이터.
- 게임패드 십자키 / 키보드 화살표로 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());