Browse Source

Fix ofxRadioList to work with ofxSourcesEditor

Add getItemName method
Add enable and disable methods
Add storedLabels and storedTitle variables for recreating toggles on enable
Add clear method
master
Krisjanis Rijnieks 11 years ago
parent
commit
e040ea7d36
  1. 141
      src/ui/ofxRadioList.cpp
  2. 12
      src/ui/ofxRadioList.h

141
src/ui/ofxRadioList.cpp

@ -3,34 +3,32 @@
ofxRadioList::ofxRadioList()
{
bHasTitle = false;
storedTitle = "";
storedSelectedItem = 0;
}
ofxRadioList::ofxRadioList(vector<string> &labels)
{
bHasTitle = false;
ofxRadioList();
setup(labels);
}
ofxRadioList::ofxRadioList(string title, vector<string> &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<ofxToggle*>(guiGroup.getControl(i));
toggle->removeListener(this, &ofxRadioList::onToggleClicked);
}
clear();
}
void ofxRadioList::setup(vector<string> &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<string> &labels)
void ofxRadioList::setup(string title, vector<string> &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<ofxToggle*>(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<ofxToggle*>(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<ofxToggle*>(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<ofxToggle*>(guiGroup.getControl(i));
ofParameter<bool>* paramPtr = static_cast<ofParameter<bool>*>(&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<ofxToggle*>(guiGroup.getControl(i));
ofParameter<bool>* paramPtr = static_cast<ofParameter<bool>*>(&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<ofxToggle*>(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<bool>* paramPtr = static_cast<ofParameter<bool>*>(&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;
}
}

12
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<string> radioSelectedEvent;
private:
vector<string> storedLabels;
string storedTitle;
ofxGuiGroup guiGroup;
bool bHasTitle;
int storedSelectedItem;
void unselectAll();
void onToggleClicked(bool &toggleValue);
};
Loading…
Cancel
Save