Back to index

gcompris  8.2.2
timer.c
Go to the documentation of this file.
00001 /* gcompris - timer.c
00002  *
00003  * Time-stamp: <2001/10/15 01:10:21 bruno>
00004  *
00005  * Copyright (C) 2002 Bruno coudoin
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 
00022 #include "gcompris_config.h"
00023 #include "timer.h"
00024 
00025 #define SOUNDLISTFILE PACKAGE
00026 
00027 static gboolean              paused;
00028 static double         x;
00029 static double         y;
00030 static double         ystep;
00031 static int            timer;
00032 static double         subratio;
00033 static TimerList      type;
00034 static gint           timer_increment (GtkWidget *widget, gpointer data);
00035 static gint           subtimer_increment (GtkWidget *widget, gpointer data);
00036 static void           start_animation();
00037 static GnomeCanvasGroup *boardRootItem = NULL;
00038 static gint           animate_id = 0;
00039 static gint           subanimate_id = 0;
00040 static GcomprisTimerEnd      gcomprisTimerEnd;
00041 
00042 GnomeCanvasItem      *gc_timer_item;
00043 
00044 void
00045 gc_timer_display(int ax, int ay, TimerList atype, int second, GcomprisTimerEnd agcomprisTimerEnd)
00046 {
00047   GdkFont *gdk_font;
00048   GdkPixbuf *pixmap = NULL;
00049   GcomprisProperties *properties = gc_prop_get();
00050 
00051   /* Timer is not requested */
00052   if(properties->timer==0)
00053     return;
00054 
00055   gdk_font = gdk_font_load (FONT_BOARD_MEDIUM);
00056 
00057   gc_timer_end();
00058 
00059   paused = FALSE;
00060 
00061   boardRootItem = GNOME_CANVAS_GROUP(
00062                                  gnome_canvas_item_new (gnome_canvas_root(gc_get_canvas()),
00063                                                      gnome_canvas_group_get_type (),
00064                                                      "x", (double) 0,
00065                                                      "y", (double) 0,
00066                                                      NULL));
00067 
00068   x = ax;
00069   y = ay;
00070   second = second / properties->timer;
00071   timer = second;
00072 
00073   type = atype;
00074   gcomprisTimerEnd = agcomprisTimerEnd;
00075 
00076   switch(type)
00077     {
00078     case GCOMPRIS_TIMER_SAND:
00079     case GCOMPRIS_TIMER_CLOCK:
00080       {
00081        gchar         *filefull = NULL;
00082        gchar         *filename = NULL;
00083        gint          fileid;
00084        
00085        fileid = (gint)timer;
00086        if(type==GCOMPRIS_TIMER_SAND)
00087          filename = g_strdup_printf("gcompris/timers/sablier%d.png", fileid);
00088        else
00089          filename = g_strdup_printf("gcompris/timers/clock%d.png", fileid);
00090        
00091        filefull = g_strdup_printf("%s/%s", properties->package_data_dir, filename);
00092        if (g_file_test ((filefull), G_FILE_TEST_EXISTS))
00093          {
00094            pixmap = gc_pixmap_load(filename);
00095 
00096            gc_timer_item = gnome_canvas_item_new (boardRootItem,
00097                                              gnome_canvas_pixbuf_get_type (),
00098                                              "pixbuf", pixmap, 
00099                                              "x", x,
00100                                              "y", y,
00101                                              NULL);
00102            
00103            gdk_pixbuf_unref(pixmap);
00104          }
00105        g_free(filename);
00106        g_free(filefull);
00107       }
00108       break;
00109     case GCOMPRIS_TIMER_TEXT:
00110       {
00111        gchar *tmpstr = g_strdup_printf("Remaining Time = %d", timer);
00112        /* Display the value for this timer */
00113        gc_timer_item = gnome_canvas_item_new (boardRootItem,
00114                                           gnome_canvas_text_get_type (),
00115                                           "text", tmpstr,
00116                                           "font_gdk", gdk_font,
00117                                           "x", x,
00118                                           "y", y,
00119                                           "anchor", GTK_ANCHOR_CENTER,
00120                                           "fill_color", "white",
00121                                           NULL);
00122        g_free(tmpstr);
00123       }
00124       break;
00125     case GCOMPRIS_TIMER_BALLOON:
00126       pixmap = gc_pixmap_load("gcompris/misc/tuxballoon.png");
00127       gc_timer_item = gnome_canvas_item_new (boardRootItem,
00128                                         gnome_canvas_pixbuf_get_type (),
00129                                         "pixbuf", pixmap, 
00130                                         "x", x,
00131                                         "y", y,
00132                                         NULL);
00133 
00134       /* Calc the number of step needed to reach the sea based on user y and second */
00135       ystep = (BOARDHEIGHT-y-gdk_pixbuf_get_height(pixmap))/second;
00136 
00137       gdk_pixbuf_unref(pixmap);
00138 
00139       pixmap = gc_pixmap_load("gcompris/misc/sea.png");
00140       gnome_canvas_item_new (boardRootItem,
00141                           gnome_canvas_pixbuf_get_type (),
00142                           "pixbuf", pixmap, 
00143                           "x", (double) 0,
00144                           "y", (double) BOARDHEIGHT - gdk_pixbuf_get_height(pixmap),
00145                           "width", (double) gdk_pixbuf_get_width(pixmap),
00146                           "height", (double) gdk_pixbuf_get_height(pixmap),
00147                           NULL);
00148       gdk_pixbuf_unref(pixmap);
00149 
00150       break;
00151     default:
00152       break;
00153     }
00154 
00155   start_animation();
00156 }
00157 
00158 void
00159 gc_timer_add(int second)
00160 {
00161   timer +=second;
00162 }
00163 
00164 void
00165 gc_timer_end()
00166 {
00167   if(boardRootItem!=NULL)
00168     gtk_object_destroy (GTK_OBJECT(boardRootItem));
00169   boardRootItem = NULL;
00170 
00171   paused = TRUE;
00172 
00173   if (animate_id)
00174     gtk_timeout_remove (animate_id);
00175   animate_id = 0;
00176 
00177   if (subanimate_id)
00178     gtk_timeout_remove (subanimate_id);
00179   subanimate_id = 0;
00180 }
00181 
00182 void
00183 gc_timer_pause(gboolean pause)
00184 {
00185   if(boardRootItem==NULL)
00186     return;
00187 
00188   paused = pause;
00189 
00190   if(pause)
00191     {
00192       if (animate_id)
00193        gtk_timeout_remove (animate_id);
00194       animate_id = 0;
00195       
00196       if (subanimate_id)
00197        gtk_timeout_remove (subanimate_id);
00198       subanimate_id = 0;
00199     }
00200   else
00201     {
00202       start_animation();
00203     }
00204 
00205 }
00206 
00207 guint
00208 gc_timer_get_remaining()
00209 {
00210   return(timer);
00211 }
00212 
00213 /*
00214  * Local functions
00215  * ---------------
00216  */
00217 static void
00218 start_animation()
00219 {
00220 
00221   switch(type)
00222     {
00223     case GCOMPRIS_TIMER_SAND:
00224     case GCOMPRIS_TIMER_CLOCK:
00225       /* No subanimation */
00226       break;
00227     case GCOMPRIS_TIMER_TEXT:
00228       /* No subanimation */
00229       break;
00230     case GCOMPRIS_TIMER_BALLOON:
00231       /* Perform under second animation */
00232       subratio = 5;
00233       subanimate_id = gtk_timeout_add (1000/subratio, (GtkFunction) subtimer_increment, NULL);
00234       break;
00235     }
00236 
00237   animate_id = gtk_timeout_add (1000, (GtkFunction) timer_increment, NULL);
00238 }
00239 
00240 
00241 static void
00242 display_time_ellapsed()
00243 {
00244   switch(type)
00245     {
00246     case GCOMPRIS_TIMER_TEXT:
00247       /* Display the value for this timer */
00248       if(gc_timer_item)
00249        gnome_canvas_item_set(gc_timer_item,
00250                            "text", _("Time Elapsed"),
00251                            NULL);
00252       break;
00253     default:
00254       break;
00255     }
00256 
00257 }
00258 
00259 
00260 static gint
00261 subtimer_increment(GtkWidget *widget, gpointer data)
00262 {
00263   if(paused)
00264     return(FALSE);
00265 
00266   switch(type)
00267     {
00268     case GCOMPRIS_TIMER_BALLOON:
00269       /* Display the value for this timer */
00270       y+=ystep/subratio;
00271       if(gc_timer_item)
00272        gnome_canvas_item_set(gc_timer_item,
00273                            "y", y,
00274                            NULL);
00275       //      gnome_canvas_update_now(gc_get_canvas());
00276       break;
00277     default:
00278       break;
00279     }
00280   return (TRUE);
00281 }
00282 
00283 static gint
00284 timer_increment(GtkWidget *widget, gpointer data)
00285 {
00286   if(paused)
00287     return(FALSE);
00288 
00289   timer--;
00290 
00291   if(timer==-1)
00292     {
00293       display_time_ellapsed();
00294       if(gcomprisTimerEnd!=NULL)
00295        {
00296          /* Call the callback */
00297          gcomprisTimerEnd();
00298 
00299        }
00300       return (FALSE);
00301     }
00302 
00303   switch(type)
00304     {
00305     case GCOMPRIS_TIMER_SAND:
00306     case GCOMPRIS_TIMER_CLOCK:
00307       if(gc_timer_item)
00308        {
00309          GcomprisProperties *properties = gc_prop_get();
00310          GdkPixbuf   *pixmap = NULL;
00311          gchar              *filefull = NULL;
00312          gchar              *filename = NULL;
00313          gint        fileid;
00314 
00315          fileid = (gint)timer;
00316          if(type==GCOMPRIS_TIMER_SAND)
00317            filename = g_strdup_printf("gcompris/timers/sablier%d.png", fileid);
00318          else
00319            filename = g_strdup_printf("gcompris/timers/clock%d.png", fileid);
00320 
00321          filefull = g_strdup_printf("%s/%s", properties->package_data_dir, filename);
00322          g_debug("timer: filefull = %s\n", filefull);
00323          if (g_file_test ((filefull), G_FILE_TEST_EXISTS))
00324            {
00325              pixmap = gc_pixmap_load(filename);
00326              gnome_canvas_item_set(gc_timer_item,
00327                                 "pixbuf", pixmap,
00328                                 NULL);
00329              gdk_pixbuf_unref(pixmap);
00330            }
00331          g_free(filename);
00332          g_free(filefull);
00333        }
00334       break;
00335     case GCOMPRIS_TIMER_TEXT:
00336       /* Display the value for this timer */
00337       if(gc_timer_item)
00338        {
00339          char *tmpstr = g_strdup_printf(_("Remaining Time = %d"), timer);
00340          gnome_canvas_item_set(gc_timer_item,
00341                             "text", tmpstr,
00342                             NULL);
00343          g_free(tmpstr);
00344        }
00345       break;
00346     case GCOMPRIS_TIMER_BALLOON:
00347       break;
00348     default:
00349       break;
00350     }
00351 
00352   return (TRUE);
00353 }