Browse Source

Refactor RadioList to comply with oF code style

master
Krisjanis Rijnieks 10 years ago
parent
commit
ed7d0fab02
  1. 372
      src/UserInterface/RadioList.cpp
  2. 89
      src/UserInterface/RadioList.h

372
src/UserInterface/RadioList.cpp

@ -1,183 +1,199 @@
#include "RadioList.h" #include "RadioList.h"
namespace ofx { namespace ofx{
namespace piMapper { namespace piMapper{
RadioList::RadioList() { RadioList::RadioList(){
storedTitle = ""; storedTitle = "";
storedSelectedItem = 0; storedSelectedItem = 0;
} }
RadioList::RadioList(vector<string>& labels, vector<string>& values) { RadioList::RadioList(vector<string> & labels, vector<string> & values){
RadioList(); RadioList();
setup(labels, values); setup(labels, values);
} }
RadioList::RadioList(string title, vector<string>& labels, vector<string>& values) { RadioList::RadioList(string title, vector<string> & labels, vector<string> & values){
RadioList(); RadioList();
setup(title, labels, values); setup(title, labels, values);
} }
RadioList::~RadioList() { clear(); } RadioList::~RadioList(){
clear();
void RadioList::setup(vector<string>& labels, vector<string>& values) { }
// Copy incomming labels for later use
storedLabels = labels; void RadioList::setup(vector<string> & labels, vector<string> & values){
storedValues = values;
// Copy incomming labels for later use
// Create toggles with labels from the labels arg storedLabels = labels;
int i; storedValues = values;
for (i = 0; i < labels.size(); i++) {
ofxToggle* toggle = new ofxToggle(); // Create toggles with labels from the labels arg
toggle->setup(false); int i;
toggle->setName(labels[i]); for(i = 0; i < labels.size(); i++){
toggle->addListener(this, &RadioList::onToggleClicked); ofxToggle * toggle = new ofxToggle();
guiGroup.add(toggle); 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) #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 #endif
} }
} }
void RadioList::setup(string title, vector<string>& labels, vector<string>& values) { void RadioList::setup(string title, vector<string> & labels, vector<string> & values){
// Store title for later use
storedTitle = title; // Store title for later use
guiGroup.setName(title); storedTitle = title;
setup(labels, values); guiGroup.setName(title);
} setup(labels, values);
}
void RadioList::draw() { guiGroup.draw(); }
void RadioList::draw(){
void RadioList::setTitle(string title) { guiGroup.draw();
storedTitle = title; }
guiGroup.setName(title);
} void RadioList::setTitle(string title){
storedTitle = title;
void RadioList::setPosition(ofPoint p) { guiGroup.setPosition(p); } guiGroup.setName(title);
}
void RadioList::setPosition(float x, float y) { guiGroup.setPosition(x, y); }
void RadioList::setPosition(ofPoint p){
void RadioList::selectItem(int index) { guiGroup.setPosition(p);
if (index >= guiGroup.getNumControls()) { }
return;
} void RadioList::setPosition(float x, float y){
guiGroup.setPosition(x, y);
unselectAll(); }
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index)); void RadioList::selectItem(int index){
toggle->removeListener(this, &RadioList::onToggleClicked); if(index >= guiGroup.getNumControls()){
*toggle = true; // Select the specific radio button return;
toggle->addListener(this, &RadioList::onToggleClicked); }
//string name = toggle->getName();
// Throw event with value that is image path instead of name unselectAll();
string value = storedValues[index];
ofNotifyEvent(onRadioSelected, value, this); ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(index));
storedSelectedItem = 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) { bool RadioList::selectItemByValue(std::string itemValue){
if (itemValue == "") { if(itemValue == ""){
ofLogNotice("RadioList") << "Item value empty"; ofLogNotice("RadioList") << "Item value empty";
return false; return false;
} }
unselectAll(); unselectAll();
int itemIndex = -1; int itemIndex = -1;
for (int i = 0; i < storedValues.size(); i++) { for(int i = 0; i < storedValues.size(); i++){
if (itemValue == storedValues[i]) { if(itemValue == storedValues[i]){
itemIndex = i; itemIndex = i;
break; break;
} }
} }
if (itemIndex >= 0) { if(itemIndex >= 0){
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(itemIndex)); ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(itemIndex));
toggle->removeListener(this, &RadioList::onToggleClicked); toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true; // Select the specific radio button *toggle = true; // Select the specific radio button
toggle->addListener(this, &RadioList::onToggleClicked); toggle->addListener(this, &RadioList::onToggleClicked);
return true; return true;
} }
ofLogNotice("RadioList") << "Item with value " << itemValue << " not found"; ofLogNotice("RadioList") << "Item with value " << itemValue << " not found";
return false; return false;
} }
void RadioList::enable() { void RadioList::enable(){
if (guiGroup.getNumControls() >= 0) { if(guiGroup.getNumControls() > 0){
clear(); clear();
} }
// Rebuild everyting // Rebuild everyting
setup(storedTitle, storedLabels, storedValues); setup(storedTitle, storedLabels, storedValues);
// Select the stored selected item without throwing an event // Select the stored selected item without throwing an event
ofxToggle* toggle = ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(storedSelectedItem));
static_cast<ofxToggle*>(guiGroup.getControl(storedSelectedItem)); toggle->removeListener(this, &RadioList::onToggleClicked);
toggle->removeListener(this, &RadioList::onToggleClicked); *toggle = true;
*toggle = true; toggle->addListener(this, &RadioList::onToggleClicked);
toggle->addListener(this, &RadioList::onToggleClicked);
cout << "num items after enable: " << guiGroup.getNumControls() << endl;
cout << "num items after enable: " << guiGroup.getNumControls() << endl; }
}
void RadioList::disable(){
void RadioList::disable() { // Just remove everything
// Just remove everything clear();
clear(); }
}
void RadioList::clear(){
void RadioList::clear() { int i;
int i; for(i = 0; i < guiGroup.getNumControls(); i++){
for (i = 0; i < guiGroup.getNumControls(); i++) { ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(i));
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i)); toggle->removeListener(this, &RadioList::onToggleClicked);
toggle->removeListener(this, &RadioList::onToggleClicked); delete toggle;
delete toggle; }
} guiGroup.clear();
guiGroup.clear(); }
}
void RadioList::unselectAll(){
void RadioList::unselectAll() { int i;
int i; for(i = 0; i < guiGroup.getNumControls(); i++){
for (i = 0; i < guiGroup.getNumControls(); i++) { ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(i));
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i)); ofParameter<bool> * paramPtr = static_cast<ofParameter<bool> *>(&toggle->getParameter());
ofParameter<bool>* paramPtr = toggle->removeListener(this, &RadioList::onToggleClicked);
static_cast<ofParameter<bool>*>(&toggle->getParameter()); *toggle = false;
toggle->removeListener(this, &RadioList::onToggleClicked); toggle->addListener(this, &RadioList::onToggleClicked);
*toggle = false; }
toggle->addListener(this, &RadioList::onToggleClicked); }
}
} ofPoint RadioList::getPosition(){
return guiGroup.getPosition();
ofPoint RadioList::getPosition() { return guiGroup.getPosition(); } }
float RadioList::getWidth() { return guiGroup.getWidth(); } float RadioList::getWidth(){
return guiGroup.getWidth();
float RadioList::getHeight() { return guiGroup.getHeight(); } }
string RadioList::getTitle() { return guiGroup.getName(); } float RadioList::getHeight(){
return guiGroup.getHeight();
string RadioList::getItemName(int index) { }
if (index >= guiGroup.getNumControls()) {
return ""; string RadioList::getTitle(){
} return guiGroup.getName();
}
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index));
return toggle->getName(); string RadioList::getItemName(int index){
} if(index >= guiGroup.getNumControls()){
return "";
int RadioList::size() { return storedValues.size(); } }
void RadioList::onToggleClicked(bool& toggleValue) ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(index));
{ return toggle->getName();
unselectAll(); }
// Search for the actual toggle triggering the event int RadioList::size(){
int i; return storedValues.size();
for (i = 0; i < guiGroup.getNumControls(); i++) { }
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
ofParameter<bool>* paramPtr = void RadioList::onToggleClicked(bool & toggleValue){
static_cast<ofParameter<bool>*>(&toggle->getParameter()); unselectAll();
if (&(paramPtr->get()) == &toggleValue) { // Search for the actual toggle triggering the event
selectItem(i); int i;
break; for(i = 0; i < guiGroup.getNumControls(); i++){
} ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(i));
} ofParameter<bool> * paramPtr = static_cast<ofParameter<bool> *>(&toggle->getParameter());
}
} if(&(paramPtr->get()) == &toggleValue){
} selectItem(i);
break;
}
}
}
} // namespace piMapper
} // namespace ofx

