Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to force event callback? #92

Open
tyhenry opened this issue Aug 8, 2016 · 6 comments
Open

ability to force event callback? #92

tyhenry opened this issue Aug 8, 2016 · 6 comments

Comments

@tyhenry
Copy link

tyhenry commented Aug 8, 2016

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
Copy link
Author

tyhenry commented Aug 8, 2016

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

@valerialaura
Copy link

valerialaura commented Aug 16, 2016

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);

@avilleret
Copy link
Contributor

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)

@braitsch
Copy link
Owner

braitsch commented Oct 2, 2016

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.

@avilleret
Copy link
Contributor

avilleret commented Oct 18, 2016

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants