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

add configurable controls #94

Merged
merged 7 commits into from
Oct 1, 2022
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/keyboardInput.json
3 changes: 3 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions gamercade_console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ gamercade_audio = { path = "../gamercade_audio" }
gamercade_sound_engine = { path = "../gamercade_sound_engine" }

# General Improvements
hashbrown = "0.12.3"
hashbrown = { version = "0.12.3", features = ["serde"] }
parking_lot = "0.12.1"

# Windowing & Graphics
winit = "0.26.1"
winit = { version = "0.26.1", features = ["serde"] }
pixels = "0.9.0"
winit_input_helper = "0.12.0"

Expand All @@ -35,6 +35,7 @@ paste = "1.0.8"

# Serialization / File Loading etc
serde = { version = "1.0.144", features = ["derive"] }
serde_json = "1.0.85"
bytemuck = "1.12.1"

# Scripting
Expand Down
8 changes: 4 additions & 4 deletions gamercade_console/src/console/contexts/input_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ macro_rules! derive_generate_input_api {

derive_generate_input_api! {
Buttons {
a: A,
b: B,
c: C,
d: D,
a: ButtonA,
b: ButtonB,
c: ButtonC,
d: ButtonD,
up: Up,
down: Down,
left: Left,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{api::MultiplayerApi, console::SessionDescriptor};

#[derive(Clone)]
pub struct MultiplayerContext {
session: SessionDescriptor,
pub(crate) session: SessionDescriptor,
}

impl MultiplayerContext {
Expand Down
8 changes: 4 additions & 4 deletions gamercade_console/src/console/input/gamepad_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ impl Default for GamepadBindings {
(Button::DPadDown, ButtonCode::Down),
(Button::DPadLeft, ButtonCode::Left),
(Button::DPadRight, ButtonCode::Right),
(Button::East, ButtonCode::A),
(Button::South, ButtonCode::B),
(Button::West, ButtonCode::C),
(Button::North, ButtonCode::D),
(Button::East, ButtonCode::ButtonA),
(Button::South, ButtonCode::ButtonB),
(Button::West, ButtonCode::ButtonC),
(Button::North, ButtonCode::ButtonD),
(Button::Start, ButtonCode::Start),
(Button::Select, ButtonCode::Select),
(Button::LeftTrigger, ButtonCode::LeftShoulder),
Expand Down
166 changes: 75 additions & 91 deletions gamercade_console/src/console/input/key_bindings.rs
Original file line number Diff line number Diff line change
@@ -1,144 +1,128 @@
use gamercade_core::{ButtonCode, InputState};
use std::path::PathBuf;

use gamercade_core::ButtonCode;

use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use winit::event::VirtualKeyCode;

use super::key_types::{Analog, AnalogAxis, AnalogDirection, AnalogSide, KeyType};

impl Analog {
pub(crate) fn adjust_input_state(self, input_state: &mut InputState) {
let value = match self.direction {
AnalogDirection::Positive => 1.0,
AnalogDirection::Negative => -1.0,
};
use super::key_types::{AnalogStick, KeyType, TriggerSide};

let stick = match self.side {
AnalogSide::Left => &mut input_state.left_stick,
AnalogSide::Right => &mut input_state.right_stick,
};
const INPUT_FILE_NAME: &str = "keyboardInput.json";

match self.axis {
AnalogAxis::X => stick.set_x_axis(value),
AnalogAxis::Y => stick.set_y_axis(value),
};
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(transparent)]
pub(crate) struct KeyBindings {
pub buttons: Vec<HashMap<VirtualKeyCode, KeyType>>,
}

#[derive(Debug)]
pub(crate) struct KeyBindings {
pub buttons: HashMap<VirtualKeyCode, KeyType>,
impl KeyBindings {
pub fn load() -> Self {
let path = PathBuf::from(INPUT_FILE_NAME);
if path.exists() {
match std::fs::read(INPUT_FILE_NAME) {
Ok(file) => match serde_json::from_slice::<Self>(&file) {
Ok(key_bindings) => {
println!("Successfully loaded key bindings from: {}", INPUT_FILE_NAME);
return key_bindings;
}
Err(e) => {
println!("{} found, but unable to parse: {}", INPUT_FILE_NAME, e);
}
},
Err(e) => println!("{} found, but unable to read: {}", INPUT_FILE_NAME, e),
};

println!("Using default config.");
Self::default()
} else {
println!(
"{} not found. Generating default input file.",
INPUT_FILE_NAME
);
let bindings = Self::default();

let json = serde_json::to_string_pretty(&bindings).unwrap();

match std::fs::write(path, json) {
Ok(()) => println!("Successfully generated default {}", INPUT_FILE_NAME),
Err(e) => println!("Error writing {}: {}", INPUT_FILE_NAME, e),
};

bindings
}
}
}

impl Default for KeyBindings {
fn default() -> Self {
let buttons = [
let buttons = vec![[
//Sticks
(
VirtualKeyCode::X,
KeyType::ButtonCode(ButtonCode::LeftStick),
),
(
VirtualKeyCode::B,
KeyType::ButtonCode(ButtonCode::RightStick),
),
(VirtualKeyCode::X, KeyType::Button(ButtonCode::LeftStick)),
(VirtualKeyCode::B, KeyType::Button(ButtonCode::RightStick)),
//Shoulders
(VirtualKeyCode::E, KeyType::Button(ButtonCode::LeftShoulder)),
(
VirtualKeyCode::E,
KeyType::ButtonCode(ButtonCode::LeftShoulder),
VirtualKeyCode::Q,
KeyType::Trigger(TriggerSide::LeftTrigger),
),
(VirtualKeyCode::Q, KeyType::Trigger(AnalogSide::Left)),
(
VirtualKeyCode::R,
KeyType::ButtonCode(ButtonCode::RightShoulder),
KeyType::Button(ButtonCode::RightShoulder),
),
(VirtualKeyCode::Y, KeyType::Trigger(AnalogSide::Right)),
//DPad:
(VirtualKeyCode::Up, KeyType::ButtonCode(ButtonCode::Up)),
(VirtualKeyCode::Down, KeyType::ButtonCode(ButtonCode::Down)),
(VirtualKeyCode::Left, KeyType::ButtonCode(ButtonCode::Left)),
(
VirtualKeyCode::Right,
KeyType::ButtonCode(ButtonCode::Right),
VirtualKeyCode::Y,
KeyType::Trigger(TriggerSide::RightTrigger),
),
//DPad:
(VirtualKeyCode::Up, KeyType::Button(ButtonCode::Up)),
(VirtualKeyCode::Down, KeyType::Button(ButtonCode::Down)),
(VirtualKeyCode::Left, KeyType::Button(ButtonCode::Left)),
(VirtualKeyCode::Right, KeyType::Button(ButtonCode::Right)),
//Buttons:
(VirtualKeyCode::U, KeyType::ButtonCode(ButtonCode::A)),
(VirtualKeyCode::I, KeyType::ButtonCode(ButtonCode::B)),
(VirtualKeyCode::J, KeyType::ButtonCode(ButtonCode::C)),
(VirtualKeyCode::K, KeyType::ButtonCode(ButtonCode::D)),
(VirtualKeyCode::Key5, KeyType::ButtonCode(ButtonCode::Start)),
(
VirtualKeyCode::Key6,
KeyType::ButtonCode(ButtonCode::Select),
),
(VirtualKeyCode::U, KeyType::Button(ButtonCode::ButtonA)),
(VirtualKeyCode::I, KeyType::Button(ButtonCode::ButtonB)),
(VirtualKeyCode::J, KeyType::Button(ButtonCode::ButtonC)),
(VirtualKeyCode::K, KeyType::Button(ButtonCode::ButtonD)),
(VirtualKeyCode::Key5, KeyType::Button(ButtonCode::Start)),
(VirtualKeyCode::Key6, KeyType::Button(ButtonCode::Select)),
//Left Stick Axis
(
VirtualKeyCode::W,
KeyType::Analog(Analog {
side: AnalogSide::Left,
axis: AnalogAxis::Y,
direction: AnalogDirection::Positive,
}),
KeyType::AnalogStick(AnalogStick::LeftYPositive),
),
(
VirtualKeyCode::S,
KeyType::Analog(Analog {
side: AnalogSide::Left,
axis: AnalogAxis::Y,
direction: AnalogDirection::Negative,
}),
KeyType::AnalogStick(AnalogStick::LeftYNegative),
),
(
VirtualKeyCode::A,
KeyType::Analog(Analog {
side: AnalogSide::Left,
axis: AnalogAxis::X,
direction: AnalogDirection::Negative,
}),
KeyType::AnalogStick(AnalogStick::LeftXNegative),
),
(
VirtualKeyCode::D,
KeyType::Analog(Analog {
side: AnalogSide::Left,
axis: AnalogAxis::X,
direction: AnalogDirection::Positive,
}),
KeyType::AnalogStick(AnalogStick::LeftXPositive),
),
//Right Stick Axis,
(
VirtualKeyCode::T,
KeyType::Analog(Analog {
side: AnalogSide::Right,
axis: AnalogAxis::Y,
direction: AnalogDirection::Positive,
}),
KeyType::AnalogStick(AnalogStick::RightYPositive),
),
(
VirtualKeyCode::G,
KeyType::Analog(Analog {
side: AnalogSide::Right,
axis: AnalogAxis::Y,
direction: AnalogDirection::Negative,
}),
KeyType::AnalogStick(AnalogStick::RightYNegative),
),
(
VirtualKeyCode::F,
KeyType::Analog(Analog {
side: AnalogSide::Right,
axis: AnalogAxis::X,
direction: AnalogDirection::Negative,
}),
KeyType::AnalogStick(AnalogStick::RightXNegative),
),
(
VirtualKeyCode::H,
KeyType::Analog(Analog {
side: AnalogSide::Right,
axis: AnalogAxis::X,
direction: AnalogDirection::Positive,
}),
KeyType::AnalogStick(AnalogStick::RightXPositive),
),
]
.into_iter()
.collect::<HashMap<VirtualKeyCode, KeyType>>();
.collect::<HashMap<VirtualKeyCode, KeyType>>()];

Self { buttons }
}
Expand Down
43 changes: 20 additions & 23 deletions gamercade_console/src/console/input/key_types.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
use gamercade_core::ButtonCode;
use serde::{Deserialize, Serialize};

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
#[serde(untagged)]
pub(crate) enum KeyType {
ButtonCode(ButtonCode),
Analog(Analog),
Trigger(AnalogSide),
Button(ButtonCode),
AnalogStick(AnalogStick),
Trigger(TriggerSide),
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct Analog {
pub(crate) side: AnalogSide,
pub(crate) axis: AnalogAxis,
pub(crate) direction: AnalogDirection,
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub(crate) enum AnalogStick {
LeftXPositive,
LeftXNegative,
LeftYPositive,
LeftYNegative,
RightXPositive,
RightXNegative,
RightYPositive,
RightYNegative,
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum AnalogSide {
Left,
Right,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum AnalogAxis {
X,
Y,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum AnalogDirection {
Positive,
Negative,
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub(crate) enum TriggerSide {
LeftTrigger,
RightTrigger,
}
Loading