kon_input/
ext.rs

1//! Extension trait for accessing Input from Context
2//!
3//! Provides convenient `input()` method on Context
4//! instead of manually calling `ctx.global::<Input>()`.
5
6use std::cell::RefMut;
7use kon_core::Context;
8use crate::Input;
9
10/// Extension trait for convenient Input access from Context
11///
12/// # Example
13/// ```ignore
14/// fn my_system(ctx: &mut Context) {
15///     let input = ctx.input();
16///     if input.is_action_pressed("Jump") {
17///         // ...
18///     }
19/// }
20/// ```
21///
22/// # Panics
23/// Panics if Input is not registered. Ensure `InputPlugin` or `DefaultPlugins` is added.
24pub trait ContextInputExt {
25    fn input(&self) -> RefMut<'_, Input>;
26}
27
28impl ContextInputExt for Context {
29    /// Returns a reference to the Input manager
30    ///
31    /// # Panics
32    /// Panics with a helpful message if InputPlugin is not registered
33    #[track_caller]
34    fn input(&self) -> RefMut<'_, Input> {
35        self.global::<Input>()
36            .expect("Failed to access Input. Ensure 'DefaultPlugins' or 'InputPlugin' is added")
37    }
38}