feat: input plugin reflects, derives

This commit is contained in:
Alexey 2026-03-19 15:01:04 +03:00
commit 0a0eb14a6b
2 changed files with 36 additions and 14 deletions

2
Cargo.lock generated
View file

@ -2422,7 +2422,7 @@ dependencies = [
[[package]] [[package]]
name = "expedition_demo" name = "expedition_demo"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy_common_assets", "bevy_common_assets",

View file

@ -1,13 +1,33 @@
use std::{any::{Any, TypeId}, collections::HashMap, hash::Hash, marker::PhantomData}; use std::{
any::{
Any,
TypeId,
},
collections::HashMap,
hash::Hash,
marker::PhantomData,
};
use bevy::{prelude::*, reflect::GetTypeRegistration}; use bevy::{
prelude::*,
reflect::GetTypeRegistration,
};
use bevy_common_assets::toml::TomlAssetPlugin; use bevy_common_assets::toml::TomlAssetPlugin;
use leafwing_input_manager::{Actionlike, plugin::InputManagerPlugin, prelude::*}; use leafwing_input_manager::{
use serde::{Deserialize, Serialize, de::DeserializeOwned}; Actionlike,
plugin::InputManagerPlugin,
prelude::*,
};
use serde::{
Deserialize,
Serialize,
de::DeserializeOwned,
};
const INPUT_ASSET_EXTENSIONS: [&'static str; 1] = ["input.toml"]; const INPUT_ASSET_EXTENSIONS: [&'static str; 1] = ["input.toml"];
#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq)] #[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq, Reflect)]
#[reflect(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
pub struct MultiInput { pub struct MultiInput {
pub keyboard: Option<Vec<KeyCode>>, pub keyboard: Option<Vec<KeyCode>>,
pub mouse: Option<Vec<MouseButton>>, pub mouse: Option<Vec<MouseButton>>,
@ -20,7 +40,8 @@ impl From<MultiInput> for InputKind {
} }
} }
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Reflect)]
#[reflect(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)] #[serde(untagged)]
pub enum InputKind { pub enum InputKind {
Button(MultiInput), Button(MultiInput),
@ -29,6 +50,7 @@ pub enum InputKind {
} }
#[derive(Default, Deref, DerefMut, Debug, Asset, Reflect, Clone, Serialize, Deserialize, PartialEq, Eq)] #[derive(Default, Deref, DerefMut, Debug, Asset, Reflect, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[reflect(Debug, Clone, PartialEq)]
pub struct InputAsset<Name> pub struct InputAsset<Name>
where Name: Sized + Hash + Eq + Reflect + TypePath + Actionlike { where Name: Sized + Hash + Eq + Reflect + TypePath + Actionlike {
#[serde(flatten)] #[serde(flatten)]
@ -205,17 +227,18 @@ impl<Name> From<InputMap<Name>> for InputAsset<Name>
} }
} }
#[derive(Resource, Deref)] #[derive(Resource, Debug, Deref, DerefMut, Reflect, Clone, PartialEq, Eq)]
#[reflect(Resource, Debug, Clone, PartialEq)]
pub struct InputAssetHandle<T: Sized + Hash + Eq + Reflect + TypePath + Actionlike> (Option<Handle<InputAsset<T>>>); pub struct InputAssetHandle<T: Sized + Hash + Eq + Reflect + TypePath + Actionlike> (Option<Handle<InputAsset<T>>>);
#[derive(Debug)] #[derive(Debug)]
pub struct InputAssetPlugin<T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration> { pub struct InputAssetPlugin<T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned> {
_phantom: PhantomData<T>, _phantom: PhantomData<T>,
extensions: &'static [&'static str], extensions: &'static [&'static str],
} }
impl<T> InputAssetPlugin<T> impl<T> InputAssetPlugin<T>
where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration { where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned {
pub fn new(extensions: &'static [&'static str]) -> Self { pub fn new(extensions: &'static [&'static str]) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData,
@ -225,7 +248,7 @@ impl<T> InputAssetPlugin<T>
} }
impl<T> Default for InputAssetPlugin<T> impl<T> Default for InputAssetPlugin<T>
where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration { where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned {
fn default() -> Self { fn default() -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData,
@ -235,12 +258,11 @@ impl<T> Default for InputAssetPlugin<T>
} }
impl<T> Plugin for InputAssetPlugin<T> impl<T> Plugin for InputAssetPlugin<T>
where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration {
{
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins(( app.add_plugins((
InputManagerPlugin::<T>::default(),
TomlAssetPlugin::<InputAsset<T>>::new(&self.extensions), TomlAssetPlugin::<InputAsset<T>>::new(&self.extensions),
InputManagerPlugin::<T>::default()
)); ));
} }
} }