gled-ui 2.34.0

gled's egui interface, shared by the desktop app and the browser
Documentation
//! Binding names written from what the bindings do, so they never tell an
//! old story: "Fader 3: sets the opacity of scene 3 in the selected row",
//! "Fader 3 meter: the live opacity of …". The window shows sentences;
//! the names are for the stored asset and the engine's logs.

use gled_model::asset::midi_controller::{MidiControllerMapping, MidiOutputBindingKind};

use super::build::{Rule, build_rules};
use super::controls::Control;
use super::feedback::{FeedbackRole, feedback_role};
use super::sentence::{Plain, action_sentence, source_sentence, text};

/// Names the bindings of `rule` after its control and what they do.
pub(crate) fn name_rule(mapping: &mut MidiControllerMapping, controls: &[Control], rule: &Rule) {
    let control = &controls[rule.control];
    for (index, suffix) in [(rule.press, ""), (rule.release, " release")] {
        let Some(index) = index else {
            continue;
        };
        let sentence = action_sentence(
            &mapping.input_bindings[index].action,
            &mapping.modes,
            &Plain,
        );
        mapping.input_bindings[index].name =
            format!("{}{suffix}: {}", control.name, text(&sentence));
    }
    for &index in &rule.feedback {
        let role = feedback_role(controls, rule, &mapping.output_bindings[index].kind);
        let shows = match &mapping.output_bindings[index].kind {
            MidiOutputBindingKind::SceneColorValue(_) => "scene color".to_owned(),
            kind => kind
                .source()
                .map(|source| text(&source_sentence(source, &mapping.modes, &Plain)))
                .unwrap_or_default(),
        };
        mapping.output_bindings[index].name =
            format!("{} {}: {shows}", control.name, role_word(role));
    }
}

fn role_word(role: FeedbackRole) -> &'static str {
    match role {
        FeedbackRole::Light => "light",
        FeedbackRole::MotorFader => "motor fader",
        FeedbackRole::Meter => "meter",
        FeedbackRole::Ring => "ring",
        FeedbackRole::PadColor => "pad color",
        FeedbackRole::Value => "value",
    }
}

/// Names the bindings of the rules of `edited` controls and modes, after
/// an edit added or removed some.
pub(crate) fn name_rules_of(
    mapping: &mut MidiControllerMapping,
    controls: &[Control],
    edited: &[(usize, Option<u8>)],
) {
    for rule in build_rules(mapping, controls) {
        if edited.contains(&(rule.control, rule.mode)) {
            name_rule(mapping, controls, &rule);
        }
    }
}