diff --git a/README.md b/README.md index ffe8880..41ded08 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Development As the projects gets a bit more popular, I see that people want to add missing features. I have a whole bunch of features that I want to add in future releases, but right now I'm trying to understand how to keep it more or less organized. -Currently I have decided to use [A successful Git branching model](http://nvie.com/posts/a-successful-git) by [Vincent Driessen](https://twitter.com/nvie), so read this article and I do not doubt that it will help you with other Git related projects. +Currently I have decided to use [A successful Git branching model](http://nvie.com/posts/a-successful-git-branching-model/) by [Vincent Driessen](https://twitter.com/nvie), so read this article and I do not doubt that it will help you with other Git related projects. I'm still working on boosting my understanding about the issue tracking system on GitHub, I believe that it would be the best way how to keep new feature requests and bugfixes organized. diff --git a/example/addons.make b/example/addons.make index 802cd29..e98663f 100644 --- a/example/addons.make +++ b/example/addons.make @@ -1,3 +1,3 @@ ofxPiMapper -ofxUI +ofxGui ofxXmlSettings diff --git a/src/ofxSourcesEditor.cpp b/src/ofxSourcesEditor.cpp index 372f13d..5b5603c 100644 --- a/src/ofxSourcesEditor.cpp +++ b/src/ofxSourcesEditor.cpp @@ -28,9 +28,7 @@ void ofxSourcesEditor::unregisterAppEvents() void ofxSourcesEditor::setup(ofEventArgs& args) { - gui = new ofxUICanvas(); - gui->disable(); - gui->disableAppDrawCallback(); + gui = new ofxRadioList(); // read directory contents ofDirectory imgDir; @@ -44,15 +42,18 @@ void ofxSourcesEditor::setup(ofEventArgs& args) vnames.push_back(imgDir.getName(i)); } - gui->addLabel(defImgDir, OFX_UI_FONT_SMALL); - ofxUIRadio *radio = gui->addRadio("images", vnames, OFX_UI_ORIENTATION_VERTICAL); - radio->activateToggle("image0.png"); - - ofAddListener(gui->newGUIEvent,this,&ofxSourcesEditor::guiEvent); + gui->setup("Images", vnames); + gui->setPosition(20, 20); + ofAddListener(gui->radioSelectedEvent, this, &ofxSourcesEditor::guiEvent); } void ofxSourcesEditor::draw() { + // Don't draw if there is no source selected + if ( surfaceManager->getSelectedSurface() == NULL ) { + return; + } + gui->draw(); } @@ -73,6 +74,12 @@ void ofxSourcesEditor::disable() void ofxSourcesEditor::enable() { + // Don't enable if there is no surface selected + if ( surfaceManager->getSelectedSurface() == NULL ) { + cout << "No surface selected. Not enable()ing source list." << endl; + return; + } + gui->enable(); } @@ -83,29 +90,14 @@ void ofxSourcesEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager) void ofxSourcesEditor::selectImageSourceRadioButton(string name) { - vector widgets = gui->getWidgets(); - - // find radio list item - ofxUIRadio* radio; - for ( int i=0; igetKind(); - if ( widgetKind == OFX_UI_WIDGET_RADIO ){ - radio = (ofxUIRadio*)widgets[i]; - break; - } - } - if (name == "none") { - ofxUIToggle* toggle = (ofxUIToggle*)radio->getActive(); - if ( toggle != NULL ) { - toggle->setValue(false); - } + gui->unselectAll(); return; } else { - for ( int i=0; igetName(); - if ( name == widgetName ) { - radio->activateToggle(name); + int i; + for (i = 0; i < gui->size(); i++) { + if (gui->getItemName(i) == name) { + gui->selectItem(i); return; } } @@ -126,24 +118,14 @@ ofTexture* ofxSourcesEditor::getTexture(int index) return &images[index]->getTextureReference(); } -void ofxSourcesEditor::guiEvent(ofxUIEventArgs &e) +void ofxSourcesEditor::guiEvent(string &imageName) { - string name = e.widget->getName(); - int kind = e.widget->getKind(); - - if(kind == OFX_UI_WIDGET_TOGGLE){ - ofxUIToggle *toggle = (ofxUIToggle *) e.widget; - cout << name << "\t value: " << toggle->getValue() << endl; - } + string name = imageName; if ( surfaceManager->getSelectedSurface() == NULL ) { return; } - if (name == "images") { - return; - } - stringstream ss; ss << defImgDir << name; cout << "attempt to load image: " << ss.str() << endl; diff --git a/src/ofxSourcesEditor.h b/src/ofxSourcesEditor.h index 7344e33..33aad38 100644 --- a/src/ofxSourcesEditor.h +++ b/src/ofxSourcesEditor.h @@ -3,8 +3,8 @@ #include "ofGraphics.h" #include "ofEvents.h" -#include "ofxUI.h" #include "ofxSurfaceManager.h" +#include "ofxRadioList.h" #define DEFAULT_IMAGES_DIR "sources/images/"; @@ -30,14 +30,11 @@ public: private: ofxSurfaceManager* surfaceManager; + ofxRadioList* gui; string defImgDir; - ofxUICanvas *gui; - void guiEvent(ofxUIEventArgs &e); + void guiEvent(string &imageName); vector images; vector imageNames; - //ofxPanel imgSrcPanel; - - //void onSourceSelect(bool& value); }; #endif \ No newline at end of file diff --git a/src/ui/ofxRadioList.cpp b/src/ui/ofxRadioList.cpp new file mode 100644 index 0000000..e974284 --- /dev/null +++ b/src/ui/ofxRadioList.cpp @@ -0,0 +1,188 @@ +#include "ofxRadioList.h" + +ofxRadioList::ofxRadioList() +{ + storedTitle = ""; + storedSelectedItem = 0; +} + +ofxRadioList::ofxRadioList(vector &labels) +{ + ofxRadioList(); + setup(labels); +} + +ofxRadioList::ofxRadioList(string title, vector &labels) +{ + ofxRadioList(); + setup(title, labels); +} + +ofxRadioList::~ofxRadioList() +{ + clear(); +} + +void ofxRadioList::setup(vector &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++) { + ofxToggle* toggle = new ofxToggle(); + toggle->setup(false); + toggle->setName(labels[i]); + toggle->addListener(this, &ofxRadioList::onToggleClicked); + guiGroup.add(toggle); + } + + cout << "num items: " << guiGroup.getNumControls() << endl; +} + +void ofxRadioList::setup(string title, vector &labels) +{ + // Store title for later use + storedTitle = title; + guiGroup.setName(title); + setup(labels); +} + +void ofxRadioList::draw() +{ + guiGroup.draw(); +} + +void ofxRadioList::setTitle(string title) +{ + storedTitle = title; + guiGroup.setName(title); +} + +void ofxRadioList::setPosition(ofPoint p) +{ + guiGroup.setPosition(p); +} + +void ofxRadioList::setPosition(float x, float y) +{ + guiGroup.setPosition(x, y); +} + +void ofxRadioList::selectItem(int index) +{ + if (index >= guiGroup.getNumControls()) { + return; + } + + unselectAll(); + + ofxToggle* toggle = static_cast(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 + setup(storedTitle, storedLabels); + + // Select the stored selected item without throwing an event + ofxToggle* toggle = static_cast(guiGroup.getControl(storedSelectedItem)); + toggle->removeListener(this, &ofxRadioList::onToggleClicked); + *toggle = true; + toggle->addListener(this, &ofxRadioList::onToggleClicked); + + cout << "num items after enable: " << guiGroup.getNumControls() << endl; +} + +void ofxRadioList::disable() +{ + // Just remove everything + clear(); +} + +void ofxRadioList::clear() +{ + int i; + for (i = 0; i < guiGroup.getNumControls(); i++) { + ofxToggle* toggle = static_cast(guiGroup.getControl(i)); + toggle->removeListener(this, &ofxRadioList::onToggleClicked); + delete toggle; + } + guiGroup.clear(); +} + +void ofxRadioList::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, &ofxRadioList::onToggleClicked); + *toggle = false; + toggle->addListener(this, &ofxRadioList::onToggleClicked); + } +} + +ofPoint ofxRadioList::getPosition() +{ + return guiGroup.getPosition(); +} + +float ofxRadioList::getWidth() +{ + return guiGroup.getWidth(); +} + +float ofxRadioList::getHeight() +{ + return guiGroup.getHeight(); +} + +string ofxRadioList::getTitle() +{ + return guiGroup.getName(); +} + +string ofxRadioList::getItemName(int index) +{ + if (index >= guiGroup.getNumControls()) { + return ""; + } + + ofxToggle* toggle = static_cast(guiGroup.getControl(index)); + return toggle->getName(); +} + +int ofxRadioList::size() +{ + return guiGroup.getNumControls(); +} + +void ofxRadioList::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; + } + } +} diff --git a/src/ui/ofxRadioList.h b/src/ui/ofxRadioList.h new file mode 100644 index 0000000..2736352 --- /dev/null +++ b/src/ui/ofxRadioList.h @@ -0,0 +1,46 @@ +#pragma once + +#include "ofGraphics.h" +#include "ofxGuiGroup.h" +#include "ofxToggle.h" +#include "ofxLabel.h" + +class ofxRadioList +{ +public: + ofxRadioList(); + ofxRadioList(vector &labels); + ofxRadioList(string title, vector &labels); + ~ofxRadioList(); + + void setup(vector &labels); + void setup(string title, vector &labels); + void draw(); + 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 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) + // to listen to this. Listner method void listenerMethod(string & radioName) + ofEvent radioSelectedEvent; + +private: + vector storedLabels; + string storedTitle; + ofxGuiGroup guiGroup; + int storedSelectedItem; + + void onToggleClicked(bool &toggleValue); +}; \ No newline at end of file