Maelstrom: Fixed position and visibility of elements in the gameover panel

https://github.com/libsdl-org/Maelstrom/commit/7c28bf78915062fa16214b1216f561ce088aaf3a

From 7c28bf78915062fa16214b1216f561ce088aaf3a Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 26 Oct 2011 23:35:57 -0400
Subject: [PATCH] Fixed position and visibility of elements in the gameover
 panel

---
 UI/gameover.xml       | 20 ++++++++++----------
 netlogic/game.cpp     | 30 +++++++++++++++++++++++++-----
 screenlib/UIPanel.cpp |  8 ++++++++
 screenlib/UIPanel.h   |  2 ++
 4 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/UI/gameover.xml b/UI/gameover.xml
index 36e03413..809a1bf2 100644
--- a/UI/gameover.xml
+++ b/UI/gameover.xml
@@ -1,28 +1,28 @@
 <UIPanel cursor="false" enterSound="113">
 	<Elements>
 		<Title name="image" id="128">
-			<Anchor anchorFrom="CENTER" anchorTo="CENTER"/>
+			<Anchor anchorFrom="CENTER" anchorTo="CENTER" y="-80"/>
 		</Title>
 
 		<!-- Multiplayer rankings -->
-		<Label name="rank1" show="false" fontName="New York" fontSize="18">
+		<Label name="rank1" fontName="New York" fontSize="18">
 			<Color r="0x75" g="0x75" b="0xFF"/>
-			<Anchor anchorFrom="TOPLEFT" anchorTo="TOPLEFT" anchor="image" x="96" y="312"/>
+			<Anchor anchorFrom="TOPLEFT" anchorTo="CENTER" x="-160" y="121"/>
 		</Label>
-		<Label name="rank2" show="false" fontName="New York" fontSize="18">
+		<Label name="rank2" fontName="New York" fontSize="18">
 			<Color r="0x75" g="0x75" b="0xFF"/>
-			<Anchor anchorFrom="TOPLEFT" anchorTo="TOPLEFT" anchor="rank1"/>
+			<Anchor anchorFrom="TOPLEFT" anchorTo="BOTTOMLEFT" anchor="rank1"/>
 		</Label>
-		<Label name="rank3" show="false" fontName="New York" fontSize="18">
+		<Label name="rank3" fontName="New York" fontSize="18">
 			<Color r="0x75" g="0x75" b="0xFF"/>
-			<Anchor anchorFrom="TOPLEFT" anchorTo="TOPLEFT" anchor="rank2"/>
+			<Anchor anchorFrom="TOPLEFT" anchorTo="BOTTOMLEFT" anchor="rank2"/>
 		</Label>
 
-		<Label name="name_label" show="false" fontName="New York" fontSize="18" text="Enter your name: ">
+		<Label name="name_label" fontName="New York" fontSize="18" text="Enter your name: ">
 			<Color r="0x75" g="0x75" b="0xFF"/>
-			<Anchor anchorFrom="TOP" anchorTo="TOP" anchor="image" y="252"/>
+			<Anchor anchorFrom="TOPRIGHT" anchorTo="CENTER" y="41"/>
 		</Label>
-		<Label name="name" show="false" fontName="New York" fontSize="18">
+		<Label name="name" fontName="New York" fontSize="18">
 			<Color r="0xFF" g="0xFF" b="0xFF"/>
 			<Anchor anchorFrom="LEFT" anchorTo="RIGHT" anchor="name_label"/>
 		</Label>
