# misc/focusnavigator (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/focusnavigator/

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.

## FocusNavigatorOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

Navigator settings. (original options = { cross_axis_penalty = 2.5 })

Example: Lower the penalty so diagonal items are picked more easily

```rust
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")));
```

- `pub cross_axis_penalty: f64` (field, since 0.3.2): Penalty multiplied by the distance off the movement axis.
  - returns f64

## FocusNavigator (struct) (Clone, Debug, Default)

Directional focus navigator.

Example: Move between 2x2 buttons with the arrows

```rust
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"));
```

- `pub fn new(options: FocusNavigatorOptions) -> FocusNavigator` (constructor, since 0.3.2): Constructor.
  - `options` FocusNavigatorOptions: Sets the axis penalty. (FocusNavigatorOptions::default() is 2.5)
  - returns FocusNavigator: A navigator with no items and no focus.
- `pub fn add_item(&mut self, id: &str, rect: Rect)` (method, since 0.3.2): Registers an item.
  - Adding the same id again does not replace it but adds another one.
  - `id` &str: Item name. (the value returned by move_focus and get_focused_id)
  - `rect` Rect: Item area. (distances are measured between center points, the caller decides the coordinate system)
- `pub fn clear(&mut self)` (method, since 0.3.2): Removes all items. (also clears the focus)
- `pub fn set_focused_id(&mut self, id: Option<&str>)` (method, since 0.3.2): Sets the focus.
  - `id` Option<&str>: 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)
- `pub fn get_focused_id(&self) -> Option<&str>` (method, since 0.3.2): Returns the current focus.
  - returns Option<&str>: Name of the focused item. None if none.
- `pub fn move_focus(&mut self, direction_x: f64, direction_y: f64) -> Option<String>` (method, since 0.3.2): 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_x` f64: Horizontal direction. (-1 left, 0, 1 right)
  - `direction_y` f64: Vertical direction. (-1 up, 0, 1 down)
  - returns Option<String>: Name of the newly focused item. None if there are no items or nowhere to move.
