From 06545df724ef8921719f83f25eadec9fb56d5d7c Mon Sep 17 00:00:00 2001
From: Krisjanis Rijnieks <krisjanis.rijnieks@gmail.com>
Date: Fri, 10 Aug 2018 11:21:42 +0200
Subject: [PATCH] Add ofxPiMapper::setImageSource()

Also fix ofxPiMapper::setVideoSource() to stop only video sources and not crash.
---
 src/Application/Application.cpp | 35 ++++++++++++++++++++++++++++++---
 src/Application/Application.h   |  1 +
 src/ofxPiMapper.cpp             |  4 ++++
 src/ofxPiMapper.h               |  1 +
 4 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/src/Application/Application.cpp b/src/Application/Application.cpp
index 52b9691..f2f929d 100644
--- a/src/Application/Application.cpp
+++ b/src/Application/Application.cpp
@@ -488,9 +488,12 @@ void Application::setVideoSource(std::string fileName){
 		if(ofIsStringInString(loadedVideos[i], fileName)){
 			if(getSurfaceManager()->getSelectedSurface() != 0){
 				BaseSource * source = getSurfaceManager()->getSelectedSurface()->getSource();
-				VideoSource * video = dynamic_cast<VideoSource *>(source);
-				video->stop();
-			
+				
+				if(source->getType() == SOURCE_TYPE_VIDEO){
+					VideoSource * video = dynamic_cast<VideoSource *>(source);
+					video->stop();
+				}
+				
 				getCmdManager()->exec(
 		 			new SetSourceCmd(
 						SourceType::SOURCE_TYPE_VIDEO,
@@ -505,6 +508,32 @@ void Application::setVideoSource(std::string fileName){
 	}
 }
 
+void Application::setImageSource(std::string fileName){
+	vector<std::string> loadedImages = getMediaServer()->getImageNames();
+	for(auto i = 0; i < loadedImages.size(); i++){
+		if(ofIsStringInString(loadedImages[i], fileName)){
+			if(getSurfaceManager()->getSelectedSurface() != 0){
+				BaseSource * source = getSurfaceManager()->getSelectedSurface()->getSource();
+				
+				if(source->getType() == SOURCE_TYPE_VIDEO){
+					VideoSource * video = dynamic_cast<VideoSource *>(source);
+					video->stop();
+				}
+			
+				getCmdManager()->exec(
+		 			new SetSourceCmd(
+						SourceType::SOURCE_TYPE_IMAGE,
+						getMediaServer()->getImagePaths()[i],
+		  				getSurfaceManager()->getSelectedSurface(),
+		  				&Gui::instance()->getSourcesEditorWidget()));
+			}else{
+				getCmdManager()->exec(new SelNextSurfaceCmd(getSurfaceManager()));
+			}
+			break;
+		}
+	}
+}
+
 void Application::addGridRow(){
 	if(getSurfaceManager()->getSelectedSurface() != 0){
 		if(getSurfaceManager()->getSelectedSurface()->getType() ==
diff --git a/src/Application/Application.h b/src/Application/Application.h
index 68628a5..1715d69 100644
--- a/src/Application/Application.h
+++ b/src/Application/Application.h
@@ -131,6 +131,7 @@ class Application {
 		void setNextSource();
 		void setFboSource(std::string sourceId);
 		void setVideoSource(std::string fileName);
+		void setImageSource(std::string fileName);
 		void addGridRow();
 		void addGridColumn();
 		void removeGridRow();
diff --git a/src/ofxPiMapper.cpp b/src/ofxPiMapper.cpp
index cb21888..5f235e2 100644
--- a/src/ofxPiMapper.cpp
+++ b/src/ofxPiMapper.cpp
@@ -190,6 +190,10 @@ void ofxPiMapper::setVideoSource(std::string fileName){
 	_application.setVideoSource(fileName);
 }
 
+void ofxPiMapper::setImageSource(std::string fileName){
+	_application.setImageSource(fileName);
+}
+
 void ofxPiMapper::reboot(){
 	_application.reboot();
 }
diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h
index 038255d..704fbb3 100644
--- a/src/ofxPiMapper.h
+++ b/src/ofxPiMapper.h
@@ -87,6 +87,7 @@ class ofxPiMapper {
 		void setNextSource();
 		void setFboSource(std::string sourceId);
 		void setVideoSource(std::string fileName);
+		void setImageSource(std::string fileName);
 
 		// System commands
 		void reboot();