Focus Management
Focus management in dill-pixel provides keyboard navigation and visual feedback for interactive elements in your application. The system consists of multiple layers of focusable elements that can be navigated using the Tab key.
Core Concepts
Focus Layers
Focus layers provide a way to manage multiple sets of focusable elements in your application. Think of them as stackable groups of interactive elements where only one layer can be active at a time.
Common Use Cases
- Menu Navigation: Separate menu items into their own layer
- Popups: Create a dedicated layer for popup content
- Game UI: Switch between different UI states (inventory, skills, etc.)
Layer Priority
You can control the order of layers to manage focus priority:
Layer Events
Default Focus
Each layer can have a default focusable element that receives focus when the layer becomes active:
Layer Cleanup
Focus layers are automatically cleaned up when changing scenes, but you can also manually remove them:
Focusable Elements
Any container implementing the IFocusable
interface can receive focus. The framework provides built-in focus support for components like Button
.
Visual Feedback
The FocusOutliner
provides visual feedback when elements receive focus. By default, it draws a cyan rectangle or rounded rectangle around the focused element.
Default Outliner Configuration
Configure the default outliner’s appearance in your dill-pixel config:
Configuration Options
Option | Type | Default | Description |
---|---|---|---|
color | number | 0x00ffff | Outline color in hex format |
shape | 'rectangle' | 'rounded rectangle' | 'rounded rectangle' | Outline shape style |
lineWidth | number | 2 | Width of the outline in pixels |
radius | number | 8 | Corner radius for rounded rectangle shape |
Custom Focus Outliner
You can create your own focus outliner by extending the base FocusOutliner
class:
Create a custom outliner component
Configure the outliner in your dill-pixel.config.ts