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

basic keybind logic #766

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ de_core = { path = "crates/core", version = "0.1.0-dev" }
de_energy = { path = "crates/energy", version = "0.1.0-dev" }
de_gui = { path = "crates/gui", version = "0.1.0-dev" }
de_index = { path = "crates/index", version = "0.1.0-dev" }
de_input = { path = "crates/input", version = "0.1.0-dev" }
de_loader = { path = "crates/loader", version = "0.1.0-dev" }
de_lobby_client = { path = "crates/lobby_client", version = "0.1.0-dev" }
de_lobby_model = { path = "crates/lobby_model", version = "0.1.0-dev" }
Expand Down Expand Up @@ -136,13 +137,15 @@ glam = "0.24"
gltf = "1.0"
itertools = "0.11.0"
iyes_progress = "0.9.0"
leafwing-input-manager = "0.10.0"
log = "0.4.17"
nalgebra = { version = "0.32.3", features = ["convert-glam024"] }
nix = "0.26.2"
ntest = "0.9.0"
parry2d = "0.13.1"
parry3d = "0.13.1"
paste = "1.0.12"
petitset = "0.2.1"
priority-queue = "1.3.0"
proc-macro2 = "1.0.63"
quote = "1.0.27"
Expand Down Expand Up @@ -191,4 +194,5 @@ features = [
"tonemapping_luts",
"default_font",
"webgl2",
"serialize"
]
4 changes: 4 additions & 0 deletions crates/controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ de_signs.workspace = true
de_spawner.workspace = true
de_terrain.workspace = true
de_types.workspace = true
de_input.workspace = true

# Other
ahash.workspace = true
bevy.workspace = true
enum-map.workspace = true
glam.workspace = true
leafwing-input-manager.workspace = true
parry2d.workspace = true
parry3d.workspace = true
petitset.workspace = true
serde.workspace = true
124 changes: 124 additions & 0 deletions crates/controller/src/actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use bevy::input::keyboard::KeyCode;
use bevy::prelude::Reflect;
use bevy::prelude::{MouseButton, Res};
use de_input::{AppKeybinding, DefaultKeybindings};
use leafwing_input_manager::prelude::{ActionState, InputMap, QwertyScanCode, UserInput};
use leafwing_input_manager::Actionlike;
use serde::{Deserialize, Serialize};

pub struct ActionPlugin;

impl bevy::app::Plugin for ActionPlugin {
fn build(&self, app: &mut App) {
app.add_action_set::<Action>();
}
}

use std::collections::HashMap;

use bevy::app::App;
use petitset::PetitSet;

#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash,
Deserialize,
Serialize,
Actionlike,
Reflect,
PartialOrd,
Ord,
)]
pub enum Action {
Exit,
SelectAllVisible,
SelectAll,
AddToSelection,
ReplaceSelection,
Up,
Down,
Left,
Right,
Pivot,
BuildBase,
BuildPowerHub,
}
impl DefaultKeybindings for Action {
fn default_keybindings() -> InputMap<Self>
where
Self: Sized,
{
use Action::*;
let keybindings = InputMap::<Self>::from(
vec![
(Exit, vec![KeyCode::Escape.into()]),
(
SelectAllVisible,
vec![UserInput::chord(vec![
KeyCode::ControlLeft,
KeyCode::ShiftLeft,
KeyCode::A,
])],
),
(
SelectAll,
vec![UserInput::chord(vec![KeyCode::ControlLeft, KeyCode::A])],
),
(
AddToSelection,
vec![
UserInput::Chord(PetitSet::from_iter(vec![
KeyCode::ControlLeft.into(),
MouseButton::Left.into(),
])),
UserInput::Chord(PetitSet::from_iter(vec![
KeyCode::ControlRight.into(),
MouseButton::Left.into(),
])),
],
),
(ReplaceSelection, vec![MouseButton::Left.into()]),
(Up, vec![QwertyScanCode::W.into(), KeyCode::Up.into()]),
(Down, vec![QwertyScanCode::S.into(), KeyCode::Down.into()]),
(Left, vec![QwertyScanCode::A.into(), KeyCode::Left.into()]),
(Right, vec![QwertyScanCode::D.into(), KeyCode::Right.into()]),
(
Pivot,
vec![UserInput::Chord(PetitSet::from_iter(vec![
KeyCode::ControlLeft.into(),
MouseButton::Middle.into(),
]))],
),
(BuildBase, vec![KeyCode::B.into()]),
(BuildPowerHub, vec![KeyCode::P.into()]),
]
.into_iter()
.collect::<HashMap<Self, Vec<UserInput>>>(),
);
println!("keybindings: {:?}", keybindings);
keybindings
}
}

impl Action {
pub fn get_factory_actions() -> Vec<(Self, de_types::objects::BuildingType)> {
use de_types::objects::BuildingType::*;
use Action::*;

vec![(BuildBase, Base), (BuildPowerHub, PowerHub)]
}
}

pub(crate) fn action_just_pressed<A: Actionlike>(
action: A,
) -> impl Fn(Res<ActionState<A>>) -> bool {
move |action_state: Res<ActionState<A>>| action_state.just_pressed(action.clone())
}

pub(crate) fn action_pressed<A: Actionlike>(action: A) -> impl Fn(Res<ActionState<A>>) -> bool {
move |action_state: Res<ActionState<A>>| action_state.pressed(action.clone())
}
Loading
Loading