[gbrainy] Decouples CairoContextEx



commit f97c3ac81f2808ef14a425f28f9ea974cf45399d
Author: Jordi Mas <jmas softcatala org>
Date:   Thu May 27 20:58:00 2010 +0200

    Decouples CairoContextEx

 .../{CairoContextEx.cs => CairoContext.cs}         |  130 ++---------------
 src/Core/Main/CairoContextEx.cs                    |  150 ++++++++++++++++++++
 src/Core/Makefile.am                               |    3 +-
 src/Core/Toolkit/DrawEventArgs.cs                  |    3 +-
 4 files changed, 168 insertions(+), 118 deletions(-)
---
diff --git a/src/Core/Libraries/CairoContextEx.cs b/src/Core/Libraries/CairoContext.cs
similarity index 71%
rename from src/Core/Libraries/CairoContextEx.cs
rename to src/Core/Libraries/CairoContext.cs
index d13d1ef..ddd434d 100644
--- a/src/Core/Libraries/CairoContextEx.cs
+++ b/src/Core/Libraries/CairoContext.cs
@@ -27,7 +27,12 @@ using gbrainy.Core.Main;
 
 namespace gbrainy.Core.Libraries
 {
-	public class CairoContextEx : Cairo.Context
+	// Basic encapsulation of:
+	//	- Cairo Context
+	//	- Pango Drawing
+	//	- RSvg image drawing
+	//
+	public class CairoContext : Cairo.Context
 	{
 		Pango.Layout layout;
 		double font_size;
@@ -36,18 +41,17 @@ namespace gbrainy.Core.Libraries
 		const double width_margin = 0.04;
 		const double line_spacing = 0.018;
 
-		public CairoContextEx (IntPtr state, Gtk.Widget widget) : base (state)
+		public CairoContext (IntPtr state, Gtk.Widget widget) : base (state)
 		{
 			layout = Pango.CairoHelper.CreateLayout (this);
 
 			// We do not honor DPI settings or font sizes (just font name)
 			// User should resize the window
 			layout.FontDescription = widget.PangoContext.FontDescription.Copy ();
-			SetPangoNormalFontSize ();
 		}
 
 		// Used by GeneratePDF
-		public CairoContextEx (Cairo.Surface s, string font, int dpis) : base (s)
+		public CairoContext (Cairo.Surface s, string font, int dpis) : base (s)
 		{
 			layout = Pango.CairoHelper.CreateLayout (this);
 			layout.FontDescription = FontDescription.FromString (font);
@@ -57,8 +61,6 @@ namespace gbrainy.Core.Libraries
 				Pango.CairoHelper.ContextSetResolution (c, dpis);
 				c.Dispose ();
 			}
-
-			SetPangoNormalFontSize ();
 		}
 
 		// No dispose of resources on this class
@@ -75,7 +77,7 @@ namespace gbrainy.Core.Libraries
 		/*
 			Font drawing using Pango
 
-			* Pango does not work well with float numbers. We should work on 
+			* Pango does not work well with float numbers. We should work on
 			the device unit space and then translate to our user space.
 
 			* Cairo Show.Text paints on the bottom-left of the coordinates
@@ -89,7 +91,7 @@ namespace gbrainy.Core.Libraries
 			Cairo.Matrix old = Matrix;
 
 			UpdateFontSize ();
-			Matrix = new Cairo.Matrix ();		
+			Matrix = new Cairo.Matrix ();
 			layout.SetText (str);
 			layout.SingleParagraphMode = true;
 			Pango.CairoHelper.ShowLayout (this, layout);
@@ -107,7 +109,7 @@ namespace gbrainy.Core.Libraries
 
 			if (width == -1) { // Calculates maximum width in the user space
 				layout.Width = (int) (((1 - width_margin) * Matrix.Xx - (CurrentPoint.X * Matrix.Xx)) * Pango.Scale.PangoScale);
-			} else 
+			} else
 				layout.Width = (int) (width * Matrix.Xx * Pango.Scale.PangoScale);
 
 			if (rotation != 0) {
@@ -125,22 +127,12 @@ namespace gbrainy.Core.Libraries
 			}
 			else
 				ShowPangoText (str);
-				
+
 			layout.FontDescription.Weight = Pango.Weight.Normal;
 			layout.Width = -1;
 			layout.Alignment = align;
 		}
 
-		public void SetPangoNormalFontSize ()
-		{
-			font_size = 0.022;
-		}
-
-		public void SetPangoLargeFontSize ()
-		{
-			font_size = 0.0325;
-		}
-
 		public void SetPangoFontSize (double size)
 		{
 			font_size = size;
@@ -148,8 +140,8 @@ namespace gbrainy.Core.Libraries
 
 		/*
 			Draw text functions
-		*/		
-		
+		*/
+
 		// Used for fractions that right align is needed
 		public void DrawTextAlignedRight (double x, double y, string str)
 		{
@@ -230,82 +222,6 @@ namespace gbrainy.Core.Libraries
 			width = w / old.Xx;
 		}
 
-		public void DrawEquilateralTriangle (double x, double y, double size)
-		{
-			MoveTo (x + (size / 2), y);
-			LineTo (x, y + size);
-			LineTo (x + size, y + size);
-			LineTo (x + (size / 2), y);
-			Stroke ();	
-		}
-
-		public void DrawDiamond (double x, double y, double size)
-		{
-			MoveTo (x + size / 2, y);
-			LineTo (x, y + size / 2);
-			LineTo (x + size / 2, y + size);
-			LineTo (x + size, y + size / 2);
-			LineTo (x + size / 2, y);
-			Stroke ();
-		}
-
-		public void FillGradient (double x, double y, double w, double h)
-		{
-			Save ();
-			LinearGradient shadow = new LinearGradient (x, y, x + w, y + h);
-			shadow.AddColorStop (0, new Cairo.Color (0, 0, 0, 0.3));
-			shadow.AddColorStop (0.5, new Cairo.Color (0, 0, 0, 0.1));
-			Source = shadow;
-			Fill ();
-			Restore ();
-			((IDisposable)shadow).Dispose ();
-		}
-
-		public void DrawClock (double x, double y, double size, int hand_short, int hand_large)
-		{
-			const double radian = Math.PI / 180;
-			double radius = size / 2;
-			double x0, y0;
-			int num, degrees;
-
-			Arc (x, y, radius, 0, 2 * Math.PI);
-			Stroke ();
-			for (degrees = 0; degrees < 360; degrees+= 30) {
-				x0 = radius * Math.Cos (degrees * radian);
-				y0 = radius * Math.Sin (degrees * radian);
-				 // Small lines
-				MoveTo (x + 0.9 * x0, y + 0.9 * y0);
-				LineTo (x + x0, y + y0);
-				Stroke ();
-				// Numbers
-				num = (degrees / 30) + 3;
-				if (num > 12) num = num - 12;
-
-				DrawTextCentered (x + x0 * 0.75,  y + y0 * 0.75, num.ToString ());
-				Stroke ();
-			}
-
-			if (hand_large >=1 && hand_large <= 12 ) {
-				// Hand Large
-				degrees = (hand_large - 3) * 30;
-				x0 = radius * Math.Cos (degrees * radian);
-				y0 = radius * Math.Sin (degrees * radian);
-				MoveTo (x, y);
-				LineTo (x + x0 * 0.55, y + y0 * 0.55);
-				Stroke ();
-			}
-
-			if (hand_short >=1 && hand_short <= 12) {
-				// Hand Short
-				degrees = (hand_short - 3) * 30;
-				x0 = radius * Math.Cos (degrees * radian);
-				y0 = radius * Math.Sin (degrees * radian);
-				MoveTo (x, y);
-				LineTo (x + x0 * 0.4, y + y0 * 0.4);
-				Stroke ();
-			}
-		}
-
 		public void FillGradient (double x, double y, double w, double h, Cairo.Color color)
 		{
 			Save ();
@@ -318,22 +234,6 @@ namespace gbrainy.Core.Libraries
 			((IDisposable)shadow).Dispose ();
 		}
 
-		virtual public void DrawBackground ()
-		{
-			try {
-				if (image == null)
-					image = new SVGImage (System.Reflection.Assembly.GetCallingAssembly (), "background.svg");
-
-				Save ();
-				Rectangle (0, 0, 1, 1);
-				Scale (0.999 / image.Width, 0.999 / image.Height);
-				image.RenderToCairo (Handle);
-				Restore ();
-
-			} catch {
-			}
-		}
-
 		public void DrawImageFromAssembly (string  resource, double x, double y, double width, double height)
 		{
 			try {
@@ -363,7 +263,7 @@ namespace gbrainy.Core.Libraries
 		}
 
 		void DrawImage (SVGImage image, double x, double y, double width, double height)
-		{		
+		{
 			Save ();
 			Translate (x, y);
 			Scale (width / image.Width, height / image.Height);
diff --git a/src/Core/Main/CairoContextEx.cs b/src/Core/Main/CairoContextEx.cs
new file mode 100644
index 0000000..eacfd20
--- /dev/null
+++ b/src/Core/Main/CairoContextEx.cs
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2007-2008 Jordi Mas i Hernàndez <jmas softcatala org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+using System;
+using Cairo;
+
+using gbrainy.Core.Libraries;
+
+namespace gbrainy.Core.Main
+{
+	// Implements functionality specific to gbrainy
+	public class CairoContextEx : CairoContext
+	{
+		Pango.Layout layout;
+		double font_size;
+		static SVGImage image = null;
+
+		const double width_margin = 0.04;
+		const double line_spacing = 0.018;
+
+		public CairoContextEx (IntPtr state, Gtk.Widget widget) : base (state, widget)
+		{
+			SetPangoNormalFontSize ();
+		}
+
+		// Used by GeneratePDF
+		public CairoContextEx (Cairo.Surface s, string font, int dpis) : base (s, font, dpis)
+		{
+			SetPangoNormalFontSize ();
+		}
+
+		virtual public void DrawBackground ()
+		{
+			try {
+				if (image == null)
+					image = new SVGImage (System.Reflection.Assembly.GetCallingAssembly (), "background.svg");
+
+				Save ();
+				Rectangle (0, 0, 1, 1);
+				Scale (0.999 / image.Width, 0.999 / image.Height);
+				image.RenderToCairo (Handle);
+				Restore ();
+
+			} catch {
+			}
+		}
+
+		public void SetPangoNormalFontSize ()
+		{
+			SetPangoFontSize (0.022);
+		}
+
+		public void SetPangoLargeFontSize ()
+		{
+			SetPangoFontSize (0.0325);
+		}
+
+		public void DrawEquilateralTriangle (double x, double y, double size)
+		{
+			MoveTo (x + (size / 2), y);
+			LineTo (x, y + size);
+			LineTo (x + size, y + size);
+			LineTo (x + (size / 2), y);
+			Stroke ();
+		}
+
+		public void DrawDiamond (double x, double y, double size)
+		{
+			MoveTo (x + size / 2, y);
+			LineTo (x, y + size / 2);
+			LineTo (x + size / 2, y + size);
+			LineTo (x + size, y + size / 2);
+			LineTo (x + size / 2, y);
+			Stroke ();
+		}
+
+		public void FillGradient (double x, double y, double w, double h)
+		{
+			Save ();
+			LinearGradient shadow = new LinearGradient (x, y, x + w, y + h);
+			shadow.AddColorStop (0, new Cairo.Color (0, 0, 0, 0.3));
+			shadow.AddColorStop (0.5, new Cairo.Color (0, 0, 0, 0.1));
+			Source = shadow;
+			Fill ();
+			Restore ();
+			((IDisposable)shadow).Dispose ();
+		}
+
+		public void DrawClock (double x, double y, double size, int hand_short, int hand_large)
+		{
+			const double radian = Math.PI / 180;
+			double radius = size / 2;
+			double x0, y0;
+			int num, degrees;
+
+			Arc (x, y, radius, 0, 2 * Math.PI);
+			Stroke ();
+			for (degrees = 0; degrees < 360; degrees+= 30) {
+				x0 = radius * Math.Cos (degrees * radian);
+				y0 = radius * Math.Sin (degrees * radian);
+				 // Small lines
+				MoveTo (x + 0.9 * x0, y + 0.9 * y0);
+				LineTo (x + x0, y + y0);
+				Stroke ();
+				// Numbers
+				num = (degrees / 30) + 3;
+				if (num > 12) num = num - 12;
+
+				DrawTextCentered (x + x0 * 0.75,  y + y0 * 0.75, num.ToString ());
+				Stroke ();
+			}
+
+			if (hand_large >=1 && hand_large <= 12 ) {
+				// Hand Large
+				degrees = (hand_large - 3) * 30;
+				x0 = radius * Math.Cos (degrees * radian);
+				y0 = radius * Math.Sin (degrees * radian);
+				MoveTo (x, y);
+				LineTo (x + x0 * 0.55, y + y0 * 0.55);
+				Stroke ();
+			}
+
+			if (hand_short >=1 && hand_short <= 12) {
+				// Hand Short
+				degrees = (hand_short - 3) * 30;
+				x0 = radius * Math.Cos (degrees * radian);
+				y0 = radius * Math.Sin (degrees * radian);
+				MoveTo (x, y);
+				LineTo (x + x0 * 0.4, y + y0 * 0.4);
+				Stroke ();
+			}
+		}
+	}
+}
diff --git a/src/Core/Makefile.am b/src/Core/Makefile.am
index 078e50e..01495b3 100644
--- a/src/Core/Makefile.am
+++ b/src/Core/Makefile.am
@@ -8,6 +8,7 @@ TARGET_CFG = ../gbrainy.Core.dll.config
 
 CSDISTFILES =  \
 		$(srcdir)/Main/ArrayListIndicesRandom.cs	\
+		$(srcdir)/Main/CairoContextEx.cs	\
 		$(srcdir)/Main/ColorPalette.cs		\
 		$(srcdir)/Main/IMouseEvent.cs		\
 		$(srcdir)/Main/Game.cs			\
@@ -44,7 +45,7 @@ CSDISTFILES =  \
 		$(srcdir)/Views/ViewsControler.cs		\
 		$(srcdir)/Views/WelcomeView.cs		\
 		$(srcdir)/Platform/Unix.cs		\
-		$(srcdir)/Libraries/CairoContextEx.cs	\
+		$(srcdir)/Libraries/CairoContext.cs	\
 		$(srcdir)/Libraries/GetText.cs		\
 		$(srcdir)/Libraries/SVGImage.cs
 
diff --git a/src/Core/Toolkit/DrawEventArgs.cs b/src/Core/Toolkit/DrawEventArgs.cs
index 2737e7a..04cbfb7 100644
--- a/src/Core/Toolkit/DrawEventArgs.cs
+++ b/src/Core/Toolkit/DrawEventArgs.cs
@@ -19,8 +19,7 @@
 
 using System; 
 
-
-using gbrainy.Core.Libraries;
+using gbrainy.Core.Main;
 
 namespace gbrainy.Core.Toolkit
 {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]