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

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 leafwing_input_manager::{Actionlike, plugin::InputManagerPlugin, prelude::*};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use leafwing_input_manager::{
Actionlike,
plugin::InputManagerPlugin,
prelude::*,
};
use serde::{
Deserialize,
Serialize,
de::DeserializeOwned,
};
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 keyboard: Option<Vec<KeyCode>>,
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)]
pub enum InputKind {
Button(MultiInput),
@ -29,6 +50,7 @@ pub enum InputKind {
}
#[derive(Default, Deref, DerefMut, Debug, Asset, Reflect, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[reflect(Debug, Clone, PartialEq)]
pub struct InputAsset<Name>
where Name: Sized + Hash + Eq + Reflect + TypePath + Actionlike {
#[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>>>);
#[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>,
extensions: &'static [&'static str],
}
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 {
Self {
_phantom: PhantomData,
@ -225,7 +248,7 @@ impl<T> 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 {
Self {
_phantom: PhantomData,
@ -235,12 +258,11 @@ impl<T> Default 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) {
app.add_plugins((
InputManagerPlugin::<T>::default(),
TomlAssetPlugin::<InputAsset<T>>::new(&self.extensions),
InputManagerPlugin::<T>::default()
));
}
}