~ubuntu-branches/ubuntu/maverick/gcompris/maverick

« back to all changes in this revision

Viewing changes to src/boards/enumerate.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* gcompris - enumerate.c
2
 
 *
3
 
 * Copyright (C) 2001 Bruno Coudoin
4
 
 *
5
 
 *   This program is free software; you can redistribute it and/or modify
6
 
 *   it under the terms of the GNU General Public License as published by
7
 
 *   the Free Software Foundation; either version 3 of the License, or
8
 
 *   (at your option) any later version.
9
 
 *
10
 
 *   This program is distributed in the hope that it will be useful,
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *   GNU General Public License for more details.
14
 
 *
15
 
 *   You should have received a copy of the GNU General Public License
16
 
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
 
19
 
#include <string.h>
20
 
 
21
 
#include "gcompris/gcompris.h"
22
 
 
23
 
#define SOUNDLISTFILE PACKAGE
24
 
 
25
 
static GcomprisBoard *gcomprisBoard = NULL;
26
 
static gboolean board_paused = TRUE;
27
 
 
28
 
static void      start_board (GcomprisBoard *agcomprisBoard);
29
 
static gint      key_press(guint keyval, gchar *commit_str, gchar *preedit_str);
30
 
static void      pause_board (gboolean pause);
31
 
static void      end_board (void);
32
 
static void      process_ok(void);
33
 
static gboolean  is_our_board (GcomprisBoard *gcomprisBoard);
34
 
static void      set_level (guint level);
35
 
static int       gamewon;
36
 
static void      game_won(void);
37
 
 
38
 
static GnomeCanvasGroup *boardRootItem = NULL;
39
 
 
40
 
static GnomeCanvasItem  *enumerate_create_item(GnomeCanvasGroup *parent);
41
 
static void              enumerate_destroy_all_items(void);
42
 
static void              enumerate_next_level(void);
43
 
static gint              item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data);
44
 
static gint              item_event_focus(GnomeCanvasItem *item, GdkEvent *event, guint index);
45
 
 
46
 
#define ANSWER_X        BOARDWIDTH - 150
47
 
#define ANSWER_WIDTH    40
48
 
#define ANSWER_HEIGHT   40
49
 
 
50
 
static int number_of_item_type  = 0;
51
 
static int number_of_item_max   = 0;
52
 
 
53
 
// List of images to use in the game
54
 
static gchar *imageList[] =
55
 
{
56
 
  "gcompris/food/banana.png",
57
 
  "gcompris/food/orange.png",
58
 
  "gcompris/food/milk_shake.png",
59
 
  "gcompris/food/pear.png",
60
 
  "gcompris/food/grapefruit.png",
61
 
  "gcompris/food/yahourt.png",
62
 
  "gcompris/food/milk_cup.png",
63
 
  "gcompris/food/suggar_box.png",
64
 
  "gcompris/food/butter.png",
65
 
  "gcompris/food/chocolate.png",
66
 
  "gcompris/food/cookie.png",
67
 
  "gcompris/food/french_croissant.png",
68
 
  "gcompris/food/chocolate_cake.png",
69
 
  "gcompris/food/marmelade.png",
70
 
  "gcompris/food/baby_bottle.png",
71
 
  "gcompris/food/bread_slice.png",
72
 
  "gcompris/food/round_cookie.png",
73
 
};
74
 
#define NUMBER_OF_IMAGES G_N_ELEMENTS(imageList)
75
 
 
76
 
static guint             answer[NUMBER_OF_IMAGES];
77
 
static guint             answer_to_find[NUMBER_OF_IMAGES];
78
 
static GnomeCanvasItem  *answer_item[NUMBER_OF_IMAGES];
79
 
static GnomeCanvasItem  *answer_item_focus[NUMBER_OF_IMAGES];
80
 
static guint             current_focus  = 0;
81
 
 
82
 
 
83
 
/* Description of this plugin */
84
 
static BoardPlugin menu_bp =
85
 
  {
86
 
    NULL,
87
 
    NULL,
88
 
    N_("Numeration training"),
89
 
    N_("Place the items in the best way to count them"),
90
 
    "Bruno Coudoin <bruno.coudoin@free.fr>",
91
 
    NULL,
92
 
    NULL,
93
 
    NULL,
94
 
    NULL,
95
 
    start_board,
96
 
    pause_board,
97
 
    end_board,
98
 
    is_our_board,
99
 
    key_press,
100
 
    process_ok,
101
 
    set_level,
102
 
    NULL,
103
 
    NULL,
104
 
    NULL,
105
 
    NULL
106
 
  };
107
 
 
108
 
/*
109
 
 * Main entry point mandatory for each Gcompris's game
110
 
 * ---------------------------------------------------
111
 
 *
112
 
 */
113
 
 
114
 
GET_BPLUGIN_INFO(enumerate)
115
 
 
116
 
/*
117
 
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
118
 
 *
119
 
 */
120
 
static void pause_board (gboolean pause)
121
 
{
122
 
  if(gcomprisBoard==NULL)
123
 
    return;
124
 
 
125
 
  if(gamewon == TRUE && pause == FALSE) /* the game is won */
126
 
    {
127
 
      game_won();
128
 
    }
129
 
 
130
 
  board_paused = pause;
131
 
}
132
 
 
133
 
/*
134
 
 */
135
 
static void start_board (GcomprisBoard *agcomprisBoard)
136
 
{
137
 
 
138
 
  if(agcomprisBoard!=NULL)
139
 
    {
140
 
      gcomprisBoard=agcomprisBoard;
141
 
 
142
 
      /* disable im_context */
143
 
      gcomprisBoard->disable_im_context = TRUE;
144
 
 
145
 
      gcomprisBoard->level=1;
146
 
      gcomprisBoard->maxlevel=9;
147
 
      gcomprisBoard->sublevel=1;
148
 
      gcomprisBoard->number_of_sublevel=1; /* Go to next level after this number of 'play' */
149
 
      gc_bar_set(GC_BAR_LEVEL|GC_BAR_OK);
150
 
 
151
 
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
152
 
                              "opt/enumerate_background.png");
153
 
 
154
 
      enumerate_next_level();
155
 
 
156
 
      gamewon = FALSE;
157
 
      pause_board(FALSE);
158
 
    }
159
 
}
160
 
/* ======================================= */
161
 
static void end_board ()
162
 
{
163
 
  if(gcomprisBoard!=NULL)
164
 
    {
165
 
      pause_board(TRUE);
166
 
      enumerate_destroy_all_items();
167
 
    }
168
 
  gcomprisBoard = NULL;
169
 
}
170
 
 
171
 
/* Get the user keys to use to get the answer */
172
 
static gint key_press(guint keyval, gchar *commit_str, gchar *preedit_str)
173
 
{
174
 
  char str[2];
175
 
  GnomeCanvasItem       *item = NULL;
176
 
 
177
 
  if(board_paused || !boardRootItem || !gcomprisBoard)
178
 
    return FALSE;
179
 
 
180
 
  /* Add some filter for control and shift key */
181
 
  switch (keyval)
182
 
    {
183
 
    case GDK_Shift_L:
184
 
    case GDK_Shift_R:
185
 
    case GDK_Control_L:
186
 
    case GDK_Control_R:
187
 
    case GDK_Caps_Lock:
188
 
    case GDK_Shift_Lock:
189
 
    case GDK_Meta_L:
190
 
    case GDK_Meta_R:
191
 
    case GDK_Alt_L:
192
 
    case GDK_Alt_R:
193
 
    case GDK_Super_L:
194
 
    case GDK_Super_R:
195
 
    case GDK_Hyper_L:
196
 
    case GDK_Hyper_R:
197
 
    case GDK_Mode_switch:
198
 
    case GDK_dead_circumflex:
199
 
    case GDK_Num_Lock:
200
 
      return FALSE;
201
 
    case GDK_KP_Enter:
202
 
    case GDK_Return:
203
 
      process_ok();
204
 
      return TRUE;
205
 
    }
206
 
 
207
 
  sprintf(str, "%c", keyval);
208
 
 
209
 
  item = answer_item[current_focus];
210
 
 
211
 
  if(GNOME_IS_CANVAS_TEXT(item))
212
 
    {
213
 
      gchar *oldtext;
214
 
      gchar *newtext;
215
 
 
216
 
      gtk_object_get (GTK_OBJECT (item), "text", &oldtext, NULL);
217
 
 
218
 
      switch(keyval)
219
 
        {
220
 
        case GDK_BackSpace:
221
 
        case GDK_Delete:
222
 
 
223
 
          if(oldtext[1] != '\0')
224
 
            newtext = g_strndup(oldtext, strlen(oldtext)-1);
225
 
          else
226
 
            newtext = "?";
227
 
 
228
 
          break;
229
 
 
230
 
        default:
231
 
 
232
 
          if(keyval<'0' || keyval>'9')
233
 
            str[0]='0';
234
 
 
235
 
          if(oldtext[0] == '?' && strlen(oldtext)==1)
236
 
            {
237
 
              oldtext[0] = ' ';
238
 
              g_strstrip(oldtext);
239
 
            }
240
 
 
241
 
          if(strlen(oldtext)<2)
242
 
            newtext = g_strconcat(oldtext, &str, NULL);
243
 
          else
244
 
            newtext = g_strdup(oldtext);
245
 
          break;
246
 
 
247
 
        }
248
 
 
249
 
      if(newtext[0] != '?')
250
 
        answer[current_focus] = atoi(newtext);
251
 
 
252
 
      gnome_canvas_item_set (item,
253
 
                             "text", newtext,
254
 
                             NULL);
255
 
 
256
 
      g_free(oldtext);
257
 
    }
258
 
 
259
 
  return TRUE;
260
 
}
261
 
 
262
 
/* ======================================= */
263
 
static void set_level (guint level)
264
 
{
265
 
 
266
 
  if(gcomprisBoard!=NULL)
267
 
    {
268
 
      gcomprisBoard->level=level;
269
 
      gcomprisBoard->sublevel=1;
270
 
      enumerate_next_level();
271
 
    }
272
 
}
273
 
/* ======================================= */
274
 
static gboolean is_our_board (GcomprisBoard *gcomprisBoard)
275
 
{
276
 
  if (gcomprisBoard)
277
 
    {
278
 
      if(g_strcasecmp(gcomprisBoard->type, "enumerate")==0)
279
 
        {
280
 
          /* Set the plugin entry */
281
 
          gcomprisBoard->plugin=&menu_bp;
282
 
 
283
 
          return TRUE;
284
 
        }
285
 
    }
286
 
  return FALSE;
287
 
}
288
 
 
289
 
/*-------------------------------------------------------------------------------*/
290
 
/*-------------------------------------------------------------------------------*/
291
 
/* set initial values for the next level */
292
 
static void enumerate_next_level()
293
 
{
294
 
 
295
 
  gc_bar_set_level(gcomprisBoard);
296
 
 
297
 
  enumerate_destroy_all_items();
298
 
  gamewon = FALSE;
299
 
 
300
 
  /* Select level difficulty */
301
 
  switch(gcomprisBoard->level)
302
 
    {
303
 
    case 1:
304
 
      number_of_item_type = 1;
305
 
      number_of_item_max  = 5;
306
 
      break;
307
 
    case 2:
308
 
      number_of_item_type = 2;
309
 
      number_of_item_max  = 5;
310
 
      break;
311
 
    case 3:
312
 
      number_of_item_type = 3;
313
 
      number_of_item_max  = 5;
314
 
      break;
315
 
    case 4:
316
 
      number_of_item_type = 3;
317
 
      number_of_item_max  = 5;
318
 
      break;
319
 
    case 5:
320
 
      number_of_item_type = 4;
321
 
      number_of_item_max  = 5;
322
 
      break;
323
 
    case 6:
324
 
      number_of_item_type = 4;
325
 
      number_of_item_max  = 6;
326
 
      break;
327
 
    case 7:
328
 
      number_of_item_type = 4;
329
 
      number_of_item_max  = 7;
330
 
      break;
331
 
    case 8:
332
 
      number_of_item_type = 4;
333
 
      number_of_item_max  = 10;
334
 
      break;
335
 
    default:
336
 
      number_of_item_type = 5;
337
 
      number_of_item_max = 10;
338
 
    }
339
 
 
340
 
  current_focus = 0;
341
 
 
342
 
  /* Try the next level */
343
 
  enumerate_create_item(gnome_canvas_root(gcomprisBoard->canvas));
344
 
 
345
 
}
346
 
/* ==================================== */
347
 
/* Destroy all the items */
348
 
static void enumerate_destroy_all_items()
349
 
{
350
 
  if(boardRootItem!=NULL)
351
 
    gtk_object_destroy (GTK_OBJECT(boardRootItem));
352
 
 
353
 
  boardRootItem = NULL;
354
 
}
355
 
/* ==================================== */
356
 
static GnomeCanvasItem *enumerate_create_item(GnomeCanvasGroup *parent)
357
 
{
358
 
  int i,j;
359
 
  int current_y;
360
 
  GnomeCanvasItem *item = NULL;
361
 
  GdkPixbuf *pixmap = NULL;
362
 
  GdkPixbuf *pixmap_answer = NULL;
363
 
 
364
 
  boardRootItem = GNOME_CANVAS_GROUP(
365
 
                                     gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
366
 
                                                            gnome_canvas_group_get_type (),
367
 
                                                            "x", (double) 0,
368
 
                                                            "y", (double) 0,
369
 
 
370
 
                                                            NULL));
371
 
 
372
 
  current_y = BOARDHEIGHT;
373
 
 
374
 
  for(i=0; i<number_of_item_type; i++)
375
 
    {
376
 
 
377
 
      pixmap = gc_pixmap_load(imageList[i]);
378
 
 
379
 
      answer_to_find[i] = g_random_int_range(1, number_of_item_max);
380
 
      answer[i] = 0;
381
 
 
382
 
      for(j=0; j<answer_to_find[i]; j++)
383
 
        {
384
 
          guint x, y;
385
 
 
386
 
          x = g_random_int_range(0, ANSWER_X-gdk_pixbuf_get_width(pixmap)-ANSWER_WIDTH);
387
 
          y = g_random_int_range(0, BOARDHEIGHT-gdk_pixbuf_get_height(pixmap));
388
 
 
389
 
          item = gnome_canvas_item_new (boardRootItem,
390
 
                                        gnome_canvas_pixbuf_get_type (),
391
 
                                        "pixbuf", pixmap,
392
 
                                        "x", (double) x,
393
 
                                        "y", (double) y,
394
 
                                        NULL);
395
 
 
396
 
          gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event, NULL);
397
 
        }
398
 
 
399
 
      /* Display the answer area */
400
 
      current_y -= ANSWER_HEIGHT*2;
401
 
 
402
 
      pixmap_answer = gc_pixmap_load("images/enumerate_answer_focus.png");
403
 
 
404
 
      item = \
405
 
        gnome_canvas_item_new (boardRootItem,
406
 
                               gnome_canvas_pixbuf_get_type (),
407
 
                               "pixbuf", pixmap_answer,
408
 
                               "x", (double) ANSWER_X - ANSWER_WIDTH/2,
409
 
                               "y", (double) current_y - ANSWER_HEIGHT/2,
410
 
                               NULL);
411
 
 
412
 
      gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event_focus, GINT_TO_POINTER(i));
413
 
 
414
 
      gdk_pixbuf_unref(pixmap_answer);
415
 
 
416
 
      pixmap_answer = gc_pixmap_load("images/enumerate_answer.png");
417
 
 
418
 
      answer_item_focus[i] = \
419
 
        gnome_canvas_item_new (boardRootItem,
420
 
                               gnome_canvas_pixbuf_get_type (),
421
 
                               "pixbuf", pixmap_answer,
422
 
                               "x", (double) ANSWER_X - ANSWER_WIDTH/2,
423
 
                               "y", (double) current_y - ANSWER_HEIGHT/2,
424
 
                               NULL);
425
 
 
426
 
      gdk_pixbuf_unref(pixmap_answer);
427
 
      gnome_canvas_item_hide(answer_item_focus[i]);
428
 
 
429
 
      item = gnome_canvas_item_new (boardRootItem,
430
 
                                    gnome_canvas_pixbuf_get_type (),
431
 
                                    "pixbuf", pixmap,
432
 
                                    "x", (double) ANSWER_X,
433
 
                                    "y", (double) current_y,
434
 
                                    "width", (double) gdk_pixbuf_get_width(pixmap)*ANSWER_HEIGHT/gdk_pixbuf_get_height(pixmap),
435
 
                                    "height", (double) ANSWER_HEIGHT,
436
 
                                    "width_set", TRUE,
437
 
                                    "height_set", TRUE,
438
 
                                    NULL);
439
 
 
440
 
      gdk_pixbuf_unref(pixmap);
441
 
 
442
 
      gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event_focus,  GINT_TO_POINTER(i));
443
 
 
444
 
      gtk_signal_connect(GTK_OBJECT(item), "event",
445
 
                         (GtkSignalFunc) gc_item_focus_event,
446
 
                         NULL);
447
 
 
448
 
      answer_item[i] = \
449
 
        gnome_canvas_item_new (boardRootItem,
450
 
                               gnome_canvas_text_get_type (),
451
 
                               "text", "?",
452
 
                               "font", gc_skin_font_board_big,
453
 
                               "x", (double) ANSWER_X + 2.5*ANSWER_WIDTH,
454
 
                               "y", (double) current_y + ANSWER_HEIGHT/2,
455
 
                               "anchor", GTK_ANCHOR_EAST,
456
 
                               "fill_color", "blue",
457
 
                               NULL);
458
 
      gtk_signal_connect(GTK_OBJECT(answer_item[i]), "event", (GtkSignalFunc) item_event_focus,
459
 
                         GINT_TO_POINTER(i));
460
 
 
461
 
    }
462
 
 
463
 
  gnome_canvas_item_show(answer_item_focus[current_focus]);
464
 
 
465
 
  return NULL;
466
 
}
467
 
/* ==================================== */
468
 
static void game_won()
469
 
{
470
 
  gcomprisBoard->sublevel++;
471
 
 
472
 
  if(gcomprisBoard->sublevel>gcomprisBoard->number_of_sublevel) {
473
 
    /* Try the next level */
474
 
    gcomprisBoard->sublevel=1;
475
 
    gcomprisBoard->level++;
476
 
    if(gcomprisBoard->level>gcomprisBoard->maxlevel) { // the current board is finished : bail out
477
 
      gc_bonus_end_display(GC_BOARD_FINISHED_RANDOM);
478
 
      return;
479
 
    }
480
 
    gc_sound_play_ogg ("sounds/bonus.wav", NULL);
481
 
  }
482
 
  enumerate_next_level();
483
 
}
484
 
 
485
 
/* ==================================== */
486
 
static gint
487
 
item_event_focus(GnomeCanvasItem *item, GdkEvent *event, guint index)
488
 
{
489
 
 
490
 
   switch (event->type)
491
 
     {
492
 
     case GDK_BUTTON_PRESS:
493
 
       gc_sound_play_ogg ("sounds/prompt.wav", NULL);
494
 
       gnome_canvas_item_hide(answer_item_focus[current_focus]);
495
 
       current_focus = index;
496
 
       gnome_canvas_item_show(answer_item_focus[current_focus]);
497
 
       return TRUE;
498
 
       break;
499
 
     default:
500
 
       break;
501
 
     }
502
 
 
503
 
   return FALSE;
504
 
}
505
 
 
506
 
/* ==================================== */
507
 
static gint
508
 
item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
509
 
{
510
 
  static double x, y;
511
 
  double item_x, item_y;
512
 
  GdkCursor *fleur;
513
 
  static int dragging;
514
 
  double new_x, new_y;
515
 
 
516
 
  item_x = event->button.x;
517
 
  item_y = event->button.y;
518
 
  gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
519
 
 
520
 
  if(board_paused)
521
 
    return FALSE;
522
 
 
523
 
 
524
 
   switch (event->type)
525
 
     {
526
 
     case GDK_BUTTON_PRESS:
527
 
       switch(event->button.button)
528
 
         {
529
 
         case 1:
530
 
           gc_sound_play_ogg ("sounds/bleep.wav", NULL);
531
 
           x = item_x;
532
 
           y = item_y;
533
 
 
534
 
           gnome_canvas_item_raise_to_top(item);
535
 
 
536
 
           fleur = gdk_cursor_new(GDK_FLEUR);
537
 
           gc_canvas_item_grab(item,
538
 
                                  GDK_POINTER_MOTION_MASK |
539
 
                                  GDK_BUTTON_RELEASE_MASK,
540
 
                                  fleur,
541
 
                                  event->button.time);
542
 
           gdk_cursor_destroy(fleur);
543
 
           dragging = TRUE;
544
 
           break;
545
 
 
546
 
         case 3:
547
 
         case 4:
548
 
           /* fish up */
549
 
           gnome_canvas_item_move(item, 0.0, -3.0);
550
 
           break;
551
 
 
552
 
         case 2:
553
 
         case 5:
554
 
           /* fish down */
555
 
           gnome_canvas_item_move(item, 0.0, 3.0);
556
 
           break;
557
 
 
558
 
         default:
559
 
           break;
560
 
         }
561
 
       break;
562
 
 
563
 
     case GDK_MOTION_NOTIFY:
564
 
       if (dragging && (event->motion.state & GDK_BUTTON1_MASK))
565
 
         {
566
 
           double x1, x2, y1, y2;
567
 
 
568
 
           gnome_canvas_item_get_bounds(item,  &x1, &y1, &x2, &y2);
569
 
 
570
 
           new_x = item_x;
571
 
           new_y = item_y;
572
 
 
573
 
           /* Check board boundaries */
574
 
           if((x1 < 0 && new_x<x)|| (x2 > BOARDWIDTH && new_x>x))
575
 
             {
576
 
               new_x = x;
577
 
             }
578
 
           if((y1 < 0 && new_y<y) || (y2 > BOARDHEIGHT && new_y>y))
579
 
             {
580
 
               new_y = y;
581
 
             }
582
 
 
583
 
           gnome_canvas_item_move(item, new_x - x, new_y - y);
584
 
           x = new_x;
585
 
           y = new_y;
586
 
         }
587
 
       break;
588
 
 
589
 
     case GDK_BUTTON_RELEASE:
590
 
       if(dragging)
591
 
         {
592
 
           gc_canvas_item_ungrab(item, event->button.time);
593
 
           dragging = FALSE;
594
 
         }
595
 
       break;
596
 
 
597
 
     default:
598
 
       break;
599
 
     }
600
 
 
601
 
  return FALSE;
602
 
}
603
 
 
604
 
static void process_ok()
605
 
{
606
 
  guint i;
607
 
  gboolean win = TRUE;
608
 
 
609
 
  for(i=0; i<number_of_item_type; i++)
610
 
    {
611
 
      if(answer[i] != answer_to_find[i])
612
 
        win = FALSE;
613
 
    }
614
 
 
615
 
  if(win) {
616
 
    gamewon = TRUE;
617
 
    gc_bonus_display(gamewon, GC_BONUS_SMILEY);
618
 
  } else {
619
 
    gamewon = FALSE;
620
 
    gc_bonus_display(gamewon, GC_BONUS_SMILEY);
621
 
  }
622
 
}
623