mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-20 07:54:50 +00:00
Cowboy prototyping
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
|
||||
use super::{InteractionMode, InteractionTransform, Selected, TransformSpace};
|
||||
|
||||
pub(super) fn draw(
|
||||
q_selection: Query<&GlobalTransform, With<Selected>>,
|
||||
q_camera_xform: Query<&Transform, With<Camera>>,
|
||||
q_window: Query<&Window, With<PrimaryWindow>>,
|
||||
interaction_mode: Res<InteractionMode>,
|
||||
mut gizmos: Gizmos,
|
||||
mut config: ResMut<GizmoConfig>,
|
||||
) {
|
||||
if !q_selection.is_empty() {
|
||||
// TODO: Use an app state or something so we only have to set this up once
|
||||
let scale_factor = q_window.single().scale_factor() as f32;
|
||||
config.line_width = 4. * scale_factor;
|
||||
config.line_perspective = false;
|
||||
config.depth_bias = -1.;
|
||||
|
||||
let InteractionMode { transform, space } = *interaction_mode;
|
||||
let camera_xform = q_camera_xform.single();
|
||||
|
||||
// TODO: We only want to render one gizmo -- this is just for testing
|
||||
for world_xform in q_selection.iter() {
|
||||
let xform = world_xform.compute_transform();
|
||||
// Note: Scaling the gizmo size by this isn't perfect due to
|
||||
// distortion from the perspective projection. It bugged me at
|
||||
// first, but even Blender exhibits this type of gizmo distortion if
|
||||
// you dial the camera's focal length way down, so I'm gonna call it
|
||||
// good enough.
|
||||
let dist = Vec3::distance(camera_xform.translation, xform.translation);
|
||||
|
||||
if transform.contains(InteractionTransform::TRANSLATE) {
|
||||
draw_translate_gizmo(&xform, &mut gizmos, space, dist / 14.);
|
||||
}
|
||||
if transform.contains(InteractionTransform::ROTATE) {
|
||||
draw_rotate_gizmo(&xform, &mut gizmos, space, dist / 20.);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn draw_translate_gizmo(xform: &Transform, gizmos: &mut Gizmos, space: TransformSpace, size: f32) {
|
||||
let origin = xform.translation;
|
||||
let basis = match space {
|
||||
TransformSpace::Local => xform,
|
||||
TransformSpace::World => &Transform::IDENTITY,
|
||||
};
|
||||
|
||||
let mut draw_axis = |direction: Vec3, color: Color| {
|
||||
let end = origin + direction * size;
|
||||
gizmos.line(origin, end, color);
|
||||
};
|
||||
|
||||
draw_axis(basis.local_x(), Color::RED);
|
||||
draw_axis(basis.local_y(), Color::GREEN);
|
||||
draw_axis(basis.local_z(), Color::BLUE);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn draw_rotate_gizmo(xform: &Transform, gizmos: &mut Gizmos, space: TransformSpace, radius: f32) {
|
||||
let origin = xform.translation;
|
||||
let basis = match space {
|
||||
TransformSpace::Local => xform,
|
||||
TransformSpace::World => &Transform::IDENTITY,
|
||||
};
|
||||
|
||||
gizmos.circle(origin, basis.local_x(), radius, Color::RED);
|
||||
gizmos.circle(origin, basis.local_y(), radius, Color::GREEN);
|
||||
gizmos.circle(origin, basis.local_z(), radius, Color::BLUE);
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
use bevy::prelude::*;
|
||||
use bitflags::bitflags;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
mod gizmos;
|
||||
|
||||
pub struct InteractionPlugin;
|
||||
|
||||
impl Plugin for InteractionPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(SelectionMode::default())
|
||||
.insert_resource(InteractionMode::default())
|
||||
.add_systems(Update, gizmos::draw);
|
||||
|
||||
app.add_systems(
|
||||
PreUpdate,
|
||||
(wasm::update_selection_mode, wasm::update_interaction_mode),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Clone, Copy)]
|
||||
pub struct Selectable(SelectionChannel);
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[wasm_bindgen]
|
||||
pub enum SelectionChannel {
|
||||
Objects,
|
||||
Bones,
|
||||
Widgets,
|
||||
}
|
||||
|
||||
impl Default for SelectionChannel {
|
||||
fn default() -> Self {
|
||||
Self::Objects
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Clone, Copy)]
|
||||
pub struct Selected;
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct SelectionMode(SelectionChannel);
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct InteractionMode {
|
||||
pub transform: InteractionTransform,
|
||||
pub space: TransformSpace,
|
||||
}
|
||||
|
||||
impl Default for InteractionMode {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
transform: InteractionTransform::TRANSLATE | InteractionTransform::ROTATE,
|
||||
space: default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[wasm_bindgen]
|
||||
pub struct InteractionTransform(u8);
|
||||
|
||||
bitflags! {
|
||||
impl InteractionTransform: u8 {
|
||||
const TRANSLATE = 0b0001;
|
||||
const ROTATE = 0b0010;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[wasm_bindgen]
|
||||
pub enum TransformSpace {
|
||||
Local,
|
||||
World,
|
||||
}
|
||||
|
||||
impl Default for TransformSpace {
|
||||
fn default() -> Self {
|
||||
Self::Local
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(target_arch = "wasm32")]
|
||||
pub mod wasm {
|
||||
use bevy::prelude::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use crate::wasm::{self, WasmMessageBuffer};
|
||||
|
||||
use super::{
|
||||
InteractionMode, InteractionTransform, SelectionChannel, SelectionMode, TransformSpace,
|
||||
};
|
||||
|
||||
static SELECT_MODE_BUF: WasmMessageBuffer<SelectionChannel> = WasmMessageBuffer::new();
|
||||
static INTERACTION_TRANSFORM_BUF: WasmMessageBuffer<InteractionTransform> =
|
||||
WasmMessageBuffer::new();
|
||||
static INTERACTION_SPACE_BUF: WasmMessageBuffer<TransformSpace> = WasmMessageBuffer::new();
|
||||
|
||||
#[wasm_bindgen(js_name = setSelectionMode)]
|
||||
pub fn set_selection_mode(mode: SelectionChannel) {
|
||||
wasm::write_message!(SELECT_MODE_BUF, mode);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = setInteractionTransform)]
|
||||
pub fn set_interaction_transform(flags: u8) {
|
||||
wasm::write_message!(INTERACTION_TRANSFORM_BUF, InteractionTransform(flags));
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = setTransformSpace)]
|
||||
pub fn set_transform_space(space: TransformSpace) {
|
||||
wasm::write_message!(INTERACTION_SPACE_BUF, space);
|
||||
}
|
||||
|
||||
pub(super) fn update_selection_mode(mut selection_mode: ResMut<SelectionMode>) {
|
||||
wasm::read_buffer!(SELECT_MODE_BUF, mode => {
|
||||
selection_mode.0 = mode;
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn update_interaction_mode(mut interaction_mode: ResMut<InteractionMode>) {
|
||||
wasm::read_buffer!(INTERACTION_TRANSFORM_BUF, transform => {
|
||||
interaction_mode.transform = transform;
|
||||
});
|
||||
wasm::read_buffer!(INTERACTION_SPACE_BUF, space => {
|
||||
interaction_mode.space = space;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user