From 4d484bfb3dfa463aaa0d2d7cf37767100524f6d7 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Wed, 15 Oct 2014 14:14:41 +0200 Subject: [PATCH] Add MediaServer initialization --- src/SourcesEditor.cpp | 197 ++++++++++++++++++++++++------------------ src/SourcesEditor.h | 10 +++ 2 files changed, 123 insertions(+), 84 deletions(-) diff --git a/src/SourcesEditor.cpp b/src/SourcesEditor.cpp index 452ddb9..2c61822 100644 --- a/src/SourcesEditor.cpp +++ b/src/SourcesEditor.cpp @@ -2,120 +2,149 @@ namespace ofx { namespace piMapper { -SourcesEditor::SourcesEditor() { - defImgDir = DEFAULT_IMAGES_DIR; - registerAppEvents(); -} + SourcesEditor::SourcesEditor() { + init(); + + // Create new MediaServer instance, + // we will need to clear this in the deconstr + mediaServer = new MediaServer(); + isMediaServerExternal = false; + } + + SourcesEditor::SourcesEditor(MediaServer* externalMediaServer) { + init(); + + // Assign external MediaServer instance pointer + mediaServer = externalMediaServer; + isMediaServerExternal = true; + } -SourcesEditor::~SourcesEditor() { - unregisterAppEvents(); - delete gui; - while (images.size()) { - delete images.back(); - images.pop_back(); + SourcesEditor::~SourcesEditor() { + unregisterAppEvents(); + delete gui; + while (images.size()) { + delete images.back(); + images.pop_back(); + } + + // If mediaServer is local, clear it + if (isMediaServerExternal) { + // Clear all loaded sources + //mediaServer->clear() + // Destroy the pointer and set it to NULL pointer + delete mediaServer; + mediaServer = NULL; + } + } + + // Initialize instance variables + void SourcesEditor::init() { + mediaServer = NULL; + isMediaServerExternal = false; + defImgDir = DEFAULT_IMAGES_DIR; + registerAppEvents(); + } + + void SourcesEditor::registerAppEvents() { + ofAddListener(ofEvents().setup, this, &SourcesEditor::setup); } -} -void SourcesEditor::registerAppEvents() { - ofAddListener(ofEvents().setup, this, &SourcesEditor::setup); -} + void SourcesEditor::unregisterAppEvents() { + ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup); + } -void SourcesEditor::unregisterAppEvents() { - ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup); -} + void SourcesEditor::setup(ofEventArgs& args) { + gui = new RadioList(); -void SourcesEditor::setup(ofEventArgs& args) { - gui = new RadioList(); + // read directory contents + ofDirectory imgDir; + imgDir.listDir(defImgDir); + imgDir.sort(); - // read directory contents - ofDirectory imgDir; - imgDir.listDir(defImgDir); - imgDir.sort(); + vector vnames; - vector vnames; + for (int i = 0; i < (int)imgDir.size(); i++) { + // images[i].loadImage(imgDir.getPath(i)); + vnames.push_back(imgDir.getName(i)); + } - for (int i = 0; i < (int)imgDir.size(); i++) { - // images[i].loadImage(imgDir.getPath(i)); - vnames.push_back(imgDir.getName(i)); + gui->setup("Images", vnames); + gui->setPosition(20, 20); + ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent); } - gui->setup("Images", vnames); - gui->setPosition(20, 20); - ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent); -} + void SourcesEditor::draw() { + // Don't draw if there is no source selected + if (surfaceManager->getSelectedSurface() == NULL) { + return; + } -void SourcesEditor::draw() { - // Don't draw if there is no source selected - if (surfaceManager->getSelectedSurface() == NULL) { - return; + gui->draw(); } - gui->draw(); -} + void SourcesEditor::loadImage(string name, string path) { + images.push_back(new ofImage()); + images.back()->loadImage(path); -void SourcesEditor::loadImage(string name, string path) { - images.push_back(new ofImage()); - images.back()->loadImage(path); + imageNames.push_back(name); - imageNames.push_back(name); + ofSendMessage("imageLoaded"); + } - ofSendMessage("imageLoaded"); -} + void SourcesEditor::disable() { gui->disable(); } -void SourcesEditor::disable() { gui->disable(); } + void SourcesEditor::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; + } -void SourcesEditor::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(); } - gui->enable(); -} - -void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) { - surfaceManager = newSurfaceManager; -} + void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) { + surfaceManager = newSurfaceManager; + } -void SourcesEditor::selectImageSourceRadioButton(string name) { - if (name == "none") { - gui->unselectAll(); - return; - } else { - int i; - for (i = 0; i < gui->size(); i++) { - if (gui->getItemName(i) == name) { - gui->selectItem(i); - return; + void SourcesEditor::selectImageSourceRadioButton(string name) { + if (name == "none") { + gui->unselectAll(); + return; + } else { + int i; + for (i = 0; i < gui->size(); i++) { + if (gui->getItemName(i) == name) { + gui->selectItem(i); + return; + } } } } -} -int SourcesEditor::getLoadedTexCount() { return images.size(); } + int SourcesEditor::getLoadedTexCount() { return images.size(); } + + ofTexture* SourcesEditor::getTexture(int index) { + if (index >= images.size()) { + throw std::runtime_error("Texture index out of bounds."); + } -ofTexture* SourcesEditor::getTexture(int index) { - if (index >= images.size()) { - throw std::runtime_error("Texture index out of bounds."); + return &images[index]->getTextureReference(); } - return &images[index]->getTextureReference(); -} + void SourcesEditor::guiEvent(string& imageName) { + string name = imageName; -void SourcesEditor::guiEvent(string& imageName) { - string name = imageName; + if (surfaceManager->getSelectedSurface() == NULL) { + return; + } - if (surfaceManager->getSelectedSurface() == NULL) { - return; + stringstream ss; + ss << defImgDir << name; + cout << "attempt to load image: " << ss.str() << endl; + ofTexture* texture = surfaceManager->loadImageSource(name, ss.str()); + surfaceManager->getSelectedSurface()->setTexture(texture); + surfaceManager->manageMemory(); } - - stringstream ss; - ss << defImgDir << name; - cout << "attempt to load image: " << ss.str() << endl; - ofTexture* texture = surfaceManager->loadImageSource(name, ss.str()); - surfaceManager->getSelectedSurface()->setTexture(texture); - surfaceManager->manageMemory(); -} } } \ No newline at end of file diff --git a/src/SourcesEditor.h b/src/SourcesEditor.h index ee4462b..73e69b5 100644 --- a/src/SourcesEditor.h +++ b/src/SourcesEditor.h @@ -4,6 +4,7 @@ #include "ofEvents.h" #include "SurfaceManager.h" #include "RadioList.h" +#include "MediaServer.h" #define DEFAULT_IMAGES_DIR "sources/images/"; @@ -11,9 +12,16 @@ namespace ofx { namespace piMapper { class SourcesEditor { public: + // Default contructor that initializes media server locally, + // thus requiring to delete the media server from memory on deconstr SourcesEditor(); + + // Alternative constructor that allows to assign external media server + SourcesEditor(MediaServer* externalMediaServer); ~SourcesEditor(); + // Init handles variable initialization in all constructors + void init(); void registerAppEvents(); void unregisterAppEvents(); @@ -29,6 +37,8 @@ class SourcesEditor { ofTexture* getTexture(int index); private: + MediaServer* mediaServer; + bool isMediaServerExternal; SurfaceManager* surfaceManager; RadioList* gui; string defImgDir;