Skip to content

Commit

Permalink
adding toggle events & getChecked instead of overriding getEnabled #74
Browse files Browse the repository at this point in the history
  • Loading branch information
braitsch committed May 10, 2016
1 parent a6c2e36 commit 2e482d3
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 37 deletions.
9 changes: 7 additions & 2 deletions example-AllComponents/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void ofApp::setup()
y += component->getHeight() + p;
component = new ofxDatGuiToggle("toggle", false);
component->setPosition(x, y);
component->onButtonEvent(this, &ofApp::onButtonEvent);
component->onToggleEvent(this, &ofApp::onToggleEvent);
components.push_back(component);

y += component->getHeight() + p;
Expand Down Expand Up @@ -109,7 +109,12 @@ void ofApp::draw()

void ofApp::onButtonEvent(ofxDatGuiButtonEvent e)
{
cout << "onButtonEvent: " << e.target->getLabel() << "::" << e.enabled << endl;
cout << "onButtonEvent: " << e.target->getLabel() << endl;
}

void ofApp::onToggleEvent(ofxDatGuiToggleEvent e)
{
cout << "onToggleEvent: " << e.target->getLabel() << "::" << e.target->getChecked() << endl;
}

void ofApp::onSliderEvent(ofxDatGuiSliderEvent e)
Expand Down
1 change: 1 addition & 0 deletions example-AllComponents/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ofApp : public ofBaseApp{
vector<ofxDatGuiComponent*> components;

void onButtonEvent(ofxDatGuiButtonEvent e);
void onToggleEvent(ofxDatGuiToggleEvent e);
void onSliderEvent(ofxDatGuiSliderEvent e);
void onDropdownEvent(ofxDatGuiDropdownEvent e);
void onMatrixEvent(ofxDatGuiMatrixEvent e);
Expand Down
17 changes: 12 additions & 5 deletions example-AllComponentsGui/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void ofApp::setup()

// once the gui has been assembled, register callbacks to listen for component specific events //
gui->onButtonEvent(this, &ofApp::onButtonEvent);
gui->onToggleEvent(this, &ofApp::onToggleEvent);
gui->onSliderEvent(this, &ofApp::onSliderEvent);
gui->onTextInputEvent(this, &ofApp::onTextInputEvent);
gui->on2dPadEvent(this, &ofApp::on2dPadEvent);
Expand All @@ -93,16 +94,21 @@ void ofApp::setup()
refreshWindow();
}

void ofApp::onSliderEvent(ofxDatGuiSliderEvent e)
void ofApp::onButtonEvent(ofxDatGuiButtonEvent e)
{
cout << "onSliderEvent: " << e.target->getLabel() << " "; e.target->printValue();
if (e.target->is("datgui opacity")) gui->setOpacity(e.scale);
cout << "onButtonEvent: " << e.target->getLabel() << endl;
}

void ofApp::onButtonEvent(ofxDatGuiButtonEvent e)
void ofApp::onToggleEvent(ofxDatGuiToggleEvent e)
{
if (e.target->is("toggle fullscreen")) toggleFullscreen();
cout << "onButtonEvent: " << e.target->getLabel() << " " << e.target->getEnabled() << endl;
cout << "onToggleEvent: " << e.target->getLabel() << " " << e.checked << endl;
}

void ofApp::onSliderEvent(ofxDatGuiSliderEvent e)
{
cout << "onSliderEvent: " << e.target->getLabel() << " "; e.target->printValue();
if (e.target->is("datgui opacity")) gui->setOpacity(e.scale);
}

void ofApp::onTextInputEvent(ofxDatGuiTextInputEvent e)
Expand Down Expand Up @@ -148,6 +154,7 @@ void ofApp::keyPressed(int key)
void ofApp::toggleFullscreen()
{
mFullscreen = !mFullscreen;
gui->getToggle("toggle fullscreen")->setChecked(mFullscreen);
refreshWindow();
}

