# Cross Input Support UGUI

## 🎮 CrossInputSupportUGUI Class

The `CrossInputSupportUGUI` class provides keyboard and controller navigation support for Inventool's UI when set up properly with the `PlayerInput`, `SelectorUGUI` and `TargetSelectable` components.

> **Namespace:** `Esper.Inventool.Input`\
> **Access:** `public`\
> **Type:** `class`\
> **Inherits:** `MonoBehaviour`

***

### 📦 Fields

| Access      | Field                  | Type                                                          | Description                                                                |
| ----------- | ---------------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------- |
| `public`    | `autoHideSelector`     | `bool`                                                        | If the selector should automatically be hidden after confirming an action. |
| `public`    | `playerMap`            | `string`                                                      | The player input map name.                                                 |
| `public`    | `menuMap`              | `string`                                                      | The menu input map name.                                                   |
| `public`    | `scrollingSensitivity` | `float`                                                       | The scrolling sensitivity used when locked on a scroller.                  |
| `public`    | `onTab`                | `UnityEvent`                                                  | Callback for when the tab button is pressed.                               |
| `public`    | `onCancel`             | `UnityEvent`                                                  | Callback for when the cancel button is pressed.                            |
| `public`    | `onConfirm`            | `UnityEvent`                                                  | Callback for when the confirm button is pressed.                           |
| `public`    | `onMove`               | `UnityEvent`                                                  | Callback for when a move button is pressed.                                |
| `public`    | `top`                  | `CrossInputSupportedWindowUGUI`                               | The window currently over all others. *(Hidden in inspector)*              |
| `public`    | `selectables`          | `List<TargetSelectable>`                                      | A list of selectables of the top window. *(Hidden in inspector)*           |
| `public`    | `selection`            | `TargetSelectable`                                            | The currently targeted selectable. *(Hidden in inspector)*                 |
| `public`    | `initialSelectable`    | `TargetSelectable`                                            | The initial selectable. *(Hidden in inspector)*                            |
| `public`    | `windows`              | `List<CrossInputSupportedWindowUGUI>`                         | A list of all registered windows in the scene. *(Hidden in inspector)*     |
| `public`    | `selector`             | `SelectorUGUI`                                                | The selector. *(Hidden in inspector)*                                      |
| `public`    | `input`                | `PlayerInput`                                                 | The player input component. *(Hidden in inspector)*                        |
| `protected` | `lastSelectables`      | `Dictionary<CrossInputSupportedWindowUGUI, TargetSelectable>` | The last selectables of each window.                                       |
| `private`   | `moveHeldCoroutine`    | `Coroutine`                                                   | A coroutine that handles move button holding.                              |
| `private`   | `lockedMoveAction`     | `Action`                                                      | An action to execute when the selector is locked on a selectable.          |
| `private`   | `moveDirection`        | `Vector2`                                                     | The current move direction.                                                |
| `private`   | `isLocked`             | `bool`                                                        | If the selector is currently locked on the selected selectable.            |

***

### 🔍 Properties

| Access          | Property   | Type                    | Description                      |
| --------------- | ---------- | ----------------------- | -------------------------------- |
| `public static` | `Instance` | `CrossInputSupportUGUI` | The active instance (singleton). |

***

### 🧰 Methods

