Maelstrom: Hooked up the main screen as a UIPanel delegate

https://github.com/libsdl-org/Maelstrom/commit/3e155fa67c877db710004fbea36c7a8a18a25ba7

From 3e155fa67c877db710004fbea36c7a8a18a25ba7 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 26 Oct 2011 18:42:22 -0400
Subject: [PATCH] Hooked up the main screen as a UIPanel delegate

---
 Makefile.am         |  1 +
 UI/main.xml         |  2 +-
 UIPanels.cpp        | 11 ++++--
 main.cpp            | 89 ++++++++++++++++++---------------------------
 main.h              | 14 +++++++
 netlogic/about.cpp  |  2 +-
 screenlib/UIPanel.h |  7 +++-
 7 files changed, 66 insertions(+), 60 deletions(-)
 create mode 100644 main.h

diff --git a/Makefile.am b/Makefile.am
index 74b9d738..6575cc6f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,7 @@ Maelstrom_SOURCES =		\
 	load.h			\
 	logic.h			\
 	main.cpp		\
+	main.h			\
 	myerror.cpp		\
 	myerror.h		\
 	netscore.cpp		\
diff --git a/UI/main.xml b/UI/main.xml
index 5d9fb0d4..6500104d 100644
--- a/UI/main.xml
+++ b/UI/main.xml
@@ -1,4 +1,4 @@
-<UIPanel>
+<UIPanel delegate="MainPanel">
 	<Elements>
 		<Title name="image" id="129">
 			<Anchor anchorFrom="TOPLEFT" anchorTo="CENTER" x="-251" y="-187"/>
diff --git a/UIPanels.cpp b/UIPanels.cpp
index 4e460054..07bd71cb 100644
--- a/UIPanels.cpp
+++ b/UIPanels.cpp
@@ -1,14 +1,17 @@
 
 #include "screenlib/UIPanel.h"
 #include "UIPanels.h"
+#include "main.h"
 #include "netlogic/about.h"
 
 
 static UIPanelDelegate *
