public
Created

S+H and save

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/*
 
Drop in replacement for S+H (Spreads)
But this node saves the current state in its own config pin
So when you reload the patch, S+H returns to the same state when you closed the patch.
 
This likely works a little different from the existing s+h node in edge cases, but in most situations it should be identical.
Also it's super simple
 
To use:
clone Template (Value) with Ctrl+enter, and paste this code into the code window
Hit save
If your node turns red, then check again in NodeBrowser for the new node :)
*/
 
#region usings
using System;
using System.ComponentModel.Composition;
 
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using VVVV.Utils.VColor;
using VVVV.Utils.VMath;
 
using VVVV.Core.Logging;
#endregion usings
 
namespace VVVV.Nodes
{
#region PluginInfo
[PluginInfo(Name = "S+H", Category = "Value", Version = "Save", AutoEvaluate=true, Author="elliotwoods", Help = "S+H that saves state into the patch", Tags = "")]
#endregion PluginInfo
public class SaveValueS_HNode : IPluginEvaluate
{
#region fields & pins
[Config("Save")]
IDiffSpread<string> FSave;
[Input("Input", DefaultValue = 1.0)]
ISpread<double> FInput;
[Input("Set")]
ISpread<bool> FSet;
[Output("Output")]
ISpread<double> FOutput;
 
[Import()]
ILogger FLogger;
#endregion fields & pins
 
//called when data for any output pin is requested
public void Evaluate(int SpreadMax)
{
if (FSave.IsChanged)
{
Load();
}
FOutput.SliceCount = SpreadMax;
 
bool save = false;
for (int i = 0; i < SpreadMax; i++)
if (FSet[i])
{
FOutput[i] = FInput[i];
save = true;
}
if (save)
Save();
}
void Load()
{
string[] values = FSave[0].Split(new char[]{','});
int count = values.Length;
FOutput.SliceCount = count;
double v;
for (int i=0; i<count; i++)
{
if (Double.TryParse(values[i], out v))
FOutput[i] = v;
else
FOutput[i] = 0;
}
//FLogger.Log(LogType.Debug, "Load");
}
void Save()
{
string o = "";
int count = FOutput.SliceCount;
for (int i=0; i<count; i++)
{
o += Convert.ToString(FOutput[i]) + ",";
}
FSave[0] = o;
//FLogger.Log(LogType.Debug, "Save");
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.