Use ofxDatGui inside class

Hello.
I want to make a class which render a button and toggle.
I’m new to C++ and iit is a little bit difficult for me to understand all about links * and &.
So I think that in constructor of my class I need to set as argument link to main app and then use it in callback functions, like onButtonEvent(), that can get T owner as first argument. How can I do it? When i use ofApp construction i’ve got an error.

Here is my class
ClipLauncher.h

#pragma once

#include "ofMain.h"
#include "ofxDatGui.h"

class ClipLauncher {
  public: ClipLauncher(int x, int y, ofApp* context);
  string fName, status;
  int idx;
  ofxDatGui* gui;
  ofxDatGuiButton* openFile;
  ofxDatGuiToggle* playClip;
  void onButtonEvent(ofxDatGuiButtonEvent e);
};

ClipLauncher.cpp

#include "clipLauncher.h"

ClipLauncher::ClipLauncher(int x, int y, ofApp* context) {
    gui = new ofxDatGui(x, y);
    openFile = gui -> addButton("open file");
    openFile -> onButtonEvent(context, &ClipLauncher::onButtonEvent);
    playClip = gui -> addToggle("run shred");
    playClip -> onButtonEvent(context, &ClipLauncher::onButtonEvent);
}

void ClipLauncher::onButtonEvent(ofxDatGuiButtonEvent e) {
    if (e.target == openFile) {
      //Open the Open File Dialog
      ofFileDialogResult openFileResult= ofSystemLoadDialog("Select .ck file");
      if (openFileResult.bSuccess) {
        e.target -> setLabel(openFileResult.getName());
        fName = openFileResult.getPath();
      }
    } else if (e.target == playClip) {
        if(playClip->getEnabled()) {
            if(fName != ""){
                string command = "chuck " + fName;
                system(command.c_str());
            } else {
                cout << "no file" << endl;
            }
        }
    }
}

Hi, you don’t need to pass a reference to your ofApp into your class if all you’re trying to do is bind the button event to your gui.

ClipLauncher.h

#include "ofxDatGui.h"
class ClipLauncher {
    public: 
        ClipLauncher(int x, int y);
        string fName, status;
        int idx;
        ofxDatGui* gui;
        ofxDatGuiButton* openFile;
        ofxDatGuiToggle* playClip;
        void onButtonEvent(ofxDatGuiButtonEvent e);
};

ClipLauncher.cpp

ClipLauncher::ClipLauncher(int x, int y) {
    gui = new ofxDatGui(x, y);
    openFile = gui->addButton("open file");
    playClip = gui->addToggle("run shred");
    gui->onButtonEvent(this, &ClipLauncher::onButtonEvent);
}

void ClipLauncher::onButtonEvent(ofxDatGuiButtonEvent e) {
    if (e.target == openFile){
        cout << "open file button was clicked" << endl;
    } else if (e.target == playClip){
        cout << "play clip button was clicked" << endl;
    }
}

ofApp.h

#include "ofMain.h"
#include "ClipLauncher.h"
class ofApp : public ofBaseApp{
    public:
	void setup();
	void update();
	void draw();
    ClipLauncher* cliplauncher;
...

ofApp.cpp

void ofApp::setup(){
    cliplauncher = new ClipLauncher(100, 100);
}
1 Like