Expand Down
1 change: 1 addition & 0 deletions example-AllComponentsGui/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ofApp : public ofBaseApp{

void keyPressed(int key);
void onButtonEvent(ofxDatGuiButtonEvent e);
void onToggleEvent(ofxDatGuiToggleEvent e);
void onSliderEvent(ofxDatGuiSliderEvent e);
void onTextInputEvent(ofxDatGuiTextInputEvent e);
void on2dPadEvent(ofxDatGui2dPadEvent e);
Expand Down
1 change: 0 additions & 1 deletion example-Buttons/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void ofApp::setup()
// and register to listen for events //
button->onButtonEvent(this, &ofApp::onButtonEvent);
toggle->onButtonEvent(this, &ofApp::onButtonEvent);

}

void ofApp::update()
Expand Down
6 changes: 6 additions & 0 deletions example-Folders/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void ofApp::setup()
f2->expand();

f1->onButtonEvent(this, &ofApp::onButtonEvent);
f1->onToggleEvent(this, &ofApp::onToggleEvent);
f1->onSliderEvent(this, &ofApp::onSliderEvent);
f1->onMatrixEvent(this, &ofApp::onMatrixEvent);
f1->onColorPickerEvent(this, &ofApp::onColorPickerEvent);
Expand All @@ -50,6 +51,11 @@ void ofApp::onButtonEvent(ofxDatGuiButtonEvent e)
cout << "onButtonEvent" << endl;
}

void ofApp::onToggleEvent(ofxDatGuiToggleEvent e)
{
cout << "onToggleEvent " << e.checked << endl;
}