-CreateMaelstromUIDelegate(const char *delegate)
+CreateMaelstromUIDelegate(UIPanel *panel, const char *delegate)
 {
-	if (strcasecmp(delegate, "AboutPanel") == 0) {
-		return new AboutPanelDelegate();
+	if (strcasecmp(delegate, "MainPanel") == 0) {
+		return new MainPanelDelegate(panel);
+	} else if (strcasecmp(delegate, "AboutPanel") == 0) {
+		return new AboutPanelDelegate(panel);
 	} else {
 		fprintf(stderr, "Warning: Couldn't find delegate '%s'\n", delegate);
 		return NULL;
@@ -27,7 +30,7 @@ CreateMaelstromUIPanel(UIManager *ui, const char *type, const char *name, const
 	}
 
 	if (panel && delegate && *delegate) {
-		panel->SetPanelDelegate(CreateMaelstromUIDelegate(delegate));
+		panel->SetPanelDelegate(CreateMaelstromUIDelegate(panel, delegate));
 	}
 
 	return panel;
diff --git a/main.cpp b/main.cpp
index df168f91..8d726313 100644
--- a/main.cpp
+++ b/main.cpp
@@ -15,6 +15,7 @@
 #include "fastrand.h"
 #include "checksum.h"
 #include "about.h"
+#include "main.h"
 
 #include "UIElementLabel.h"
 #include "UIElementKeyButton.h"
@@ -33,9 +34,6 @@ Bool	gUpdateBuffer;
 Bool	gRunning;
 int	gNoDelay;
 
-// Local functions in this file...
-static void SetupMainScreen(void);
-static void DrawMainScreen(void);
 
 // Main Menu actions:
 static void RunDoAbout(void)
@@ -316,8 +314,6 @@ int main(int argc, char *argv[])
 		exit(0);
 	}
 
-	SetupMainScreen();
-
 	DropEvents();
 	gRunning = true;
 //	while ( sound->Playing() )
@@ -325,10 +321,6 @@ int main(int argc, char *argv[])
 	ui->ShowPanel(PANEL_MAIN);
 
 	while ( gRunning ) {
-		
-		/* -- Update the screen if necessary */
-		if ( gUpdateBuffer )
-			DrawMainScreen();
 
 		ui->Draw();
 
@@ -374,124 +366,117 @@ printf("DrawText: %d,%d '%s'\n", x, y-screen->GetImageHeight(textimage)+2, text)
 /* ----------------------------------------------------------------- */
 /* -- Setup the main screen */
 
-void SetupMainScreen()
+bool
+MainPanelDelegate::OnLoad()
 {
-	UIPanel *panel;
 	UIElementLabel *label;
 	UIElementButton *button;
 
-	panel = ui->GetPanel(PANEL_MAIN);
-	if (!panel) {
-		return;
-	}
-
 	/* Set the version */
-	label = panel->GetElement<UIElementLabel>("version");
+	label = m_panel->GetElement<UIElementLabel>("version");
 	if (label) {
 		label->SetText(VERSION_STRING);
 	}
 
 	/* Hook up the action click callbacks */
-	button = panel->GetElement<UIElementButton>("PlayButton");
+	button = m_panel->GetElement<UIElementButton>("PlayButton");
 	if (button) {
 		button->SetClickCallback(RunPlayGame);
 	}
-	button = panel->GetElement<UIElementButton>("ControlsButton");
+	button = m_panel->GetElement<UIElementButton>("ControlsButton");
 	if (button) {
 		button->SetClickCallback(RunConfigureControls);
 	}
-	button = panel->GetElement<UIElementButton>("ZapButton");
+	button = m_panel->GetElement<UIElementButton>("ZapButton");
 	if (button) {
 		button->SetClickCallback(RunZapScores);
 	}
-	button = panel->GetElement<UIElementButton>("AboutButton");
+	button = m_panel->GetElement<UIElementButton>("AboutButton");
 	if (button) {
 		button->SetClickCallback(RunDoAbout);
 	}
-	button = panel->GetElement<UIElementButton>("QuitButton");
+	button = m_panel->GetElement<UIElementButton>("QuitButton");
 	if (button) {
 		button->SetClickCallback(RunQuitGame);
 	}
-	button = panel->GetElement<UIElementButton>("VolumeDownButton");
+	button = m_panel->GetElement<UIElementButton>("VolumeDownButton");
 	if (button) {
 		button->SetClickCallback(DecrementSound);
 	}
-	button = panel->GetElement<UIElementButton>("VolumeUpButton");
+	button = m_panel->GetElement<UIElementButton>("VolumeUpButton");
 	if (button) {
 		button->SetClickCallback(IncrementSound);
 	}
-	button = panel->GetElement<UIElementButton>("ToggleFullscreen");
+	button = m_panel->GetElement<UIElementButton>("ToggleFullscreen");
 	if (button) {
 		button->SetClickCallback(RunToggleFullscreen);
 	}
-	button = panel->GetElement<UIElementButton>("Cheat");
+	button = m_panel->GetElement<UIElementButton>("Cheat");
 	if (button) {
 		button->SetClickCallback(RunCheat);
 	}
-	button = panel->GetElement<UIElementButton>("Special");
+	button = m_panel->GetElement<UIElementButton>("Special");
 	if (button) {
 		button->SetClickCallback(ShowDawn);
 	}
-	button = panel->GetElement<UIElementButton>("Screenshot");
+	button = m_panel->GetElement<UIElementButton>("Screenshot");
 	if (button) {
 		button->SetClickCallback(RunScreenshot);
 	}
 
-	button = panel->GetElement<UIElementButton>("SetVolume0");
+	button = m_panel->GetElement<UIElementButton>("SetVolume0");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(0));
 	}
-	button = panel->GetElement<UIElementButton>("SetVolume1");
+	button = m_panel->GetElement<UIElementButton>("SetVolume1");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(1));
 	}
-	button = panel->GetElement<UIElementButton>("SetVolume2");
+	button = m_panel->GetElement<UIElementButton>("SetVolume2");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(2));
 	}
-	button = panel->GetElement<UIElementButton>("SetVolume3");
+	button = m_panel->GetElement<UIElementButton>("SetVolume3");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(3));
 	}
-	button = panel->GetElement<UIElementButton>("SetVolume4");
+	button = m_panel->GetElement<UIElementButton>("SetVolume4");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(4));
 	}
-	button = panel->GetElement<UIElementButton>("SetVolume5");
+	button = m_panel->GetElement<UIElementButton>("SetVolume5");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(5));
 	}
-	button = panel->GetElement<UIElementButton>("SetVolume6");
+	button = m_panel->GetElement<UIElementButton>("SetVolume6");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(6));
 	}
-	button = panel->GetElement<UIElementButton>("SetVolume7");
+	button = m_panel->GetElement<UIElementButton>("SetVolume7");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(7));
 	}
