Maelstrom: All UIAreas have an associated screen, and the UI manager is the top level UIArea

From 9960bf989a4c294b077976ffd8ab098f85b8f3a0 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 23 Oct 2011 02:57:59 -0400
Subject: [PATCH] All UIAreas have an associated screen, and the UI manager is
 the top level UIArea

---
 screenlib/SDL_FrameBuf.cpp |  8 ++++----
 screenlib/SDL_FrameBuf.h   | 12 ++++++++++--
 screenlib/UIArea.cpp       |  3 ++-
 screenlib/UIArea.h         | 11 ++++++++---
 screenlib/UIElement.cpp    |  3 +--
 screenlib/UIElement.h      |  1 -
 screenlib/UIManager.cpp    |  3 +--
 screenlib/UIManager.h      |  5 ++---
 screenlib/UIPanel.cpp      |  5 ++---
 screenlib/UIPanel.h        |  4 ----
 10 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/screenlib/SDL_FrameBuf.cpp b/screenlib/SDL_FrameBuf.cpp
index 0a484f96..a190e6c9 100644
--- a/screenlib/SDL_FrameBuf.cpp
+++ b/screenlib/SDL_FrameBuf.cpp
@@ -33,7 +33,7 @@
 #define MAX(A, B)	((A > B) ? A : B)
 
 /* Constructors cannot fail. :-/ */