void ofApp::onSliderEvent(ofxDatGuiSliderEvent e)
{
cout << "onSliderEvent" << endl;
Expand Down
1 change: 1 addition & 0 deletions example-Folders/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ofApp : public ofBaseApp{
ofxDatGuiFolder* f2;

void onButtonEvent(ofxDatGuiButtonEvent e);
void onToggleEvent(ofxDatGuiToggleEvent e);
void onSliderEvent(ofxDatGuiSliderEvent e);
void onMatrixEvent(ofxDatGuiMatrixEvent e);
void on2dPadEvent(ofxDatGui2dPadEvent e);
Expand Down
14 changes: 9 additions & 5 deletions example-xGenerativeLines/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void ofApp::setup()

// register a few callbacks to listen for our gui events //
gui->onButtonEvent(this, &ofApp::onButtonEvent);
gui->onToggleEvent(this, &ofApp::onToggleEvent);
gui->onSliderEvent(this, &ofApp::onSliderEvent);
gui->onTextInputEvent(this, &ofApp::onTextInputEvent);
gui->onColorPickerEvent(this, &ofApp::onColorPickerEvent);
Expand All @@ -46,7 +47,7 @@ void ofApp::setup()
lineWeight = s2->getValue();
Line::MaxLength = s3->getValue();
gui->setOpacity(gui->getSlider("datgui opacity")->getScale());
drawingPaused = gui->getButton("pause drawing")->getEnabled();
drawingPaused = gui->getToggle("pause drawing")->getChecked();

// finally add some generative lines to draw //
lines.push_back(Line(ofGetWidth()*.2, ofGetHeight()/2, gui->getColorPicker("line 1")->getColor()));
Expand Down Expand Up @@ -81,10 +82,13 @@ void ofApp::onSliderEvent(ofxDatGuiSliderEvent e)

void ofApp::onButtonEvent(ofxDatGuiButtonEvent e)
{
if (e.target->is("clear")) {
reset();
} else if (e.target->is("pause drawing")) {
drawingPaused = e.enabled;
if (e.target->is("clear")) reset();
}

void ofApp::onToggleEvent(ofxDatGuiToggleEvent e)
{
if (e.target->is("pause drawing")) {
drawingPaused = e.checked;
}
}

Expand Down
1 change: 1 addition & 0 deletions example-xGenerativeLines/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ofApp : public ofBaseApp{
ofxDatGuiSlider* s4;

void onButtonEvent(ofxDatGuiButtonEvent e);
void onToggleEvent(ofxDatGuiToggleEvent e);
void onSliderEvent(ofxDatGuiSliderEvent e);
void onTextInputEvent(ofxDatGuiTextInputEvent e);
void onDropdownEvent(ofxDatGuiDropdownEvent e);
Expand Down
36 changes: 17 additions & 19 deletions src/components/ofxDatGuiButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ class ofxDatGuiButton : public ofxDatGuiComponent {
}
}

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

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

protected:
Expand All @@ -96,9 +92,9 @@ class ofxDatGuiToggle : public ofxDatGuiButton {

public:

ofxDatGuiToggle(string label, bool enabled) : ofxDatGuiButton(label)
ofxDatGuiToggle(string label, bool checked = false) : ofxDatGuiButton(label)
{
mEnabled = enabled;
mChecked = checked;
mType = ofxDatGuiType::TOGGLE;
setTheme(ofxDatGuiComponent::theme.get());
}
Expand All @@ -122,17 +118,17 @@ class ofxDatGuiToggle : public ofxDatGuiButton {

void toggle()
{
mEnabled = !mEnabled;
mChecked = !mChecked;
}

void setEnabled(bool enable)
void setChecked(bool check)
{
mEnabled = enable;
mChecked = check;
}

bool getEnabled()
bool getChecked()
{
return mEnabled;
return mChecked;
}

void draw()
Expand All @@ -141,7 +137,7 @@ class ofxDatGuiToggle : public ofxDatGuiButton {
ofPushStyle();
ofxDatGuiButton::draw();
ofSetColor(mIcon.color);
if (mEnabled == true){
if (mChecked == 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);
Expand All @@ -150,24 +146,26 @@ class ofxDatGuiToggle : public ofxDatGuiButton {
}
}

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

protected:

void onMouseRelease(ofPoint m)
{
mEnabled = !mEnabled;
mChecked = !mChecked;
ofxDatGuiComponent::onFocusLost();
ofxDatGuiComponent::onMouseRelease(m);
// dispatch event out to main application //
if (buttonEventCallback != nullptr) {
ofxDatGuiButtonEvent e(this, mEnabled);
buttonEventCallback(e);
} else{
ofxDatGuiLog::write(ofxDatGuiMsg::EVENT_HANDLER_NULL);
if (toggleEventCallback == nullptr) {
// attempt to call generic button callback //
ofxDatGuiButton::onMouseRelease(m);
} else {
toggleEventCallback(ofxDatGuiToggleEvent(this, mChecked));
}
}

private:
bool mEnabled;
bool mChecked;
shared_ptr<ofImage> radioOn;
shared_ptr<ofImage> radioOff;

Expand Down
14 changes: 13 additions & 1 deletion src/components/ofxDatGuiGroups.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ class ofxDatGuiFolder : public ofxDatGuiGroup {
}
}

void dispatchToggleEvent(ofxDatGuiToggleEvent e)
{
if (toggleEventCallback != nullptr) {
toggleEventCallback(e);
// allow toggle events to decay into button events //
} else if (buttonEventCallback != nullptr) {
buttonEventCallback(ofxDatGuiButtonEvent(e.target));
} else{
ofxDatGuiLog::write(ofxDatGuiMsg::EVENT_HANDLER_NULL);
}
}

void dispatchSliderEvent(ofxDatGuiSliderEvent e)
{
if (sliderEventCallback != nullptr) {
Expand Down Expand Up @@ -262,7 +274,7 @@ class ofxDatGuiFolder : public ofxDatGuiGroup {
{
ofxDatGuiToggle* toggle = new ofxDatGuiToggle(label, enabled);
toggle->setStripeColor(mStyle.stripe.color);
toggle->onButtonEvent(this, &ofxDatGuiFolder::dispatchButtonEvent);
toggle->onToggleEvent(this, &ofxDatGuiFolder::dispatchToggleEvent);
attachItem(toggle);
return toggle;
}
Expand Down
17 changes: 14 additions & 3 deletions src/core/ofxDatGuiEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#pragma once

class ofxDatGuiButton;
class ofxDatGuiToggle;
class ofxDatGuiSlider;
class ofxDatGuiDropdown;
class ofxDatGuiTextInput;
Expand Down Expand Up @@ -59,15 +60,25 @@ class ofxDatGuiInternalEvent{
class ofxDatGuiButtonEvent{

public:
ofxDatGuiButtonEvent(ofxDatGuiButton* t, bool e = false)
ofxDatGuiButtonEvent(ofxDatGuiButton* t)
{
target = t;
enabled = e;
}
bool enabled;
ofxDatGuiButton* target;
};

class ofxDatGuiToggleEvent{

public:
ofxDatGuiToggleEvent(ofxDatGuiToggle* t, bool e = false)
{
target = t;
checked = e;
}
bool checked;
ofxDatGuiToggle* target;
};

class ofxDatGuiSliderEvent{

public:
Expand Down
10 changes: 10 additions & 0 deletions src/core/ofxDatGuiIntObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ class ofxDatGuiInteractiveObject{
{
buttonEventCallback = std::bind(listenerMethod, owner, std::placeholders::_1);
}

// toggle events //
typedef std::function<void(ofxDatGuiToggleEvent)> onToggleEventCallback;
onToggleEventCallback toggleEventCallback;

template<typename T, typename args, class ListenerClass>
void onToggleEvent(T* owner, void (ListenerClass::*listenerMethod)(args))
{
toggleEventCallback = std::bind(listenerMethod, owner, std::placeholders::_1);
}

// slider events //
typedef std::function<void(ofxDatGuiSliderEvent)> onSliderEventCallback;
Expand Down
32 changes: 31 additions & 1 deletion src/ofxDatGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ ofxDatGuiButton* ofxDatGui::addButton(string label)
ofxDatGuiToggle* ofxDatGui::addToggle(string label, bool enabled)
{
ofxDatGuiToggle* button = new ofxDatGuiToggle(label, enabled);
button->onButtonEvent(this, &ofxDatGui::onButtonEventCallback);
button->onToggleEvent(this, &ofxDatGui::onToggleEventCallback);
attachItem(button);
return button;
}
Expand Down Expand Up @@ -390,6 +390,7 @@ ofxDatGuiFolder* ofxDatGui::addFolder(string label, ofColor color)
{
ofxDatGuiFolder* folder = new ofxDatGuiFolder(label, color);
folder->onButtonEvent(this, &ofxDatGui::onButtonEventCallback);
folder->onToggleEvent(this, &ofxDatGui::onToggleEventCallback);
folder->onSliderEvent(this, &ofxDatGui::onSliderEventCallback);
folder->on2dPadEvent(this, &ofxDatGui::on2dPadEventCallback);
folder->onMatrixEvent(this, &ofxDatGui::onMatrixEventCallback);
Expand Down Expand Up @@ -438,6 +439,23 @@ ofxDatGuiButton* ofxDatGui::getButton(string bl, string fl)
return o;
}

ofxDatGuiToggle* ofxDatGui::getToggle(string bl, string fl)
{
ofxDatGuiToggle* o = nullptr;
if (fl != ""){
ofxDatGuiFolder* f = static_cast<ofxDatGuiFolder*>(getComponent(ofxDatGuiType::FOLDER, fl));
if (f) o = static_cast<ofxDatGuiToggle*>(f->getComponent(ofxDatGuiType::TOGGLE, bl));
} else{
o = static_cast<ofxDatGuiToggle*>(getComponent(ofxDatGuiType::TOGGLE, bl));
}
if (o==nullptr){
o = ofxDatGuiToggle::getInstance();
ofxDatGuiLog::write(ofxDatGuiMsg::COMPONENT_NOT_FOUND, fl!="" ? fl+"-"+bl : bl);
trash.push_back(o);
}
return o;
}

ofxDatGuiSlider* ofxDatGui::getSlider(string sl, string fl)
{
ofxDatGuiSlider* o = nullptr;
Expand Down Expand Up @@ -632,6 +650,18 @@ void ofxDatGui::onButtonEventCallback(ofxDatGuiButtonEvent e)
}
}

void ofxDatGui::onToggleEventCallback(ofxDatGuiToggleEvent e)
{
if (toggleEventCallback != nullptr) {
toggleEventCallback(e);
// allow toggle events to decay into button events //
} else if (buttonEventCallback != nullptr) {
buttonEventCallback(ofxDatGuiButtonEvent(e.target));
} else{
ofxDatGuiLog::write(ofxDatGuiMsg::EVENT_HANDLER_NULL);
}
}

void ofxDatGui::onSliderEventCallback(ofxDatGuiSliderEvent e)
{
if (sliderEventCallback != nullptr) {
Expand Down
Loading

0 comments on commit 2e482d3

Please sign in to comment.