~ml-launchpad/ubuntu/natty/gcompris/fix-for-777349

« back to all changes in this revision

Viewing changes to src/boards/read_colors.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 - read_colors.c
2
 
 *
3
 
 * Copyright (C) 2002 Pascal Georges
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
 
/* libxml includes */
22
 
#include <libxml/tree.h>
23
 
#include <libxml/parser.h>
24
 
 
25
 
#include "gcompris/gcompris.h"
26
 
 
27
 
#define SOUNDLISTFILE PACKAGE
28
 
 
29
 
static GcomprisBoard *gcomprisBoard = NULL;
30
 
static gboolean board_paused = TRUE;
31
 
 
32
 
static void              start_board (GcomprisBoard *agcomprisBoard);
33
 
static void              pause_board (gboolean pause);
34
 
static void              end_board (void);
35
 
static gboolean          is_our_board (GcomprisBoard *gcomprisBoard);
36
 
static int gamewon;
37
 
 
38
 
static void              process_ok(void);
39
 
static void              highlight_selected(int);
40
 
static void              game_won(void);
41
 
 
42
 
static void              init_xml();
43
 
static gboolean          read_xml_file(char *fname);
44
 
 
45
 
/* ================================================================ */
46
 
static GnomeCanvasGroup *boardRootItem = NULL;
47
 
static GnomeCanvasItem *highlight_image_item = NULL;
48
 
static GnomeCanvasItem *color_item = NULL;
49
 
static GnomeCanvasItem *clock_image_item = NULL;
50
 
static GdkPixbuf *clock_pixmap = NULL;
51
 
 
52
 
static GnomeCanvasItem *read_colors_create_item(GnomeCanvasGroup *parent);
53
 
static void read_colors_destroy_all_items(void);
54
 
static void read_colors_next_level(void);
55
 
static gint item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data);
56
 
static void update_clock();
57
 
 
58
 
static int highlight_width, highlight_height, errors;
59
 
static GList * listColors = NULL;
60
 
 
61
 
#define LAST_COLOR 11
62
 
#define MAX_ERRORS 10
63
 
#define CLOCK_X 40
64
 
#define CLOCK_Y 420
65
 
 
66
 
static char* colors[LAST_COLOR];
67
 
 
68
 
static int X[] = {100,300,500,700,200,400,600,100,300,500,700};
69
 
static int Y[] = {90,90,90,90,180,180,180,270,270,270,270};
70
 
 
71
 
/* (x1,y1) and (x2, y2) are the coordinates of the rectangle where to
72
 
   draw the color's name */
73
 
static int color_x1 = 240, color_x2 = 570;
74
 
static int color_y1 = 395, color_y2 = 490;
75
 
 
76
 
#define RADIUS 60
77
 
 
78
 
/* Description of this plugin */
79
 
static BoardPlugin menu_bp =
80
 
  {
81
 
    NULL,
82
 
    NULL,
83
 
    "Read Colors",
84
 
    "Click on the corresponding color",
85
 
    "Pascal Georges pascal.georges1@free.fr>",
86
 
    NULL,
87
 
    NULL,
88
 
    NULL,
89
 
    NULL,
90
 
    start_board,
91
 
    pause_board,
92
 
    end_board,
93
 
    is_our_board,
94
 
    NULL,
95
 
    process_ok,
96
 
    NULL,//set_level,
97
 
    NULL,
98
 
    NULL,
99
 
    NULL,
100
 
    NULL
101
 
  };
102
 
 
103
 
/* =====================================================================
104
 
 *
105
 
 * =====================================================================*/
106
 
GET_BPLUGIN_INFO(read_colors)
107
 
 
108
 
/* =====================================================================
109
 
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
110
 
 * =====================================================================*/
111
 
static void pause_board (gboolean pause)
112
 
