From a964d89b80c2cfc62d4f8787185b08540f03315d Mon Sep 17 00:00:00 2001
From: Leon Styhre <leon@leonstyhre.com>
Date: Sun, 25 Jun 2023 13:30:49 +0200
Subject: [PATCH] Added up and down button navigation to switch between the
 media viewer and the PDF viewer

---
 es-app/src/MediaViewer.cpp |  1 +
 es-app/src/PDFViewer.cpp   | 12 ++++++++++--
 es-app/src/PDFViewer.h     |  2 ++
 es-core/src/Window.cpp     |  8 ++++++--
 es-core/src/Window.h       |  1 +
 5 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/es-app/src/MediaViewer.cpp b/es-app/src/MediaViewer.cpp
index 311322b2a..004e2bdcb 100644
--- a/es-app/src/MediaViewer.cpp
+++ b/es-app/src/MediaViewer.cpp
@@ -14,6 +14,7 @@
 
 MediaViewer::MediaViewer()
     : mRenderer {Renderer::getInstance()}
+    , mGame {nullptr}
 {
     Window::getInstance()->setMediaViewer(this);
 }
diff --git a/es-app/src/PDFViewer.cpp b/es-app/src/PDFViewer.cpp
index 2e4a0d3c5..0d695ba96 100644
--- a/es-app/src/PDFViewer.cpp
+++ b/es-app/src/PDFViewer.cpp
@@ -24,6 +24,7 @@
 
 PDFViewer::PDFViewer()
     : mRenderer {Renderer::getInstance()}
+    , mGame {nullptr}
 {
     Window::getInstance()->setPDFViewer(this);
     mTexture = TextureResource::get("");
@@ -50,10 +51,11 @@ bool PDFViewer::startPDFViewer(FileData* game)
         return false;
     }
 
-    mManualPath = game->getManualPath();
+    mGame = game;
+    mManualPath = mGame->getManualPath();
 
     if (!Utils::FileSystem::exists(mManualPath)) {
-        LOG(LogError) << "No PDF manual found for game \"" << game->getName() << "\"";
+        LOG(LogError) << "No PDF manual found for game \"" << mGame->getName() << "\"";
         NavigationSounds::getInstance().playThemeNavigationSound(SCROLLSOUND);
         ViewController::getInstance()->stopViewVideos();
         return false;
@@ -136,6 +138,12 @@ void PDFViewer::stopPDFViewer()
     mPageImage.reset();
 }
 
+void PDFViewer::launchMediaViewer()
+{
+    Window::getInstance()->stopPDFViewer();
+    Window::getInstance()->startMediaViewer(mGame);
+}
+
 bool PDFViewer::getDocumentInfo()
 {
     std::string commandOutput;
diff --git a/es-app/src/PDFViewer.h b/es-app/src/PDFViewer.h
index 3bc2a6ab2..2ce7106f7 100644
--- a/es-app/src/PDFViewer.h
+++ b/es-app/src/PDFViewer.h
@@ -20,6 +20,7 @@ public:
 
     bool startPDFViewer(FileData* game) override;
     void stopPDFViewer() override;
+    void launchMediaViewer() override;
 
     bool getDocumentInfo();
     void convertPage(int pageNum);
@@ -43,6 +44,7 @@ private:
     std::shared_ptr<TextureResource> mTexture;
     std::unique_ptr<ImageComponent> mPageImage;
     std::map<int, PageEntry> mPages;
+    FileData* mGame;
 
     std::string mESConvertPath;
     std::string mManualPath;
diff --git a/es-core/src/Window.cpp b/es-core/src/Window.cpp
index ca31147a9..4b35477ff 100644
--- a/es-core/src/Window.cpp
+++ b/es-core/src/Window.cpp
@@ -221,7 +221,7 @@ void Window::input(InputConfig* config, Input input)
     }
 
     if (mMediaViewer && mRenderMediaViewer) {
-        if (config->isMappedLike("y", input) && input.value != 0) {
+        if (config->isMappedLike("up", input) && input.value != 0) {
             mMediaViewer->launchPDFViewer();
             return;
         }
@@ -236,7 +236,11 @@ void Window::input(InputConfig* config, Input input)
     }
 
     if (mPDFViewer && mRenderPDFViewer) {
-        if (config->isMappedLike("right", input) && input.value != 0)
+        if (config->isMappedLike("down", input) && input.value != 0) {
+            mPDFViewer->launchMediaViewer();
+            return;
+        }
+        else if (config->isMappedLike("right", input) && input.value != 0)
             mPDFViewer->showNextPage();
         else if (config->isMappedLike("left", input) && input.value != 0)
             mPDFViewer->showPreviousPage();
diff --git a/es-core/src/Window.h b/es-core/src/Window.h
index afbaafd9c..a91362558 100644
--- a/es-core/src/Window.h
+++ b/es-core/src/Window.h
@@ -70,6 +70,7 @@ public:
     public:
         virtual bool startPDFViewer(FileData* game) = 0;
         virtual void stopPDFViewer() = 0;
+        virtual void launchMediaViewer() = 0;
 
         virtual void showNextPage() = 0;
         virtual void showPreviousPage() = 0;