From ed7d0fab02ca3853bfd91b34d0ebf41c97f4a469 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Sat, 26 Dec 2015 17:08:28 +0000 Subject: [PATCH] Refactor RadioList to comply with oF code style --- src/UserInterface/RadioList.cpp | 372 +++++++++++++++++--------------- src/UserInterface/RadioList.h | 89 ++++---- 2 files changed, 239 insertions(+), 222 deletions(-) diff --git a/src/UserInterface/RadioList.cpp b/src/UserInterface/RadioList.cpp index 6722c4f..3a3ed0d 100644 --- a/src/UserInterface/RadioList.cpp +++ b/src/UserInterface/RadioList.cpp @@ -1,183 +1,199 @@ #include "RadioList.h" -namespace ofx { -namespace piMapper { -RadioList::RadioList() { - storedTitle = ""; - storedSelectedItem = 0; -} - -RadioList::RadioList(vector& labels, vector& values) { - RadioList(); - setup(labels, values); -} - -RadioList::RadioList(string title, vector& labels, vector& values) { - RadioList(); - setup(title, labels, values); -} - -RadioList::~RadioList() { clear(); } - -void RadioList::setup(vector& labels, vector& values) { - // Copy incomming labels for later use - storedLabels = labels; - storedValues = values; - - // Create toggles with labels from the labels arg - int i; - for (i = 0; i < labels.size(); i++) { - ofxToggle* toggle = new ofxToggle(); - toggle->setup(false); - toggle->setName(labels[i]); - toggle->addListener(this, &RadioList::onToggleClicked); - guiGroup.add(toggle); +namespace ofx{ + namespace piMapper{ + RadioList::RadioList(){ + storedTitle = ""; + storedSelectedItem = 0; + } + + RadioList::RadioList(vector & labels, vector & values){ + RadioList(); + setup(labels, values); + } + + RadioList::RadioList(string title, vector & labels, vector & values){ + RadioList(); + setup(title, labels, values); + } + + RadioList::~RadioList(){ + clear(); + } + + void RadioList::setup(vector & labels, vector & values){ + + // Copy incomming labels for later use + storedLabels = labels; + storedValues = values; + + // Create toggles with labels from the labels arg + int i; + for(i = 0; i < labels.size(); i++){ + ofxToggle * toggle = new ofxToggle(); + toggle->setup(false); + toggle->setName(labels[i]); + toggle->addListener(this, &RadioList::onToggleClicked); + guiGroup.add(toggle); #if OF_VERSION_MAJOR == 0 && (OF_VERSION_MINOR >= 8 && OF_VERSION_PATCH >= 2) || (OF_VERSION_MINOR >= 9 && OF_VERSION_PATCH >= 0) - toggle->registerMouseEvents(); + toggle->registerMouseEvents(); #endif - } -} - -void RadioList::setup(string title, vector& labels, vector& values) { - // Store title for later use - storedTitle = title; - guiGroup.setName(title); - setup(labels, values); -} - -void RadioList::draw() { guiGroup.draw(); } - -void RadioList::setTitle(string title) { - storedTitle = title; - guiGroup.setName(title); -} - -void RadioList::setPosition(ofPoint p) { guiGroup.setPosition(p); } - -void RadioList::setPosition(float x, float y) { guiGroup.setPosition(x, y); } - -void RadioList::selectItem(int index) { - if (index >= guiGroup.getNumControls()) { - return; - } - - unselectAll(); - - ofxToggle* toggle = static_cast(guiGroup.getControl(index)); - toggle->removeListener(this, &RadioList::onToggleClicked); - *toggle = true; // Select the specific radio button - toggle->addListener(this, &RadioList::onToggleClicked); - //string name = toggle->getName(); - // Throw event with value that is image path instead of name - string value = storedValues[index]; - ofNotifyEvent(onRadioSelected, value, this); - storedSelectedItem = index; -} + } + } + + void RadioList::setup(string title, vector & labels, vector & values){ + + // Store title for later use + storedTitle = title; + guiGroup.setName(title); + setup(labels, values); + } + + void RadioList::draw(){ + guiGroup.draw(); + } + + void RadioList::setTitle(string title){ + storedTitle = title; + guiGroup.setName(title); + } + + void RadioList::setPosition(ofPoint p){ + guiGroup.setPosition(p); + } + + void RadioList::setPosition(float x, float y){ + guiGroup.setPosition(x, y); + } + + void RadioList::selectItem(int index){ + if(index >= guiGroup.getNumControls()){ + return; + } + + unselectAll(); + + ofxToggle * toggle = static_cast(guiGroup.getControl(index)); + toggle->removeListener(this, &RadioList::onToggleClicked); + *toggle = true; // Select the specific radio button + toggle->addListener(this, &RadioList::onToggleClicked); + //string name = toggle->getName(); + // Throw event with value that is image path instead of name + string value = storedValues[index]; + ofNotifyEvent(onRadioSelected, value, this); + storedSelectedItem = index; + } - bool RadioList::selectItemByValue(std::string itemValue) { - if (itemValue == "") { - ofLogNotice("RadioList") << "Item value empty"; - return false; - } - unselectAll(); - int itemIndex = -1; - for (int i = 0; i < storedValues.size(); i++) { - if (itemValue == storedValues[i]) { - itemIndex = i; - break; - } - } - if (itemIndex >= 0) { - ofxToggle* toggle = static_cast(guiGroup.getControl(itemIndex)); - toggle->removeListener(this, &RadioList::onToggleClicked); - *toggle = true; // Select the specific radio button - toggle->addListener(this, &RadioList::onToggleClicked); - return true; - } - ofLogNotice("RadioList") << "Item with value " << itemValue << " not found"; - return false; - } - -void RadioList::enable() { - if (guiGroup.getNumControls() >= 0) { - clear(); - } - - // Rebuild everyting - setup(storedTitle, storedLabels, storedValues); - - // Select the stored selected item without throwing an event - ofxToggle* toggle = - static_cast(guiGroup.getControl(storedSelectedItem)); - toggle->removeListener(this, &RadioList::onToggleClicked); - *toggle = true; - toggle->addListener(this, &RadioList::onToggleClicked); - - cout << "num items after enable: " << guiGroup.getNumControls() << endl; -} - -void RadioList::disable() { - // Just remove everything - clear(); -} - -void RadioList::clear() { - int i; - for (i = 0; i < guiGroup.getNumControls(); i++) { - ofxToggle* toggle = static_cast(guiGroup.getControl(i)); - toggle->removeListener(this, &RadioList::onToggleClicked); - delete toggle; - } - guiGroup.clear(); -} - -void RadioList::unselectAll() { - int i; - for (i = 0; i < guiGroup.getNumControls(); i++) { - ofxToggle* toggle = static_cast(guiGroup.getControl(i)); - ofParameter* paramPtr = - static_cast*>(&toggle->getParameter()); - toggle->removeListener(this, &RadioList::onToggleClicked); - *toggle = false; - toggle->addListener(this, &RadioList::onToggleClicked); - } -} - -ofPoint RadioList::getPosition() { return guiGroup.getPosition(); } - -float RadioList::getWidth() { return guiGroup.getWidth(); } - -float RadioList::getHeight() { return guiGroup.getHeight(); } - -string RadioList::getTitle() { return guiGroup.getName(); } - -string RadioList::getItemName(int index) { - if (index >= guiGroup.getNumControls()) { - return ""; - } - - ofxToggle* toggle = static_cast(guiGroup.getControl(index)); - return toggle->getName(); -} - -int RadioList::size() { return storedValues.size(); } - -void RadioList::onToggleClicked(bool& toggleValue) -{ - unselectAll(); - - // Search for the actual toggle triggering the event - int i; - for (i = 0; i < guiGroup.getNumControls(); i++) { - ofxToggle* toggle = static_cast(guiGroup.getControl(i)); - ofParameter* paramPtr = - static_cast*>(&toggle->getParameter()); - - if (&(paramPtr->get()) == &toggleValue) { - selectItem(i); - break; - } - } -} -} -} + bool RadioList::selectItemByValue(std::string itemValue){ + if(itemValue == ""){ + ofLogNotice("RadioList") << "Item value empty"; + return false; + } + unselectAll(); + int itemIndex = -1; + for(int i = 0; i < storedValues.size(); i++){ + if(itemValue == storedValues[i]){ + itemIndex = i; + break; + } + } + if(itemIndex >= 0){ + ofxToggle * toggle = static_cast(guiGroup.getControl(itemIndex)); + toggle->removeListener(this, &RadioList::onToggleClicked); + *toggle = true; // Select the specific radio button + toggle->addListener(this, &RadioList::onToggleClicked); + return true; + } + ofLogNotice("RadioList") << "Item with value " << itemValue << " not found"; + return false; + } + + void RadioList::enable(){ + if(guiGroup.getNumControls() > 0){ + clear(); + } + + // Rebuild everyting + setup(storedTitle, storedLabels, storedValues); + + // Select the stored selected item without throwing an event + ofxToggle * toggle = static_cast(guiGroup.getControl(storedSelectedItem)); + toggle->removeListener(this, &RadioList::onToggleClicked); + *toggle = true; + toggle->addListener(this, &RadioList::onToggleClicked); + + cout << "num items after enable: " << guiGroup.getNumControls() << endl; + } + + void RadioList::disable(){ + // Just remove everything + clear(); + } + + void RadioList::clear(){ + int i; + for(i = 0; i < guiGroup.getNumControls(); i++){ + ofxToggle * toggle = static_cast(guiGroup.getControl(i)); + toggle->removeListener(this, &RadioList::onToggleClicked); + delete toggle; + } + guiGroup.clear(); + } + + void RadioList::unselectAll(){ + int i; + for(i = 0; i < guiGroup.getNumControls(); i++){ + ofxToggle * toggle = static_cast(guiGroup.getControl(i)); + ofParameter * paramPtr = static_cast *>(&toggle->getParameter()); + toggle->removeListener(this, &RadioList::onToggleClicked); + *toggle = false; + toggle->addListener(this, &RadioList::onToggleClicked); + } + } + + ofPoint RadioList::getPosition(){ + return guiGroup.getPosition(); + } + + float RadioList::getWidth(){ + return guiGroup.getWidth(); + } + + float RadioList::getHeight(){ + return guiGroup.getHeight(); + } + + string RadioList::getTitle(){ + return guiGroup.getName(); + } + + string RadioList::getItemName(int index){ + if(index >= guiGroup.getNumControls()){ + return ""; + } + + ofxToggle * toggle = static_cast(guiGroup.getControl(index)); + return toggle->getName(); + } + + int RadioList::size(){ + return storedValues.size(); + } + + void RadioList::onToggleClicked(bool & toggleValue){ + unselectAll(); + + // Search for the actual toggle triggering the event + int i; + for(i = 0; i < guiGroup.getNumControls(); i++){ + ofxToggle * toggle = static_cast(guiGroup.getControl(i)); + ofParameter * paramPtr = static_cast *>(&toggle->getParameter()); + + if(&(paramPtr->get()) == &toggleValue){ + selectItem(i); + break; + } + } + } + } // namespace piMapper +} // namespace ofx diff --git a/src/UserInterface/RadioList.h b/src/UserInterface/RadioList.h index c0e1daf..82dfaa1 100644 --- a/src/UserInterface/RadioList.h +++ b/src/UserInterface/RadioList.h @@ -5,49 +5,50 @@ #include "ofxToggle.h" #include "ofxLabel.h" -namespace ofx { -namespace piMapper { -class RadioList { - public: - RadioList(); - RadioList(vector& labels, vector& values); - RadioList(string title, vector& labels, vector& values); - ~RadioList(); +namespace ofx{ + namespace piMapper{ + class RadioList{ + public: + RadioList(); + RadioList(vector & labels, vector & values); + RadioList(string title, vector & labels, vector & values); + ~RadioList(); + + void setup(vector & labels, vector & values); + void setup(string title, vector & labels, vector & values); + void draw(); + void setTitle(string title); + void setPosition(ofPoint p); + void setPosition(float x, float y); + void selectItem(int index); + bool selectItemByValue(std::string itemValue); + void enable(); + void disable(); + void clear(); + void unselectAll(); + ofPoint getPosition(); + float getWidth(); + float getHeight(); + string getTitle(); + string getItemName(int index); + int size(); + + // This event notifies about a toggle being selected and passes it's name to + // the listeners. + // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, + // &listenerClass::listenerMethod) + // to listen to this. Listner method void listenerMethod(string & radioName) + ofEvent onRadioSelected; - void setup(vector &labels, vector& values); - void setup(string title, vector& labels, vector& values); - void draw(); - void setTitle(string title); - void setPosition(ofPoint p); - void setPosition(float x, float y); - void selectItem(int index); - bool selectItemByValue(std::string itemValue); - void enable(); - void disable(); - void clear(); - void unselectAll(); - ofPoint getPosition(); - float getWidth(); - float getHeight(); - string getTitle(); - string getItemName(int index); - int size(); + private: + vector storedLabels; + vector storedValues; + string storedTitle; + ofxGuiGroup guiGroup; + int storedSelectedItem; - // This event notifies about a toggle being selected and passes it's name to - // the listeners. - // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, - // &listenerClass::listenerMethod) - // to listen to this. Listner method void listenerMethod(string & radioName) - ofEvent onRadioSelected; - - private: - vector storedLabels; - vector storedValues; - string storedTitle; - ofxGuiGroup guiGroup; - int storedSelectedItem; - - void onToggleClicked(bool &toggleValue); -}; -} -} \ No newline at end of file + void onToggleClicked(bool & toggleValue); + + }; + } // namespace piMapper +} // namespace ofx \ No newline at end of file