{
113
 
  if(gcomprisBoard==NULL)
114
 
    return;
115
 
 
116
 
  gc_bar_hide(FALSE);
117
 
  if(gamewon == TRUE && pause == FALSE) /* the game is won */
118
 
    game_won();
119
 
 
120
 
  board_paused = pause;
121
 
}
122
 
 
123
 
/* =====================================================================
124
 
 *
125
 
 * =====================================================================*/
126
 
static void start_board (GcomprisBoard *agcomprisBoard) {
127
 
  GList * list = NULL;
128
 
  int * item;
129
 
  int i,list_length;
130
 
 
131
 
  if(agcomprisBoard!=NULL) {
132
 
    gcomprisBoard=agcomprisBoard;
133
 
    gc_set_background(gnome_canvas_root(gcomprisBoard->canvas), "read_colors/read_colors_background.png");
134
 
    gcomprisBoard->level=1;
135
 
    gcomprisBoard->maxlevel=1;
136
 
    gc_bar_set(0);
137
 
 
138
 
    gamewon = FALSE;
139
 
    errors = MAX_ERRORS;
140
 
 
141
 
    init_xml();
142
 
 
143
 
    // we generate a list of color indexes in a random order
144
 
    while (g_list_length(listColors) > 0)
145
 
      listColors = g_list_remove(listColors, g_list_nth_data(listColors,0));
146
 
 
147
 
    for (i=0; i<LAST_COLOR; i++)
148
 
      list = g_list_append(list, GINT_TO_POINTER(i));
149
 
 
150
 
    while ((list_length = g_list_length(list))) {
151
 
      i = list_length == 1 ? 0 : g_random_int_range(0,g_list_length(list)-1);
152
 
      item = g_list_nth_data(list, i);
153
 
      listColors = g_list_append(listColors, item);
154
 
      list = g_list_remove(list, item);
155
 
    }
156
 
    g_list_free(list);
157
 
 
158
 
    gtk_signal_connect(GTK_OBJECT(gcomprisBoard->canvas), "event",  (GtkSignalFunc) item_event, NULL);
159
 
    read_colors_next_level();
160
 
    pause_board(FALSE);
161
 
  }
162
 
}
163
 
 
164
 
/* =====================================================================
165
 
 *
166
 
 * =====================================================================*/
167
 
static void end_board () {
168
 
 
169
 
  if(gcomprisBoard!=NULL){
170
 
    pause_board(TRUE);
171
 
    gc_score_end();
172
 
    read_colors_destroy_all_items();
173
 
    // free list
174
 
    while (g_list_length(listColors) > 0)
175
 
      listColors = g_list_remove(listColors, g_list_nth_data(listColors,0));
176
 
 
177
 
    g_list_free(listColors);
178
 
    listColors = NULL;
179
 
  }
180
 
  gcomprisBoard = NULL;
181
 
}
182
 
 
183
 
/* =====================================================================
184
 
 *
185
 
 * =====================================================================*/
186
 
static gboolean is_our_board (GcomprisBoard *gcomprisBoard) {
187
 
  if (gcomprisBoard) {
188
 
    if(g_strcasecmp(gcomprisBoard->type, "read_colors")==0) {
189
 
      /* Set the plugin entry */
190
 
      gcomprisBoard->plugin=&menu_bp;
191
 
      return TRUE;
192
 
    }
193
 
  }
194
 
  return FALSE;
195
 
}
196
 
/* =====================================================================
197
 
 * set initial values for the next level
198
 
 * =====================================================================*/
199
 
static void read_colors_next_level() {
200
 
 
201
 
  read_colors_destroy_all_items();
202
 
  gamewon = FALSE;
203
 
 
204
 
  /* Try the next level */
205
 
  read_colors_create_item(gnome_canvas_root(gcomprisBoard->canvas));
206
 
 
207
 
  /* show text of color to find */
208
 
  color_item = gnome_canvas_item_new (boardRootItem,
209
 
                                      gnome_canvas_text_get_type (),
210
 
                                      "text", colors[GPOINTER_TO_INT(g_list_nth_data(listColors,0))],
211
 
                                      "font", gc_skin_font_board_title_bold,
212
 
                                      "x", (double) (color_x1+color_x2)/2,
213
 
                                      "y", (double) (color_y1+color_y2)/2,
214
 
                                      "anchor", GTK_ANCHOR_CENTER,
215
 
                                      "fill_color", "darkblue",
216
 
                                      NULL);
217
 
 
218
 
}
219
 