89
src/UserInterface/RadioList.h

@ -5,49 +5,50 @@
#include "ofxToggle.h" #include "ofxToggle.h"
#include "ofxLabel.h" #include "ofxLabel.h"
namespace ofx { namespace ofx{
namespace piMapper { namespace piMapper{
class RadioList { class RadioList{
public: public:
RadioList(); RadioList();
RadioList(vector<string>& labels, vector<string>& values); RadioList(vector<string> & labels, vector<string> & values);
RadioList(string title, vector<string>& labels, vector<string>& values); RadioList(string title, vector<string> & labels, vector<string> & values);
~RadioList(); ~RadioList();
void setup(vector<string> & labels, vector<string> & values);
void setup(string title, vector<string> & labels, vector<string> & 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<string> onRadioSelected;
void setup(vector<string> &labels, vector<string>& values); private:
void setup(string title, vector<string>& labels, vector<string>& values); vector<string> storedLabels;
void draw(); vector<string> storedValues;
void setTitle(string title); string storedTitle;
void setPosition(ofPoint p); ofxGuiGroup guiGroup;
void setPosition(float x, float y); int storedSelectedItem;
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 void onToggleClicked(bool & toggleValue);
// the listeners.
// Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, };
// &listenerClass::listenerMethod) } // namespace piMapper
// to listen to this. Listner method void listenerMethod(string & radioName) } // namespace ofx
ofEvent<string> onRadioSelected;
private:
vector<string> storedLabels;
vector<string> storedValues;
string storedTitle;
ofxGuiGroup guiGroup;
int storedSelectedItem;
void onToggleClicked(bool &toggleValue);
};
}
}
Loading…
Cancel
Save