|
| 1 | +use crate::{ |
| 2 | + midi, |
| 3 | + widget::{ByTitle, MidiControllerBoolUdp, MidiControllerU7Udp, Widget}, |
| 4 | +}; |
| 5 | +use caw_core::{Sig, SigShared}; |
| 6 | +use caw_midi::MidiMessagesT; |
| 7 | +use lazy_static::lazy_static; |
| 8 | + |
| 9 | +pub type NumKeysBits7WithSpace = ( |
| 10 | + Sig<SigShared<MidiControllerU7Udp>>, |
| 11 | + Sig<SigShared<MidiControllerBoolUdp>>, |
| 12 | +); |
| 13 | + |
| 14 | +lazy_static! { |
| 15 | + // Used to prevent multiple windows from opening at the same time with the same name. Note that |
| 16 | + // when using with evcxr this seems to get cleared when a cell is recomputed, but this is still |
| 17 | + // valuable in the context of stereo signals, where a function is evaluated once for the left |
| 18 | + // channel and once for the right channel. In such a case, this prevents each knob openned by |
| 19 | + // that function from being openned twice. |
| 20 | + static ref BY_TITLE: ByTitle<NumKeysBits7WithSpace> = Default::default(); |
| 21 | +} |
| 22 | + |
| 23 | +fn new_num_keys_bits_7_with_space(title: String) -> NumKeysBits7WithSpace { |
| 24 | + BY_TITLE.get_or_insert(title.as_str(), || { |
| 25 | + let channel = midi::alloc_channel(); |
| 26 | + let controller = midi::alloc_controller(channel); |
| 27 | + let space_controller = midi::alloc_controller(channel); |
| 28 | + let widget = Widget::new( |
| 29 | + title.clone(), |
| 30 | + channel, |
| 31 | + "num-keys-bits7", |
| 32 | + vec![ |
| 33 | + format!("--controller={}", controller), |
| 34 | + format!("--space-controller={}", space_controller), |
| 35 | + ], |
| 36 | + ) |
| 37 | + .unwrap(); |
| 38 | + let controllers = widget.channel().controllers(); |
| 39 | + let value = controllers.get_u7(controller.into()).shared(); |
| 40 | + let space = controllers.get_bool(space_controller.into()).shared(); |
| 41 | + (value, space) |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +fn new_num_keys_bits_7(title: String) -> Sig<SigShared<MidiControllerU7Udp>> { |
| 46 | + new_num_keys_bits_7_with_space(title).0 |
| 47 | +} |
| 48 | + |
| 49 | +mod builder { |
| 50 | + use super::*; |
| 51 | + use caw_builder_proc_macros::builder; |
| 52 | + use caw_core::{Sig, SigShared}; |
| 53 | + |
| 54 | + builder! { |
| 55 | + #[constructor = "num_keys_bits_7_"] |
| 56 | + #[constructor_doc = "A 7-bit integer representing the state of the first 7 number keys"] |
| 57 | + #[generic_setter_type_name = "X"] |
| 58 | + #[build_fn = "new_num_keys_bits_7"] |
| 59 | + #[build_ty = "Sig<SigShared<MidiControllerU7Udp>>"] |
| 60 | + pub struct Props { |
| 61 | + title: String, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + pub fn num_keys_bits_7(title: impl Into<String>) -> Props { |
| 66 | + num_keys_bits_7_(title.into()) |
| 67 | + } |
| 68 | + |
| 69 | + builder! { |
| 70 | + #[constructor = "num_keys_bits_7_with_space_"] |
| 71 | + #[constructor_doc = "A 7-bit integer representing the state of the first 7 number keys"] |
| 72 | + #[generic_setter_type_name = "X"] |
| 73 | + #[build_fn = "new_num_keys_bits_7_with_space"] |
| 74 | + #[build_ty = "NumKeysBits7WithSpace"] |
| 75 | + pub struct WithSpaceProps { |
| 76 | + title: String, |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + pub fn num_keys_bits_7_with_space( |
| 81 | + title: impl Into<String>, |
| 82 | + ) -> WithSpaceProps { |
| 83 | + num_keys_bits_7_with_space_(title.into()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +pub use builder::{num_keys_bits_7, num_keys_bits_7_with_space}; |
0 commit comments