/* =====================================================================
220
 
 * Destroy all the items
221
 
 * =====================================================================*/
222
 
static void read_colors_destroy_all_items() {
223
 
  if(boardRootItem!=NULL)
224
 
    gtk_object_destroy (GTK_OBJECT(boardRootItem));
225
 
 
226
 
  boardRootItem = NULL;
227
 
}
228
 
 
229
 
/* =====================================================================
230
 
 *
231
 
 * =====================================================================*/
232
 
static GnomeCanvasItem *read_colors_create_item(GnomeCanvasGroup *parent) {
233
 
  GdkPixbuf *highlight_pixmap = NULL;
234
 
  char *str = NULL;
235
 
 
236
 
  boardRootItem = GNOME_CANVAS_GROUP(
237
 
                                     gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
238
 
                                                            gnome_canvas_group_get_type (),
239
 
                                                            "x", (double) 0,
240
 
                                                            "y", (double) 0,
241
 
                                                            NULL));
242
 
 
243
 
  str = g_strdup_printf("%s/%s", gcomprisBoard->boarddir, "read_colors_highlight.png");
244
 
  highlight_pixmap = gc_pixmap_load(str);
245
 
 
246
 
  highlight_image_item = gnome_canvas_item_new (boardRootItem,
247
 
                                                gnome_canvas_pixbuf_get_type (),
248
 
                                                "pixbuf", highlight_pixmap,
249
 
                                                "x", (double) 0,
250
 
                                                "y", (double) 0,
251
 
                                                "width", (double) gdk_pixbuf_get_width(highlight_pixmap),
252
 
                                                "height", (double) gdk_pixbuf_get_height(highlight_pixmap),
253
 
                                                "width_set", TRUE,
254
 
                                                "height_set", TRUE,
255
 
                                                NULL);
256
 
 
257
 
  highlight_width = gdk_pixbuf_get_width(highlight_pixmap);
258
 
  highlight_height = gdk_pixbuf_get_height(highlight_pixmap);
259
 
 
260
 
  g_free(str);
261
 
  gnome_canvas_item_hide(highlight_image_item);
262
 
 
263
 
  gdk_pixbuf_unref(highlight_pixmap);
264
 
 
265
 
  /* setup the clock */
266
 
  str = g_strdup_printf("%s%d.png", "gcompris/timers/clock",errors);
267
 
  clock_pixmap = gc_pixmap_load(str);
268
 
 
269
 
  clock_image_item = gnome_canvas_item_new (boardRootItem,
270
 
                                            gnome_canvas_pixbuf_get_type (),
271
 
                                            "pixbuf", clock_pixmap,
272
 
                                            "x", (double) CLOCK_X,
273
 
                                            "y", (double) CLOCK_Y,
274
 
                                            "width", (double) gdk_pixbuf_get_width(clock_pixmap),
275
 
                                            "height", (double) gdk_pixbuf_get_height(clock_pixmap),
276
 
                                            "width_set", TRUE,
277
 
                                            "height_set", TRUE,
278
 
                                            NULL);
279
 
 
280
 
  g_free(str);
281
 
 
282
 
  return NULL;
283
 
}
284
 
/* =====================================================================
285
 
 *
286
 
 * =====================================================================*/
287
 
static void update_clock() {
288
 
  char *str = g_strdup_printf("%s%d.png", "gcompris/timers/clock",errors);
289
 
 
290
 
  gtk_object_destroy (GTK_OBJECT(clock_image_item));
291
 
 
292
 
  clock_pixmap = gc_pixmap_load(str);
293
 
 
294
 
  clock_image_item = gnome_canvas_item_new (boardRootItem,
295
 
                                            gnome_canvas_pixbuf_get_type (),
296
 
                                            "pixbuf", clock_pixmap,
297
 
                                            "x", (double) CLOCK_X,
298
 
                                            "y", (double) CLOCK_Y,
299
 
                                            "width", (double) gdk_pixbuf_get_width(clock_pixmap),
300
 
                                            "height", (double) gdk_pixbuf_get_height(clock_pixmap),
301
 
                                            "width_set", TRUE,
302
 
                                            "height_set", TRUE,
303
 
                                            NULL);
304
 
 
305
 
  gdk_pixbuf_unref(clock_pixmap);
306
 
  g_free(str);
307
 
}
308
 
/* =====================================================================
309
 
 *
310
 
 * =====================================================================*/
311
 
static void game_won() {
312
 
  gcomprisBoard->sublevel++;
313
 
 
314
 
  listColors = g_list_remove(listColors, g_list_nth_data(listColors,0));
315
 
 
316
 
  if( g_list_length(listColors) <= 0 ) { // the current board is finished : bail out
317
 
    gc_bonus_end_display(GC_BOARD_FINISHED_TUXLOCO);
318
 
    return;
319
 
  }
320
 
 
321
 
  read_colors_next_level();
322
 
}
323
 
/* =====================================================================
324
 
 *
325
 
 * =====================================================================*/
326
 
static gboolean process_ok_timeout() {
327
 
  gc_bonus_display(gamewon, GC_BONUS_SMILEY);
328
 
  if (!gamewon)
329
 
    errors--;
330
 
  if (errors <1)
331
 
    errors = 1;
332
 
  update_clock();
333
 
 
334
 
  if (errors <= 1) {
335
 
    gc_bonus_end_display(GC_BOARD_FINISHED_TOOMANYERRORS);
336
 
  }
337
 
 
338
 
        return FALSE;
339
 
}
340
 
 
341
 
static void process_ok() {
342
 
  gc_bar_hide(TRUE);
343
 
  // leave time to display the right answer
344
 
  g_timeout_add(TIME_CLICK_TO_BONUS, process_ok_timeout, NULL);
345
 
}
346
 
 
347
 
/* =====================================================================
348
 
 *
349
 
 * =====================================================================*/
350
 
static gint item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data) {
351
 
  double x, y;
352
 
  int i, clicked;
353
 
 
354
 
  x = event->button.x;
355
 
  y = event->button.y;
356
 
 
357
 
  if (!gcomprisBoard || board_paused)
358
 
    return FALSE;
359
 
 
360
 
  switch (event->type)
361
 
    {
362
 
    case GDK_BUTTON_PRESS:
363
 
      gnome_canvas_c2w (gcomprisBoard->canvas, x, y, &x, &y);
364
 
      clicked = -1;
365
 
      for (i=0; i<LAST_COLOR; i++) {
366
 
        if (hypot((double) (X[i]-x),(double)(Y[i]-y)) < RADIUS) {
367
 
          clicked = i;
368
 
          break;
369
 
        }
370
 
      }
371
 
 
372
 
      if (clicked >= 0) {
373
 
        gc_sound_play_ogg ("sounds/bleep.wav", NULL);
374
 
        board_paused = TRUE;
375
 
        highlight_selected(clicked);
376
 
        gamewon = (clicked == GPOINTER_TO_INT(g_list_nth_data(listColors,0)));
377
 
        process_ok();
378
 
      }
379
 
      break;
380
 
 
381
 
    default:
382
 
      break;
383
 
    }
384
 
  return FALSE;
385
 
}
386
 
 
387
 
/* =====================================================================
388
 
 *
389
 
 * =====================================================================*/
390
 
