From 0a0eb14a6b0087d35dc609443fd711978a3d35c7 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Thu, 19 Mar 2026 15:01:04 +0300 Subject: [PATCH] feat: input plugin reflects, derives --- Cargo.lock | 2 +- src/input/plugin.rs | 48 +++++++++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 06b99c8..8b4d53a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2422,7 +2422,7 @@ dependencies = [ [[package]] name = "expedition_demo" -version = "0.1.0" +version = "0.2.0" dependencies = [ "bevy", "bevy_common_assets", diff --git a/src/input/plugin.rs b/src/input/plugin.rs index 30052c1..51f6de1 100644 --- a/src/input/plugin.rs +++ b/src/input/plugin.rs @@ -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>, pub mouse: Option>, @@ -20,7 +40,8 @@ impl From 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 where Name: Sized + Hash + Eq + Reflect + TypePath + Actionlike { #[serde(flatten)] @@ -205,17 +227,18 @@ impl From> for InputAsset } } -#[derive(Resource, Deref)] +#[derive(Resource, Debug, Deref, DerefMut, Reflect, Clone, PartialEq, Eq)] +#[reflect(Resource, Debug, Clone, PartialEq)] pub struct InputAssetHandle (Option>>); #[derive(Debug)] -pub struct InputAssetPlugin { +pub struct InputAssetPlugin { _phantom: PhantomData, extensions: &'static [&'static str], } impl InputAssetPlugin - 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 InputAssetPlugin } impl Default for InputAssetPlugin - 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 Default for InputAssetPlugin } impl Plugin for InputAssetPlugin - 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::::default(), TomlAssetPlugin::>::new(&self.extensions), - InputManagerPlugin::::default() )); } }