Browse Source

Refactor RadioList to comply with oF code style

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

374
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);
bool RadioList::selectItemByValue(std::string itemValue) { //string name = toggle->getName();
if (itemValue == "") { // Throw event with value that is image path instead of name
ofLogNotice("RadioList") << "Item value empty"; string value = storedValues[index];
return false; ofNotifyEvent(onRadioSelected, value, this);
} storedSelectedItem = index;
unselectAll(); }
int itemIndex = -1;
for (int i = 0; i < storedValues.size(); i++) { bool RadioList::selectItemByValue(std::string itemValue){
if (itemValue == storedValues[i]) { if(itemValue == ""){
itemIndex = i; ofLogNotice("RadioList") << "Item value empty";
break; return false;
} }
} unselectAll();
if (itemIndex >= 0) { int itemIndex = -1;
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(itemIndex)); for(int i = 0; i < storedValues.size(); i++){
toggle->removeListener(this, &RadioList::onToggleClicked); if(itemValue == storedValues[i]){
*toggle = true; // Select the specific radio button itemIndex = i;
toggle->addListener(this, &RadioList::onToggleClicked); break;
return true; }
} }
ofLogNotice("RadioList") << "Item with value " << itemValue << " not found"; if(itemIndex >= 0){
return false; ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(itemIndex));
} toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true; // Select the specific radio button
void RadioList::enable() { toggle->addListener(this, &RadioList::onToggleClicked);
if (guiGroup.getNumControls() >= 0) { return true;
clear(); }
} ofLogNotice("RadioList") << "Item with value " << itemValue << " not found";
return false;
// Rebuild everyting }
setup(storedTitle, storedLabels, storedValues);
void RadioList::enable(){
// Select the stored selected item without throwing an event if(guiGroup.getNumControls() > 0){
ofxToggle* toggle = clear();
static_cast<ofxToggle*>(guiGroup.getControl(storedSelectedItem)); }
toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true; // Rebuild everyting
toggle->addListener(this, &RadioList::onToggleClicked); setup(storedTitle, storedLabels, storedValues);
cout << "num items after enable: " << guiGroup.getNumControls() << endl; // Select the stored selected item without throwing an event
} ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(storedSelectedItem));
toggle->removeListener(this, &RadioList::onToggleClicked);
void RadioList::disable() { *toggle = true;
// Just remove everything toggle->addListener(this, &RadioList::onToggleClicked);
clear();
} cout << "num items after enable: " << guiGroup.getNumControls() << endl;
}
void RadioList::clear() {
int i; void RadioList::disable(){
for (i = 0; i < guiGroup.getNumControls(); i++) { // Just remove everything
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i)); clear();
toggle->removeListener(this, &RadioList::onToggleClicked); }
delete toggle;
} void RadioList::clear(){
guiGroup.clear(); int i;
} for(i = 0; i < guiGroup.getNumControls(); i++){
ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(i));
void RadioList::unselectAll() { toggle->removeListener(this, &RadioList::onToggleClicked);
int i; delete toggle;
for (i = 0; i < guiGroup.getNumControls(); i++) { }
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i)); guiGroup.clear();
ofParameter<bool>* paramPtr = }
static_cast<ofParameter<bool>*>(&toggle->getParameter());
toggle->removeListener(this, &RadioList::onToggleClicked); void RadioList::unselectAll(){
*toggle = false; int i;
toggle->addListener(this, &RadioList::onToggleClicked); for(i = 0; i < guiGroup.getNumControls(); i++){
} ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(i));
} ofParameter<bool> * paramPtr = static_cast<ofParameter<bool> *>(&toggle->getParameter());
toggle->removeListener(this, &RadioList::onToggleClicked);
ofPoint RadioList::getPosition() { return guiGroup.getPosition(); } *toggle = false;
toggle->addListener(this, &RadioList::onToggleClicked);
float RadioList::getWidth() { return guiGroup.getWidth(); } }
}
float RadioList::getHeight() { return guiGroup.getHeight(); }
ofPoint RadioList::getPosition(){
string RadioList::getTitle() { return guiGroup.getName(); } return guiGroup.getPosition();
}
string RadioList::getItemName(int index) {
if (index >= guiGroup.getNumControls()) { float RadioList::getWidth(){
return ""; return guiGroup.getWidth();
} }
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index)); float RadioList::getHeight(){
return toggle->getName(); return guiGroup.getHeight();
} }
int RadioList::size() { return storedValues.size(); } string RadioList::getTitle(){
return guiGroup.getName();
void RadioList::onToggleClicked(bool& toggleValue) }
{
unselectAll(); string RadioList::getItemName(int index){
if(index >= guiGroup.getNumControls()){
// Search for the actual toggle triggering the event return "";
int i; }
for (i = 0; i < guiGroup.getNumControls(); i++) {
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i)); ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(index));
ofParameter<bool>* paramPtr = return toggle->getName();
static_cast<ofParameter<bool>*>(&toggle->getParameter()); }
if (&(paramPtr->get()) == &toggleValue) { int RadioList::size(){
selectItem(i); return storedValues.size();
break; }
}
} 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<ofxToggle *>(guiGroup.getControl(i));
ofParameter<bool> * paramPtr = static_cast<ofParameter<bool> *>(&toggle->getParameter());
if(&(paramPtr->get()) == &toggleValue){
selectItem(i);
break;
}
}
}
} // namespace piMapper
} // namespace ofx

85
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(vector<string> & labels, vector<string> & values);
void setup(string title, vector<string>& labels, vector<string>& values); void setup(string title, vector<string> & labels, vector<string> & values);
void draw(); void draw();
void setTitle(string title); void setTitle(string title);
void setPosition(ofPoint p); void setPosition(ofPoint p);
void setPosition(float x, float y); void setPosition(float x, float y);
void selectItem(int index); void selectItem(int index);
bool selectItemByValue(std::string itemValue); bool selectItemByValue(std::string itemValue);
void enable(); void enable();
void disable(); void disable();
void clear(); void clear();
void unselectAll(); void unselectAll();
ofPoint getPosition(); ofPoint getPosition();
float getWidth(); float getWidth();
float getHeight(); float getHeight();
string getTitle(); string getTitle();
string getItemName(int index); string getItemName(int index);
int size(); int size();
// This event notifies about a toggle being selected and passes it's name to // This event notifies about a toggle being selected and passes it's name to
// the listeners. // the listeners.
// Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr,
// &listenerClass::listenerMethod) // &listenerClass::listenerMethod)
// to listen to this. Listner method void listenerMethod(string & radioName) // to listen to this. Listner method void listenerMethod(string & radioName)
ofEvent<string> onRadioSelected; ofEvent<string> onRadioSelected;
private: private:
vector<string> storedLabels; vector<string> storedLabels;
vector<string> storedValues; vector<string> storedValues;
string storedTitle; string storedTitle;
ofxGuiGroup guiGroup; ofxGuiGroup guiGroup;
int storedSelectedItem; int storedSelectedItem;
void onToggleClicked(bool &toggleValue); void onToggleClicked(bool & toggleValue);
};
} };
} } // namespace piMapper
} // namespace ofx
Loading…
Cancel
Save