Browse Source

Add `SetNextSourceCmd`

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

106
src/Commands/SetNextSourceCmd.cpp

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

19
src/Commands/SetNextSourceCmd.h

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

Loading…
Cancel
Save