Skip to content

Commit

Permalink
#858 Allow MIDI scripts to access compartment Lua even when just calc…
Browse files Browse the repository at this point in the history
…ulating feedback address
  • Loading branch information
helgoboss committed Feb 14, 2024
1 parent 6de8513 commit 228435b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
2 changes: 1 addition & 1 deletion main/lib/helgoboss-learn
48 changes: 34 additions & 14 deletions main/src/domain/main_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,10 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
}
if let Some(mapping_with_source) = self.all_mappings().find(|m| {
m.feedback_is_effectively_on()
&& m.source()
.has_same_feedback_address_as_value(&released_event.feedback_value)
&& m.source().has_same_feedback_address_as_value(
&released_event.feedback_value,
self.basics.source_context(m.compartment()),
)
}) {
if let Some(followed_mapping) = self.follow_maybe_virtual_mapping(mapping_with_source) {
if self.basics.instance_feedback_is_effectively_enabled() {
Expand Down Expand Up @@ -1118,7 +1120,10 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
}
if m.feedback_is_effectively_on() {
// Mark source as used
if let Some(addr) = m.source().extract_feedback_address() {
if let Some(addr) = m
.source()
.extract_feedback_address(self.basics.source_context(m.compartment()))
{
unused_sources.remove(&addr);
}
}
Expand All @@ -1142,7 +1147,10 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
continue;
}
// Mark source as used
if let Some(addr) = m.source().extract_feedback_address() {
if let Some(addr) = m
.source()
.extract_feedback_address(self.basics.source_context(m.compartment()))
{
unused_sources.remove(&addr);
}
}
Expand Down Expand Up @@ -1194,7 +1202,10 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
}
if m.feedback_is_effectively_on() {
// Mark source as used
if let Some(addr) = m.source().extract_feedback_address() {
if let Some(addr) = m
.source()
.extract_feedback_address(self.basics.source_context(compartment))
{
unused_sources.remove(&addr);
}
}
Expand Down Expand Up @@ -1400,7 +1411,10 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
}
if m.feedback_is_effectively_on() {
// Mark source as used
if let Some(addr) = m.source().extract_feedback_address() {
if let Some(addr) = m
.source()
.extract_feedback_address(self.basics.source_context(m.compartment()))
{
unused_sources.remove(&addr);
}
}
Expand Down Expand Up @@ -1483,7 +1497,10 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
);
if m.feedback_is_effectively_on() {
// Mark source as used
if let Some(addr) = m.source().extract_feedback_address() {
if let Some(addr) = m
.source()
.extract_feedback_address(self.basics.source_context(m.compartment()))
{
unused_sources.remove(&addr);
}
}
Expand Down Expand Up @@ -2385,8 +2402,9 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
.filter(|m| m.feedback_is_effectively_on())
.filter_map(|m| {
Some((
m.source().extract_feedback_address()?,
m.off_feedback(self.basics.source_context(m.compartment()), NoopLogger)?,
m.source()
.extract_feedback_address(self.basics.source_context(compartment))?,
m.off_feedback(self.basics.source_context(compartment), NoopLogger)?,
))
})
.collect()
Expand All @@ -2396,7 +2414,9 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
.filter(|m| m.feedback_is_effectively_on())
.filter_map(|m| {
Some((
m.source().extract_feedback_address()?,
m.source().extract_feedback_address(
self.basics.source_context(m.compartment()),
)?,
m.off_feedback(self.basics.source_context(m.compartment()), NoopLogger)?,
))
})
Expand Down Expand Up @@ -2658,10 +2678,10 @@ impl<EH: DomainEventHandler> MainProcessor<EH> {
// An existing mapping is being overwritten.
if previous_mapping.feedback_is_effectively_on() {
// And its light is currently on.
if mapping
.source()
.has_same_feedback_address_as_source(previous_mapping.source())
{
if mapping.source().has_same_feedback_address_as_source(
previous_mapping.source(),
self.basics.source_context(mapping.compartment()),
) {
// Source is the same.
if mapping.feedback_is_effectively_on() {
// Lights should still be on.
Expand Down
25 changes: 19 additions & 6 deletions main/src/domain/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1622,11 +1622,14 @@ impl CompoundMappingSource {
// TODO-medium There are quite some places in which we are fine with a borrowed version but
// the problem is the MIDI source can't simply give us a borrowed one. Maybe we should
// create one at MIDI source creation time! But for this we need to make MidiSource a struct.
pub fn extract_feedback_address(&self) -> Option<CompoundMappingSourceAddress> {
pub fn extract_feedback_address(
&self,
source_context: RealearnSourceContext,
) -> Option<CompoundMappingSourceAddress> {
use CompoundMappingSource::*;
match self {
Midi(s) => Some(CompoundMappingSourceAddress::Midi(
s.extract_feedback_address()?,
s.extract_feedback_address(source_context)?,
)),
Osc(s) => Some(CompoundMappingSourceAddress::Osc(
s.feedback_address().clone(),
Expand All @@ -1644,11 +1647,17 @@ impl CompoundMappingSource {
/// Used for:
///
/// - Source takeover (feedback)
pub fn has_same_feedback_address_as_value(&self, value: &FinalSourceFeedbackValue) -> bool {
pub fn has_same_feedback_address_as_value(
&self,
value: &FinalSourceFeedbackValue,
source_context: RealearnSourceContext,
) -> bool {
use CompoundMappingSource::*;
match (self, value) {
(Osc(s), FinalSourceFeedbackValue::Osc(v)) => s.has_same_feedback_address_as_value(v),
(Midi(s), FinalSourceFeedbackValue::Midi(v)) => s.has_same_feedback_address_as_value(v),
(Midi(s), FinalSourceFeedbackValue::Midi(v)) => {
s.has_same_feedback_address_as_value(v, source_context)
}
_ => false,
}
}
Expand All @@ -1658,11 +1667,15 @@ impl CompoundMappingSource {
/// Used for:
///
/// - Feedback diffing
pub fn has_same_feedback_address_as_source(&self, other: &Self) -> bool {
pub fn has_same_feedback_address_as_source(
&self,
other: &Self,
source_context: RealearnSourceContext,
) -> bool {
use CompoundMappingSource::*;
match (self, other) {
(Osc(s1), Osc(s2)) => s1.has_same_feedback_address_as_source(s2),
(Midi(s1), Midi(s2)) => s1.has_same_feedback_address_as_source(s2),
(Midi(s1), Midi(s2)) => s1.has_same_feedback_address_as_source(s2, source_context),
(Virtual(s1), Virtual(s2)) => s1.has_same_feedback_address_as_source(s2),
_ => false,
}
Expand Down

0 comments on commit 228435b

Please sign in to comment.