-	button = panel->GetElement<UIElementButton>("SetVolume8");
+	button = m_panel->GetElement<UIElementButton>("SetVolume8");
 	if (button) {
 		button->SetButtonDelegate(new SetVolumeDelegate(8));
 	}
 
-	DrawMainScreen();
-}
-
+	gUpdateBuffer = true;
 
-/* ----------------------------------------------------------------- */
-/* -- Draw the main screen */
+	return true;
+}
 
-void DrawMainScreen()
+void
+MainPanelDelegate::OnTick()
 {
-	UIPanel *panel;
 	UIElementLabel *label;
 	char name[32];
 	char text[128];
 
-	panel = ui->GetPanel("main");
-	if (!panel) {
+	if (!gUpdateBuffer) {
 		return;
 	}
+	gUpdateBuffer = false;
 
 	for (int index = 0; index < 10; index++) {
 		Uint8 R, G, B;
@@ -507,14 +492,14 @@ void DrawMainScreen()
 		}
 
 		sprintf(name, "name_%d", index);
-		label = panel->GetElement<UIElementLabel>(name);
+		label = m_panel->GetElement<UIElementLabel>(name);
 		if (label) {
 			label->SetTextColor(R, G, B);
 			label->SetText(hScores[index].name);
 		}
 
 		sprintf(name, "score_%d", index);
-		label = panel->GetElement<UIElementLabel>(name);
+		label = m_panel->GetElement<UIElementLabel>(name);
 		if (label) {
 			label->SetTextColor(R, G, B);
 			sprintf(text, "%d", hScores[index].score);
@@ -522,7 +507,7 @@ void DrawMainScreen()
 		}
 
 		sprintf(name, "wave_%d", index);
-		label = panel->GetElement<UIElementLabel>(name);
+		label = m_panel->GetElement<UIElementLabel>(name);
 		if (label) {
 			label->SetTextColor(R, G, B);
 			sprintf(text, "%d", hScores[index].wave);
@@ -530,19 +515,17 @@ void DrawMainScreen()
 		}
 	}
 
-	label = panel->GetElement<UIElementLabel>("last_score");
+	label = m_panel->GetElement<UIElementLabel>("last_score");
 	if (label) {
 		sprintf(text, "%d", GetScore());
 		label->SetText(text);
 	}
 
-	label = panel->GetElement<UIElementLabel>("volume");
+	label = m_panel->GetElement<UIElementLabel>("volume");
 	if (label) {
 		sprintf(text, "%d", gSoundLevel);
 		label->SetText(text);
 	}
-
-	ui->Draw();
 }
 
 
diff --git a/main.h b/main.h
new file mode 100644
index 00000000..d36f987b
--- /dev/null
+++ b/main.h
@@ -0,0 +1,14 @@
+
+#ifndef _main_h
+#define _main_h
+
+class MainPanelDelegate : public UIPanelDelegate
+{
+public:
+	MainPanelDelegate(UIPanel *panel) : UIPanelDelegate(panel) { }
+
+	virtual bool OnLoad();
+	virtual void OnTick();
+};
+
+#endif // _main_h
diff --git a/netlogic/about.cpp b/netlogic/about.cpp
index 041e6f13..a0dcb855 100644
--- a/netlogic/about.cpp
+++ b/netlogic/about.cpp
@@ -4,7 +4,7 @@
 #include "about.h"
 
 
-AboutPanelDelegate::AboutPanelDelegate()
+AboutPanelDelegate::AboutPanelDelegate(UIPanel *panel) : UIPanelDelegate(panel)
 {
 	numsprites = 0;
 }
diff --git a/screenlib/UIPanel.h b/screenlib/UIPanel.h
index a96f59d1..d21cf9bc 100644
--- a/screenlib/UIPanel.h
+++ b/screenlib/UIPanel.h
@@ -33,16 +33,21 @@
 
 class FrameBuf;
 class UIManager;
-
+class UIPanel;
 
 class UIPanelDelegate
 {
 public:
+	UIPanelDelegate(UIPanel *panel) { m_panel = panel; }
+
 	virtual bool OnLoad() { return true; }
 	virtual void OnShow() { }
 	virtual void OnHide() { }
 	virtual void OnTick() { }
 	virtual void OnDraw() { }
+
+protected:
+	UIPanel *m_panel;
 };
 
 class UIPanel : public UIArea