Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional Non-Entity Resource for handling inputs #103

Closed
wants to merge 1 commit into from
Closed

Optional Non-Entity Resource for handling inputs #103

wants to merge 1 commit into from

Conversation

huhlig
Copy link
Contributor

@huhlig huhlig commented Apr 7, 2022

This incorporates a monomorphizable resource that operates the same as the input component. This allows for input handling not attached to an entity.

Example:

#[derive(Actionlike, Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum DebugAction {
    ToggleConsole,
    ToggleOverlay,
    TogglePathing,
}

impl DebugAction {
    pub fn input_resource() -> InputResource<Self> {
        let mut res = InputResource::default();
        res.input_map
            .insert(DebugAction::ToggleConsole, KeyCode::F1);
        res.input_map
            .insert(DebugAction::ToggleOverlay, KeyCode::F2);
        res.input_map
            .insert(DebugAction::TogglePathing, KeyCode::F3);
        res
    }
}

#[derive(Debug, Default)]
pub struct DebugState {
    text_overlay: Option<TextOverlay>,
}

#[derive(Debug)]
struct TextOverlay {
    position: Entity,
    clock: Entity,
}

/// Should be called once to setup Gameplay Mode
pub fn handle_input(
    mut commands: Commands,
    mut debug_state: ResMut<DebugState>,
    mut asset_server: ResMut<AssetServer>,
    mut input_resource: Res<InputResource<DebugAction>>,
) {
    if input_resource
        .action_state
        .just_pressed(&DebugAction::ToggleOverlay)
    {
        if let Some(text_overlay) = &mut debug_state.text_overlay {
            trace!("Disable Debug Overlay");
            commands.entity(text_overlay.position).despawn();
            commands.entity(text_overlay.clock).despawn();
            debug_state.text_overlay = None;
        } else {
            trace!("Enable Debug Overlay");
            debug_state.text_overlay = Some(TextOverlay {
                position: commands
                    .spawn_bundle(TextBundle {
                        text: Text::with_section(
                            String::from("Position"),
                            TextStyle {
                                font: asset_server.load("fonts/spacemono.ttf"),
                                font_size: 30.0,
                                color: Color::WHITE,
                            },
                            TextAlignment {
                                vertical: VerticalAlign::Top,
                                horizontal: HorizontalAlign::Left,
                            },
                        ),
                        ..Default::default()
                    })
                    .id(),
                clock: commands
                    .spawn_bundle(TextBundle {
                        text: Text::with_section(
                            String::from("Clock"),
                            TextStyle {
                                font: asset_server.load("fonts/spacemono.ttf"),
                                font_size: 30.0,
                                color: Color::WHITE,
                            },
                            TextAlignment {
                                vertical: VerticalAlign::Bottom,
                                horizontal: HorizontalAlign::Right,
                            },
                        ),
                        ..Default::default()
                    })
                    .id(),
            });
        }
    }
}

src/lib.rs Outdated
@@ -143,3 +143,20 @@ impl<A: Actionlike> Default for InputManagerBundle<A> {
}
}
}

/// Resource for Inputs
pub struct InputResource<A: Actionlike> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I think I'd prefer if we just supported reusing the ActionState and InputMap components as resources directly.

This is a bit more intuitive and will involve fewer levels of unwrapping for users.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree here. Things like Res<InputResource<>> look strange :)

@alice-i-cecile
Copy link
Contributor

As discussed on Discord, we should migrate https://github.com/Leafwing-Studios/leafwing_input_manager/blob/dev/examples/binding_menu.rs to use this pattern :)

@alice-i-cecile
Copy link
Contributor

This will need to be revised to account for #106.

@Shatur
Copy link
Collaborator

Shatur commented Apr 7, 2022

Yep, just move your code after

    if disable_input.is_some() {
        return;
    }

@alice-i-cecile
Copy link
Contributor

Superceded by #109.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants