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

« back to all changes in this revision

Viewing changes to src/boards/leftright.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 - leftright.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 "gcompris/gcompris.h"
20
 
 
21
 
#define SOUNDLISTFILE PACKAGE
22
 
 
23
 
static GcomprisBoard *gcomprisBoard = NULL;
24
 
static gboolean board_paused = TRUE;
25
 
 
26
 
static void start_board (GcomprisBoard *agcomprisBoard);
27
 
static void pause_board (gboolean pause);
28
 
static void end_board (void);
29
 
static gboolean is_our_board (GcomprisBoard *gcomprisBoard);
30
 
static void set_level (guint level);
31
 
static int gamewon;
32
 
static int last_hand = -1;
33
 
 
34
 
static void process_ok(void);
35
 
static void highlight_selected(int);
36
 
static void game_won();
37
 
 
38
 
#define LEFT 0
39
 
#define RIGHT 1
40
 
 
41
 
// the values are taken from the backgound image colors-bg.jpg
42
 
#define BUTTON_AREA_X1 83
43
 
#define BUTTON_AREA_X2 487
44
 
#define BUTTON_AREA_Y1 380
45
 
 
46
 
#define CENTER_LEFT_X 200
47
 
#define CENTER_LEFT_Y 430
48
 
#define CENTER_RIGHT_X 600
49
 
#define CENTER_RIGHT_Y CENTER_LEFT_Y
50
 
 
51
 
#define HAND_X 400
52
 
#define HAND_Y 200
53
 
 
54
 
// Defines the clickable areas
55
 
#define CLICKABLE_X1 90
56
 
#define CLICKABLE_X2 300
57
 
#define CLICKABLE_X3 490
58
 
#define CLICKABLE_X4 700
59
 
#define CLICKABLE_Y1 390
60
 
#define CLICKABLE_Y2 480
61
 
 
62
 
#define NUMBER_OF_SUBLEVELS 6
63
 
#define NUMBER_OF_LEVELS 4
64
 
 
65
 
#define TEXT_COLOR "yellow"
66
 
 
67
 
/* ================================================================ */
68
 
static GnomeCanvasGroup *boardRootItem = NULL;
69
 
 
70
 
static GnomeCanvasItem *hand_image_item = NULL;
71
 
static GnomeCanvasItem *left_highlight_image_item = NULL, *right_highlight_image_item = NULL;
72
 
 
73
 
static GnomeCanvasItem *leftright_create_item(GnomeCanvasGroup *parent);
74
 
static void leftright_destroy_all_items(void);
75
 
static void leftright_next_level(void);
76
 
static gint item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data);
77
 
static int answer;
78
 
 
79
 
static char *hands[32] = {"main_droite_dessus_0.png","main_droite_paume_0.png",
80
 
                          "main_gauche_dessus_0.png","main_gauche_paume_0.png",
81
 
                          "main_droite_dessus_90.png","main_droite_paume_90.png",
82
 
                          "main_gauche_dessus_90.png","main_gauche_paume_90.png",
83
 
                          "main_droite_dessus_180.png","main_droite_paume_180.png",
84
 
                          "main_gauche_dessus_180.png","main_gauche_paume_180.png",
85
 
                          "main_droite_dessus_270.png","main_droite_paume_270.png",
86
 
                          "main_gauche_dessus_270.png","main_gauche_paume_270.png",
87
 
                          "poing_droit_dessus_0.png", "poing_droit_paume_0.png",
88
 
                          "poing_gauche_dessus_0.png", "poing_gauche_paume_0.png",
89
 
                          "poing_droit_dessus_90.png", "poing_droit_paume_90.png",
90
 
                          "poing_gauche_dessus_90.png", "poing_gauche_paume_90.png",
91
 
                          "poing_droit_dessus_180.png", "poing_droit_paume_180.png",
92
 
                          "poing_gauche_dessus_180.png", "poing_gauche_paume_180.png",
93
 
                          "poing_droit_dessus_270.png", "poing_droit_paume_270.png",
94
 
                          "poing_gauche_dessus_270.png", "poing_gauche_paume_270.png"
95
 
};
96
 
 
97
 
/* Description of this plugin */
98
 
static BoardPlugin menu_bp =
99
 
  {
100
 
    NULL,
101
 
    NULL,
102
 
    "Find your left and right hands",
103
 
    "Given a picture of a hand, work out if it's a right or left hand",
104
 
    "Pascal Georges pascal.georges1@free.fr>",
105
 
    NULL,
106
 
    NULL,
107
 
    NULL,
108
 
    NULL,
109
 
    start_board,
110
 
    pause_board,
111
 
    end_board,
112
 
    is_our_board,
113
 
    NULL,
114
 
    process_ok,
115
 
    set_level,
116
 
    NULL,
117
 
    NULL,
118
 
    NULL,
119
 
    NULL
120
 
  };
