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();
}
SourcesEditor::~SourcesEditor() { // Create new MediaServer instance,
unregisterAppEvents(); // we will need to clear this in the deconstr
delete gui; mediaServer = new MediaServer();
while (images.size()) { isMediaServerExternal = false;
delete images.back();
images.pop_back();
} }
}
void SourcesEditor::registerAppEvents() { SourcesEditor::SourcesEditor(MediaServer* externalMediaServer) {
ofAddListener(ofEvents().setup, this, &SourcesEditor::setup); init();
}
void SourcesEditor::unregisterAppEvents() {
ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup);
}
void SourcesEditor::setup(ofEventArgs& args) { // Assign external MediaServer instance pointer
gui = new RadioList(); mediaServer = externalMediaServer;
isMediaServerExternal = true;
}
// read directory contents SourcesEditor::~SourcesEditor() {
ofDirectory imgDir; unregisterAppEvents();
imgDir.listDir(defImgDir); delete gui;
imgDir.sort(); while (images.size()) {
delete images.back();
images.pop_back();
}
vector<string> vnames; // 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;
}
}
for (int i = 0; i < (int)imgDir.size(); i++) { // Initialize instance variables
// images[i].loadImage(imgDir.getPath(i)); void SourcesEditor::init() {
vnames.push_back(imgDir.getName(i)); mediaServer = NULL;
isMediaServerExternal = false;
defImgDir = DEFAULT_IMAGES_DIR;
registerAppEvents();
} }
gui->setup("Images", vnames); void SourcesEditor::registerAppEvents() {
gui->setPosition(20, 20); ofAddListener(ofEvents().setup, this, &SourcesEditor::setup);
ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent); }
}
void SourcesEditor::draw() { void SourcesEditor::unregisterAppEvents() {
// Don't draw if there is no source selected ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup);
if (surfaceManager->getSelectedSurface() == NULL) {
return;
} }
gui->draw(); void SourcesEditor::setup(ofEventArgs& args) {
} gui = new RadioList();
void SourcesEditor::loadImage(string name, string path) { // read directory contents
images.push_back(new ofImage()); ofDirectory imgDir;
images.back()->loadImage(path); imgDir.listDir(defImgDir);
imgDir.sort();
imageNames.push_back(name); vector<string> vnames;
ofSendMessage("imageLoaded"); for (int i = 0; i < (int)imgDir.size(); i++) {
} // images[i].loadImage(imgDir.getPath(i));
vnames.push_back(imgDir.getName(i));
}
void SourcesEditor::disable() { gui->disable(); } 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::enable() { gui->draw();
// 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::loadImage(string name, string path) {
} images.push_back(new ofImage());
images.back()->loadImage(path);
void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) { imageNames.push_back(name);
surfaceManager = newSurfaceManager;
}
void SourcesEditor::selectImageSourceRadioButton(string name) { ofSendMessage("imageLoaded");
if (name == "none") { }
gui->unselectAll();
return; void SourcesEditor::disable() { gui->disable(); }
} else {
int i; void SourcesEditor::enable() {
for (i = 0; i < gui->size(); i++) { // Don't enable if there is no surface selected
if (gui->getItemName(i) == name) { if (surfaceManager->getSelectedSurface() == NULL) {
gui->selectItem(i); cout << "No surface selected. Not enable()ing source list." << endl;
return; return;
}
} }
gui->enable();
} }
}
int SourcesEditor::getLoadedTexCount() { return images.size(); } void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
surfaceManager = newSurfaceManager;
}
ofTexture* SourcesEditor::getTexture(int index) { void SourcesEditor::selectImageSourceRadioButton(string name) {
if (index >= images.size()) { if (name == "none") {
throw std::runtime_error("Texture index out of bounds."); gui->unselectAll();
return;
} else {
int i;
for (i = 0; i < gui->size(); i++) {
if (gui->getItemName(i) == name) {
gui->selectItem(i);
return;
}
}
}
} }
return &images[index]->getTextureReference(); int SourcesEditor::getLoadedTexCount() { return images.size(); }
}
void SourcesEditor::guiEvent(string& imageName) { ofTexture* SourcesEditor::getTexture(int index) {
string name = imageName; if (index >= images.size()) {
throw std::runtime_error("Texture index out of bounds.");
}
if (surfaceManager->getSelectedSurface() == NULL) { return &images[index]->getTextureReference();
return;
} }
stringstream ss; void SourcesEditor::guiEvent(string& imageName) {
ss << defImgDir << name; string name = imageName;
cout << "attempt to load image: " << ss.str() << endl;
ofTexture* texture = surfaceManager->loadImageSource(name, ss.str()); if (surfaceManager->getSelectedSurface() == NULL) {
surfaceManager->getSelectedSurface()->setTexture(texture); return;
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