static void highlight_selected(int c) {
391
 
  int x, y;
392
 
 
393
 
  g_assert(c>=0 && c<LAST_COLOR);
394
 
 
395
 
  x = X[c];
396
 
  y = Y[c];
397
 
 
398
 
  x -= highlight_width/2;
399
 
  y -= highlight_height/2;
400
 
  gnome_canvas_item_show(highlight_image_item);
401
 
  gc_item_absolute_move(highlight_image_item, x, y);
402
 
}
403
 
/* ===================================
404
 
 *                XML stuff
405
 
 * ==================================== */
406
 
static void init_xml()
407
 
{
408
 
  char *filename;
409
 
 
410
 
  filename = gc_file_find_absolute("%s/board1.xml",
411
 
                                   gcomprisBoard->boarddir);
412
 
 
413
 
  g_assert(read_xml_file(filename)== TRUE);
414
 
 
415
 
  g_free(filename);
416
 
}
417
 
 
418
 
/* ==================================== */
419
 
static void add_xml_data(xmlDocPtr doc, xmlNodePtr xmlnode, GNode * child)
420
 
{
421
 
  char *text = NULL;
422
 
  char *sColor = NULL;
423
 
  int i;
424
 
 
425
 
  xmlnode = xmlnode->xmlChildrenNode;
426
 
 
427
 
  xmlnode = xmlnode->next;
428
 
 
429
 
  while (xmlnode != NULL) {
430
 
 
431
 
    // try to match color[i]
432
 
    for (i=0; i<LAST_COLOR; i++) {
433
 
      sColor = g_strdup_printf("color%d", i+1);
434
 
      if (!strcmp((char *)xmlnode->name, sColor)) {
435
 
        text = \
436
 
          gettext((char *)xmlNodeListGetString(doc, xmlnode->xmlChildrenNode, 1));
437
 
        colors[i] = text;
438
 
        g_free(sColor);
439
 
        break;
440
 
      }
441
 
      g_free(sColor);
442
 
    } // end for
443
 
    xmlnode = xmlnode->next;
444
 
  }
445
 
 
446
 
  // I really don't know why this test, but otherwise, the list is doubled
447
 
  // with 1 line on 2 filled with NULL elements
448
 
  if ( text == NULL)
449
 
    return;
450
 
 
451
 
}
452
 
 
453
 
/* ==================================== */
454
 
static void parse_doc(xmlDocPtr doc)
455
 
{
456
 
  xmlNodePtr node;
457
 
 
458
 
  for(node = doc->children->children; node != NULL; node = node->next) {
459
 
    if ( g_strcasecmp((gchar *)node->name, "Board") == 0 )
460
 
      add_xml_data(doc, node,NULL);
461
 
  }
462
 
 
463
 
}
464
 
/* ==================================== */
465
 
/* read an xml file into our memory structures and update our view,
466
 
   dump any old data we have in memory if we can load a new set */
467
 
static gboolean read_xml_file(char *fname)
468
 
{
469
 
  /* pointer to the new doc */
470
 
  xmlDocPtr doc;
471
 
 
472
 
  g_return_val_if_fail(fname!=NULL,FALSE);
473
 
 
474
 
  /* parse the new file and put the result into newdoc */
475
 
  doc = xmlParseFile(fname);
476
 
 
477
 
  /* in case something went wrong */
478
 
  if(!doc)
479
 
    return FALSE;
480
 
 
481
 
  if(/* if there is no root element */
482
 
     !doc->children ||
483
 
     /* if it doesn't have a name */
484
 
     !doc->children->name ||
485
 
     /* if it isn't a ImageId node */
486
 
     g_strcasecmp((char *)doc->children->name,"ReadColors")!=0) {
487
 
    xmlFreeDoc(doc);
488
 
    return FALSE;
489
 
  }
490
 
 
491
 
  parse_doc(doc);
492
 
  xmlFreeDoc(doc);
493
 
  return TRUE;
494
 
}