Skip to content

Commit

Permalink
Add more bundles impls
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Apr 29, 2023
1 parent 0f04af3 commit 088be8d
Show file tree
Hide file tree
Showing 11 changed files with 808 additions and 64 deletions.
3 changes: 2 additions & 1 deletion bevy_proto_backend/src/impls/bevy_impls/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ impl_external_schematic! {
impl<T: Asset> FromSchematicInput<ProtoAsset> for Handle<T> {
fn from_input(input: ProtoAsset, entity: &mut EntityMut, _: &EntityTree) -> Self {
match input {
ProtoAsset::AssetPath(path) => entity.world().resource::<AssetServer>().get_handle(path)
ProtoAsset::AssetPath(path) => entity.world().resource::<AssetServer>().get_handle(path),
ProtoAsset::HandleId(handle_id) => entity.world().resource::<AssetServer>().get_handle(handle_id),
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions bevy_proto_backend/src/impls/bevy_impls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#[cfg(feature = "bevy_animation")]
mod animation;
mod asset;
mod core;
pub mod animation;
pub mod asset;
pub mod core;
#[cfg(feature = "bevy_core_pipeline")]
mod core_pipeline;
pub mod core_pipeline;
#[cfg(feature = "bevy_gltf")]
mod gltf;
pub mod gltf;
#[cfg(feature = "bevy_pbr")]
mod pbr;
pub mod pbr;
#[cfg(feature = "bevy_render")]
mod render;
pub mod render;
#[cfg(feature = "bevy_sprite")]
mod sprite;
pub mod sprite;
#[cfg(feature = "bevy_text")]
mod text;
mod transform;
pub mod text;
pub mod transform;
#[cfg(feature = "bevy_ui")]
mod ui;
mod window;
pub mod ui;
pub mod window;

pub(super) fn register_impls(app: &mut bevy::app::App) {
asset::register(app);
Expand Down
4 changes: 2 additions & 2 deletions bevy_proto_backend/src/impls/bevy_impls/pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ impl_external_schematic! {

impl_external_schematic! {
pub struct EnvironmentMapLight {
#[schematic(asset)]
#[schematic(asset(lazy))]
pub diffuse_map: Handle<Image>,
#[schematic(asset)]
#[schematic(asset(lazy))]
pub specular_map: Handle<Image>,
}
}
Expand Down
26 changes: 21 additions & 5 deletions bevy_proto_backend/src/impls/bevy_impls/render.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use bevy::app::App;
use bevy::prelude::{Camera, Entity, OrthographicProjection, PerspectiveProjection, Projection};
use bevy::reflect::{std_traits::ReflectDefault, FromReflect, Reflect};
use bevy::reflect::{std_traits::ReflectDefault, FromReflect, Reflect, TupleStruct};
use bevy::render::camera::CameraRenderGraph;
use bevy::render::mesh::skinning::SkinnedMesh;
use bevy::render::primitives::Aabb;
use bevy::render::view::{ColorGrading, RenderLayers, Visibility};
use std::borrow::Cow;

use crate::impls::macros::{from_to_default, register_schematic};
use crate::tree::ProtoEntityList;
Expand Down Expand Up @@ -36,15 +37,30 @@ impl_external_schematic! {

impl_external_schematic! {
#[schematic(from = CameraRenderGraphInput)]
struct CameraRenderGraph();
struct CameraRenderGraph {}
// ---
#[derive(Reflect, FromReflect)]
pub struct CameraRenderGraphInput(String);
#[derive(Reflect, FromReflect, Default)]
#[reflect(Default)]
pub struct CameraRenderGraphInput(Cow<'static, str>);

impl From<CameraRenderGraphInput> for CameraRenderGraph {
fn from(value: CameraRenderGraphInput) -> Self {
Self::new(value.0)
}
}

impl From<CameraRenderGraph> for CameraRenderGraphInput {
fn from(value: CameraRenderGraph) -> Self {
Self(
value
.field(0)
.unwrap()
.downcast_ref::<Cow<'static, str>>()
.unwrap()
.clone(),
)
}
}
}

impl_external_schematic! {
Expand Down Expand Up @@ -114,7 +130,7 @@ impl_external_schematic! {

impl_external_schematic! {
pub struct SkinnedMesh {
#[schematic(asset)]
#[schematic(asset(lazy))]
pub inverse_bindposes: Handle<SkinnedMeshInverseBindposes>,
#[schematic(from = ProtoEntityList)]
pub joints: Vec<Entity>,
Expand Down
9 changes: 9 additions & 0 deletions bevy_proto_backend/src/impls/bevy_impls/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy::reflect::{std_traits::ReflectDefault, FromReflect, Reflect};
use bevy::sprite::{Anchor, Mesh2dHandle, Sprite, TextureAtlasSprite};

use crate::impls::macros::{from_to_default, register_schematic};
use crate::proto::ProtoAsset;
use bevy_proto_derive::impl_external_schematic;

pub(super) fn register(app: &mut App) {
Expand All @@ -16,7 +17,15 @@ impl_external_schematic! {
}

impl_external_schematic! {
#[schematic(input(vis = pub))]
struct Mesh2dHandle(#[schematic(asset)] pub Handle<Mesh>);
// ---
impl Default for Mesh2dHandleInput {
fn default() -> Self {
let base = <Mesh2dHandle as Default>::default();
Self(ProtoAsset::HandleId(base.0.id()))
}
}
}

impl_external_schematic! {
Expand Down
43 changes: 35 additions & 8 deletions bevy_proto_backend/src/impls/bevy_impls/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use bevy::prelude::{BackgroundColor, Button, Color, Label};
use bevy::reflect::{std_traits::ReflectDefault, FromReflect, Reflect};
use bevy::ui::{
AlignContent, AlignItems, AlignSelf, CalculatedClip, CalculatedSize, Direction, Display,
FlexDirection, FlexWrap, FocusPolicy, Interaction, JustifyContent, Overflow, PositionType,
RelativeCursorPosition, Size, Style, UiImage, UiRect, Val, ZIndex,
FlexDirection, FlexWrap, FocusPolicy, Interaction, JustifyContent, Node, Overflow,
PositionType, RelativeCursorPosition, Size, Style, UiImage, UiRect, Val, ZIndex,
};

use crate::impls::macros::{from_to_default, register_schematic};
use crate::proto::ProtoAsset;
use bevy_proto_derive::impl_external_schematic;

pub(super) fn register(app: &mut App) {
Expand All @@ -21,6 +22,7 @@ pub(super) fn register(app: &mut App) {
FocusPolicy,
Interaction,
Label,
Node,
RelativeCursorPosition,
Style,
UiImage,
Expand All @@ -47,11 +49,11 @@ impl_external_schematic! {
// ---
#[derive(Reflect, FromReflect)]
pub struct ButtonInput;
impl From<ButtonInput> for Button {
fn from(_: ButtonInput) -> Self {
Self
}
}
from_to_default!(
Button,
ButtonInput,
|_: Input| Self
);
}

impl_external_schematic! {
Expand Down Expand Up @@ -147,6 +149,19 @@ impl_external_schematic! {
}
}

impl_external_schematic! {
#[schematic(from = NodeInput)]
struct Node {}
// ---
#[derive(Reflect, FromReflect)]
pub struct NodeInput;
from_to_default!(
Node,
NodeInput,
|_: Input| Self::default()
);
}

impl_external_schematic! {
#[schematic(from = RelativeCursorPositionInput)]
struct RelativeCursorPosition {}
Expand Down Expand Up @@ -485,14 +500,26 @@ impl_external_schematic! {
}

impl_external_schematic! {
#[schematic(input(vis = pub))]
pub struct UiImage {
#[schematic(asset)]
#[schematic(asset(lazy))]
pub texture: Handle<Image>,
#[reflect(default)]
pub flip_x: bool,
#[reflect(default)]
pub flip_y: bool,
}

impl Default for UiImageInput {
fn default() -> Self {
let base = UiImage::default();
Self {
texture: ProtoAsset::HandleId(base.texture.id()),
flip_x: base.flip_x,
flip_y: base.flip_y,
}
}
}
}

impl_external_schematic! {
Expand Down
28 changes: 27 additions & 1 deletion bevy_proto_backend/src/impls/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// Registers the given types along with their respective [`ReflectSchematic`].
#[macro_export]
#[doc(hidden)]
macro_rules! register_schematic {
($app: ident, $($ty: ty),* $(,)?) => {{
$(
Expand All @@ -13,10 +15,32 @@ macro_rules! register_schematic {

pub(super) use register_schematic;

/// Implements `From` going from `$real` to `$mock`.
///
/// The `$body` should be a closure that takes in a value of type `Input`
/// and maps to `Self`.
#[macro_export]
#[doc(hidden)]
macro_rules! from {
($real: ty, $mock: ty, $body: expr) => {
const _: () = {
type Input = $mock;

impl From<Input> for $real {
fn from(value: Input) -> Self {
$body(value)
}
}
};
};
}

/// Implements `From` going from `$real` to `$mock` and vice-versa.
///
/// The `$body` should be a closure that takes in a value of type `Input`
/// and maps to `Self`.
#[macro_export]
#[doc(hidden)]
macro_rules! from_to {
($real: ty, $mock: ty, $body: expr) => {
const _: () = {
Expand Down Expand Up @@ -48,9 +72,11 @@ pub(super) use from_to;
///
/// The `$body` should be a closure that takes in a value of type `Input`
/// and maps to `Self`.
#[macro_export]
#[doc(hidden)]
macro_rules! from_to_default {
($real: ty, $mock: ty, $body: expr) => {
$crate::impls::macros::from_to!($real, $mock, $body);
$crate::from_to!($real, $mock, $body);

const _: () = {
impl Default for $mock {
Expand Down
2 changes: 1 addition & 1 deletion bevy_proto_backend/src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod bevy_impls;
pub mod bevy_impls;
mod macros;

pub(crate) fn register_impls(app: &mut bevy::app::App) {
Expand Down
3 changes: 2 additions & 1 deletion bevy_proto_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pub use plugin::*;
pub mod children;
pub mod cycles;
pub mod deps;
mod impls;
#[doc(hidden)]
pub mod impls;
pub mod load;
pub mod path;
pub mod plugin;
Expand Down
10 changes: 9 additions & 1 deletion bevy_proto_backend/src/proto/assets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::asset::AssetPath;
use bevy::asset::{Asset, AssetPath, Handle, HandleId};
use bevy::prelude::{FromReflect, Reflect};

/// Replacement type for asset handles in a [`Schematic::Input`] generated by the
Expand All @@ -10,13 +10,21 @@ use bevy::prelude::{FromReflect, Reflect};
pub enum ProtoAsset {
/// The path to an asset relative to the `assets` directory.
AssetPath(String),
/// An existing [`HandleId`].
HandleId(HandleId),
}

impl ProtoAsset {
/// Returns the [`AssetPath`] pointed to by this enum, if any.
pub fn to_asset_path(&self) -> Option<AssetPath<'_>> {
match self {
ProtoAsset::AssetPath(path) => Some(AssetPath::from(path)),
ProtoAsset::HandleId(_) => None,
}
}

/// Returns a [`ProtoAsset::HandleId`] containing a default [`Handle`] for `T`.
pub fn default_handle_id<T: Asset>() -> Self {
Self::HandleId(Handle::<T>::default().id())
}
}
Loading

0 comments on commit 088be8d

Please sign in to comment.