Skip to content

Commit

Permalink
Extend morph controls to 56 targets
Browse files Browse the repository at this point in the history
  • Loading branch information
nicopap committed Apr 12, 2023
1 parent c086792 commit e291dde
Showing 1 changed file with 85 additions and 35 deletions.
120 changes: 85 additions & 35 deletions examples/tools/scene_viewer/morph_viewer_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl fmt::Display for Target {
}
}
impl Target {
fn text_section(&self, key: char, style: TextStyle) -> TextSection {
fn text_section(&self, key: &str, style: TextStyle) -> TextSection {
TextSection::new(format!("[{key}] {self}\n"), style)
}
fn new(
Expand Down Expand Up @@ -108,36 +108,87 @@ struct WeightsControl {

const WEIGHT_PER_SECOND: f32 = 0.8;

// TODO(#8158) uppercase (total animation count is 64)
const AVAILABLE_KEYS: [(char, KeyCode); 28] = [
('r', KeyCode::R),
('t', KeyCode::T),
('z', KeyCode::Z),
('i', KeyCode::I),
('o', KeyCode::O),
('p', KeyCode::P),
('f', KeyCode::F),
('g', KeyCode::G),
('h', KeyCode::H),
('j', KeyCode::J),
('k', KeyCode::K),
('y', KeyCode::Y),
('x', KeyCode::X),
('c', KeyCode::C),
('v', KeyCode::V),
('b', KeyCode::B),
('n', KeyCode::N),
('m', KeyCode::M),
('0', KeyCode::Key0),
('1', KeyCode::Key1),
('2', KeyCode::Key2),
('3', KeyCode::Key3),
('4', KeyCode::Key4),
('5', KeyCode::Key5),
('6', KeyCode::Key6),
('7', KeyCode::Key7),
('8', KeyCode::Key8),
('9', KeyCode::Key9),
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<KeyCode>) -> 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
}
}
const ALL_MODIFIERS: &[KeyCode] = &[KeyCode::LShift, KeyCode::LControl, KeyCode::LAlt];
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::LShift], KeyCode::R),
MorphKey::new("lshift-T", &[KeyCode::LShift], KeyCode::T),
MorphKey::new("lshift-Z", &[KeyCode::LShift], KeyCode::Z),
MorphKey::new("lshift-I", &[KeyCode::LShift], KeyCode::I),
MorphKey::new("lshift-O", &[KeyCode::LShift], KeyCode::O),
MorphKey::new("lshift-P", &[KeyCode::LShift], KeyCode::P),
MorphKey::new("lshift-F", &[KeyCode::LShift], KeyCode::F),
MorphKey::new("lshift-G", &[KeyCode::LShift], KeyCode::G),
MorphKey::new("lshift-H", &[KeyCode::LShift], KeyCode::H),
MorphKey::new("lshift-J", &[KeyCode::LShift], KeyCode::J),
MorphKey::new("lshift-K", &[KeyCode::LShift], KeyCode::K),
MorphKey::new("lshift-Y", &[KeyCode::LShift], KeyCode::Y),
MorphKey::new("lshift-X", &[KeyCode::LShift], KeyCode::X),
MorphKey::new("lshift-C", &[KeyCode::LShift], KeyCode::C),
MorphKey::new("lshift-V", &[KeyCode::LShift], KeyCode::V),
MorphKey::new("lshift-B", &[KeyCode::LShift], KeyCode::B),
MorphKey::new("lshift-N", &[KeyCode::LShift], KeyCode::N),
MorphKey::new("lshift-M", &[KeyCode::LShift], KeyCode::M),
MorphKey::new("lshift-0", &[KeyCode::LShift], KeyCode::Key0),
MorphKey::new("lshift-1", &[KeyCode::LShift], KeyCode::Key1),
MorphKey::new("lshift-2", &[KeyCode::LShift], KeyCode::Key2),
MorphKey::new("lshift-3", &[KeyCode::LShift], KeyCode::Key3),
MorphKey::new("lshift-4", &[KeyCode::LShift], KeyCode::Key4),
MorphKey::new("lshift-5", &[KeyCode::LShift], KeyCode::Key5),
MorphKey::new("lshift-6", &[KeyCode::LShift], KeyCode::Key6),
MorphKey::new("lshift-7", &[KeyCode::LShift], KeyCode::Key7),
MorphKey::new("lshift-8", &[KeyCode::LShift], KeyCode::Key8),
MorphKey::new("lshift-9", &[KeyCode::LShift], KeyCode::Key9),
];

fn update_text(
Expand All @@ -156,7 +207,7 @@ fn update_text(
if actual_weight != target.weight {
target.weight = actual_weight;
}
let key_name = AVAILABLE_KEYS[i].0;
let key_name = &AVAILABLE_KEYS[i].name;
let mut text = text.single_mut();
text.sections[i + 2].value = format!("[{key_name}] {target}\n");
}
Expand All @@ -169,8 +220,7 @@ fn update_morphs(
) {
let Some(mut controls) = controls else { return; };
for (i, target) in controls.weights.iter_mut().enumerate() {
let key = AVAILABLE_KEYS[i].1;
if !input.pressed(key) {
if !AVAILABLE_KEYS[i].active(&input) {
continue;
}
let Ok(mut weights) = morphs.get_mut(target.entity) else {
Expand Down Expand Up @@ -231,7 +281,7 @@ fn detect_morphs(
TextSection::new("---------------\n", style.clone()),
];
let target_to_text =
|(i, target): (usize, &Target)| target.text_section(AVAILABLE_KEYS[i].0, style.clone());
|(i, target): (usize, &Target)| target.text_section(AVAILABLE_KEYS[i].name, style.clone());
sections.extend(detected.iter().enumerate().map(target_to_text));
commands.insert_resource(WeightsControl { weights: detected });
commands.spawn(TextBundle::from_sections(sections).with_style(Style {
Expand Down

0 comments on commit e291dde

Please sign in to comment.