121
 
 
122
 
/* =====================================================================
123
 
 *
124
 
 * =====================================================================*/
125
 
GET_BPLUGIN_INFO(leftright)
126
 
 
127
 
/* =====================================================================
128
 
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
129
 
 * =====================================================================*/
130
 
static void pause_board (gboolean pause)
131
 
{
132
 
  if(gcomprisBoard==NULL)
133
 
    return;
134
 
 
135
 
  gc_bar_hide(FALSE);
136
 
  if(gamewon == TRUE && pause == FALSE) /* the game is won */
137
 
    game_won();
138
 
 
139
 
  board_paused = pause;
140
 
}
141
 
 
142
 
/* =====================================================================
143
 
 *
144
 
 * =====================================================================*/
145
 
static void start_board (GcomprisBoard *agcomprisBoard) {
146
 
  if(agcomprisBoard!=NULL) {
147
 
    gchar *img;
148
 
 
149
 
    gcomprisBoard=agcomprisBoard;
150
 
    img = gc_skin_image_get("leftright-bg.jpg");
151
 
    gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
152
 
                            img);
153
 
    g_free(img);
154
 
    gcomprisBoard->level=1;
155
 
    gcomprisBoard->maxlevel=NUMBER_OF_LEVELS;
156
 
    gcomprisBoard->sublevel=1;
157
 
 
158
 
    gcomprisBoard->number_of_sublevel = NUMBER_OF_SUBLEVELS;
159
 
    gc_score_start(SCORESTYLE_NOTE, 10, 50, gcomprisBoard->number_of_sublevel);
160
 
    gc_bar_set(GC_BAR_LEVEL);
161
 
 
162
 
    leftright_next_level();
163
 
 
164
 
    gamewon = FALSE;
165
 
    pause_board(FALSE);
166
 
  }
167
 
}
168
 
 
169
 
/* =====================================================================
170
 
 *
171
 
 * =====================================================================*/
172
 
static void end_board () {
173
 
 
174
 
  if(gcomprisBoard!=NULL){
175
 
    pause_board(TRUE);
176
 
    gc_score_end();
177
 
    leftright_destroy_all_items();
178
 
  }
179
 
  gcomprisBoard = NULL;
180
 
}
181
 
 
182
 
/* =====================================================================
183
 
 *
184
 
 * =====================================================================*/
185
 
static void set_level (guint level) {
186
 
  if(gcomprisBoard!=NULL) {
187
 
    gcomprisBoard->level=level;
188
 
    gcomprisBoard->sublevel=1;
189
 
    leftright_next_level();
190
 
  }
191
 
}
192
 
 
193
 
/* =====================================================================
194
 
 *
195
 
 * =====================================================================*/
196
 
static gboolean is_our_board (GcomprisBoard *gcomprisBoard) {
197
 
  if (gcomprisBoard) {
198
 
    if(g_strcasecmp(gcomprisBoard->type, "leftright")==0) {
199
 
      /* Set the plugin entry */
200
 
      gcomprisBoard->plugin=&menu_bp;
201
 
      return TRUE;
202
 
    }
203
 
  }
204
 
  return FALSE;
205
 
}
206
 
/* =====================================================================
207
 
 * set initial values for the next level
208
 
 * =====================================================================*/
209
 
static void leftright_next_level() {
210
 
  gc_bar_set_level(gcomprisBoard);
211
 
 
212
 
  leftright_destroy_all_items();
213
 
  gamewon = FALSE;
214
 
 
215
 
  gc_score_set(gcomprisBoard->sublevel);
216
 
 
217
 
  /* Try the next level */
218
 
  leftright_create_item(gnome_canvas_root(gcomprisBoard->canvas));
219
 
 
220
 
}
221
 
/* =====================================================================
222
 
 * Destroy all the items
223
 
 * =====================================================================*/
224
 
static void leftright_destroy_all_items() {
225
 
  if(boardRootItem!=NULL)
226
 
    gtk_object_destroy (GTK_OBJECT(boardRootItem));
227
 
 
228
 
  boardRootItem = NULL;
229
 
}
230
 
 
231
 
/* =====================================================================
232
 
 *
233
 
 * =====================================================================*/
234
 
