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

Data binding #13

Open
borg opened this issue Oct 16, 2015 · 7 comments
Open

Data binding #13

borg opened this issue Oct 16, 2015 · 7 comments

Comments

@borg
Copy link

borg commented Oct 16, 2015

Is there any reason data binding is limited to sliders? It is super useful and I did a quick implementation for the button as well but if it's in the works I'd rather hold back as I'm not sure I'm following your nullptr implementation very well.


#pragma once
#include "ofxDatGuiComponent.h"

class ofxDatGuiButton : public ofxDatGuiComponent {

    public:

        ofxDatGuiButton(string label, ofxDatGuiTemplate* tmplt=nullptr) : ofxDatGuiComponent(label, tmplt)
        {
            mType = ofxDatGuiType::BUTTON;
            mStripeColor = mTemplate->button.color.stripe;
            setWidth(mRow.width);
        }

        static ofxDatGuiButton* getInstance()
        {
            return new ofxDatGuiButton("X");
        }

        void setOrigin(int x, int y)
        {
            ofxDatGuiComponent::setOrigin(x, y);
            mLabelAreaWidth = mRow.width;
        }

        void draw()
        {
            if (!mVisible) return;
            ofPushStyle();
                drawBkgd();
                ofxDatGuiComponent::drawLabel();
                ofxDatGuiComponent::drawStripe();
            ofPopStyle();
        }

        void drawBkgd()
        {
        // anything that extends ofxDatGuiButton has the same rollover effect //
            if (mMouseDown){
                ofxDatGuiComponent::drawBkgd(mTemplate->row.color.mouseDown, 255);
            }   else if (mMouseOver){
                ofxDatGuiComponent::drawBkgd(mTemplate->row.color.mouseOver, 255);
            }   else{
                ofxDatGuiComponent::drawBkgd();
            }
        }

        bool hitTest(ofPoint m)
        {
            return (m.x>=x && m.x<= x+mRow.width && m.y>=y && m.y<= y+mRow.height);
        }

        void onMouseRelease(ofPoint m)
        {
            ofxDatGuiComponent::onMouseRelease(m);
        // dispatch event out to main application //
            if (buttonEventCallback != nullptr) {
                ofxDatGuiButtonEvent e(this);
                buttonEventCallback(e);
            }   else{
                ofxDatGuiLog::write(ofxDatGuiMsg::EVENT_HANDLER_NULL);
            }
        }

        virtual void toggle(){}
        virtual void setEnabled(bool enable){}
        virtual bool getEnabled(){return false;}

};

class ofxDatGuiToggle : public ofxDatGuiButton {

    public:

        ofxDatGuiToggle(string label, bool enabled, ofxDatGuiTemplate* tmplt=nullptr) : ofxDatGuiButton(label, tmplt)
        {
            mEnabled = enabled;
            mStripeColor = mTemplate->toggle.color.stripe;
            if (!radioOn.isAllocated()) radioOn.load(OFXDG_ASSET_DIR+"/icon-radio-on.png");
            if (!radioOff.isAllocated()) radioOff.load(OFXDG_ASSET_DIR+"/icon-radio-off.png");
        }

        void setOrigin(int x, int y)
        {
            ofxDatGuiComponent::setOrigin(x, y);
            mLabelMarginRight = mRow.width-mIcon.x;
        }

        void setTemplate(ofxDatGuiTemplate* tmplt)
        {
            ofxDatGuiButton::setTemplate(tmplt);
            setWidth(mRow.width);
        }

        void toggle()
        {
            mEnabled =!mEnabled;


            if (mBoundBool != nullptr) {
                *mBoundBool = mEnabled;
            }

        }

        void setEnabled(bool enable)
        {
            mEnabled = enable;


           if (mBoundBool != nullptr) {
                *mBoundBool = mEnabled;
            }

        }

        bool getEnabled()
        {
            return mEnabled;
        }

        void draw()
        {
            ofxDatGuiButton::drawBkgd();
            ofxDatGuiComponent::drawLabel();
            ofxDatGuiComponent::drawStripe();
            ofPushStyle();
                ofSetColor(mTemplate->row.color.label);
                if (mEnabled == true){
                    radioOn.draw(x+mIcon.x, y+mIcon.y, mIcon.size, mIcon.size);
                }   else{
                    radioOff.draw(x+mIcon.x, y+mIcon.y, mIcon.size, mIcon.size);
                }
            ofPopStyle();
        }

        void onMouseRelease(ofPoint m)
        {
            ofxDatGuiComponent::onMouseRelease(m);
            mEnabled = !mEnabled;
           //borg, check?
           if (mBoundBool != nullptr) {
                *mBoundBool = mEnabled;
            }
        // dispatch event out to main application //
            if (buttonEventCallback != nullptr) {
                ofxDatGuiButtonEvent e(this, mEnabled);
                buttonEventCallback(e);
            }   else{
                ofxDatGuiLog::write(ofxDatGuiMsg::EVENT_HANDLER_NULL);
            }
        }


        //borg added
        inline void bind(bool *val)
        {
            mBoundBool = val;
        }

    private:
        bool mEnabled;
        ofImage radioOn;
        ofImage radioOff;

        bool *mBoundBool = nullptr;;

};
@braitsch
Copy link
Owner

More data binding support is coming. There's been some talk on the forum about integration with ofParameter so I'm thinking about how I want to implement binding consistently across all components to possibly integrate with ofParameter. Good to know that this is of interest to you though as that helps push this towards a priority.

@borg
Copy link
Author

borg commented Oct 16, 2015

ofParameter integration would definitely be my recommended option too. Sweet.

@martialgallorini
Copy link

ofParameter is the way to go i think

@crucialfelix
Copy link

c++ 11 lambda callbacks would be wonderful. ofParameter support them a bit.

@braitsch
Copy link
Owner

v1.1 now has limited support for ofParameter (sliders only).
Support for more components will likely be in the next release.

@smukkejohan
Copy link

I started an approach here at classes to bind ofParameters to combinations ofxDatGui elements. Works well for me, still incomplete. Would definitely be nicer to integrate the ofParameter binding more tightly with datGui.

https://github.com/RecoilPerformanceGroup/Stereo2016/blob/master/ofStereo2016/src/ofxDatGuiParameterBindings.hpp
https://github.com/RecoilPerformanceGroup/Stereo2016/blob/master/ofStereo2016/src/ofxDatGuiParameterBindings.cpp

An example of how I use them:

ofxDatGui* gui = new ofxDatGui( 0, 0 );
ofParameter<ofColor> color{"color", ofColor::white,ofColor::black,ofColor::white};
ofParameter<ofVec2f> offset{"offset", ofVec3f{50,50,50}, ofVec3f{0,0,0}, ofVec3f{100,100,100}};
ofParameter<ofVec3f> pos{"position", ofVec3f{50,50,50}, ofVec3f{0,0,0}, ofVec3f{100,100,100}};
ofParameter<ofVec3f> pos2{"position", ofVec3f{50,50,50}, ofVec3f{0,0,0}, ofVec3f{100,100,100}};

new ColorPickerWithAlpha(color, gui);
new SlidersVec3f(pos, gui));
new SlidersVec2f(offset, gui));
new PadAndZ(pos2, gui));

@BenBergman
Copy link

BenBergman commented May 17, 2017

Data binding and ofParamater support for other input types would be a great addition. I see PR #62 has some bind compatibility for toggle and text fields. I would love to have some more binding in master.

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

6 participants