ofxDatGui icon indicating copy to clipboard operation
ofxDatGui copied to clipboard

ability to force event callback?

Open tyhenry opened this issue 8 years ago • 6 comments

Hi - thank you again for the fantastic addon.

I have a question: is there a way to force a component to run its assigned callback?

For instance, in setup():

  • I assign a callback function to a component
  • then manually set a default value for the component
  • then would like the component to run the callback for that value

i.e.:

gui = new ofxDatGui(0,0);  
ofxDatGuiSlider* rpmSlider = gui->addSlider("rpm", 7.0,18.0);  
rpmSlider->onSliderEvent(this, &ofApp::changeRpm);
rpmSlider->setValue(10.0);  
rpmSlider->**[force event callback here?]**;  

Thanks!

[Note: I'm using both lambda and normal callbacks so would be great to have a solution that works for both.]

Thanks!

tyhenry avatar Aug 08 '16 18:08 tyhenry

update: quick fix - I've been able to run the callbacks for sliders and text inputs by modifying the src:

ofxDatGuiSlider.h:

public:  // was protected:
    void dispatchSliderChangedEvent()...

and

ofxDatGuiTextInput.h:

public: // was protected:
    void onFocusLost()....

but maybe there's an easier way

tyhenry avatar Aug 08 '16 18:08 tyhenry

Same technique, but with no need of modifying the source code: you can force it by calling setFocused(false); (which will call onFocusLost())

tf->setText(clipboardContents); tf->setFocused(false);

valerialaura avatar Aug 16 '16 19:08 valerialaura

i'm also looking for such a solution all components don't have the same mechanism and I'm looking for something general to trig callback and update bind data when recalling settings. see #98

setFocused(false) ssems to work only with text input component (to disable keyboard listening)

avilleret avatar Sep 19 '16 16:09 avilleret

Hi, everyone. I just added a dispatchEvent() method to all components that accept an event callback which will allow you to manually invoke the callback (if one is set) whenever you want. So if you want to invoke a slider's callback after programmatically changing its value you can:

ofxDatGuiSlider* mySlider = gui->addSlider("slider", 0,100);  
mySlider->onSliderEvent(this, &ofApp::onSliderChanged);
mySlider->setValue(5.0);  
mySlider->dispatchEvent();

In the case of Matrices & ScrollViews the dispatched event object will contain the last ofxDatGuiMatrixButton or ofxDatGuiScrollViewItem that was selected.

This is experimental and currently only available on the dev branch so please test and report any issues you experience. Once this is stable I will merge it into master and update the docs. Thanks.

braitsch avatar Oct 02 '16 02:10 braitsch

thanks @braitsch for this feature !

I've made some tests and found 2 issues, I've open separate issue ticket, see #104, #105 and #106

avilleret avatar Oct 18 '16 08:10 avilleret

I think we can close this issue since dispatchEvent() seem to do the trick (and it works fine)

avilleret avatar Oct 23 '16 17:10 avilleret