[gbrainy] Better wrapping support and MeasureString



commit b6bf5b6f0c52b67d39efbc479c55bf8a878817e1
Author: Jordi Mas <jmas softcatala org>
Date:   Sun Mar 7 15:54:39 2010 +0100

    Better wrapping support and MeasureString

 src/Core/Libraries/CairoContextEx.cs |   44 +++++++++++++++++++++++----------
 src/Core/Views/FinishView.cs         |   28 ++++++++++++++-------
 src/Core/Views/WelcomeView.cs        |    6 +++-
 src/Games/Memory/MemoryFacts.cs      |    2 +-
 4 files changed, 53 insertions(+), 27 deletions(-)
---
diff --git a/src/Core/Libraries/CairoContextEx.cs b/src/Core/Libraries/CairoContextEx.cs
index b38ace0..fc887da 100644
--- a/src/Core/Libraries/CairoContextEx.cs
+++ b/src/Core/Libraries/CairoContextEx.cs
@@ -34,6 +34,7 @@ namespace gbrainy.Core.Libraries
 		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)
 		{
@@ -154,7 +155,7 @@ namespace gbrainy.Core.Libraries
 			layout.SingleParagraphMode = true;
 			layout.Width = -1;
 			layout.GetPixelSize (out w, out h);
-			MoveTo ((old.X0 + x * old.Xx) - w, y * old.Xx);
+			MoveTo ((old.X0 + x * old.Xx) - w, y * old.Yy);
 			Pango.CairoHelper.ShowLayout (this, layout);
 			Matrix = old;
 		}
@@ -177,33 +178,48 @@ namespace gbrainy.Core.Libraries
 			Matrix = old;
 		}
 
-		public double DrawStringWithWrapping (double x, double y, string str)
-		{
-			return DrawStringWithWrapping (x, y, str, -1);
-		}
-
-		public double DrawStringWithWrapping (double x, double y, string str, double width)
+		public void DrawStringWithWrapping (double x, double y, string str, double max_width)
 		{
 			int w, h;
 			Cairo.Matrix old = Matrix;
 
+			if (max_width < 0 || max_width > 1)
+				throw new InvalidOperationException ("Invalid maximum width value");
+
 			MoveTo (x, y);
 			UpdateFontSize ();
 			Matrix = new Cairo.Matrix ();
 
-			if (width == -1)
-				layout.Width = (int) ((1.0 - x -  width_margin) * old.Xx * Pango.Scale.PangoScale);
-			else	
-				layout.Width = (int) (width * old.Xx * Pango.Scale.PangoScale);
-
-			layout.Spacing = (int) (0.018 * (old.Xx * Pango.Scale.PangoScale));
+			layout.Width = (int) (max_width * old.Xx * Pango.Scale.PangoScale);
+			layout.Spacing = (int) (line_spacing * (old.Yy * Pango.Scale.PangoScale));
 
 			layout.SingleParagraphMode = false;
 			layout.SetText (str);
 			Pango.CairoHelper.ShowLayout (this, layout);
 			layout.GetPixelSize (out w, out h);
 			Matrix = old;
-			return y + h / old.Xx;
+		}
+
+		public void MeasureString (string str, double max_width, bool wrapping, out double width, out double height)
+		{
+			int w, h;
+			Cairo.Matrix old = Matrix;
+
+			if (max_width < 0 || max_width > 1)
+				throw new InvalidOperationException ("Invalid maximum width value");
+
+			UpdateFontSize ();
+			Matrix = new Cairo.Matrix ();
+
+			layout.Width = (int) (max_width * old.Xx * Pango.Scale.PangoScale);
+			layout.Spacing = (int) (line_spacing * (old.Xx * Pango.Scale.PangoScale));
+
+			layout.SingleParagraphMode = !wrapping;
+			layout.SetText (str);
+			layout.GetPixelSize (out w, out h);
+			Matrix = old;
+			height = h / old.Yy;
+			width = w / old.Xx;
 		}
 
 		public void DrawEquilateralTriangle (double x, double y, double size)
diff --git a/src/Core/Views/FinishView.cs b/src/Core/Views/FinishView.cs
index 874f525..bd3013c 100644
--- a/src/Core/Views/FinishView.cs
+++ b/src/Core/Views/FinishView.cs
@@ -125,7 +125,8 @@ namespace gbrainy.Core.Views
 			double y = 0.04, x = 0.05;
 			const double space_small = 0.02;
 			List <PlayerHistory.PersonalRecord> records;
-			string s;
+			string s, tip;
+			double width, height;
 
 			gr.Scale (area_width, area_height);
 			gr.DrawBackground ();
@@ -167,7 +168,7 @@ namespace gbrainy.Core.Views
 			y += 0.4;
 
 			records	= session.PlayerHistory.GetLastGameRecords ();
-			gr.MoveTo (x, y);		
+			gr.MoveTo (x, y);
 
 			if (records.Count == 0) {
 				bool caching = cached_sessionid != session.ID;
@@ -184,12 +185,16 @@ namespace gbrainy.Core.Views
 				{
 					if (caching)
 						tips.Add (GameTips.Tip);
-						
-					y = gr.DrawStringWithWrapping (x, y,  "- " + tips [i]);
-					if (y > 0.88)
+	
+					tip = "- " + tips [i];
+
+					gr.MeasureString (tip, 1.0 - x, true, out width, out height);
+
+					if (y + height > 0.98)
 						break;
 
-					y += space_small;
+					gr.DrawStringWithWrapping (x, y, tip , 1.0 - x);
+					y += height + space_small;
 				}
 
 				if (caching)
@@ -203,7 +208,6 @@ namespace gbrainy.Core.Views
 
 				for (int i = 0; i < records.Count; i++)
 				{
-
 					switch (records[i].GameType) {
 					case Game.Types.LogicPuzzle:
 						s = String.Format (Catalog.
@@ -233,11 +237,15 @@ namespace gbrainy.Core.Views
 						break;
 					}
 
-					y = gr.DrawStringWithWrapping (x, y,  "- " + s);
-					if (y > 0.88)
+					tip = "- " + s;
+
+					gr.MeasureString (tip, 1.0 - x, true, out width, out height);
+
+					if (y + height > 0.98)
 						break;
 
-					y += space_small;
+					gr.DrawStringWithWrapping (x, y, tip , 1.0 - x);
+					y += height + space_small;
 				}
 			}
 
diff --git a/src/Core/Views/WelcomeView.cs b/src/Core/Views/WelcomeView.cs
index 51d7497..0b969e2 100644
--- a/src/Core/Views/WelcomeView.cs
+++ b/src/Core/Views/WelcomeView.cs
@@ -143,10 +143,12 @@ namespace gbrainy.Core.Views
 			gr.Stroke ();
 
 			gr.DrawStringWithWrapping (0.05, y + 0.07, 
-				Catalog.GetString ("gbrainy is a brain teaser game and trainer to have fun and to keep your brain trained. It includes:"));
+				Catalog.GetString ("gbrainy is a brain teaser game and trainer to have fun and to keep your brain trained. It includes:"),
+				1 - 0.05);
 
 			y = 0.22 + space * 3;
-			gr.DrawStringWithWrapping (0.05, y + 0.17,  Catalog.GetString ("Use the Settings to adjust the difficulty level of the game."));
+			gr.DrawStringWithWrapping (0.05, y + 0.17,  Catalog.GetString ("Use the Settings to adjust the difficulty level of the game."),
+				1 - 0.05);
 			gr.Stroke ();
 
 			foreach (Toolkit.Container container in containers)
diff --git a/src/Games/Memory/MemoryFacts.cs b/src/Games/Memory/MemoryFacts.cs
index 9359335..9c7c41b 100644
--- a/src/Games/Memory/MemoryFacts.cs
+++ b/src/Games/Memory/MemoryFacts.cs
@@ -159,7 +159,7 @@ namespace gbrainy.Games.Memory
 				text += facts[i].fact;
 				text += "\n\n";
 			}
-			gr.DrawStringWithWrapping (0.3, DrawAreaY + 0.2, text);
+			gr.DrawStringWithWrapping (0.3, DrawAreaY + 0.2, text, 0.95 - 0.3);
 		}
 
 		public override bool CheckAnswer (string answer)



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