[gbrainy] Mouse support for ExtraCircle puzzle



commit 5d9380d5c29e23e08a24b7c4fbdc7526e368a79c
Author: Jordi Mas <jmas softcatala org>
Date:   Thu Jan 6 16:39:10 2011 +0100

    Mouse support for ExtraCircle puzzle

 src/Games/Logic/PuzzleExtraCircle.cs |  123 +++++++++++++++++++++-------------
 1 files changed, 77 insertions(+), 46 deletions(-)
---
diff --git a/src/Games/Logic/PuzzleExtraCircle.cs b/src/Games/Logic/PuzzleExtraCircle.cs
index 8edda39..58797eb 100644
--- a/src/Games/Logic/PuzzleExtraCircle.cs
+++ b/src/Games/Logic/PuzzleExtraCircle.cs
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Jordi Mas i Hernàndez <jmas softcatala org>
+ * Copyright (C) 2008-2011 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,20 +22,32 @@ using Cairo;
 using Mono.Unix;
 
 using gbrainy.Core.Main;
+using gbrainy.Core.Toolkit;
 
 namespace gbrainy.Games.Logic
 {
 	public class PuzzleExtraCircle : Game
 	{
-		private const int total_slices = 6;
-		private const int circles = 4;
-		private const double radius = 0.1;
-		private const double radian = Math.PI / 180;
-		private int ans_pos;
-		private Color[] cercle_colors;
-		private Color[] badcercle_colors;
-		private int[] start_indices;
-		private ColorPalette cp;
+		const int total_slices = 6;
+		const int circles = 4;
+		const double radius = 0.1;
+		const double radian = Math.PI / 180;
+		int ans_pos;
+		Color[] cercle_colors;
+		Color[] badcercle_colors;
+		int[] start_indices;
+		ColorPalette cp;
+		CircleParameters[] circle_parameters;
+
+		struct CircleParameters
+		{
+			public Color [] Colors {get; set; }
+
+			public CircleParameters (Color [] colors)
+			{
+				Colors = colors;
+			}
+		};
 
 		public override string Name {
 			get {return Catalog.GetString ("Extra circle");}
@@ -74,23 +86,67 @@ namespace gbrainy.Games.Logic
 				cercle_colors [i] = cp.Cairo (i);
 				badcercle_colors [i] = cp.Cairo (i);
 			}
-		
+
 			// Correct answer
 			random_indices.Initialize ();
 			clr = badcercle_colors [random_indices[0]];
 			badcercle_colors [random_indices[0]] =  badcercle_colors [random_indices[1]];
 			badcercle_colors [random_indices[1]] = clr;
 
-			// Indices
+			// Create random color order for the session
 			start_indices = new int [circles];
 			for (int i = 0; i < circles; i++)
 				start_indices[i] = (random_indices[i]);
 
 			ans_pos = random.Next (circles);
 			right_answer = GetPossibleAnswer (ans_pos);
+
+			const double text_offset = 0.04;
+			const double with_used = 0.9; // Total width used for drawing all the figures
+			const double margin = 0.1 / circles / 2;
+			const double box_size = with_used / circles;
+			double y;
+			HorizontalContainer container;
+			DrawableArea drawable_area;
+			Color [] colors;
+
+			y = DrawAreaY + 0.1 + (radius / 2);
+
+			container = new HorizontalContainer (0.05, y, with_used, box_size);
+			AddWidget (container);
+
+			circle_parameters = new CircleParameters [circles];
+			for (int i = 0; i < circles; i++)
+			{
+				if (ans_pos == i)
+					colors = badcercle_colors;
+				else
+					colors = cercle_colors;
+
+				circle_parameters [i] = new CircleParameters (colors);
+				drawable_area = new DrawableArea (box_size, box_size);
+				drawable_area.SelectedArea = new Rectangle ((box_size - box_size) / 2, 0, box_size, box_size);
+				drawable_area.Data = i;
+				drawable_area.DataEx = GetPossibleAnswer (i);
+
+				drawable_area.DrawEventHandler += delegate (object sender, DrawEventArgs e)
+				{
+					int idx = (int) e.Data;
+					CircleParameters circle = circle_parameters [idx];
+					double x1, y1;
+
+					x1 = y1 = radius + margin;
+
+					DrawCircle (e.Context, x1, y1, circle.Colors, start_indices [idx]);
+					e.Context.DrawTextCentered (e.Width / 2, box_size + text_offset,
+						GetPossibleFigureAnswer (idx));
+					e.Context.Stroke ();
+				};
+				container.AddChild (drawable_area);
+			}
 		}
 
-		static private void DrawSlice (CairoContextEx gr, double x, double y, double dg, Color color)
+		static void DrawSlice (CairoContextEx gr, double x, double y, double dg, Color color)
 		{
 			double x1, y1, smallest_x, smallest_y, degrees;
 
@@ -103,7 +159,7 @@ namespace gbrainy.Games.Logic
 			gr.LineTo (x1, y1);
 			if (x1 < smallest_x) smallest_x = x1;
 			if (y1 < smallest_y) smallest_y = y1;
-		
+
 			degrees = dg * radian;
 			gr.MoveTo (x, y);
 			x1 = x + radius * Math.Cos (degrees);
@@ -117,49 +173,24 @@ namespace gbrainy.Games.Logic
 			gr.Stroke ();
 		}
 
-		private void DrawCircle (CairoContextEx gr, double x, double y, Color[] colors, int color_indice)
-		{		
+		void DrawCircle (CairoContextEx gr, double x, double y, Color[] colors, int color_indice)
+		{
 			double dg;
 			gr.Arc (x, y, radius, 0, 2 * Math.PI);
 			gr.Stroke ();
-		
+
 			gr.Save ();
-			for (int slice = 0; slice < total_slices; slice++) 
-			{	
+			for (int slice = 0; slice < total_slices; slice++)
+			{
 				dg = slice * (360 / total_slices);
 				DrawSlice (gr, x, y, dg, colors [color_indice]);
-			
+
 				color_indice++;
 				if (color_indice >= colors.Length)
 					color_indice = 0;
-			
-			}
-			gr.Restore ();
-		}
-
-		public override void Draw (CairoContextEx gr, int area_width, int area_height, bool rtl)
-		{		
-			double x = DrawAreaX, y = DrawAreaY;
-			Color [] colors;
-
-			base.Draw (gr, area_width, area_height, rtl);
-
-			x+= radius / 2;
-			y+= radius / 2;
 
-			for (int i = 0; i < circles; i++)
-			{
-				if (ans_pos == i)
-					colors = badcercle_colors;
-				else
-					colors = cercle_colors;
-
-				DrawCircle (gr, x + i * 0.23, y + 0.2, colors, start_indices[i]);
-
-				gr.MoveTo (x - 0.07 + i * 0.22, y + 0.36);
-				gr.ShowPangoText (GetPossibleFigureAnswer (i));
-				gr.Stroke ();
 			}
+			gr.Restore ();
 		}
 	}
 }



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