Skip to content

Commit

Permalink
#757 Make step of returning used props obsolete
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed Jun 4, 2023
1 parent 612a218 commit 8874b7b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 105 deletions.
2 changes: 1 addition & 1 deletion main/lib/helgoboss-learn
38 changes: 20 additions & 18 deletions main/src/domain/lua_feedback_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use helgoboss_learn::{
};
use mlua::{Function, Lua, LuaSerdeExt, Table, ToLua, Value};
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashSet;
use std::error::Error;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -101,20 +103,25 @@ impl<'a> FeedbackScript for LuaFeedbackScript<'a> {
.map_err(|e| e.to_string().into())
}

fn used_props(&self) -> Result<Vec<String>, Box<dyn Error>> {
let lua = self.lua.as_ref();
// Build input data
let context_table = {
let table = lua.create_table()?;
table.set("mode", 1)?;
table
fn used_props(&self) -> Result<HashSet<String>, Box<dyn Error>> {
let prop_provider = TrackingPropProvider::default();
let input = FeedbackScriptInput {
prop_provider: &prop_provider,
};
self.env.raw_set(self.context_key.clone(), context_table)?;
// Invoke script
let value: Value = self.function.call(())?;
// Process return value
let output: LuaScriptUsedPropsOutput = self.lua.as_ref().from_value(value)?;
Ok(output.used_props)
self.feedback_internal(input)?;
Ok(prop_provider.used_props.take())
}
}

#[derive(Default)]
struct TrackingPropProvider {
used_props: RefCell<HashSet<String>>,
}

impl PropProvider for TrackingPropProvider {
fn get_prop_value(&self, key: &str) -> Option<PropValue> {
self.used_props.borrow_mut().insert(key.to_string());
None
}
}

Expand All @@ -123,11 +130,6 @@ struct LuaScriptFeedbackOutput {
feedback_event: Option<ScriptFeedbackEvent>,
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct LuaScriptUsedPropsOutput {
used_props: Vec<String>,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
136 changes: 50 additions & 86 deletions resources/main-presets/sl-mk3-to-pot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,30 +291,23 @@ return {
feedback = {
kind = "Dynamic",
script = [[
if context.mode == 1 then
return {
used_props = {
"target.text_value",
local name = context.prop("target.text_value") or "-"
local secondary_prop_key = ]] .. serialize(secondary_prop_key) .. [[
local secondary_prop_value = secondary_prop_key and context.prop(secondary_prop_key) or ""
local available = context.prop("target.available")
return {
feedback_event = {
color = ]] .. color_string .. [[,
value = {
label = "]] .. title .. [[",
name = name,
secondary_prop = secondary_prop_value,
available = available,
}
}
else
local name = context.prop("target.text_value") or "-"
local secondary_prop_key = ]] .. serialize(secondary_prop_key) .. [[
local secondary_prop_value = secondary_prop_key and context.prop(secondary_prop_key) or ""
local available = context.prop("target.available")
return {
feedback_event = {
color = ]] .. color_string .. [[,
value = {
label = "]] .. title .. [[",
name = name,
secondary_prop = secondary_prop_value,
available = available,
}
},
}
end]],
},
}
]],
},
},
target = target,
Expand Down Expand Up @@ -372,22 +365,15 @@ return {
feedback = {
kind = "Dynamic",
script = [[
if context.mode == 1 then
return {
used_props = {
"target.available",
local available = context.prop("target.available")
return {
feedback_event = {
value = {
available = available,
}
}
else
local available = context.prop("target.available")
return {
feedback_event = {
value = {
available = available,
}
},
}
end]],
},
}
]],
},
},
target = action.target,
Expand Down Expand Up @@ -579,23 +565,15 @@ return {
feedback = {
kind = "Dynamic",
script = [[
if context.mode == 1 then
return {
used_props = {
"target.text_value",
"target.preset.name",
local preset_name = context.prop("target.preset.name")
return {
feedback_event = {
value = {
preset_name = preset_name,
}
}
else
local preset_name = context.prop("target.preset.name")
return {
feedback_event = {
value = {
preset_name = preset_name,
}
},
}
end]],
},
}
]],
},
},
target = load_preset_target,
Expand Down Expand Up @@ -719,20 +697,13 @@ return {
feedback = {
kind = "Dynamic",
script = [[
if context.mode == 1 then
return {
used_props = {
"target.text_value",
}
}
else
local bank = context.prop("target.text_value")
return {
feedback_event = {
value = "Bank: " .. (bank + 1)
},
}
end]],
local bank = context.prop("target.text_value")
return {
feedback_event = {
value = "Bank: " .. (bank + 1)
},
}
]],
},
},
target = {
Expand Down Expand Up @@ -919,25 +890,18 @@ return {
feedback = {
kind = "Dynamic",
script = [[
if context.mode == 1 then
return {
used_props = {
"target.text_value",
return {
feedback_event = {
value = {
section_name = context.prop("target.fx_parameter.macro.new_section.name"),
macro_name = context.prop("target.fx_parameter.macro.name"),
param_value = context.prop("y"),
param_value_label = context.prop("target.text_value"),
param_name = context.prop("target.fx_parameter.name"),
}
}
else
return {
feedback_event = {
value = {
section_name = context.prop("target.fx_parameter.macro.new_section.name"),
macro_name = context.prop("target.fx_parameter.macro.name"),
param_value = context.prop("y"),
param_value_label = context.prop("target.text_value"),
param_name = context.prop("target.fx_parameter.name"),
}
},
}
end]],
},
}
]],
},
},
target = {
Expand Down

0 comments on commit 8874b7b

Please sign in to comment.