API / misc/focusnavigator
misc/focusnavigator
방향 포커스 내비게이터.
- 원본 misc/focusnavigator.js 를 옮긴 것이다. 게임패드 십자키 / 키보드 화살표로 UI 항목 사이를 옮겨 다닐 때, "이동 방향 앞쪽에 있으면서 축에서 벗어난 만큼 벌점을 준 거리" 가 가장 가까운 항목으로 포커스를 옮긴다. (콘솔 UI 내비게이션의 표준 방식)
- 항목은 (id, rect) 로 등록한다. 그리는 방식과 상관없는 순수 좌표 알고리즘이다.
- 원본과 다른 점: id 는 String, null 은 None 이다. options 객체는 FocusNavigatorOptions 구조체다.
Directional focus navigator.
- A port of the original misc/focusnavigator.js. When moving between UI items with the gamepad d-pad / keyboard arrows, moves the focus to the item with the closest "distance ahead in the movement direction, penalized by how far it strays off the axis". (the standard approach for console UI navigation)
- Items are registered as (id, rect). A pure coordinate algorithm independent of how things are drawn.
- Differences from the original: id is String, and null is None. The options object is the FocusNavigatorOptions struct.
FocusNavigatorOptionsstruct traits Clone, Copy, Debug, PartialEq, Default
내비게이터 설정. (원본 options = { cross_axis_penalty = 2.5 })
Navigator settings. (original options = { cross_axis_penalty = 2.5 })
예제Example벌점을 낮춰 대각선 항목도 잘 고르게 하기Lower the penalty so diagonal items are picked more easily
use vanilla::{FocusNavigator, FocusNavigatorOptions, Rect};
let options = FocusNavigatorOptions {
cross_axis_penalty: 0.5,
};
let mut focus_navigator = FocusNavigator::new(options);
focus_navigator.add_item("start", Rect::create(0.0, 0.0, 40.0, 40.0));
focus_navigator.add_item("diagonal", Rect::create(100.0, 60.0, 40.0, 40.0));
focus_navigator.add_item("far", Rect::create(200.0, 0.0, 40.0, 40.0));
focus_navigator.set_focused_id(Some("start"));
assert_eq!(focus_navigator.move_focus(1.0, 0.0), Some(String::from("diagonal")));FocusNavigatorstruct traits Clone, Debug, Default
방향 포커스 내비게이터.
Directional focus navigator.
예제Example2x2 단추 사이를 화살표로 옮겨 다니기Move between 2x2 buttons with the arrows
use vanilla::{FocusNavigator, Rect};
let mut focus_navigator = FocusNavigator::default();
focus_navigator.add_item("play", Rect::create(0.0, 0.0, 80.0, 40.0));
focus_navigator.add_item("options", Rect::create(200.0, 0.0, 80.0, 40.0));
focus_navigator.add_item("credits", Rect::create(0.0, 100.0, 80.0, 40.0));
focus_navigator.add_item("quit", Rect::create(200.0, 100.0, 80.0, 40.0));
assert_eq!(focus_navigator.move_focus(0.0, 1.0), Some(String::from("play")));
assert_eq!(focus_navigator.move_focus(1.0, 0.0), Some(String::from("options")));
assert_eq!(focus_navigator.move_focus(0.0, 1.0), Some(String::from("quit")));
assert_eq!(focus_navigator.move_focus(1.0, 0.0), None);
assert_eq!(focus_navigator.get_focused_id(), Some("quit"));