static GnomeCanvasItem *leftright_create_item(GnomeCanvasGroup *parent) {
235
 
  GdkPixbuf *highlight_pixmap = NULL;
236
 
  GdkPixbuf *hand_pixmap = NULL;
237
 
  gchar *str;
238
 
  int i;
239
 
 
240
 
  boardRootItem = GNOME_CANVAS_GROUP(
241
 
                                     gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
242
 
                                                            gnome_canvas_group_get_type (),
243
 
                                                            "x", (double) 0,
244
 
                                                            "y", (double) 0,
245
 
                                                            NULL));
246
 
 
247
 
  highlight_pixmap = gc_pixmap_load("leftright/leftright-select.png");
248
 
 
249
 
  left_highlight_image_item = gnome_canvas_item_new (boardRootItem,
250
 
                                                     gnome_canvas_pixbuf_get_type (),
251
 
                                                     "pixbuf", highlight_pixmap,
252
 
                                                     "x", (double) BUTTON_AREA_X1,
253
 
                                                     "y", (double) BUTTON_AREA_Y1,
254
 
                                                     "width", (double) gdk_pixbuf_get_width(highlight_pixmap),
255
 
                                                     "height", (double) gdk_pixbuf_get_height(highlight_pixmap),
256
 
                                                     "width_set", TRUE,
257
 
                                                     "height_set", TRUE,
258
 
                                                     NULL);
259
 
 
260
 
  right_highlight_image_item = gnome_canvas_item_new (boardRootItem,
261
 
                                                      gnome_canvas_pixbuf_get_type (),
262
 
                                                      "pixbuf", highlight_pixmap,
263
 
                                                      "x", (double) BUTTON_AREA_X2,
264
 
                                                      "y", (double) BUTTON_AREA_Y1,
265
 
                                                      "width", (double) gdk_pixbuf_get_width(highlight_pixmap),
266
 
                                                      "height", (double) gdk_pixbuf_get_height(highlight_pixmap),
267
 
                                                      "width_set", TRUE,
268
 
                                                      "height_set", TRUE,
269
 
                                                      NULL);
270
 
 
271
 
  gnome_canvas_item_hide(right_highlight_image_item);
272
 
  gnome_canvas_item_hide(left_highlight_image_item);
273
 
 
274
 
  gnome_canvas_item_new (boardRootItem,
275
 
                         gnome_canvas_text_get_type (),
276
 
                         "text", _("left"),
277
 
                         "font", gc_skin_font_board_big,
278
 
                         "x", (double) CENTER_LEFT_X + 1.0,
279
 
                         "y", (double) CENTER_LEFT_Y + 1.0,
280
 
                         "anchor", GTK_ANCHOR_CENTER,
281
 
                         "fill_color", "black",
282
 
                         NULL);
283
 
 
284
 
  gnome_canvas_item_new (boardRootItem,
285
 
                         gnome_canvas_text_get_type (),
286
 
                         "text", _("left"),
287
 
                         "font", gc_skin_font_board_big,
288
 
                         "x", (double) CENTER_LEFT_X,
289
 
                         "y", (double) CENTER_LEFT_Y,
290
 
                         "anchor", GTK_ANCHOR_CENTER,
291
 
                         "fill_color", TEXT_COLOR,
292
 
                         NULL);
293
 
 
294
 
  gnome_canvas_item_new (boardRootItem,
295
 
                         gnome_canvas_text_get_type (),
296
 
                         "text", _("right"),
297
 
                         "font", gc_skin_font_board_big,
298
 
                         "x", (double) CENTER_RIGHT_X + 1.0,
299
 
                         "y", (double) CENTER_RIGHT_Y + 1.0,
300
 
                         "anchor", GTK_ANCHOR_CENTER,
301
 
                         "fill_color", "black",
302
 
                         NULL);
303
 
 
304
 
  gnome_canvas_item_new (boardRootItem,
305
 
                         gnome_canvas_text_get_type (),
306
 
                         "text", _("right"),
307
 
                         "font", gc_skin_font_board_big,
308
 
                         "x", (double) CENTER_RIGHT_X,
309
 
                         "y", (double) CENTER_RIGHT_Y,
310
 
                         "anchor", GTK_ANCHOR_CENTER,
311
 
                         "fill_color", TEXT_COLOR,
312
 
                         NULL);
313
 
 
314
 
  // make sure that next hand is not the same as previous
315
 
  do {
316
 
    i = g_random_int_range(0,gcomprisBoard->level*8-1);
317
 
  } while ( i == last_hand );
318
 
 
319
 
  last_hand = i;
320
 
 
321
 
  if ( ((int) (i/2)) % 2 == 1)
322
 
    answer = LEFT;
323
 
  else
324
 
    answer = RIGHT;
325
 
 
326
 
  str = g_strdup_printf("%s/%s", gcomprisBoard->boarddir, hands[i]);
327
 
  hand_pixmap = gc_pixmap_load(str);
