diff --git a/src/ui/ofxRadioList.cpp b/src/ui/ofxRadioList.cpp index 9faddf2..c32097f 100644 --- a/src/ui/ofxRadioList.cpp +++ b/src/ui/ofxRadioList.cpp @@ -1,17 +1,29 @@ #include "ofxRadioList.h" -ofxRadioList::ofxRadioList(){} +ofxRadioList::ofxRadioList() +{ + bHasTitle = false; +} ofxRadioList::ofxRadioList(vector &labels) { + bHasTitle = false; setup(labels); } -ofxRadioList::~ofxRadioList() +ofxRadioList::ofxRadioList(string title, vector &labels) { + bHasTitle = false; + setup(title, labels); +} +ofxRadioList::~ofxRadioList() +{ int i; for (i = 0; i < guiGroup.getNumControls(); i++) { + if (bHasTitle && i == 0) { + continue; + } ofxToggle* toggle = static_cast(guiGroup.getControl(i)); toggle->removeListener(this, &ofxRadioList::onToggleClicked); } @@ -30,11 +42,41 @@ void ofxRadioList::setup(vector &labels) } } +void ofxRadioList::setup(string title, vector &labels) +{ + ofxLabel* label = new ofxLabel(title); + guiGroup.add(label); + bHasTitle = true; + setup(labels); +} + void ofxRadioList::draw() { guiGroup.draw(); } +void ofxRadioList::setTitle(string title) +{ + if (bHasTitle) { + ofxLabel* label = static_cast(guiGroup.getControl(0)); + label->setup(title); + } else { + ofxLabel* label = new ofxLabel(title); + vector toggles; + int i; + for (i = 0; i < guiGroup.getNumControls(); i++) { + ofxToggle* toggle = static_cast(guiGroup.getControl(i)); + toggles.push_back(toggle); + } + guiGroup.clear(); + guiGroup.add(label); + bHasTitle = true; + for (i = 0; i < toggles.size(); i++) { + guiGroup.add(toggles[i]); + } + } +} + void ofxRadioList::setPosition(ofPoint p) { guiGroup.setPosition(p); @@ -60,10 +102,24 @@ float ofxRadioList::getHeight() return guiGroup.getHeight(); } +string ofxRadioList::getTitle() +{ + if (bHasTitle) { + ofxLabel* label = static_cast(guiGroup.getControl(0)); + ofParameter* parameter = static_cast*>(&label->getParameter()); + return parameter->get(); + } else { + return ""; + } +} + void ofxRadioList::unselectAll() { int i; for (i = 0; i < guiGroup.getNumControls(); i++) { + if (bHasTitle && i == 0) { + continue; + } ofxToggle* toggle = static_cast(guiGroup.getControl(i)); ofParameter* paramPtr = static_cast*>(&toggle->getParameter()); toggle->removeListener(this, &ofxRadioList::onToggleClicked); @@ -79,6 +135,9 @@ void ofxRadioList::onToggleClicked(bool &toggleValue) // Search for the actual toggle triggering the event int i; for (i = 0; i < guiGroup.getNumControls(); i++) { + if (bHasTitle && i == 0) { + continue; + } ofxToggle* toggle = static_cast(guiGroup.getControl(i)); ofParameter* paramPtr = static_cast*>(&toggle->getParameter()); diff --git a/src/ui/ofxRadioList.h b/src/ui/ofxRadioList.h index c0aed7d..9954513 100644 --- a/src/ui/ofxRadioList.h +++ b/src/ui/ofxRadioList.h @@ -3,29 +3,36 @@ #include "ofGraphics.h" #include "ofxGuiGroup.h" #include "ofxToggle.h" +#include "ofxLabel.h" class ofxRadioList { public: ofxRadioList(); ofxRadioList(vector &labels); + ofxRadioList(string title, vector &labels); ~ofxRadioList(); void setup(vector &labels); + void setup(string title, vector &labels); void draw(); + void setTitle(string title); void setPosition(ofPoint p); void setPosition(float x, float y); ofPoint getPosition(); float getWidth(); float getHeight(); + string getTitle(); + string getItem(int index); - // This event notifies about a toggle being selected and passes it's name to the listeners + // This event notifies about a toggle being selected and passes it's name to the listeners. // Use ofAddListener(ofxRadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod) // to listen to this. Listner method void listenerMethod(string & radioName) ofEvent radioSelectedEvent; private: ofxGuiGroup guiGroup; + bool bHasTitle; void unselectAll(); void onToggleClicked(bool &toggleValue);