Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SliderListener : IDisposable
- {
- private IGH_Component owner;
- Guid currentId;
- List<Grasshopper.Kernel.Special.GH_NumberSlider> Sliders;
- string slavePrefix;
- string masterPrefix;
- bool enabled = true;
- /// <summary>
- /// This class takes care of all the wizardry involved in events inside a C# block, especially the part where we need to self-destruct,
- /// or a C# block gets copied, recompiled or other stuff that creates indestructable event listeners.
- /// </summary>
- /// <param name="owner">The GH_Component that is the owner of this listener. If this gets modified or is locked, we'll inititate a self destruct sequence.</param>
- /// <param name="masterPrefix">We'll catch on to the sliders that start with this prefix</param>
- /// <param name="slavePrefix">We'll look for slaves that are equal to the master's name, except the prefix is different</param>
- /// <param name="debug">Should we talk to you through the Rhino command line?</param>
- public SliderListener (IGH_Component owner, string masterPrefix, string slavePrefix, bool debug)
- {
- this.owner = owner;
- this.currentId = Guid.NewGuid();
- owner.Message = currentId.ToString();
- this.debug = debug;
- this.slavePrefix = slavePrefix;
- this.masterPrefix = masterPrefix;
- this.Sliders = FindSlidersThatStartWithName(masterPrefix);
- if (slavePrefix.Length < 4 || masterPrefix.Length < 4)
- {
- throw new Exception("Please use a prefix longer than x");
- }
- Debug("Found " + Sliders.Count + " sliders");
- foreach (var slider in this.Sliders)
- {
- slider.SolutionExpired += Callback;
- }
- this.owner.ObjectChanged += CheckStillValid;
- }
- void CheckStillValid(IGH_DocumentObject sender, GH_ObjectChangedEventArgs e)
- {
- if (!IsValid())
- {
- this.Dispose();
- }
- }
- public Func<Grasshopper.Kernel.Special.GH_NumberSlider, Grasshopper.Kernel.Special.GH_NumberSlider, bool> CustomCallback;
- public bool IsValid ()
- {
- return !(!enabled || this.owner == null || this.owner.OnPingDocument() == null
- || this.owner.Locked || !this.owner.Message.Equals(this.currentId.ToString()));
- }
- private void Debug(string message)
- {
- if (this.debug)
- {
- RhinoApp.WriteLine(message);
- }
- }
- private void Callback(IGH_DocumentObject sender, GH_SolutionExpiredEventArgs e)
- {
- Debug("Hello from the slider callback!");
- var slider = sender as Grasshopper.Kernel.Special.GH_NumberSlider;
- if (slider == null || !this.IsValid())
- {
- // ok, we're out of here, let's get rid of all the slaves.
- this.Dispose();
- return;
- }
- var slaves = FindSlavesByNameOfMaster(sender.NickName);
- Debug("Found " + slaves.Count + " slaves");
- foreach (var slave in slaves)
- {
- if (CustomCallback != null)
- {
- bool test = CustomCallback(slider, slave);
- }
- }
- }
- public void Dispose()
- {
- Debug("Disposing the slider listener2");
- // just in case someone keeps hanging on to this class after we've called dispose.
- enabled = false;
- foreach (var numberSlider in Sliders)
- {
- numberSlider.SolutionExpired -= this.Callback;
- }
- if (this.owner != null)
- {
- owner.ObjectChanged -= CheckStillValid;
- }
- }
- private List<Grasshopper.Kernel.Special.GH_NumberSlider> FindSlidersThatStartWithName(string name)
- {
- return this.owner.OnPingDocument()
- .Objects
- .OfType<Grasshopper.Kernel.Special.GH_NumberSlider>()
- .Where(obj => obj.NickName.StartsWith(name))
- .ToList();
- }
- private List<Grasshopper.Kernel.Special.GH_NumberSlider> FindSlavesByNameOfMaster(string mastersName)
- {
- string slave = mastersName.Replace(this.masterPrefix, this.slavePrefix);
- if (slave.Equals(mastersName))
- {
- // something went wrong, and the master's name is no longer a master's name.
- // a slider should not control itself.
- return new List<Grasshopper.Kernel.Special.GH_NumberSlider>();
- }
- return this.owner.OnPingDocument()
- .Objects
- .OfType<Grasshopper.Kernel.Special.GH_NumberSlider>()
- .Where(obj => obj.NickName.Equals(slave))
- .ToList();
- }
- public bool debug { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement