Browse Source

Add `SetNextSourceCmd`

master
Krisjanis Rijnieks 9 years ago
parent
commit
dfc621e04f
  1. 102
      src/Commands/SetNextSourceCmd.cpp
  2. 19
      src/Commands/SetNextSourceCmd.h

102
src/Commands/SetNextSourceCmd.cpp

@ -3,13 +3,7 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
SetNextSourceCmd::SetNextSourceCmd(int sourceType, SetNextSourceCmd::SetNextSourceCmd(BaseSurface * surface, SourcesEditor * sourcesEditor){
string sourceId,
BaseSurface * surface,
SourcesEditor * sourcesEditor){
_sourceType = sourceType;
_sourceId = sourceId;
_surface = surface; _surface = surface;
_sourcesEditor = sourcesEditor; _sourcesEditor = sourcesEditor;
} }
@ -17,39 +11,87 @@ SetNextSourceCmd::SetNextSourceCmd(int sourceType,
void SetNextSourceCmd::exec(){ void SetNextSourceCmd::exec(){
ofLogNotice("SetNextSourceCmd", "exec"); ofLogNotice("SetNextSourceCmd", "exec");
_oldSourceType = _surface->getSource()->getType(); // Get current source
if(_surface->getSource()->isLoadable()){ BaseSource * source = _surface->getSource();
_oldSourceId = _surface->getSource()->getPath(); int sourceType = source->getType();
string sourceId;
if(source->isLoadable()){
sourceId = source->getPath();
}else{ }else{
_oldSourceId = _surface->getSource()->getName(); sourceId = source->getName();
} }
if(_sourceType == SourceType::SOURCE_TYPE_IMAGE){ // MediaServer shortcut
_sourcesEditor->setImageSource(_sourceId); MediaServer * mediaServer = _sourcesEditor->getMediaServer();
}else if(_sourceType == SourceType::SOURCE_TYPE_VIDEO){
_sourcesEditor->setVideoSource(_sourceId); // Read sources into a single vector
}else if(_sourceType == SourceType::SOURCE_TYPE_FBO){ for(unsigned int i = 0; i < mediaServer->getImagePaths().size(); ++i){
_sourcesEditor->setFboSource(_sourceId); SourceData data;
}else if(_sourceType == SourceType::SOURCE_TYPE_NONE){ data.type = SourceType::SOURCE_TYPE_IMAGE;
_sourcesEditor->clearSource(); data.id = mediaServer->getImagePaths()[i];
_sources.push_back(data);
}
for(unsigned int i = 0; i < mediaServer->getVideoPaths().size(); ++i){
SourceData data;
data.type = SourceType::SOURCE_TYPE_VIDEO;
data.id = mediaServer->getVideoPaths()[i];
_sources.push_back(data);
}
for(unsigned int i = 0; i < mediaServer->getFboSourceNames().size(); ++i){
SourceData data;
data.type = SourceType::SOURCE_TYPE_FBO;
data.id = mediaServer->getFboSourceNames()[i];
_sources.push_back(data);
}
if(_sources.size() <= 0){
return;
}
_sourceIndex = -1;
// Search for current source among all
for(unsigned int i = 0; i < _sources.size(); ++i){
if(sourceType == _sources[i].type && sourceId == _sources[i].id){
_sourceIndex = i;
break;
}
} }
if(_sourceIndex == -1){
return;
}
_nextSourceIndex = _sourceIndex + 1;
if(_nextSourceIndex >= _sources.size()){
_nextSourceIndex = 0;
}
// Load new source
BaseSource * newSource = mediaServer->loadMedia(
_sources[_nextSourceIndex].id,
_sources[_nextSourceIndex].type);
_surface->setSource(newSource);
// Unload old one
mediaServer->unloadMedia(sourceId);
} }
void SetNextSourceCmd::undo(){ void SetNextSourceCmd::undo(){
ofLogNotice("SetNextSourceCmd", "undo"); ofLogNotice("SetNextSourceCmd", "undo");
if(_oldSourceType == SourceType::SOURCE_TYPE_IMAGE){ MediaServer * mediaServer = _sourcesEditor->getMediaServer();
_sourcesEditor->setImageSource(_oldSourceId);
}else if(_oldSourceType == SourceType::SOURCE_TYPE_VIDEO){ // Load back old source
_sourcesEditor->setVideoSource(_oldSourceId); BaseSource * prevSource = mediaServer->loadMedia(
}else if(_oldSourceType == SourceType::SOURCE_TYPE_FBO){ _sources[_sourceIndex].id,
_sourcesEditor->setFboSource(_oldSourceId); _sources[_sourceIndex].type);
}else if(_oldSourceType == SourceType::SOURCE_TYPE_NONE){
_sourcesEditor->clearSource(); _surface->setSource(prevSource);
}
_surface = 0; mediaServer->unloadMedia(_sources[_nextSourceIndex].id);
_sourcesEditor = 0;
} }
} // namespace piMapper } // namespace piMapper

19
src/Commands/SetNextSourceCmd.h

@ -3,30 +3,31 @@
#include "BaseCmd.h" #include "BaseCmd.h"
#include "BaseSurface.h" #include "BaseSurface.h"
#include "SourcesEditor.h" #include "SourcesEditor.h"
#include "MediaServer.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
struct SourceData {
int type;
string id;
};
class SourcesEditor; class SourcesEditor;
class SetNextSourceCmd : public BaseUndoCmd { class SetNextSourceCmd : public BaseUndoCmd {
public: public:
SetNextSourceCmd(int sourceType, SetNextSourceCmd(BaseSurface * surface, SourcesEditor * sourcesEditor);
string sourceId,
BaseSurface * surface,
SourcesEditor * sourcesEditor);
void exec(); void exec();
void undo(); void undo();
private: private:
int _sourceType;
string _sourceId;
BaseSurface * _surface; BaseSurface * _surface;
SourcesEditor * _sourcesEditor; SourcesEditor * _sourcesEditor;
vector <SourceData> _sources;
int _oldSourceType; int _sourceIndex; // Previous source index
string _oldSourceId; int _nextSourceIndex;
}; };

Loading…
Cancel
Save