vanilladocs
vanillapunch.com
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

소스source

내비게이터 설정. (원본 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")));
필드fieldpub cross_axis_penalty: f64#0.3.2 부터since 0.3.2소스source

이동 축에서 벗어난 거리에 곱하는 벌점.

Penalty multiplied by the distance off the movement axis.

반환Returnsf64

FocusNavigatorstruct traits Clone, Debug, Default

짝Counterpart: vanilla.js FocusNavigator소스source

방향 포커스 내비게이터.

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"));
생성자constructorpub fn new(options: FocusNavigatorOptions) -> FocusNavigator#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

optionsFocusNavigatorOptions
축 벌점 설정. (FocusNavigatorOptions::default() 는 2.5)Sets the axis penalty. (FocusNavigatorOptions::default() is 2.5)

반환ReturnsFocusNavigator 항목과 포커스가 없는 내비게이터.A navigator with no items and no focus.

짝Counterpart: vanilla.js FocusNavigator.constructor

메서드methodpub fn add_item(&mut self, id: &str, rect: Rect)#0.3.2 부터since 0.3.2소스source

항목 등록.

  • 같은 id 를 다시 넣어도 바꾸지 않고 하나 더 넣는다.

Registers an item.

  • Adding the same id again does not replace it but adds another one.
id&str
항목 이름. (move_focus 와 get_focused_id 가 돌려주는 값)Item name. (the value returned by move_focus and get_focused_id)
rectRect
항목 영역. (가운데 점끼리 거리를 잰다, 좌표계는 부르는 쪽이 정한다)Item area. (distances are measured between center points, the caller decides the coordinate system)

짝Counterpart: vanilla.js FocusNavigator.addItem

메서드methodpub fn clear(&mut self)#0.3.2 부터since 0.3.2소스source

항목 모두 빼기. (포커스도 비운다)

Removes all items. (also clears the focus)

짝Counterpart: vanilla.js FocusNavigator.clear

메서드methodpub fn set_focused_id(&mut self, id: Option<&str>)#0.3.2 부터since 0.3.2소스source

포커스 정하기.

Sets the focus.

idOption<&str>
포커스할 항목 이름. None 이면 비운다. (등록하지 않은 이름도 그대로 둔다, 다음 move_focus 가 첫 항목으로 바꾼다)Name of the item to focus. None clears it. (an unregistered name is kept as is, and the next move_focus changes it to the first item)

짝Counterpart: vanilla.js FocusNavigator.setFocusedId

메서드methodpub fn get_focused_id(&self) -> Option<&str>#0.3.2 부터since 0.3.2소스source

지금 포커스 반환.

Returns the current focus.

반환ReturnsOption<&str> 포커스한 항목 이름. 없으면 None.Name of the focused item. None if none.

짝Counterpart: vanilla.js FocusNavigator.getFocusedId

메서드methodpub fn move_focus(&mut self, direction_x: f64, direction_y: f64) -> Option<String>#0.3.2 부터since 0.3.2소스source

방향 이동. (direction_x / direction_y 는 -1, 0, 1. 예: 오른쪽 = (1, 0))

  • 포커스가 없으면 방향과 상관없이 가장 왼쪽 위 항목을 고른다.
  • 포커스한 항목이 목록에 없으면 첫 항목을 고른다.
  • 옮길 곳이 없으면 포커스를 그대로 두고 None 을 돌려준다.
  • 후보는 이동 방향 앞쪽(앞으로 간 거리가 0 보다 큰) 항목이고, 점수는 앞으로 간 거리 + 축에서 벗어난 거리 * 벌점이다. (가운데 점끼리)

Moves in a direction. (direction_x / direction_y are -1, 0, 1. e.g. right = (1, 0))

  • With no focus, picks the top left item regardless of direction.
  • If the focused item is not in the list, picks the first item.
  • If there is nowhere to move, keeps the focus and returns None.
  • Candidates are items ahead in the movement direction (forward distance greater than 0), scored as forward distance + distance off the axis * penalty. (between center points)
direction_xf64
가로 방향. (-1 왼쪽, 0, 1 오른쪽)Horizontal direction. (-1 left, 0, 1 right)
direction_yf64
세로 방향. (-1 위, 0, 1 아래)Vertical direction. (-1 up, 0, 1 down)

반환ReturnsOption<String> 새로 포커스한 항목 이름. 항목이 하나도 없거나 옮길 곳이 없으면 None.Name of the newly focused item. None if there are no items or nowhere to move.

짝Counterpart: vanilla.js FocusNavigator.moveFocus