1 /*
2  * Copyright (C) 2007-2009 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 
19 using System;
20 using System.Timers;
21 using System.ComponentModel;
22 
23 using gbrainy.Core.Main;
24 using gbrainy.Core.Services;
25 
26 namespace gbrainy.Core.Views
27 {
28 	public class CountDownView : IDrawable, IDrawRequest
29 	{
30 		int countdown_time;
31 		System.Timers.Timer timer;
32 		EventHandler finish;
33 		ISynchronizeInvoke synchronize;
34 
35 		public event EventHandler DrawRequest; // Not used in this view
36 
CountDownView(ITranslations translations, EventHandler OnFinish)37 		public CountDownView (ITranslations translations, EventHandler OnFinish)
38 		{
39 			Translations = translations;
40 			timer = new System.Timers.Timer ();
41 			timer.Elapsed += TimerUpdater;
42 			timer.Interval = (1 * 1000); // 1 second
43 			finish = OnFinish;
44 		}
45 
46 		public ISynchronizeInvoke SynchronizingObject {
47 			set { synchronize = value; }
48 			get { return synchronize; }
49 		}
50 
51 		private ITranslations Translations { get; set; }
52 
Start()53 		public void Start ()
54 		{
55 			timer.SynchronizingObject = SynchronizingObject;
56 			countdown_time = 3;
57 			timer.Enabled = true;
58 		}
59 
EndDrawCountDown()60 		public void EndDrawCountDown ()
61 		{
62 			if (timer == null)
63 				return;
64 
65 			timer.Enabled = false;
66 			timer.Dispose ();
67 			timer = null;
68 		}
69 
Draw(CairoContextEx gr, int area_width, int area_height, bool rtl)70 		public void Draw (CairoContextEx gr, int area_width, int area_height, bool rtl)
71 		{
72 			gr.Scale (area_width, area_height);
73 
74 			gr.LineWidth = 0.01;
75 			gr.SetSourceColor (new Cairo.Color (0, 0, 0, 1));
76 
77 			gr.SetPangoLargeFontSize ();
78 			gr.DrawTextCentered (0.5, 0.1, Translations.GetString ("Get ready to memorize the next objects..."));
79 			gr.Stroke ();
80 
81 			gr.SetPangoFontSize (0.35);
82 			gr.DrawTextCentered (0.5, 0.5, countdown_time.ToString ());
83 			gr.Stroke ();
84 
85 			gr.Arc (0.5, 0.5, 0.25, 0, 2 * Math.PI);
86 			gr.Stroke ();
87 			gr.Arc (0.5, 0.5, 0.28, 0, 2 * Math.PI);
88 			gr.Stroke ();
89 		}
90 
91 		// It is executed in another thread
TimerUpdater(object source, ElapsedEventArgs e)92 		void TimerUpdater (object source, ElapsedEventArgs e)
93 		{
94 			lock (this) {
95 
96 				if (countdown_time <= 1) {
97 					finish (this, EventArgs.Empty);
98 				} else
99 					countdown_time--;
100 
101 				if (DrawRequest != null)
102 					DrawRequest (this, EventArgs.Empty);
103 			}
104 		}
105 	}
106 }
107