| Access              | Method                                             | Returns                  | Description                                                             |
| ------------------- | -------------------------------------------------- | ------------------------ | ----------------------------------------------------------------------- |
| `public`            | `GetSelectedItemStack(bool includeAny = false)`    | `ItemStack`              | Gets the selected item stack (if any).                                  |
| `protected virtual` | `TryReturn()`                                      | `bool`                   | Reselects the last selectable if possible.                              |
| `private`           | `SetActionMap()`                                   | `void`                   | Sets the action map depending on which window is open.                  |
| `public`            | `Register(CrossInputSupportedWindowUGUI window)`   | `void`                   | Registers an input supported window.                                    |
| `public`            | `Unregister(CrossInputSupportedWindowUGUI window)` | `void`                   | Unregisters an input supported window.                                  |
| `public`            | `DetermineTop()`                                   | `void`                   | Determines the top window and repopulates the selectable list.          |
| `public`            | `FindSelectables()`                                | `List<TargetSelectable>` | Finds the visible selectables of the top window.                        |
| `private`           | `LateFindInitial()`                                | `IEnumerator`            | Calls `FindInitial` after a delay.                                      |
| `public`            | `FindInitial()`                                    | `TargetSelectable`       | Finds the initial selectable of the top window.                         |
| `public`            | `Select(TargetSelectable selectable)`              | `void`                   | Selects a selectable.                                                   |
| `public`            | `SelectInitial()`                                  | `void`                   | Selects the initial selectable.                                         |
| `public`            | `Deselect()`                                       | `void`                   | Deselects the selected selectable.                                      |
| `public`            | `AssessSelection()`                                | `void`                   | Checks the state of the selected selectable and deselects if necessary. |
| `public`            | `Unlock()`                                         | `void`                   | Unlocks the selector.                                                   |
| `public`            | `Lock()`                                           | `void`                   | Locks the selector.                                                     |
| `public`            | `Move(Vector2 dir)`                                | `void`                   | Selects the closest selectable in the given direction.                  |
| `private`           | `OnMenuMove(InputValue inputValue)`                | `void`                   | Handles directional input for menu navigation.                          |
| `private`           | `OnTab(InputValue inputValue)`                     | `void`                   | Handles tab input.                                                      |
| `private`           | `OnCancel(InputValue inputValue)`                  | `void`                   | Handles cancel input.                                                   |
| `private`           | `OnConfirm(InputValue inputValue)`                 | `void`                   | Handles confirm input.                                                  |
| `private`           | `OnDeselect()`                                     | `void`                   | Handles deselection.                                                    |
| `private`           | `MoveHeld()`                                       | `IEnumerator`            | Handles move button hold.                                               |

***

### 🧩 Nested Types

#### `public enum InitialSelectablePosition`

Defines the placement of the initial selectable.

| Name          | Description          |
| ------------- | -------------------- |
| `TopLeft`     | Top‑left corner.     |
| `TopRight`    | Top‑right corner.    |
| `BottomLeft`  | Bottom‑left corner.  |
| `BottomRight` | Bottom‑right corner. |

***

### Active Instance

You can get the active `CrossInputSupportUGUI` instance with `CrossInputSupportUGUI.Instance`. Ensure there's an instance of it in your scene before using this field.

## Registration

The `CrossInputSupportUGUI` class only works on registered`CrossInputSupportedWindowUGUI` components. Use the `Register` method to register a `CrossInputSupportedWindowUGUI`.

```csharp
CrossInputSupportUGUI.Instance.Register();
```

Use `Unregister` to have the opposite effect.

```csharp
CrossInputSupportUGUI.Instance.Unregister();
```

## Top Window

The window at the top of every other one will have navigation control. To update the top window, use the `DetermineTop` method.

```csharp
CrossInputSupportUGUI.Instance.DetermineTop();
```

## Selectables

Selectables are UI objects that can be selected by the `CrossInputSupportUGUI` component.

### Find Selectables

To find all selectables of the top document, use the `FindSelectables` method. This will update the `selectables` field.

```csharp
CrossInputSupportUGUI.Instance.FindSelectables();
```

### Find Initial

The initial selectable is the first one selected when navigating as decided by the `initialSelectablePosition` field. Use the `FindInitial` method to get the initial selectable. this will update the `initialSelectable` field.

## Selection

A Selectable is hovered when navigating with any move button. The hovered selectable will have the selector targeting it.

### Select

To simulate a selection, use the `Select` method. If the selector is locked, this method will do nothing.

```csharp
CrossInputSupportUGUI.Instance.Select(visualElement);
```

To select the initial selectable, use `SelectInitial`.

```csharp
CrossInputSupportUGUI.Instance.SelectInitial();
```

### Deselect

Use `Deselect` to stop hovering and hide the selector graphic.

```csharp
CrossInputSupportUGUI.Instance.Deselect();
```

Alternatively, you can use `AssessSelection`, which will deselect the current selection if it's not enabled or selectable anymore.

```csharp
CrossInputSupportUGUI.Instance.AssessSelection();
```

### Locking

To lock on a selectable, when one is hovered, use the `Lock` method. When a selectable is locked on, navigation will be prevented.

```csharp
CrossInputSupportUGUI.Instance.Lock();
```

### Unlocking

The `Unlock` method will stop locking on to a selectable.

```csharp
CrossInputSupportUGUI.Instance.Unlock();
```

### Moving

Use the Move method to move the selector towards a direction.

```csharp
CrossInputSupportUGUI.Instance.Move(Vector2.down);
```

## Getting the Selected Item Stack

To get the [item-stack](https://stylishesper.gitbook.io/inventool/scripting/api/items/item-stack "mention") that the selector is currently over, use the `GetSelectedItemStack` method. This method will return null if there is no selected item stack.

```csharp
ItemStack itemStack = CrossInputSupportUGUI.Instance.GetSelectedItemStack()
```
