Skip to content

Commit

Permalink
begin wired-input implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Jun 27, 2024
1 parent c05bf78 commit f0ae009
Show file tree
Hide file tree
Showing 20 changed files with 3,197 additions and 485 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

45 changes: 45 additions & 0 deletions crates/unavi-scripting/examples/wired-input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use bevy::prelude::*;
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};
use unavi_scripting::{ScriptBundle, ScriptingPlugin};

fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(AssetPlugin {
file_path: "../unavi-app/assets".to_string(),
..Default::default()
}),
PanOrbitCameraPlugin,
ScriptingPlugin,
))
.add_systems(Startup, (setup_scene, load_script))
.run();
}

fn setup_scene(mut ambient: ResMut<AmbientLight>, mut commands: Commands) {
ambient.brightness = 100.0;
ambient.color = Color::rgb(0.95, 0.95, 1.0);

commands.spawn(DirectionalLightBundle {
transform: Transform::from_xyz(4.5, 10.0, -7.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});

let mut transform = Transform::from_xyz(0.0, 3.0, -10.0);
transform.look_at(Vec3::new(0.0, 0.5, 0.0), Vec3::new(0.0, 1.0, 0.0));

commands.spawn((
Camera3dBundle {
transform,
..Default::default()
},
PanOrbitCamera::default(),
));
}

pub fn load_script(asset_server: Res<AssetServer>, mut commands: Commands) {
commands.spawn((
ScriptBundle::load("example:wired-input", &asset_server),
SpatialBundle::default(),
));
}
12 changes: 11 additions & 1 deletion crates/unavi-scripting/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use wasm_bridge::component::Linker;
use super::state::StoreState;

pub mod wired_gltf;
mod wired_input;
mod wired_log;

pub fn add_host_script_apis(linker: &mut Linker<StoreState>) -> Result<()> {
wired_log::add_to_host(linker)?;
wired_gltf::add_to_host(linker)?;
wired_input::add_to_host(linker)?;
wired_log::add_to_host(linker)?;
Ok(())
}

Expand Down Expand Up @@ -114,4 +116,12 @@ mod tests {
assert!(!logs_contain("ERROR"));
assert!(!logs_contain("error"));
}

#[tokio::test]
#[traced_test]
async fn example_wired_input() {
test_script("example:wired-input").await;
assert!(!logs_contain("ERROR"));
assert!(!logs_contain("error"));
}
}
4 changes: 4 additions & 0 deletions crates/unavi-scripting/src/host/wired_gltf/bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use bevy::utils::HashSet;
use wasm_bridge::component::Resource;
use wired::math::types::{Quat, Transform, Vec3};

use crate::host::wired_input::handler::SpatialHandler;

use self::wired::gltf::material::Color;

wasm_bridge::component::bindgen!({
Expand Down Expand Up @@ -45,6 +48,7 @@ pub struct Primitive {
#[derive(Default)]
pub struct Node {
pub children: HashSet<u32>,
pub handlers: Vec<Resource<SpatialHandler>>,
pub mesh: Option<u32>,
pub name: String,
pub parent: Option<u32>,
Expand Down
34 changes: 34 additions & 0 deletions crates/unavi-scripting/src/host/wired_input/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use wasm_bridge::component::Resource;

use crate::state::StoreState;

use super::wired::input::{
handler::{HostSpatialHandler, Node},
types::InputEvent,
};

pub struct SpatialHandler;

impl HostSpatialHandler for StoreState {
fn new(&mut self, node: Resource<Node>) -> wasm_bridge::Result<Resource<SpatialHandler>> {
let handler = SpatialHandler;
let res = self.table.push_child(handler, &node)?;
let rep = res.rep();

let node = self.table.get_mut(&node)?;
node.handlers.push(res);

Ok(Resource::new_own(rep))
}

fn handle_input(
&mut self,
_self_: Resource<SpatialHandler>,
) -> wasm_bridge::Result<Option<InputEvent>> {
Ok(None)
}

fn drop(&mut self, _rep: Resource<SpatialHandler>) -> wasm_bridge::Result<()> {
Ok(())
}
}
26 changes: 26 additions & 0 deletions crates/unavi-scripting/src/host/wired_input/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anyhow::Result;
use wasm_bridge::component::Linker;
use wired::input::handler::Host;

use crate::state::StoreState;

pub mod handler;

wasm_bridge::component::bindgen!({
path: "../../wired-protocol/spatial/wit/wired-input",
world: "host",
with: {
"wired:gltf/material/material": super::wired_gltf::bindgen::Material,
"wired:gltf/mesh/mesh": super::wired_gltf::bindgen::Mesh,
"wired:gltf/mesh/primitive": super::wired_gltf::bindgen::Primitive,
"wired:gltf/node/node": super::wired_gltf::bindgen::Node,
"wired:input/handler/spatial-handler": handler::SpatialHandler,
}
});

impl Host for StoreState {}

pub fn add_to_host(linker: &mut Linker<StoreState>) -> Result<()> {
wired::input::handler::add_to_linker(linker, |s| s)?;
Ok(())
}
26 changes: 0 additions & 26 deletions crates/unavi-scripting/src/host/wired_math/mod.rs

This file was deleted.

178 changes: 0 additions & 178 deletions crates/unavi-scripting/src/host/wired_math/quat.rs

This file was deleted.

Loading

0 comments on commit f0ae009

Please sign in to comment.