Maelstrom: Fullscreen panels fade in and out as they are shown and hidden

From 5a8c45d2950d975f7e438bba739383a3ab1d4649 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 23 Oct 2011 02:02:04 -0400
Subject: [PATCH] Fullscreen panels fade in and out as they are shown and
 hidden

---
 init.cpp                   | 22 +++++++++++-----------
 screenlib/SDL_FrameBuf.cpp |  2 +-
 screenlib/UIManager.cpp    | 36 ++++++++++++++++++++++++++++++++++++
 screenlib/UIManager.h      |  4 ++++
 4 files changed, 52 insertions(+), 12 deletions(-)

diff --git a/init.cpp b/init.cpp
index cd9ca23f..781006fc 100644
--- a/init.cpp
+++ b/init.cpp
@@ -65,7 +65,9 @@ static int LoadSmallSprite(Mac_Resource *spriteres,
 void DoSplash(void)
 {
 	UIPanel *panel;
+	int i;
 
+	screen->FadeOut();
 	screen->Clear();
 
 	panel = ui->LoadPanel("splash");
@@ -75,8 +77,15 @@ void DoSplash(void)
 	}
 	screen->Update();
 
+	for ( i=0; i<5; ++i ) {
+		if ( DropEvents() ) {
+			break;
+		}
+		Delay(60);
+	}
+
 	if (panel) {
-		delete panel;
+		ui->DeletePanel(panel);
 	}
 }
 
@@ -817,15 +826,7 @@ int DoInitializations(Uint32 window_flags, Uint32 render_flags)
 	screen->ClipBlit(&gClipRect);
 
 	/* Do the Ambrosia Splash screen */
-	screen->Fade();
 	DoSplash();
-	screen->Fade();
-	for ( i=0; i<5; ++i ) {
-		if ( DropEvents() ) {
-			break;
-		}
-		Delay(60);
-	}
 
 	/* -- Throw up our intro screen */
 	intro = Load_Title(screen, 130);
@@ -833,10 +834,9 @@ int DoInitializations(Uint32 window_flags, Uint32 render_flags)
 		error("Can't load intro title! (ID=%d)\n", 130);
 		return(-1);
 	}
-	screen->Fade();
 	DoIntroScreen(1);
 	sound->PlaySound(gPrizeAppears, 1);
-	screen->Fade();
+	screen->FadeIn();
 
 	/* -- Load in our sprites and other needed resources */
 	{
diff --git a/screenlib/SDL_FrameBuf.cpp b/screenlib/SDL_FrameBuf.cpp
index 0108e8e8..0a484f96 100644
--- a/screenlib/SDL_FrameBuf.cpp
+++ b/screenlib/SDL_FrameBuf.cpp
@@ -131,7 +131,7 @@ void
 FrameBuf:: Fade(void)
 {
 // Temporary for development
-return;
+//return;
 	const int max = 32;
 	Uint16 ramp[256];   
 
diff --git a/screenlib/UIManager.cpp b/screenlib/UIManager.cpp
index f831e373..98b689ea 100644
--- a/screenlib/UIManager.cpp
+++ b/screenlib/UIManager.cpp
@@ -20,6 +20,7 @@
     slouken@libsdl.org
 */
 
+#include "SDL_FrameBuf.h"
 #include "UIManager.h"
 #include "UIPanel.h"
 
@@ -69,8 +70,22 @@ void
 UIManager::ShowPanel(UIPanel *panel)
 {
 	if (panel && !m_visible.find(panel)) {
+		/* If this is fullscreen, then hide any previous fullscreen panel */
+		if (panel->IsFullscreen()) {
+			for (unsigned int i = m_visible.length(); i--; ) {
+				if (m_visible[i]->IsFullscreen()) {
+					m_visible[i]->Hide();
+					m_screen->FadeOut();
+					break;
+				}
+			}
+		}
+
 		m_visible.add(panel);
 		panel->Show();
+		if (panel->IsFullscreen()) {
+			m_screen->FadeIn();
+		}
 	}
 }
 
@@ -79,6 +94,27 @@ UIManager::HidePanel(UIPanel *panel)
 {
 	if (panel && m_visible.remove(panel)) {
 		panel->Hide();
+		if (panel->IsFullscreen()) {
+			m_screen->FadeOut();
+
+			for (unsigned int i = m_visible.length(); i--; ) {
+				if (m_visible[i]->IsFullscreen()) {
+					m_visible[i]->Show();
+					m_screen->FadeIn();
+					break;
+				}
+			}
+		}
+
+	}
+}
+
+void
+UIManager::DeletePanel(UIPanel *panel)
+{
+	if (panel) {
+		HidePanel(panel);
+		delete panel;
 	}
 }
 
diff --git a/screenlib/UIManager.h b/screenlib/UIManager.h
index 661e6e9d..ba73392b 100644
--- a/screenlib/UIManager.h
+++ b/screenlib/UIManager.h
@@ -68,6 +68,10 @@ class UIManager : public ErrorBase
 	void HidePanel(const char *name) {
 		HidePanel(GetPanel(name));
 	}
+	void DeletePanel(UIPanel *panel);
+	void DeletePanel(const char *name) {
+		DeletePanel(GetPanel(name));
+	}
 
 	void Draw();
 	bool HandleEvent(const SDL_Event &event);