-FrameBuf:: FrameBuf() : UIArea()
+FrameBuf:: FrameBuf() : ErrorBase()
 {
 	/* Initialize various variables to null state */
 	window = NULL;
@@ -64,11 +64,11 @@ FrameBuf:: Init(int width, int height, Uint32 window_flags, Uint32 render_flags,
 	}
 
 	/* Set the UI area */
-	m_rect.w = width;
-	m_rect.h = height;
+	rect.w = width;
+	rect.h = height;
 
 	/* Set the blit clipping rectangle */
-	clip = m_rect;
+	clip = rect;
 
 	/* Copy the image colormap */
 	if ( colors ) {
diff --git a/screenlib/SDL_FrameBuf.h b/screenlib/SDL_FrameBuf.h
index 864a63e6..1a440be8 100644
--- a/screenlib/SDL_FrameBuf.h
+++ b/screenlib/SDL_FrameBuf.h
@@ -33,14 +33,13 @@
 
 #include "SDL.h"
 #include "ErrorBase.h"
-#include "UIArea.h"
 
 typedef enum {
 	DOCLIP,
 	NOCLIP
 } clipval;
 
-class FrameBuf : public UIArea {
+class FrameBuf : public ErrorBase {
 
 public:
 	FrameBuf();
@@ -75,6 +74,14 @@ class FrameBuf : public UIArea {
 		}
 	}
 
+	/* Information routines */
+	int Width() const {
+		return rect.w;
+	}
+	int Height() const {
+		return rect.h;
+	}
+
 	/* Blit and update routines */
 	void QueueBlit(int dstx, int dsty, SDL_Texture *src,
 			int srcx, int srcy, int w, int h, clipval do_clip);
@@ -170,6 +177,7 @@ class FrameBuf : public UIArea {
 	SDL_Renderer *renderer;
 	Uint32 image_map[256];
 	int faded;
+	SDL_Rect rect;
 	SDL_Rect clip;
 
 	void UpdateDrawColor(Uint32 color) {
diff --git a/screenlib/UIArea.cpp b/screenlib/UIArea.cpp
index f12086f2..f0ac4ef4 100644
--- a/screenlib/UIArea.cpp
+++ b/screenlib/UIArea.cpp
@@ -50,8 +50,9 @@ static AnchorLocation ParseAnchorLocation(const char *text)
 
 }
 
-UIArea::UIArea() : ErrorBase()
+UIArea::UIArea(FrameBuf *screen) : ErrorBase()
 {
+	m_screen = screen;
 	m_rect.x = 0;
 	m_rect.y = 0;
 	m_rect.w = 0;
diff --git a/screenlib/UIArea.h b/screenlib/UIArea.h
index da98bbff..f592a2c4 100644
--- a/screenlib/UIArea.h
+++ b/screenlib/UIArea.h
@@ -24,9 +24,7 @@
 #define _UIArea_h
 
 #include "SDL_rect.h"
-
 #include "../utils/rapidxml.h"
-
 #include "ErrorBase.h"
 
 enum {
@@ -51,10 +49,12 @@ enum AnchorLocation {
 	BOTTOMRIGHT = (Y_BOTTOM|X_RIGHT)
 };
 
+class FrameBuf;
+
 class UIArea : public ErrorBase
 {
 public:
-	UIArea();
+	UIArea(FrameBuf *screen);
 
 	bool Load(rapidxml::xml_node<> *node);
 
@@ -71,6 +71,10 @@ class UIArea : public ErrorBase
 		m_rect.w = w;
 		m_rect.h = h;
 	}
+
+	FrameBuf *GetScreen() const {
+		return m_screen;
+	}
 	const SDL_Rect *GetRect() const {
 		return &m_rect;
 	}
@@ -99,6 +103,7 @@ class UIArea : public ErrorBase
 	}
 
 protected:
+	FrameBuf *m_screen;
 	SDL_Rect m_rect;
 	bool m_shown;
 };
diff --git a/screenlib/UIElement.cpp b/screenlib/UIElement.cpp
index 6b43892a..0280be16 100644
--- a/screenlib/UIElement.cpp
+++ b/screenlib/UIElement.cpp
@@ -23,12 +23,11 @@
 #include "UIPanel.h"
 #include "UIElement.h"
 
-UIElement::UIElement(UIPanel *panel, const char *name) : UIArea()
+UIElement::UIElement(UIPanel *panel, const char *name) : UIArea(panel->GetScreen())
 {
 	m_name = new char[strlen(name)+1];
 	strcpy(m_name, name);
 
-	m_screen = panel->GetScreen();
 	m_panel = panel;
 }
 
diff --git a/screenlib/UIElement.h b/screenlib/UIElement.h
index a83a9015..acafbf82 100644
--- a/screenlib/UIElement.h
+++ b/screenlib/UIElement.h
@@ -51,7 +51,6 @@ class UIElement : public UIArea
 
 protected:
 	char *m_name;
-	FrameBuf *m_screen;
 	UIPanel *m_panel;
 };
 
diff --git a/screenlib/UIManager.cpp b/screenlib/UIManager.cpp
index 98b689ea..b5f0238b 100644
--- a/screenlib/UIManager.cpp
+++ b/screenlib/UIManager.cpp
@@ -25,9 +25,8 @@
 #include "UIPanel.h"
 
 
-UIManager::UIManager(FrameBuf *screen, UIElementFactory factory) : ErrorBase()
+UIManager::UIManager(FrameBuf *screen, UIElementFactory factory) : UIArea(screen)
 {
-	m_screen = screen;
 	m_elementFactory = factory;
 }
 
diff --git a/screenlib/UIManager.h b/screenlib/UIManager.h
index ba73392b..28500940 100644
--- a/screenlib/UIManager.h
+++ b/screenlib/UIManager.h
@@ -25,7 +25,7 @@
 
 #include "SDL.h"
 #include "../utils/array.h"
-#include "ErrorBase.h"
+#include "UIArea.h"
 
 class FrameBuf;
 class UIPanel;
@@ -33,7 +33,7 @@ class UIElement;
 
 typedef UIElement *(*UIElementFactory)(UIPanel *panel, const char *name);
 
-class UIManager : public ErrorBase
+class UIManager : public UIArea
 {
 public:
 	UIManager(FrameBuf *screen, UIElementFactory factory);
@@ -77,7 +77,6 @@ class UIManager : public ErrorBase
 	bool HandleEvent(const SDL_Event &event);
 
 protected:
-	FrameBuf *m_screen;
 	UIElementFactory m_elementFactory;
 	array<UIPanel *> m_panels;
 	array<UIPanel *> m_visible;
diff --git a/screenlib/UIPanel.cpp b/screenlib/UIPanel.cpp
index 7d534776..9cb59c72 100644
--- a/screenlib/UIPanel.cpp
+++ b/screenlib/UIPanel.cpp
@@ -28,10 +28,9 @@
 #include "UIElement.h"
 
 
-UIPanel::UIPanel(UIManager *ui, const char *name) : UIArea()
+UIPanel::UIPanel(UIManager *ui, const char *name) : UIArea(ui->GetScreen())
 {
 	m_ui = ui;
-	m_screen = ui->GetScreen();
 	m_name = new char[strlen(name)+1];
 	strcpy(m_name, name);
 
@@ -147,7 +146,7 @@ UIArea *
 UIPanel::GetAnchorElement(const char *name)
 {
 	if (!name) {
-		return m_screen;
+		return m_ui;
 	}
 	return NULL;
 }
diff --git a/screenlib/UIPanel.h b/screenlib/UIPanel.h
index 3506494b..10d0dec3 100644
--- a/screenlib/UIPanel.h
+++ b/screenlib/UIPanel.h
@@ -41,9 +41,6 @@ class UIPanel : public UIArea
 	UIPanel(UIManager *ui, const char *name);
 	virtual ~UIPanel();
 
-	FrameBuf *GetScreen() const {
-		return m_screen;
-	}
 	const char *GetName() const {
 		return m_name;
 	}
@@ -68,7 +65,6 @@ class UIPanel : public UIArea
 
 protected:
 	UIManager *m_ui;
-	FrameBuf *m_screen;
 	char *m_name;
 	bool m_fullscreen;
 	array<UIElement *> m_elements;