~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to src/gcompris/timer.c

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-04-21 16:16:27 UTC
  • Revision ID: james.westby@ubuntu.com-20020421161627-s07yahahm817qxs6
Tags: upstream-1.0.3
ImportĀ upstreamĀ versionĀ 1.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gcompris - timer.c
 
2
 *
 
3
 * Time-stamp: <2001/10/15 01:10:21 bruno>
 
4
 *
 
5
 * Copyright (C) 2002 Bruno coudoin
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 */
 
21
 
 
22
#include "timer.h"
 
23
 
 
24
#define SOUNDLISTFILE PACKAGE
 
25
 
 
26
static double            x;
 
27
static double            y;
 
28
static double            ystep;
 
29
static int               timer;
 
30
static double            subratio;
 
31
static double            ratio;
 
32
static TimerList         type;
 
33
GnomeCanvasItem         *item;
 
34
static gint              timer_increment (GtkWidget *widget, gpointer data);
 
35
static gint              subtimer_increment (GtkWidget *widget, gpointer data);
 
36
static GnomeCanvasGroup *boardRootItem = NULL;
 
37
static gint              animate_id = 0;
 
38
static gint              subanimate_id = 0;
 
39
static GcomprisTimerEnd  gcomprisTimerEnd;
 
40
 
 
41
void gcompris_timer_display(int ax, int ay, TimerList atype, int second, GcomprisTimerEnd agcomprisTimerEnd)
 
42
{
 
43
  GdkFont *gdk_font;
 
44
  GdkPixbuf *pixmap = NULL;
 
45
 
 
46
  gdk_font = gdk_font_load (FONT_BOARD_MEDIUM);
 
47
 
 
48
  gcompris_timer_end();
 
49
 
 
50
  boardRootItem = GNOME_CANVAS_GROUP(
 
51
                                     gnome_canvas_item_new (gnome_canvas_root(gcompris_get_canvas()),
 
52
                                                            gnome_canvas_group_get_type (),
 
53
                                                            "x", (double) 0,
 
54
                                                            "y", (double) 0,
 
55
                                                            NULL));
 
56
 
 
57
  x = ax;
 
58
  y = ay;
 
59
  timer = second;
 
60
  ratio = second / 10;
 
61
 
 
62
  type = atype;
 
63
  gcomprisTimerEnd = agcomprisTimerEnd;
 
64
 
 
65
  switch(type)
 
66
    {
 
67
    case GCOMPRIS_TIMER_SAND:
 
68
    case GCOMPRIS_TIMER_CLOCK:
 
69
      if(type==GCOMPRIS_TIMER_SAND)
 
70
        pixmap = gcompris_load_pixmap("gcompris/timers/sablier10.png");
 
71
      else
 
72
        pixmap = gcompris_load_pixmap("gcompris/timers/clock10.png");
 
73
 
 
74
      item = gnome_canvas_item_new (boardRootItem,
 
75
                                    gnome_canvas_pixbuf_get_type (),
 
76
                                    "pixbuf", pixmap, 
 
77
                                    "x", x,
 
78
                                    "y", y,
 
79
                                    NULL);
 
80
      gdk_pixbuf_unref(pixmap);
 
81
      break;
 
82
    case GCOMPRIS_TIMER_TEXT:
 
83
      /* Display the value for this timer */
 
84
      item = gnome_canvas_item_new (boardRootItem,
 
85
                                    gnome_canvas_text_get_type (),
 
86
                                    "text", g_strdup_printf("Remaining Time = %d", timer),
 
87
                                    "font_gdk", gdk_font,
 
88
                                    "x", x,
 
89
                                    "y", y,
 
90
                                    "anchor", GTK_ANCHOR_CENTER,
 
91
                                    "fill_color", "white",
 
92
                                    NULL);      
 
93
      break;
 
94
    case GCOMPRIS_TIMER_BALLOON:
 
95
      pixmap = gcompris_load_pixmap("gcompris/misc/tuxballoon.png");
 
96
      item = gnome_canvas_item_new (boardRootItem,
 
97
                                    gnome_canvas_pixbuf_get_type (),
 
98
                                    "pixbuf", pixmap, 
 
99
                                    "x", x,
 
100
                                    "y", y,
 
101
                                    NULL);
 
102
 
 
103
      /* Calc the number of step needed to reach the sea based on user y and second */
 
104
      ystep = (BOARDHEIGHT-y-gdk_pixbuf_get_height(pixmap))/second;
 
105
 
 
106
      gdk_pixbuf_unref(pixmap);
 
107
 
 
108
      pixmap = gcompris_load_pixmap("gcompris/misc/sea.png");
 
109
      gnome_canvas_item_new (boardRootItem,
 
110
                             gnome_canvas_pixbuf_get_type (),
 
111
                             "pixbuf", pixmap, 
 
112
                             "x", (double) 0,
 
113
                             "y", (double) BOARDHEIGHT - gdk_pixbuf_get_height(pixmap),
 
114
                             "width", (double) gdk_pixbuf_get_width(pixmap),
 
115
                             "height", (double) gdk_pixbuf_get_height(pixmap),
 
116
                             NULL);
 
117
      gdk_pixbuf_unref(pixmap);
 
118
 
 
119
      /* Perform under second animation */
 
120
      subratio = 5;
 
121
      subanimate_id = gtk_timeout_add (1000/subratio, (GtkFunction) subtimer_increment, NULL);
 
122
 
 
123
      break;
 
124
    default:
 
125
      break;
 
126
    }
 
127
 
 
128
    animate_id = gtk_timeout_add (1000, (GtkFunction) timer_increment, NULL);
 
129
}
 
130
 
 
131
void gcompris_timer_add(int second)
 
132
{
 
133
  timer +=second;
 
134
}
 
135
 
 
136
void gcompris_timer_end()
 
137
{
 
138
  if(boardRootItem!=NULL)
 
139
    gtk_object_destroy (GTK_OBJECT(boardRootItem));
 
140
  boardRootItem = NULL;
 
141
 
 
142
  if (animate_id)
 
143
    gtk_timeout_remove (animate_id);
 
144
  animate_id = 0;
 
145
 
 
146
  if (subanimate_id)
 
147
    gtk_timeout_remove (subanimate_id);
 
148
  subanimate_id = 0;
 
149
}
 
150
 
 
151
static void display_time_ellapsed()
 
152
{
 
153
  switch(type)
 
154
    {
 
155
    case GCOMPRIS_TIMER_TEXT:
 
156
      /* Display the value for this timer */
 
157
      if(item)
 
158
        gnome_canvas_item_set(item,
 
159
                              "text", _("Time Ellapsed"),
 
160
                              NULL);
 
161
      break;
 
162
    default:
 
163
      break;
 
164
    }
 
165
 
 
166
}
 
167
 
 
168
 
 
169
static gint subtimer_increment(GtkWidget *widget, gpointer data)
 
170
{
 
171
  switch(type)
 
172
    {
 
173
    case GCOMPRIS_TIMER_BALLOON:
 
174
      /* Display the value for this timer */
 
175
      y+=ystep/subratio;
 
176
      if(item)
 
177
        gnome_canvas_item_set(item,
 
178
                              "y", y,
 
179
                              NULL);
 
180
      //      gnome_canvas_update_now(gcompris_get_canvas());
 
181
      break;
 
182
    default:
 
183
      break;
 
184
    }
 
185
  return (TRUE);
 
186
}
 
187
 
 
188
static gint timer_increment(GtkWidget *widget, gpointer data)
 
189
{
 
190
  timer--;
 
191
 
 
192
  if(timer==-1)
 
193
    {
 
194
      display_time_ellapsed();
 
195
      if(gcomprisTimerEnd!=NULL)
 
196
        {
 
197
          /* Call the callback */
 
198
          gcomprisTimerEnd();
 
199
 
 
200
        }
 
201
      return (FALSE);
 
202
    }
 
203
 
 
204
  switch(type)
 
205
    {
 
206
    case GCOMPRIS_TIMER_SAND:
 
207
    case GCOMPRIS_TIMER_CLOCK:
 
208
      if(item)
 
209
        {
 
210
          GdkPixbuf     *pixmap = NULL;
 
211
          gchar         *filefull = NULL;
 
212
          gchar         *filename = NULL;
 
213
          gint          fileid;
 
214
 
 
215
          fileid = (gint)timer/ratio;
 
216
          if(type==GCOMPRIS_TIMER_SAND)
 
217
            filename = g_strdup_printf("gcompris/timers/sablier%d.png", fileid);
 
218
          else
 
219
            filename = g_strdup_printf("gcompris/timers/clock%d.png", fileid);
 
220
 
 
221
          filefull = g_strdup_printf("%s/%s", PACKAGE_DATA_DIR, filename);
 
222
          if (g_file_exists (filefull))
 
223
            {
 
224
              pixmap = gcompris_load_pixmap(filename);
 
225
              gnome_canvas_item_set(item,
 
226
                                    "pixbuf", pixmap,
 
227
                                    NULL);
 
228
              gdk_pixbuf_unref(pixmap);
 
229
            }
 
230
          g_free(filename);
 
231
          g_free(filefull);
 
232
        }
 
233
      break;
 
234
    case GCOMPRIS_TIMER_TEXT:
 
235
      /* Display the value for this timer */
 
236
      if(item)
 
237
        gnome_canvas_item_set(item,
 
238
                              "text", g_strdup_printf(_("Remaining Time = %d"), timer),
 
239
                              NULL);
 
240
      break;
 
241
    case GCOMPRIS_TIMER_BALLOON:
 
242
      break;
 
243
    default:
 
244
      break;
 
245
    }
 
246
 
 
247
  return (TRUE);
 
248
}