use std::{
any::{TypeId, type_name},
collections::HashMap,
};
use bevy_core_pipeline::{prelude::*, tonemapping::Tonemapping};
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{prelude::*, query::QuerySingleError, system::SystemParam};
use bevy_gizmos::gizmos::Gizmos;
use bevy_pbr::{AmbientLight, DirectionalLight, MeshMaterial3d, StandardMaterial};
use bevy_reflect::Reflect;
use bevy_render::{
camera::{Exposure, ScalingMode},
prelude::*,
view::RenderLayers,
};
use bevy_color::palettes::css::{LIME, WHITE};
use bevy_ecs::resource::Resource;
use bevy_math::{Isometry3d, Quat, Rect, Vec2, Vec3};
use bevy_transform::components::Transform;
use bevy_utils::default;
use bytemuck::TransparentWrapper;
use tracing::warn;
use utils::*;
mod cursor;
use cursor::{CursorCommand, update_cursor_command};
mod keyboard;
use keyboard::{KeyboardCommand, update_keyboard_command};
pub mod components;
pub use components::*;
pub mod plugins;
pub use plugins::*;
pub mod resources;
pub use resources::*;
mod systems;
pub(crate) use systems::*;
pub(crate) mod utils;
pub const GENERAL_RENDER_LAYER: usize = 0;
pub const PHYSICAL_RENDER_LAYER: usize = 1;
pub const VISUAL_CUE_RENDER_LAYER: usize = 2;
pub const SELECTED_OUTLINE_LAYER: usize = 3;
pub const HOVERED_OUTLINE_LAYER: usize = 4;
pub const MODEL_PREVIEW_LAYER: usize = 6;
pub const XRAY_RENDER_LAYER: usize = 5;
#[derive(Resource, Clone, Default)]
pub struct UserCameraDisplay {
pub region: Rect,
}
pub fn main_view_render_layers() -> RenderLayers {
RenderLayers::from_layers(&[
GENERAL_RENDER_LAYER,
PHYSICAL_RENDER_LAYER,
VISUAL_CUE_RENDER_LAYER,
SELECTED_OUTLINE_LAYER,
HOVERED_OUTLINE_LAYER,
XRAY_RENDER_LAYER,
])
}
pub const DEFAULT_CAMERA_EV100: f32 = 3.5;
pub const MIN_FOV: f32 = 5.0;
pub const MAX_FOV: f32 = 120.0;
pub const MIN_SCALE: f32 = 0.5;
pub const MAX_SCALE: f32 = 500.0;
pub const MAX_PITCH: f32 = 85.0;
pub const MIN_SELECTION_DIST: f32 = 10.0;
pub const MAX_SELECTION_DIST: f32 = 30.0;
#[derive(PartialEq, Debug, Copy, Clone, Reflect)]
pub enum CameraCommandType {
Inactive,
Pan,
Orbit,
TranslationZoom,
ScaleZoom,
FovZoom,
}
#[derive(Component)]
#[component(immutable)]
pub struct CameraTarget {
pub point: Vec3,
}
#[derive(Clone, Copy, Resource)]
pub struct PanToElement {
pub target: Option<Entity>,
pub interruptible: bool,
pub persistent: bool,
}
impl Default for PanToElement {
fn default() -> Self {
Self {
target: None,
interruptible: true,
persistent: false,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Deref, DerefMut, Resource)]
pub struct HeadlightToggle(pub bool);
impl Default for HeadlightToggle {
fn default() -> Self {
Self(true)
}
}
#[derive(SystemParam)]
pub struct ActiveCameraQuery<'w, 's> {
pub proj_mode: Res<'w, ProjectionMode>,
pub ortho_cam: Query<'w, 's, Entity, With<OrthographicCameraRoot>>,
pub persp_cam: Query<'w, 's, Entity, With<PerspectiveCameraRoot>>,
}
pub fn active_camera_maybe(active_cam: &ActiveCameraQuery) -> Result<Entity, QuerySingleError> {
match *active_cam.proj_mode {
ProjectionMode::Perspective => active_cam
.persp_cam
.single()
.inspect_err(|err| warn!("could not get active camera due to: {:#}", err)),
ProjectionMode::Orthographic => active_cam
.ortho_cam
.single()
.inspect_err(|err| warn!("could not get active camera due to: {:#}", err)),
}
}
#[derive(Reflect, Hash, Clone, PartialEq, Eq, Debug)]
#[reflect(Clone)]
pub struct TypeInfo {
#[reflect(ignore)]
type_id: Option<TypeId>,
type_name: String,
type_name_short: String,
}
impl TypeInfo {
pub fn new<T: 'static>() -> Self {
let type_name = type_name::<T>();
let type_name_short = type_name.split("::").last().unwrap_or("???");
Self {
type_id: Some(TypeId::of::<T>()),
type_name: type_name.to_string(),
type_name_short: type_name_short.to_string(),
}
}
pub fn type_id(&self) -> TypeId {
self.type_id.unwrap()
}
pub fn type_name(&self) -> &String {
&self.type_name
}
pub fn type_name_short(&self) -> &String {
&self.type_name_short
}
}
pub type CameraControlsBlocker<T> = BlockerRegistration<T, CameraControlBlockers>;
pub(crate) fn update_blocker_registry<T, U>(blocker_registry: ResMut<U>, camera_blocker: Res<T>)
where
T: Resource + TransparentWrapper<bool>,
U: Resource + TransparentWrapper<HashMap<TypeInfo, bool>>,
{
let blocker_registry = U::peel_mut(blocker_registry.into_inner());
let blocker = T::peel_ref(camera_blocker.into_inner());
let type_info = TypeInfo::new::<T>();
if blocker == &true {
let blocked = blocker_registry.entry(type_info).or_insert(true);
*blocked = true;
} else {
let blocked = blocker_registry.entry(type_info).or_insert(false);
*blocked = false;
}
}
pub(crate) fn set_block_status<U>(block_status: ResMut<BlockStatus<U>>, blocker_registry: Res<U>)
where
U: Resource + TransparentWrapper<HashMap<TypeInfo, bool>>,
{
let block_status = BlockStatus::<U>::peel_mut(block_status.into_inner());
let blocker_registry = U::peel_ref(blocker_registry.into_inner());
*block_status = blocker_registry.iter().any(|(_, blocker)| blocker == &true);
}