1 /*
2  * Copyright (C) 2007-2008 Jordi Mas i Hernàndez <jmas@softcatala.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 using System;
19 using Cairo;
20 
21 using gbrainy.Core.Libraries;
22 using gbrainy.Core.Services;
23 
24 namespace gbrainy.Core.Main
25 {
26 	// Implements functionality specific to gbrainy
27 	public class CairoContextEx : CairoContext
28 	{
29 		static SVGImage image;
30 
CairoContextEx(IntPtr handle)31 		public CairoContextEx (IntPtr handle) : base (handle)
32 		{
33 			CommonConstructor ();
34 		}
35 
36 		// Used by GeneratePDF
CairoContextEx(Cairo.Surface s, string font, int dpis)37 		public CairoContextEx (Cairo.Surface s, string font, int dpis) : base (s, font, dpis)
38 		{
39 			CommonConstructor ();
40 		}
41 
CommonConstructor()42 		void CommonConstructor ()
43 		{
44 			Theme theme;
45 
46 			theme = ThemeManager.FromName (Preferences.Get <string> (Preferences.ThemeKey));
47 			FontFace = theme.FontFace;
48 
49 			SetPangoNormalFontSize ();
50 		}
51 
ResetDrawBackgroundCache()52 		public static void ResetDrawBackgroundCache ()
53 		{
54 			if (image == null)
55 				return;
56 
57 			image.Dispose ();
58 			image = null;
59 		}
60 
DrawBackground()61 		virtual public void DrawBackground ()
62 		{
63 			try {
64 				if (image == null)
65 				{
66 					Theme theme;
67 
68 					theme = ThemeManager.FromName (Preferences.Get <string> (Preferences.ThemeKey));
69 					image = new SVGImage (System.IO.Path.Combine (Defines.DATA_DIR, theme.BackgroundImage));
70 				}
71 
72 				Save ();
73 				Rectangle (0, 0, 1, 1);
74                 Stroke ();
75 				Scale (0.999999 / image.Width, 0.999999 / image.Height);
76 				image.RenderToCairo (Handle);
77 				Restore ();
78 			}
79 			catch (Exception e)
80 			{
81 				Console.WriteLine ("CairoContextEx.DrawBackground {0}", e);
82 			}
83 		}
84 
SetPangoNormalFontSize()85 		public void SetPangoNormalFontSize ()
86 		{
87 			SetPangoFontSize (0.022);
88 		}
89 
SetPangoLargeFontSize()90 		public void SetPangoLargeFontSize ()
91 		{
92 			SetPangoFontSize (0.0325);
93 		}
94 
DrawEquilateralTriangle(double x, double y, double size)95 		public void DrawEquilateralTriangle (double x, double y, double size)
96 		{
97 			MoveTo (x + (size / 2), y);
98 			LineTo (x, y + size);
99 			LineTo (x + size, y + size);
100 			LineTo (x + (size / 2), y);
101 			Stroke ();
102 		}
103 
DrawDiamond(double x, double y, double size)104 		public void DrawDiamond (double x, double y, double size)
105 		{
106 			MoveTo (x + size / 2, y);
107 			LineTo (x, y + size / 2);
108 			LineTo (x + size / 2, y + size);
109 			LineTo (x + size, y + size / 2);
110 			LineTo (x + size / 2, y);
111 			Stroke ();
112 		}
113 
114 		// Draws a regular pentagon
DrawPentagon(double x, double y, double size)115 		public void DrawPentagon (double x, double y, double size)
116 		{
117 			MoveTo (x + (0.4998 * size), y + ( 0.0051 * size));
118 			LineTo (x + (0.9949 * size), y + (0.3648 * size));
119 			LineTo (x + (0.8058 * size), y + (0.9468 * size));
120 			LineTo (x + (0.1938 * size), y + (0.9468 * size));
121 			LineTo (x + (0.0046 * size), y + (0.3648 * size));
122 			LineTo (x + (0.4998 * size), y + (0.0051 * size));
123 			Stroke ();
124 		}
125 
FillGradient(double x, double y, double w, double h)126 		public void FillGradient (double x, double y, double w, double h)
127 		{
128 			Save ();
129 			LinearGradient shadow = new LinearGradient (x, y, x + w, y + h);
130 			shadow.AddColorStop (0, new Cairo.Color (0, 0, 0, 0.3));
131 			shadow.AddColorStop (0.5, new Cairo.Color (0, 0, 0, 0.1));
132 			SetSource(shadow);
133 			Fill ();
134 			Restore ();
135 			((IDisposable)shadow).Dispose ();
136 		}
137 
DrawClock(double x, double y, double size, int hand_short, int hand_large)138 		public void DrawClock (double x, double y, double size, int hand_short, int hand_large)
139 		{
140 			const double radian = Math.PI / 180;
141 			double radius = size / 2;
142 			double x0, y0;
143 			int degrees;
144 			string dir;
145 			IConfiguration config;
146 
147 			config = ServiceLocator.Instance.GetService <IConfiguration> ();
148 			dir = config.Get <string> (ConfigurationKeys.GamesGraphics);
149 			DrawImageFromFile (System.IO.Path.Combine (dir, "clock.svg"), x, y, size, size);
150 
151 			x += size / 2;
152 			y += size / 2;
153 
154 			if (hand_large >=1 && hand_large <= 12 ) {
155 				// Hand Large
156 				degrees = (hand_large - 3) * 30;
157 				x0 = radius * Math.Cos (degrees * radian);
158 				y0 = radius * Math.Sin (degrees * radian);
159 				MoveTo (x, y);
160 				LineTo (x + x0 * 0.45, y + y0 * 0.45);
161 				Stroke ();
162 			}
163 
164 			if (hand_short >=1 && hand_short <= 12) {
165 				// Hand Short
166 				degrees = (hand_short - 3) * 30;
167 				x0 = radius * Math.Cos (degrees * radian);
168 				y0 = radius * Math.Sin (degrees * radian);
169 				MoveTo (x, y);
170 				LineTo (x + x0 * 0.30, y + y0 * 0.30);
171 				Stroke ();
172 			}
173 		}
174 	}
175 }
176