[hitori] Bug 575897 — Add timing support



commit b5e1b07672099b0739fbe00c80cd905d4d6d17ef
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sun Oct 31 11:06:06 2010 +0000

    Bug 575897 â?? Add timing support
    
    Add a statusbar with a timer in it. Closes: bgo#575897

 data/hitori.ui  |    7 +++++++
 src/interface.c |    4 ++++
 src/main.c      |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.h      |    7 +++++++
 4 files changed, 68 insertions(+), 0 deletions(-)
---
diff --git a/data/hitori.ui b/data/hitori.ui
index aebdf41..fea113c 100644
--- a/data/hitori.ui
+++ b/data/hitori.ui
@@ -185,6 +185,13 @@
 						<signal name="button-release-event" handler="hitori_button_release_cb"/>
 					</object>
 				</child>
+				<child>
+					<object class="GtkStatusbar" id="hitori_statusbar">
+						<child>
+							<object class="GtkLabel" id="hitori_timer"/>
+						</child>
+					</object>
+				</child>
 			</object>
 		</child>
 	</object>
diff --git a/src/interface.c b/src/interface.c
index 8a2ea11..5bfc41b 100644
--- a/src/interface.c
+++ b/src/interface.c
@@ -82,9 +82,13 @@ hitori_create_interface (Hitori *hitori)
 	hitori->undo_action = GTK_ACTION (gtk_builder_get_object (builder, "undo_menu"));
 	hitori->redo_action = GTK_ACTION (gtk_builder_get_object (builder, "redo_menu"));
 	hitori->hint_action = GTK_ACTION (gtk_builder_get_object (builder, "hint_menu"));
+	hitori->timer_label = GTK_LABEL (gtk_builder_get_object (builder, "hitori_timer"));
 
 	g_object_unref (builder);
 
+	/* Reset the timer */
+	hitori_reset_timer (hitori);
+
 	return hitori->window;
 }
 
diff --git a/src/main.c b/src/main.c
index 1585b0a..eacf7d1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -36,6 +36,9 @@ hitori_new_game (Hitori *hitori, guint board_size)
 	hitori_generate_board (hitori, board_size, -1);
 	hitori_clear_undo_stack (hitori);
 	gtk_widget_queue_draw (hitori->drawing_area);
+
+	hitori_reset_timer (hitori);
+	hitori_start_timer (hitori);
 }
 
 void
@@ -125,6 +128,8 @@ hitori_enable_events (Hitori *hitori)
 	if (hitori->undo_stack->undo != NULL)
 		gtk_action_set_sensitive (hitori->redo_action, TRUE);
 	gtk_action_set_sensitive (hitori->hint_action, TRUE);
+
+	hitori_start_timer (hitori);
 }
 
 void
@@ -134,6 +139,51 @@ hitori_disable_events (Hitori *hitori)
 	gtk_action_set_sensitive (hitori->redo_action, FALSE);
 	gtk_action_set_sensitive (hitori->undo_action, FALSE);
 	gtk_action_set_sensitive (hitori->hint_action, FALSE);
+
+	hitori_pause_timer (hitori);
+}
+
+static void
+set_timer_label (Hitori *hitori)
+{
+	/* Translators: this is the format for the timer label. The first parameter is the number of minutes which have elapsed since the start of the
+	 * game; the second parameter is the number of seconds. */
+	gchar *text = g_strdup_printf (_("Time: %02u:%02u"), hitori->timer_value / 60, hitori->timer_value % 60);
+	gtk_label_set_text (hitori->timer_label, text);
+	g_free (text);
+}
+
+static gboolean
+update_timer_cb (Hitori *hitori)
+{
+	hitori->timer_value++;
+	set_timer_label (hitori);
+
+	return TRUE;
+}
+
+void
+hitori_start_timer (Hitori *hitori)
+{
+	// Remove any old timeout
+	hitori_pause_timer (hitori);
+
+	set_timer_label (hitori);
+	hitori->timeout_id = g_timeout_add_seconds (1, (GSourceFunc) update_timer_cb, hitori);
+}
+
+void
+hitori_pause_timer (Hitori *hitori)
+{
+	if (hitori->timeout_id > 0)
+		g_source_remove (hitori->timeout_id);
+}
+
+void
+hitori_reset_timer (Hitori *hitori)
+{
+	hitori->timer_value = 0;
+	set_timer_label (hitori);
 }
 
 void
diff --git a/src/main.h b/src/main.h
index 6ab9aee..c60d827 100644
--- a/src/main.h
+++ b/src/main.h
@@ -85,6 +85,10 @@ typedef struct {
 
 	gboolean display_error;
 	HitoriVector error_position;
+
+	guint timer_value; /* seconds into the game */
+	GtkLabel *timer_label;
+	guint timeout_id;
 } Hitori;
 
 void hitori_new_game (Hitori *hitori, guint board_size);
@@ -94,6 +98,9 @@ void hitori_print_board (Hitori *hitori);
 void hitori_free_board (Hitori *hitori);
 void hitori_enable_events (Hitori *hitori);
 void hitori_disable_events (Hitori *hitori);
+void hitori_start_timer (Hitori *hitori);
+void hitori_pause_timer (Hitori *hitori);
+void hitori_reset_timer (Hitori *hitori);
 void hitori_set_error_position (Hitori *hitori, HitoriVector position);
 void hitori_quit (Hitori *hitori);
 



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