Browse Source

Add MediaServer initialization

master
Krisjanis Rijnieks 11 years ago
parent
commit
4d484bfb3d
  1. 197
      src/SourcesEditor.cpp
  2. 10
      src/SourcesEditor.h

197
src/SourcesEditor.cpp

@ -2,120 +2,149 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
SourcesEditor::SourcesEditor() { SourcesEditor::SourcesEditor() {
defImgDir = DEFAULT_IMAGES_DIR; init();
registerAppEvents();
} // 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() { SourcesEditor::~SourcesEditor() {
unregisterAppEvents(); unregisterAppEvents();
delete gui; delete gui;
while (images.size()) { while (images.size()) {
delete images.back(); delete images.back();
images.pop_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() { void SourcesEditor::unregisterAppEvents() {
ofAddListener(ofEvents().setup, this, &SourcesEditor::setup); ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup);
} }
void SourcesEditor::unregisterAppEvents() { void SourcesEditor::setup(ofEventArgs& args) {
ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup); gui = new RadioList();
}
void SourcesEditor::setup(ofEventArgs& args) { // read directory contents
gui = new RadioList(); ofDirectory imgDir;
imgDir.listDir(defImgDir);
imgDir.sort();
// read directory contents vector<string> vnames;
ofDirectory imgDir;
imgDir.listDir(defImgDir);
imgDir.sort();
vector<string> 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++) { gui->setup("Images", vnames);
// images[i].loadImage(imgDir.getPath(i)); gui->setPosition(20, 20);
vnames.push_back(imgDir.getName(i)); ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent);
} }
gui->setup("Images", vnames); void SourcesEditor::draw() {
gui->setPosition(20, 20); // Don't draw if there is no source selected
ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent); if (surfaceManager->getSelectedSurface() == NULL) {
} return;
}
void SourcesEditor::draw() { gui->draw();
// Don't draw if there is no source selected
if (surfaceManager->getSelectedSurface() == NULL) {
return;
} }
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) { imageNames.push_back(name);
images.push_back(new ofImage());
images.back()->loadImage(path);
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() { gui->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(); void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
} surfaceManager = newSurfaceManager;
}
void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
surfaceManager = newSurfaceManager;
}
void SourcesEditor::selectImageSourceRadioButton(string name) { void SourcesEditor::selectImageSourceRadioButton(string name) {
if (name == "none") { if (name == "none") {
gui->unselectAll(); gui->unselectAll();
return; return;
} else { } else {
int i; int i;
for (i = 0; i < gui->size(); i++) { for (i = 0; i < gui->size(); i++) {
if (gui->getItemName(i) == name) { if (gui->getItemName(i) == name) {
gui->selectItem(i); gui->selectItem(i);
return; 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) { return &images[index]->getTextureReference();
if (index >= images.size()) {
throw std::runtime_error("Texture index out of bounds.");
} }
return &images[index]->getTextureReference(); void SourcesEditor::guiEvent(string& imageName) {
} string name = imageName;
void SourcesEditor::guiEvent(string& imageName) { if (surfaceManager->getSelectedSurface() == NULL) {
string name = imageName; return;
}
if (surfaceManager->getSelectedSurface() == NULL) { stringstream ss;
return; 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();
}
} }
} }

10
src/SourcesEditor.h

@ -4,6 +4,7 @@
#include "ofEvents.h" #include "ofEvents.h"
#include "SurfaceManager.h" #include "SurfaceManager.h"
#include "RadioList.h" #include "RadioList.h"
#include "MediaServer.h"
#define DEFAULT_IMAGES_DIR "sources/images/"; #define DEFAULT_IMAGES_DIR "sources/images/";
@ -11,9 +12,16 @@ namespace ofx {
namespace piMapper { namespace piMapper {
class SourcesEditor { class SourcesEditor {
public: public:
// Default contructor that initializes media server locally,
// thus requiring to delete the media server from memory on deconstr
SourcesEditor(); SourcesEditor();
// Alternative constructor that allows to assign external media server
SourcesEditor(MediaServer* externalMediaServer);
~SourcesEditor(); ~SourcesEditor();
// Init handles variable initialization in all constructors
void init();
void registerAppEvents(); void registerAppEvents();
void unregisterAppEvents(); void unregisterAppEvents();
@ -29,6 +37,8 @@ class SourcesEditor {
ofTexture* getTexture(int index); ofTexture* getTexture(int index);
private: private:
MediaServer* mediaServer;
bool isMediaServerExternal;
SurfaceManager* surfaceManager; SurfaceManager* surfaceManager;
RadioList* gui; RadioList* gui;
string defImgDir; string defImgDir;

Loading…
Cancel
Save