Advertisement
Guest User

Untitled

a guest
Dec 10th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1.         class SliderListener : IDisposable
  2.         {
  3.             private IGH_Component owner;
  4.             Guid currentId;
  5.             List<Grasshopper.Kernel.Special.GH_NumberSlider> Sliders;
  6.             string slavePrefix;
  7.             string masterPrefix;
  8.             bool enabled = true;
  9.             /// <summary>
  10.             ///  This class takes care of all the wizardry involved in events inside a C# block, especially the part where we need to self-destruct,
  11.             ///  or a C# block gets copied, recompiled or other stuff that creates indestructable event listeners.
  12.             /// </summary>
  13.             /// <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>
  14.             /// <param name="masterPrefix">We'll catch on to the sliders that start with this prefix</param>
  15.             /// <param name="slavePrefix">We'll look for slaves that are equal to the master's name, except the prefix is different</param>
  16.             /// <param name="debug">Should we talk to you through the Rhino command line?</param>
  17.             public SliderListener (IGH_Component owner, string masterPrefix, string slavePrefix, bool debug)
  18.             {
  19.                 this.owner = owner;
  20.                 this.currentId = Guid.NewGuid();
  21.                 owner.Message = currentId.ToString();
  22.                 this.debug = debug;
  23.                 this.slavePrefix = slavePrefix;
  24.                 this.masterPrefix = masterPrefix;
  25.                 this.Sliders = FindSlidersThatStartWithName(masterPrefix);
  26.                 if (slavePrefix.Length < 4 || masterPrefix.Length < 4)
  27.                 {
  28.                     throw new Exception("Please use a prefix longer than x");
  29.                 }
  30.  
  31.                 Debug("Found " + Sliders.Count + " sliders");
  32.                 foreach (var slider in this.Sliders)
  33.                 {
  34.                     slider.SolutionExpired += Callback;
  35.                 }
  36.  
  37.                 this.owner.ObjectChanged += CheckStillValid;
  38.             }
  39.  
  40.             void CheckStillValid(IGH_DocumentObject sender, GH_ObjectChangedEventArgs e)
  41.             {
  42.                 if (!IsValid())
  43.                 {
  44.                     this.Dispose();
  45.                 }
  46.             }
  47.  
  48.             public Func<Grasshopper.Kernel.Special.GH_NumberSlider, Grasshopper.Kernel.Special.GH_NumberSlider, bool> CustomCallback;
  49.  
  50.             public bool IsValid ()
  51.             {
  52.                 return !(!enabled || this.owner == null || this.owner.OnPingDocument() == null
  53.                     || this.owner.Locked || !this.owner.Message.Equals(this.currentId.ToString()));
  54.             }
  55.  
  56.             private void Debug(string message)
  57.             {
  58.                 if (this.debug)
  59.                 {
  60.                     RhinoApp.WriteLine(message);
  61.                 }
  62.             }
  63.  
  64.             private void Callback(IGH_DocumentObject sender, GH_SolutionExpiredEventArgs e)
  65.             {
  66.                 Debug("Hello from the slider callback!");
  67.                 var slider = sender as Grasshopper.Kernel.Special.GH_NumberSlider;
  68.                 if (slider == null || !this.IsValid())
  69.                 {
  70.                     // ok, we're out of here, let's get rid of all the slaves.
  71.                     this.Dispose();
  72.                     return;
  73.                 }
  74.  
  75.                 var slaves = FindSlavesByNameOfMaster(sender.NickName);
  76.                 Debug("Found " + slaves.Count + " slaves");
  77.                 foreach (var slave in slaves)
  78.                 {
  79.                     if (CustomCallback != null)
  80.                     {
  81.                         bool test = CustomCallback(slider, slave);
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             public void Dispose()
  87.             {
  88.                 Debug("Disposing the slider listener2");
  89.                 // just in case someone keeps hanging on to this class after we've called dispose.
  90.                 enabled = false;
  91.                 foreach (var numberSlider in Sliders)
  92.                 {
  93.                     numberSlider.SolutionExpired -= this.Callback;
  94.                 }
  95.                 if (this.owner != null)
  96.                 {
  97.                     owner.ObjectChanged -= CheckStillValid;
  98.                 }
  99.             }
  100.  
  101.             private List<Grasshopper.Kernel.Special.GH_NumberSlider> FindSlidersThatStartWithName(string name)
  102.             {
  103.                 return this.owner.OnPingDocument()
  104.                     .Objects
  105.                     .OfType<Grasshopper.Kernel.Special.GH_NumberSlider>()
  106.                     .Where(obj => obj.NickName.StartsWith(name))
  107.                     .ToList();
  108.             }
  109.  
  110.  
  111.             private List<Grasshopper.Kernel.Special.GH_NumberSlider> FindSlavesByNameOfMaster(string mastersName)
  112.             {
  113.  
  114.                 string slave = mastersName.Replace(this.masterPrefix, this.slavePrefix);
  115.                
  116.                 if (slave.Equals(mastersName))
  117.                 {
  118.                     // something went wrong, and the master's name is no longer a master's name.
  119.                     // a slider should not control itself.
  120.                     return new List<Grasshopper.Kernel.Special.GH_NumberSlider>();
  121.                 }
  122.  
  123.                 return this.owner.OnPingDocument()
  124.                     .Objects
  125.                     .OfType<Grasshopper.Kernel.Special.GH_NumberSlider>()
  126.                     .Where(obj => obj.NickName.Equals(slave))
  127.                     .ToList();
  128.             }
  129.  
  130.             public bool debug { get; set; }
  131.         }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement