[gbrainy] Refactors Palette code and avoids text equals color for all texts in the Memory Colour text



commit 767dc54d54757d8a436d7ec9b5a151ca0b700f1d
Author: Jordi Mas <jmas softcatala org>
Date:   Mon Mar 8 19:27:18 2010 +0100

    Refactors Palette code and avoids text equals color for all texts in the Memory Colour text

 src/Core/Libraries/CairoContextEx.cs      |    4 +-
 src/Core/Main/ColorPalette.cs             |   61 ++++++++---------------------
 src/Games/Logic/PuzzleExtraCircle.cs      |    3 +-
 src/Games/Logic/PuzzleLargerShape.cs      |    3 +-
 src/Games/Memory/MemoryColouredFigures.cs |    3 +-
 src/Games/Memory/MemoryColouredText.cs    |   38 +++++++++++++-----
 src/Games/Memory/MemoryCountDots.cs       |    5 +-
 7 files changed, 51 insertions(+), 66 deletions(-)
---
diff --git a/src/Core/Libraries/CairoContextEx.cs b/src/Core/Libraries/CairoContextEx.cs
index fc887da..ad22429 100644
--- a/src/Core/Libraries/CairoContextEx.cs
+++ b/src/Core/Libraries/CairoContextEx.cs
@@ -184,7 +184,7 @@ namespace gbrainy.Core.Libraries
 			Cairo.Matrix old = Matrix;
 
 			if (max_width < 0 || max_width > 1)
-				throw new InvalidOperationException ("Invalid maximum width value");
+				throw new ArgumentOutOfRangeException ("Invalid maximum width value");
 
 			MoveTo (x, y);
 			UpdateFontSize ();
@@ -206,7 +206,7 @@ namespace gbrainy.Core.Libraries
 			Cairo.Matrix old = Matrix;
 
 			if (max_width < 0 || max_width > 1)
-				throw new InvalidOperationException ("Invalid maximum width value");
+				throw new ArgumentOutOfRangeException ("Invalid maximum width value");
 
 			UpdateFontSize ();
 			Matrix = new Cairo.Matrix ();
diff --git a/src/Core/Main/ColorPalette.cs b/src/Core/Main/ColorPalette.cs
index 25a6d6c..768a3bc 100644
--- a/src/Core/Main/ColorPalette.cs
+++ b/src/Core/Main/ColorPalette.cs
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2007 Javier M Mora <javiermm gmail com>
+ * Copyright (C) 2010 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
@@ -22,27 +23,10 @@ using Mono.Unix;
 
 namespace gbrainy.Core.Main
 {
-	//Utility class for color operations
+	// Utility class that contains the color palette used for all games
 	public class ColorPalette
 	{
-		private ArrayListIndicesRandom color_order;
-
-		private double alpha;
-		public double Alpha {
-			set { alpha = value; }
-			get { return alpha; }
-		}
-
-		public int Count {
-			get { return color_order.Count; }
-		}
-
-		// Are defined "First", "PrimaryColors", "PrimarySecundaryColors", and "Last" to
-		// create iterators. So: 
-		//   for (Colors.Id it= Colors.Id.First; it<Colors.Id.PrimaryColors; it++);
-		//   for (Colors.Id it= Colors.Id.First; it<Colors.Id.PrimarySecundaryColors; it++);
-		//   for (Colors.Id it= Colors.Id.First; it<Colors.Id.Last; it++);
-		//
+		double alpha;
 
 		public enum Id
 		{
@@ -82,26 +66,18 @@ namespace gbrainy.Core.Main
 			new Cairo.Color (.9, .9, .9)
 		};
 
-		public ColorPalette (Id id)
-		{
-			color_order = new ArrayListIndicesRandom((int)id);
-			alpha=1;
-		}
-
-		public ColorPalette (int size)
+		public ColorPalette ()
 		{
-			color_order = new ArrayListIndicesRandom(size);
-			alpha=1;
+			alpha = 1;
 		}
 
-		public void Initialize()
-		{
-			color_order.Initialize();
+		public double Alpha {
+			set { alpha = value; }
+			get { return alpha; }
 		}
 
-		public Cairo.Color Cairo (int index) 
-		{
-			return Cairo (CairoColor[(int)color_order[index]]);
+		public int Count {
+			get { return ColorName.Length; }
 		}
 
 		public Cairo.Color Cairo (Id id) 
@@ -109,24 +85,19 @@ namespace gbrainy.Core.Main
 			return Cairo (CairoColor[(int)id]);
 		}
 
-		public Cairo.Color Cairo(Cairo.Color color)
+		public Cairo.Color Cairo (int id)
 		{
-			return new Cairo.Color(color.R, color.G, color.B, alpha);
+			return Cairo (CairoColor[id]);
 		}
 
-		public string Name(int index)
+		public string Name (int index)
 		{
-			return ColorName[(int)color_order[index]];
+			return ColorName [index];
 		}
 
-		public string Name(Id id)
+		Cairo.Color Cairo (Cairo.Color color)
 		{
-			return ColorName[(int)id];
-		}
-
-		public int Size()
-		{
-			return color_order.Count;
+			return new Cairo.Color(color.R, color.G, color.B, alpha);
 		}
 	}
 }
diff --git a/src/Games/Logic/PuzzleExtraCircle.cs b/src/Games/Logic/PuzzleExtraCircle.cs
index c4859a8..ddfafd3 100644
--- a/src/Games/Logic/PuzzleExtraCircle.cs
+++ b/src/Games/Logic/PuzzleExtraCircle.cs
@@ -69,8 +69,7 @@ namespace gbrainy.Games.Logic
 			ArrayListIndicesRandom random_indices = new ArrayListIndicesRandom (total_slices);
 			Color clr;
 
-			cp = new ColorPalette (ColorPalette.Id.Last);
-			cp.Initialize ();
+			cp = new ColorPalette ();
 
 			cercle_colors = new Color [total_slices];
 			badcercle_colors =  new Color [total_slices];
diff --git a/src/Games/Logic/PuzzleLargerShape.cs b/src/Games/Logic/PuzzleLargerShape.cs
index 89521e0..f1b8aa5 100644
--- a/src/Games/Logic/PuzzleLargerShape.cs
+++ b/src/Games/Logic/PuzzleLargerShape.cs
@@ -126,8 +126,7 @@ namespace gbrainy.Games.Logic
 
 		public override void Initialize ()
 		{
-			palette = new ColorPalette (ColorPalette.Id.PrimaryColors);
-			palette.Initialize ();
+			palette = new ColorPalette ();
 
 			switch (random.Next (2)) {
 			case 0:
diff --git a/src/Games/Memory/MemoryColouredFigures.cs b/src/Games/Memory/MemoryColouredFigures.cs
index f111164..85cc9ce 100644
--- a/src/Games/Memory/MemoryColouredFigures.cs
+++ b/src/Games/Memory/MemoryColouredFigures.cs
@@ -81,8 +81,7 @@ namespace gbrainy.Games.Memory
 			rect_h = 0.3 / columns;
 			squares_colours = new SquareColor [squares * answers];
 			color_sheme = random.Next (2);
-			palette = new ColorPalette(ColorPalette.Id.PrimarySecundaryColors);
-			palette.Initialize();
+			palette = new ColorPalette ();
 
 			for (int i = 0; i < squares; i++)
 				squares_colours[i] = (SquareColor) random.Next ((int) SquareColor.Length);
diff --git a/src/Games/Memory/MemoryColouredText.cs b/src/Games/Memory/MemoryColouredText.cs
index b4a40cf..5bcbdd8 100644
--- a/src/Games/Memory/MemoryColouredText.cs
+++ b/src/Games/Memory/MemoryColouredText.cs
@@ -31,6 +31,7 @@ namespace gbrainy.Games.Memory
 		private int question;
 		private string question_colorname;
 		private int colors_shown;
+		private ArrayListIndicesRandom color_order;
 
 		public override string Name {
 			get {return Catalog.GetString ("Colored text");}
@@ -47,6 +48,8 @@ namespace gbrainy.Games.Memory
 
 		public override void Initialize ()
 		{
+			bool done = false;
+
 			switch (CurrentDifficulty) {
 			case Difficulty.Easy:
 				colors_shown = 3;
@@ -59,12 +62,25 @@ namespace gbrainy.Games.Memory
 				break;
 			}
 
-			palette = new ColorPalette (colors_shown);
-			palette.Initialize ();
+			palette = new ColorPalette ();
+
+			// It is not acceptable that all the random colors names match the right colors
+			while (done == false) {
+				color_order = new ArrayListIndicesRandom (colors_shown);
+				color_order.Initialize ();
+
+				for (int i = 0; i < colors_shown; i++)
+				{
+					if (palette.Name (color_order [i]) != palette.Name (i)) {
+						done = true;
+						break;
+					}
+				}
+			}			
 		
-			question = random.Next (palette.Count);
-			right_answer = palette.Name (question);
-			question_colorname = palette.Name ((ColorPalette.Id) question);
+			question = random.Next (colors_shown);
+			right_answer = palette.Name (color_order [question]);
+			question_colorname = palette.Name (question);
 		
 			base.Initialize ();
 		}
@@ -77,15 +93,17 @@ namespace gbrainy.Games.Memory
 
 		private void DrawObject (CairoContextEx gr)
 		{
-			palette.Alpha=alpha;
+			double x = DrawAreaX + 0.125, y = DrawAreaY + 0.2;
+			int idx;
 
-			double x= DrawAreaX + 0.125, y = DrawAreaY + 0.2;
+			palette.Alpha = alpha;
 
-			for (int i = 0; i < palette.Count ; i++)
+			for (int i = 0; i < colors_shown; i++)
 			{
-				gr.Color = palette.Cairo(i);
+				idx = color_order [i];
+				gr.Color = palette.Cairo (idx);
 				gr.MoveTo (x, y);
-				gr.ShowPangoText ( palette.Name((ColorPalette.Id)i) );
+				gr.ShowPangoText (palette.Name (i));
 				gr.Stroke ();
 			
 				if (i == 2) {
diff --git a/src/Games/Memory/MemoryCountDots.cs b/src/Games/Memory/MemoryCountDots.cs
index 41babfb..1e164f7 100644
--- a/src/Games/Memory/MemoryCountDots.cs
+++ b/src/Games/Memory/MemoryCountDots.cs
@@ -72,8 +72,7 @@ namespace gbrainy.Games.Memory
 			location_order = new ArrayListIndicesRandom (NUMCOLUMNS*NUMCOLUMNS);
 			location_order.Initialize();
 
-			palette = new ColorPalette(ColorPalette.Id.Last);
-			palette.Initialize();
+			palette = new ColorPalette ();
 
 			// dotsPerColor is compared with iterator of dots. (this iterator is 0 based, so I
 			// have to substract 1 to make dotsPerColor contents 0 based.
@@ -99,7 +98,7 @@ namespace gbrainy.Games.Memory
 			palette.Alpha = alpha;
 			double x = DrawAreaX + 0.15, y = DrawAreaY + 0.1;
 
-			gr.Color = palette.Cairo(ColorPalette.Id.Black);
+			gr.Color = palette.Cairo (ColorPalette.Id.Black);
 			double pos_x = x, pos_y = y;
 			const double figure_size = 0.6;
 			const double square_size = figure_size / NUMCOLUMNS ;



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