328
 
  hand_image_item = gnome_canvas_item_new (boardRootItem,
329
 
                                           gnome_canvas_pixbuf_get_type (),
330
 
                                           "pixbuf", hand_pixmap,
331
 
                                           "x", (double) HAND_X - (gdk_pixbuf_get_width(hand_pixmap)/2),
332
 
                                           "y", (double) HAND_Y - (gdk_pixbuf_get_height(hand_pixmap)/2),
333
 
                                           "width", (double) gdk_pixbuf_get_width(hand_pixmap),
334
 
                                           "height", (double) gdk_pixbuf_get_height(hand_pixmap),
335
 
                                           "width_set", TRUE,
336
 
                                           "height_set", TRUE,
337
 
                                           NULL);
338
 
 
339
 
  g_free(str);
340
 
 
341
 
  gdk_pixbuf_unref(highlight_pixmap);
342
 
  gdk_pixbuf_unref(hand_pixmap);
343
 
 
344
 
  gtk_signal_connect(GTK_OBJECT(gcomprisBoard->canvas), "event",  (GtkSignalFunc) item_event, NULL);
345
 
 
346
 
  return NULL;
347
 
}
348
 
/* =====================================================================
349
 
 *
350
 
 * =====================================================================*/
351
 
static void game_won() {
352
 
  gcomprisBoard->sublevel++;
353
 
  if(gcomprisBoard->sublevel > gcomprisBoard->number_of_sublevel) {
354
 
    /* Try the next level */
355
 
    gcomprisBoard->sublevel=1;
356
 
    gcomprisBoard->level++;
357
 
    if(gcomprisBoard->level>gcomprisBoard->maxlevel) { // the current board is finished : bail out
358
 
      gc_bonus_end_display(GC_BOARD_FINISHED_TUXLOCO);
359
 
      return;
360
 
    }
361
 
  }
362
 
  leftright_next_level();
363
 
}
364
 
 
365
 
/* =====================================================================
366
 
 *
367
 
 * =====================================================================*/
368
 
static gboolean process_ok_timeout() {
369
 
  gc_bonus_display(gamewon, GC_BONUS_SMILEY);
370
 
  return FALSE;
371
 
}
372
 
 
373
 
static void process_ok() {
374
 
  gc_bar_hide(TRUE);
375
 
  // leave time to display the right answer
376
 
  g_timeout_add(TIME_CLICK_TO_BONUS, process_ok_timeout, NULL);
377
 
}
378
 
/* =====================================================================
379
 
 *
380
 
 * =====================================================================*/
381
 
static gint item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data) {
382
 
  double x, y;
383
 
  int side;
384
 
 
385
 
  x = event->button.x;
386
 
  y = event->button.y;
387
 
 
388
 
  if (!gcomprisBoard || board_paused)
389
 
    return FALSE;
390
 
 
391
 
  switch (event->type)
392
 
    {
393
 
    case GDK_BUTTON_PRESS:
394
 
      gnome_canvas_c2w(gcomprisBoard->canvas, x, y, &x, &y);
395
 
 
396
 
      if (y>CLICKABLE_Y1 && y<CLICKABLE_Y2) {
397
 
        if (x>CLICKABLE_X1 && x<CLICKABLE_X2) { // the left button is clicked
398
 
          gc_sound_play_ogg ("sounds/bleep.wav", NULL);
399
 
          board_paused = TRUE;
400
 
          side = LEFT;
401
 
          highlight_selected(side);
402
 
          gamewon = (side == answer);
403
 
          process_ok();
404
 
        }
405
 
        if (x>CLICKABLE_X3 && x<CLICKABLE_X4) { // the left button is clicked
406
 
          gc_sound_play_ogg ("sounds/bleep.wav", NULL);
407
 
          board_paused = TRUE;
408
 
          side = RIGHT;
409
 
          highlight_selected(side);
410
 
          gamewon = (side == answer);
411
 
          process_ok();
412
 
        }
413
 
      }
414
 
 
415
 
      break;
416
 
 
417
 
    default:
418
 
      break;
419
 
    }
420
 
  return FALSE;
421
 
}
422
 
 
423
 
/* =====================================================================
424
 
 *
425
 
 * =====================================================================*/
426
 
static void highlight_selected(int side) {
427
 
  if (side == LEFT) {
428
 
    gnome_canvas_item_hide(right_highlight_image_item);
429
 
    gnome_canvas_item_show(left_highlight_image_item);
430
 
  }
431
 
  if (side == RIGHT) {
432
 
    gnome_canvas_item_show(right_highlight_image_item);
433
 
    gnome_canvas_item_hide(left_highlight_image_item);
434
 
  }
435
 
}