//! Enable controls for morph targets detected in a loaded scene. //! //! Collect morph targets and assign keys to them, //! shows on screen additional controls for morph targets. //! //! Illustrates how to access and modify individual morph target weights. //! See the [`update_morphs`] system for details. //! //! Also illustrates how to read morph target names in [`detect_morphs`]. use crate::scene_viewer_plugin::SceneHandle; use bevy::prelude::*; use std::fmt; const WEIGHT_PER_SECOND: f32 = 0.8; const ALL_MODIFIERS: &[KeyCode] = &[KeyCode::ShiftLeft, KeyCode::ControlLeft, KeyCode::AltLeft]; const AVAILABLE_KEYS: [MorphKey; 56] = [ MorphKey::new("r", &[], KeyCode::R), MorphKey::new("t", &[], KeyCode::T), MorphKey::new("z", &[], KeyCode::Z), MorphKey::new("i", &[], KeyCode::I), MorphKey::new("o", &[], KeyCode::O), MorphKey::new("p", &[], KeyCode::P), MorphKey::new("f", &[], KeyCode::F), MorphKey::new("g", &[], KeyCode::G), MorphKey::new("h", &[], KeyCode::H), MorphKey::new("j", &[], KeyCode::J), MorphKey::new("k", &[], KeyCode::K), MorphKey::new("y", &[], KeyCode::Y), MorphKey::new("x", &[], KeyCode::X), MorphKey::new("c", &[], KeyCode::C), MorphKey::new("v", &[], KeyCode::V), MorphKey::new("b", &[], KeyCode::B), MorphKey::new("n", &[], KeyCode::N), MorphKey::new("m", &[], KeyCode::M), MorphKey::new("0", &[], KeyCode::Key0), MorphKey::new("1", &[], KeyCode::Key1), MorphKey::new("2", &[], KeyCode::Key2), MorphKey::new("3", &[], KeyCode::Key3), MorphKey::new("4", &[], KeyCode::Key4), MorphKey::new("5", &[], KeyCode::Key5), MorphKey::new("6", &[], KeyCode::Key6), MorphKey::new("7", &[], KeyCode::Key7), MorphKey::new("8", &[], KeyCode::Key8), MorphKey::new("9", &[], KeyCode::Key9), MorphKey::new("lshift-R", &[KeyCode::ShiftLeft], KeyCode::R), MorphKey::new("lshift-T", &[KeyCode::ShiftLeft], KeyCode::T), MorphKey::new("lshift-Z", &[KeyCode::ShiftLeft], KeyCode::Z), MorphKey::new("lshift-I", &[KeyCode::ShiftLeft], KeyCode::I), MorphKey::new("lshift-O", &[KeyCode::ShiftLeft], KeyCode::O), MorphKey::new("lshift-P", &[KeyCode::ShiftLeft], KeyCode::P), MorphKey::new("lshift-F", &[KeyCode::ShiftLeft], KeyCode::F), MorphKey::new("lshift-G", &[KeyCode::ShiftLeft], KeyCode::G), MorphKey::new("lshift-H", &[KeyCode::ShiftLeft], KeyCode::H), MorphKey::new("lshift-J", &[KeyCode::ShiftLeft], KeyCode::J), MorphKey::new("lshift-K", &[KeyCode::ShiftLeft], KeyCode::K), MorphKey::new("lshift-Y", &[KeyCode::ShiftLeft], KeyCode::Y), MorphKey::new("lshift-X", &[KeyCode::ShiftLeft], KeyCode::X), MorphKey::new("lshift-C", &[KeyCode::ShiftLeft], KeyCode::C), MorphKey::new("lshift-V", &[KeyCode::ShiftLeft], KeyCode::V), MorphKey::new("lshift-B", &[KeyCode::ShiftLeft], KeyCode::B), MorphKey::new("lshift-N", &[KeyCode::ShiftLeft], KeyCode::N), MorphKey::new("lshift-M", &[KeyCode::ShiftLeft], KeyCode::M), MorphKey::new("lshift-0", &[KeyCode::ShiftLeft], KeyCode::Key0), MorphKey::new("lshift-1", &[KeyCode::ShiftLeft], KeyCode::Key1), MorphKey::new("lshift-2", &[KeyCode::ShiftLeft], KeyCode::Key2), MorphKey::new("lshift-3", &[KeyCode::ShiftLeft], KeyCode::Key3), MorphKey::new("lshift-4", &[KeyCode::ShiftLeft], KeyCode::Key4), MorphKey::new("lshift-5", &[KeyCode::ShiftLeft], KeyCode::Key5), MorphKey::new("lshift-6", &[KeyCode::ShiftLeft], KeyCode::Key6), MorphKey::new("lshift-7", &[KeyCode::ShiftLeft], KeyCode::Key7), MorphKey::new("lshift-8", &[KeyCode::ShiftLeft], KeyCode::Key8), MorphKey::new("lshift-9", &[KeyCode::ShiftLeft], KeyCode::Key9), ]; #[derive(Clone, Copy)] enum WeightChange { Increase, Decrease, } impl WeightChange { fn reverse(&mut self) { *self = match *self { WeightChange::Increase => WeightChange::Decrease, WeightChange::Decrease => WeightChange::Increase, } } fn sign(self) -> f32 { match self { WeightChange::Increase => 1.0, WeightChange::Decrease => -1.0, } } fn change_weight(&mut self, weight: f32, change: f32) -> f32 { let mut change = change * self.sign(); let new_weight = weight + change; if new_weight <= 0.0 || new_weight >= 1.0 { self.reverse(); change = -change; } weight + change } } struct Target { entity_name: Option, entity: Entity, name: Option, index: usize, weight: f32, change_dir: WeightChange, } impl fmt::Display for Target { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match (self.name.as_ref(), self.entity_name.as_ref()) { (None, None) => write!(f, "animation{} of {:?}", self.index, self.entity), (None, Some(entity)) => write!(f, "animation{} of {entity}", self.index), (Some(target), None) => write!(f, "{target} of {:?}", self.entity), (Some(target), Some(entity)) => write!(f, "{target} of {entity}"), }?; write!(f, ": {}", self.weight) } } impl Target { fn text_section(&self, key: &str, style: TextStyle) -> TextSection { TextSection::new(format!("[{key}] {self}\n"), style) } fn new( entity_name: Option<&Name>, weights: &[f32], target_names: Option<&[String]>, entity: Entity, ) -> Vec { let get_name = |i| target_names.and_then(|names| names.get(i)); let entity_name = entity_name.map(|n| n.as_str()); weights .iter() .enumerate() .map(|(index, weight)| Target { entity_name: entity_name.map(|n| n.to_owned()), entity, name: get_name(index).cloned(), index, weight: *weight, change_dir: WeightChange::Increase, }) .collect() } } #[derive(Resource)] struct WeightsControl { weights: Vec, } struct MorphKey { name: &'static str, modifiers: &'static [KeyCode], key: KeyCode, } impl MorphKey { const fn new(name: &'static str, modifiers: &'static [KeyCode], key: KeyCode) -> Self { MorphKey { name, modifiers, key, } } fn active(&self, inputs: &Input) -> bool { let mut modifier = self.modifiers.iter(); let mut non_modifier = ALL_MODIFIERS.iter().filter(|m| !self.modifiers.contains(m)); let key = inputs.pressed(self.key); let modifier = modifier.all(|m| inputs.pressed(*m)); let non_modifier = non_modifier.all(|m| !inputs.pressed(*m)); key && modifier && non_modifier } } fn update_text( controls: Option>, mut text: Query<&mut Text>, morphs: Query<&MorphWeights>, ) { let Some(mut controls) = controls else { return; }; for (i, target) in controls.weights.iter_mut().enumerate() { let Ok(weights) = morphs.get(target.entity) else { continue; }; let Some(&actual_weight) = weights.weights().get(target.index) else { continue; }; if actual_weight != target.weight { target.weight = actual_weight; } let key_name = &AVAILABLE_KEYS[i].name; let mut text = text.single_mut(); text.sections[i + 2].value = format!("[{key_name}] {target}\n"); } } fn update_morphs( controls: Option>, mut morphs: Query<&mut MorphWeights>, input: Res>, time: Res