heronarts / LX

Core library for 3D LED lighting engines
https://chromatik.co/
Other
41 stars 25 forks source link

Add public accessor for AHDSREnvelope.stage #102

Closed jkbelcher closed 11 months ago

jkbelcher commented 11 months ago

I'm using an AHDSREnvelope from code. It seems like I should be able to check the current stage with a getStage() method. It would also be nice to listen for stage changes rather than poll them every frame. Would you consider adding a Listener class to this modulator with a stageChanged() event?

mcslee commented 11 months ago

I guess I'm not opposed to a listener - just curious, what is the use case? What are you doing differently on a stage-by-stage basis?

mcslee commented 11 months ago

Here, how's this: https://github.com/heronarts/LX/commit/aa1dced4d41f69d6c9d6474537a1a91878af2a5f

Rather than dealing with an array of listeners, just using an anonymous subclass pattern here, you can do:

AHDSREnvelope env = new AHDSREnvelope(...) {
  protected void onStageChanged(AHDSREnvelope.Stage stage) {
    // do your thing
  }
};
jkbelcher commented 11 months ago

Thanks, the anonymous subclass works great here.

The use case is a collection of objects with parallel lifetimes, each with its own envelope defining the lifecycle. Exact uses still in flux but have included: 1) Is lifecycle over? getLevel() == 0.0 could mean it was created this frame, or it is past the decay stage. Can't tell the difference. 2) Trying to log a timestamp at the beginning of various phases, particularly sustain and decay, for triggering events after a certain amount of delay.