diff --git a/netlogic/game.cpp b/netlogic/game.cpp
index 91523022..eb444eef 100644
--- a/netlogic/game.cpp
+++ b/netlogic/game.cpp
@@ -684,6 +684,7 @@ static int cmp_byfrags(const void *A, const void *B)
 static void DoGameOver(void)
 {
 	UIPanel *panel;
+	UIElement *image;
 	UIElementLabel *label;
 	SDL_Event event;
 	int which = -1, i;
@@ -708,6 +709,12 @@ static void DoGameOver(void)
 	if (!panel) {
 		return;
 	}
+	panel->HideAll();
+
+	image = panel->GetElement<UIElement>("image");
+	if (image) {
+		image->Show();
+	}
 
 	/* Show the player ranking */
 	if ( gNumPlayers > 1 ) {
@@ -722,6 +729,7 @@ static void DoGameOver(void)
 				sprintf(num2, "%3.1d", final[i].Frags);
 				sprintf(buffer, "Player %d: %-.7s Points, %-.3s Frags", final[i].Player, num1, num2);
 				label->SetText(buffer);
+				label->Show();
 			}
 		}
 	}
@@ -744,8 +752,7 @@ static void DoGameOver(void)
 	/* -- They got a high score! */
 	gLastHigh = which;
 
-	label = panel->GetElement<UIElementLabel>("name");
-	if (label && (which != -1) && (gStartLevel == 1) && (gStartLives == 3) &&
+	if ((which != -1) && (gStartLevel == 1) && (gStartLives == 3) &&
 					(gNumPlayers == 1) && !gDeathMatch ) {
 		sound->PlaySound(gBonusShot, 5);
 		for ( i = 8; i >= which ; --i ) {
@@ -757,10 +764,21 @@ static void DoGameOver(void)
 		/* -- Let them enter their name */
 		chars_in_handle = 0;
 		handle[0] = '\0';
+		label = panel->GetElement<UIElementLabel>("name_label");
+		if (label) {
+			label->Show();
+		}
+		label = panel->GetElement<UIElementLabel>("name");
+		if (label) {
+			label->Show();
+		}
+		ui->Draw();
 
 		while ( screen->PollEvent(&event) ) /* Loop, flushing events */;
 		SDL_StartTextInput();
-		while ( !done ) {
+		while ( label && !done ) {
+			bool updated = false;
+
 			screen->WaitEvent(&event);
 
 			if ( event.type == SDL_KEYUP ) {
@@ -773,6 +791,8 @@ static void DoGameOver(void)
 						if ( chars_in_handle ) {
 							sound->PlaySound(gExplosionSound, 5);
 							--chars_in_handle;
+							handle[chars_in_handle] = '\0';
+							label->SetText(handle);
 						}
 						break;
 					default:
@@ -785,12 +805,12 @@ static void DoGameOver(void)
 					if ( chars_in_handle < 15 ) {
 						sound->PlaySound(gShotSound, 5);
 						handle[chars_in_handle++] = key;
+						handle[chars_in_handle] = '\0';
+						label->SetText(handle);
 					} else
 						sound->PlaySound(gBonk, 5);
 				}
 			}
-			handle[chars_in_handle] = '\0';
-			label->SetText(handle);
 			ui->Draw();
 		}
 		SDL_StopTextInput();
diff --git a/screenlib/UIPanel.cpp b/screenlib/UIPanel.cpp
index 0f97a5c0..9136f00c 100644
--- a/screenlib/UIPanel.cpp
+++ b/screenlib/UIPanel.cpp
@@ -176,6 +176,14 @@ UIPanel::Hide()
 	}
 }
 
+void
+UIPanel::HideAll()
+{
+	for (unsigned i = 0; i < m_elements.length(); ++i) {
+		m_elements[i]->Hide();
+	}
+}
+
 void
 UIPanel::Draw()
 {
diff --git a/screenlib/UIPanel.h b/screenlib/UIPanel.h
index 5b398930..301ad18f 100644
--- a/screenlib/UIPanel.h
+++ b/screenlib/UIPanel.h
@@ -93,6 +93,8 @@ class UIPanel : public UIArea
 	virtual void Show();
 	virtual void Hide();
 
+	void HideAll();
+
 	void Draw();
 	bool HandleEvent(const SDL_Event &event);