From dfc621e04f3f18b61a970e4bed0d61866a4485c3 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Mon, 29 Feb 2016 11:58:37 +0100 Subject: [PATCH] Add `SetNextSourceCmd` --- src/Commands/SetNextSourceCmd.cpp | 106 +++++++++++++++++++++--------- src/Commands/SetNextSourceCmd.h | 19 +++--- 2 files changed, 84 insertions(+), 41 deletions(-) diff --git a/src/Commands/SetNextSourceCmd.cpp b/src/Commands/SetNextSourceCmd.cpp index 5a19b3d..1f1da1d 100644 --- a/src/Commands/SetNextSourceCmd.cpp +++ b/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 diff --git a/src/Commands/SetNextSourceCmd.h b/src/Commands/SetNextSourceCmd.h index 1fc9b9a..41e915d 100644 --- a/src/Commands/SetNextSourceCmd.h +++ b/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 _sources; + int _sourceIndex; // Previous source index + int _nextSourceIndex; };