Browse Source

Add getTitle() method to ofxRadioList

master
Krisjanis Rijnieks 11 years ago
parent
commit
55cc4cfac6
  1. 63
      src/ui/ofxRadioList.cpp
  2. 9
      src/ui/ofxRadioList.h

63
src/ui/ofxRadioList.cpp

@ -1,17 +1,29 @@
#include "ofxRadioList.h"
ofxRadioList::ofxRadioList(){}
ofxRadioList::ofxRadioList()
{
bHasTitle = false;
}
ofxRadioList::ofxRadioList(vector<string> &labels)
{
bHasTitle = false;
setup(labels);
}
ofxRadioList::~ofxRadioList()
ofxRadioList::ofxRadioList(string title, vector<string> &labels)
{
bHasTitle = false;
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);
}
@ -30,11 +42,41 @@ void ofxRadioList::setup(vector<string> &labels)
}
}
void ofxRadioList::setup(string title, vector<string> &labels)
{
ofxLabel* label = new ofxLabel(title);
guiGroup.add(label);
bHasTitle = true;
setup(labels);
}
void ofxRadioList::draw()
{
guiGroup.draw();
}
void ofxRadioList::setTitle(string title)
{
if (bHasTitle) {
ofxLabel* label = static_cast<ofxLabel*>(guiGroup.getControl(0));
label->setup(title);
} else {
ofxLabel* label = new ofxLabel(title);
vector<ofxToggle*> toggles;
int i;
for (i = 0; i < guiGroup.getNumControls(); i++) {
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
toggles.push_back(toggle);
}
guiGroup.clear();
guiGroup.add(label);
bHasTitle = true;
for (i = 0; i < toggles.size(); i++) {
guiGroup.add(toggles[i]);
}
}
}
void ofxRadioList::setPosition(ofPoint p)
{
guiGroup.setPosition(p);
@ -60,10 +102,24 @@ float ofxRadioList::getHeight()
return guiGroup.getHeight();
}
string ofxRadioList::getTitle()
{
if (bHasTitle) {
ofxLabel* label = static_cast<ofxLabel*>(guiGroup.getControl(0));
ofParameter<string>* parameter = static_cast<ofParameter<string>*>(&label->getParameter());
return parameter->get();
} else {
return "";
}
}
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);
@ -79,6 +135,9 @@ void ofxRadioList::onToggleClicked(bool &toggleValue)
// Search for the actual toggle triggering the event
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());

9
src/ui/ofxRadioList.h

@ -3,29 +3,36 @@
#include "ofGraphics.h"
#include "ofxGuiGroup.h"
#include "ofxToggle.h"
#include "ofxLabel.h"
class ofxRadioList
{
public:
ofxRadioList();
ofxRadioList(vector<string> &labels);
ofxRadioList(string title, vector<string> &labels);
~ofxRadioList();
void setup(vector<string> &labels);
void setup(string title, vector<string> &labels);
void draw();
void setTitle(string title);
void setPosition(ofPoint p);
void setPosition(float x, float y);
ofPoint getPosition();
float getWidth();
float getHeight();
string getTitle();
string getItem(int index);
// This event notifies about a toggle being selected and passes it's name to the listeners
// This event notifies about a toggle being selected and passes it's name to the listeners.
// Use ofAddListener(ofxRadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod)
// to listen to this. Listner method void listenerMethod(string & radioName)
ofEvent<string> radioSelectedEvent;
private:
ofxGuiGroup guiGroup;
bool bHasTitle;
void unselectAll();
void onToggleClicked(bool &toggleValue);

Loading…
Cancel
Save