1
/* gcompris - leftright.c
3
* Copyright (C) 2002 Pascal Georges
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.
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.
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/>.
19
#include "gcompris/gcompris.h"
21
#define SOUNDLISTFILE PACKAGE
23
static GcomprisBoard *gcomprisBoard = NULL;
24
static gboolean board_paused = TRUE;
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);
32
static int last_hand = -1;
34
static void process_ok(void);
35
static void highlight_selected(int);
36
static void game_won();
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
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
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
62
#define NUMBER_OF_SUBLEVELS 6
63
#define NUMBER_OF_LEVELS 4
65
#define TEXT_COLOR "yellow"
67
/* ================================================================ */
68
static GnomeCanvasGroup *boardRootItem = NULL;
70
static GnomeCanvasItem *hand_image_item = NULL;
71
static GnomeCanvasItem *left_highlight_image_item = NULL, *right_highlight_image_item = NULL;
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);
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"
97
/* Description of this plugin */
98
static BoardPlugin menu_bp =
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>",
122
/* =====================================================================
124
* =====================================================================*/
125
GET_BPLUGIN_INFO(leftright)
127
/* =====================================================================
128
* in : boolean TRUE = PAUSE : FALSE = CONTINUE
129
* =====================================================================*/
130
static void pause_board (gboolean pause)
132
if(gcomprisBoard==NULL)
136
if(gamewon == TRUE && pause == FALSE) /* the game is won */
139
board_paused = pause;
142
/* =====================================================================
144
* =====================================================================*/
145
static void start_board (GcomprisBoard *agcomprisBoard) {
146
if(agcomprisBoard!=NULL) {
149
gcomprisBoard=agcomprisBoard;
150
img = gc_skin_image_get("leftright-bg.jpg");
151
gc_set_background(gnome_canvas_root(gcomprisBoard->canvas),
154
gcomprisBoard->level=1;
155
gcomprisBoard->maxlevel=NUMBER_OF_LEVELS;
156
gcomprisBoard->sublevel=1;
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);
162
leftright_next_level();
169
/* =====================================================================
171
* =====================================================================*/
172
static void end_board () {
174
if(gcomprisBoard!=NULL){
177
leftright_destroy_all_items();
179
gcomprisBoard = NULL;
182
/* =====================================================================
184
* =====================================================================*/
185
static void set_level (guint level) {
186
if(gcomprisBoard!=NULL) {
187
gcomprisBoard->level=level;
188
gcomprisBoard->sublevel=1;
189
leftright_next_level();
193
/* =====================================================================
195
* =====================================================================*/
196
static gboolean is_our_board (GcomprisBoard *gcomprisBoard) {
198
if(g_strcasecmp(gcomprisBoard->type, "leftright")==0) {
199
/* Set the plugin entry */
200
gcomprisBoard->plugin=&menu_bp;
206
/* =====================================================================
207
* set initial values for the next level
208
* =====================================================================*/
209
static void leftright_next_level() {
210
gc_bar_set_level(gcomprisBoard);
212
leftright_destroy_all_items();
215
gc_score_set(gcomprisBoard->sublevel);
217
/* Try the next level */
218
leftright_create_item(gnome_canvas_root(gcomprisBoard->canvas));
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));
228
boardRootItem = NULL;
231
/* =====================================================================
233
* =====================================================================*/
234
static GnomeCanvasItem *leftright_create_item(GnomeCanvasGroup *parent) {
235
GdkPixbuf *highlight_pixmap = NULL;
236
GdkPixbuf *hand_pixmap = NULL;
240
boardRootItem = GNOME_CANVAS_GROUP(
241
gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
242
gnome_canvas_group_get_type (),
247
highlight_pixmap = gc_pixmap_load("leftright/leftright-select.png");
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),
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),
271
gnome_canvas_item_hide(right_highlight_image_item);
272
gnome_canvas_item_hide(left_highlight_image_item);
274
gnome_canvas_item_new (boardRootItem,
275
gnome_canvas_text_get_type (),
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",
284
gnome_canvas_item_new (boardRootItem,
285
gnome_canvas_text_get_type (),
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,
294
gnome_canvas_item_new (boardRootItem,
295
gnome_canvas_text_get_type (),
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",
304
gnome_canvas_item_new (boardRootItem,
305
gnome_canvas_text_get_type (),
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,
314
// make sure that next hand is not the same as previous
316
i = g_random_int_range(0,gcomprisBoard->level*8-1);
317
} while ( i == last_hand );
321
if ( ((int) (i/2)) % 2 == 1)
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),
341
gdk_pixbuf_unref(highlight_pixmap);
342
gdk_pixbuf_unref(hand_pixmap);
344
gtk_signal_connect(GTK_OBJECT(gcomprisBoard->canvas), "event", (GtkSignalFunc) item_event, NULL);
348
/* =====================================================================
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);
362
leftright_next_level();
365
/* =====================================================================
367
* =====================================================================*/
368
static gboolean process_ok_timeout() {
369
gc_bonus_display(gamewon, GC_BONUS_SMILEY);
373
static void process_ok() {
375
// leave time to display the right answer
376
g_timeout_add(TIME_CLICK_TO_BONUS, process_ok_timeout, NULL);
378
/* =====================================================================
380
* =====================================================================*/
381
static gint item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data) {
388
if (!gcomprisBoard || board_paused)
393
case GDK_BUTTON_PRESS:
394
gnome_canvas_c2w(gcomprisBoard->canvas, x, y, &x, &y);
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);
401
highlight_selected(side);
402
gamewon = (side == answer);
405
if (x>CLICKABLE_X3 && x<CLICKABLE_X4) { // the left button is clicked
406
gc_sound_play_ogg ("sounds/bleep.wav", NULL);
409
highlight_selected(side);
410
gamewon = (side == answer);
423
/* =====================================================================
425
* =====================================================================*/
426
static void highlight_selected(int side) {
428
gnome_canvas_item_hide(right_highlight_image_item);
429
gnome_canvas_item_show(left_highlight_image_item);
432
gnome_canvas_item_show(right_highlight_image_item);
433
gnome_canvas_item_hide(left_highlight_image_item);