diff --git a/src/ui/ofxRadioList.cpp b/src/ui/ofxRadioList.cpp index c32097f..f86e63f 100644 --- a/src/ui/ofxRadioList.cpp +++ b/src/ui/ofxRadioList.cpp @@ -3,34 +3,32 @@ ofxRadioList::ofxRadioList() { bHasTitle = false; + storedTitle = ""; + storedSelectedItem = 0; } ofxRadioList::ofxRadioList(vector &labels) { - bHasTitle = false; + ofxRadioList(); setup(labels); } ofxRadioList::ofxRadioList(string title, vector &labels) { - bHasTitle = false; + ofxRadioList(); 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); - } + clear(); } void ofxRadioList::setup(vector &labels) { + // Copy incomming labels for later use + storedLabels = labels; + // Create toggles with labels from the labels arg int i; for (i = 0; i < labels.size(); i++) { @@ -44,6 +42,9 @@ void ofxRadioList::setup(vector &labels) void ofxRadioList::setup(string title, vector &labels) { + // Store title for later use + storedTitle = title; + ofxLabel* label = new ofxLabel(title); guiGroup.add(label); bHasTitle = true; @@ -87,6 +88,88 @@ void ofxRadioList::setPosition(float x, float y) guiGroup.setPosition(x, y); } +void ofxRadioList::selectItem(int index) +{ + if (bHasTitle) { + // We don't count the ofxLabel as an item, thus if title is set + // items in guiGroup start from index 1 + index += 1; + } + + if (index >= guiGroup.getNumControls()) { + return; + } + + unselectAll(); + + ofxToggle* toggle = static_cast(guiGroup.getControl(index)); + toggle->removeListener(this, &ofxRadioList::onToggleClicked); + *toggle = true; // Select the specific radio button + toggle->addListener(this, &ofxRadioList::onToggleClicked); + string name = toggle->getName(); + ofNotifyEvent(radioSelectedEvent, name, this); + + storedSelectedItem = index; +} + +void ofxRadioList::enable() +{ + if (guiGroup.getNumControls() >= 0) { + clear(); + } + + // Rebuild everyting + if (bHasTitle) { + setup(storedTitle, storedLabels); + } else { + setup(storedLabels); + } + + if (bHasTitle && storedSelectedItem == 0) { + return; + } + + // Select the stored selected item without throwing an event + ofxToggle* toggle = static_cast(guiGroup.getControl(storedSelectedItem)); + toggle->removeListener(this, &ofxRadioList::onToggleClicked); + *toggle = true; + toggle->addListener(this, &ofxRadioList::onToggleClicked); +} + +void ofxRadioList::disable() +{ + // Just remove everything + clear(); +} + +void ofxRadioList::clear() +{ + 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); + } + guiGroup.clear(); +} + +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); + *toggle = false; + toggle->addListener(this, &ofxRadioList::onToggleClicked); + } +} + ofPoint ofxRadioList::getPosition() { return guiGroup.getPosition(); @@ -113,18 +196,26 @@ string ofxRadioList::getTitle() } } -void ofxRadioList::unselectAll() +string ofxRadioList::getItemName(int index) { - 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); - *toggle = false; - toggle->addListener(this, &ofxRadioList::onToggleClicked); + if (bHasTitle) { + index += 1; + } + + if (index >= guiGroup.getNumControls()) { + return ""; + } + + ofxToggle* toggle = static_cast(guiGroup.getControl(index)); + return toggle->getName(); +} + +int ofxRadioList::size() +{ + if (bHasTitle) { + return guiGroup.getNumControls() - 1; + } else { + return guiGroup.getNumControls(); } } @@ -142,12 +233,20 @@ void ofxRadioList::onToggleClicked(bool &toggleValue) ofParameter* paramPtr = static_cast*>(&toggle->getParameter()); if (&(paramPtr->get()) == &toggleValue) { + /* toggle->removeListener(this, &ofxRadioList::onToggleClicked); *toggle = true; // Select the specific radio button toggle->addListener(this, &ofxRadioList::onToggleClicked); string name = toggle->getName(); ofNotifyEvent(radioSelectedEvent, name, this); //cout << toggle->getName() << endl; // debug + */ + if (bHasTitle) { + selectItem(i - 1); + } else { + selectItem(i); + } + break; } } diff --git a/src/ui/ofxRadioList.h b/src/ui/ofxRadioList.h index 9954513..df960bc 100644 --- a/src/ui/ofxRadioList.h +++ b/src/ui/ofxRadioList.h @@ -19,11 +19,17 @@ public: void setTitle(string title); void setPosition(ofPoint p); void setPosition(float x, float y); + void selectItem(int index); + void enable(); + void disable(); + void clear(); + void unselectAll(); ofPoint getPosition(); float getWidth(); float getHeight(); string getTitle(); - string getItem(int index); + string getItemName(int index); + int size(); // This event notifies about a toggle being selected and passes it's name to the listeners. // Use ofAddListener(ofxRadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod) @@ -31,9 +37,11 @@ public: ofEvent radioSelectedEvent; private: + vector storedLabels; + string storedTitle; ofxGuiGroup guiGroup; bool bHasTitle; + int storedSelectedItem; - void unselectAll(); void onToggleClicked(bool &toggleValue); }; \ No newline at end of file