Back to index

gcompris  8.2.2
Defines | Functions | Variables
read_colors.c File Reference
#include <string.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include "gcompris/gcompris.h"

Go to the source code of this file.

Defines

#define SOUNDLISTFILE   PACKAGE
#define LAST_COLOR   11
#define MAX_ERRORS   10
#define CLOCK_X   40
#define CLOCK_Y   420
#define RADIUS   60

Functions

static void start_board (GcomprisBoard *agcomprisBoard)
static void pause_board (gboolean pause)
static void end_board (void)
static gboolean is_our_board (GcomprisBoard *gcomprisBoard)
static void process_ok (void)
static void highlight_selected (int)
static void game_won (void)
static void init_xml ()
static gboolean read_xml_file (char *fname)
static GnomeCanvasItem * read_colors_create_item (GnomeCanvasGroup *parent)
static void read_colors_destroy_all_items (void)
static void read_colors_next_level (void)
static gint item_event (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
static void update_clock ()
static gboolean process_ok_timeout ()
static void add_xml_data (xmlDocPtr doc, xmlNodePtr xmlnode, GNode *child)
static void parse_doc (xmlDocPtr doc)

Variables

static GcomprisBoard * gcomprisBoard = NULL
static gboolean board_paused = TRUE
static int gamewon
static GnomeCanvasGroup * boardRootItem = NULL
static GnomeCanvasItem * highlight_image_item = NULL
static GnomeCanvasItem * color_item = NULL
static GnomeCanvasItem * clock_image_item = NULL
static GdkPixbuf * clock_pixmap = NULL
static int highlight_width
static int highlight_height
static int errors
static GList * listColors = NULL
static char * colors [LAST_COLOR]
static int X [] = {100,300,500,700,200,400,600,100,300,500,700}
static int Y [] = {90,90,90,90,180,180,180,270,270,270,270}
static int color_x1 = 240
static int color_x2 = 570
static int color_y1 = 395
static int color_y2 = 490
static BoardPlugin menu_bp

Define Documentation

#define CLOCK_X   40

Definition at line 64 of file read_colors.c.

#define CLOCK_Y   420

Definition at line 65 of file read_colors.c.

#define LAST_COLOR   11

Definition at line 62 of file read_colors.c.

#define MAX_ERRORS   10

Definition at line 63 of file read_colors.c.

#define RADIUS   60

Definition at line 77 of file read_colors.c.

#define SOUNDLISTFILE   PACKAGE

Definition at line 28 of file read_colors.c.


Function Documentation

static void add_xml_data ( xmlDocPtr  doc,
xmlNodePtr  xmlnode,
GNode *  child 
) [static]

Definition at line 419 of file read_colors.c.

{
  char *text = NULL;
  char *sColor = NULL;
  int i;

  xmlnode = xmlnode->xmlChildrenNode;

  xmlnode = xmlnode->next;

  while (xmlnode != NULL) {

    // try to match color[i]
    for (i=0; i<LAST_COLOR; i++) {
      sColor = g_strdup_printf("color%d", i+1);
      if (!strcmp((char *)xmlnode->name, sColor)) {
       text = \
         gettext((char *)xmlNodeListGetString(doc, xmlnode->xmlChildrenNode, 1));
       colors[i] = text;
       g_free(sColor);
       break;
      }
      g_free(sColor);
    } // end for
    xmlnode = xmlnode->next;
  }

  // I really don't know why this test, but otherwise, the list is doubled
  // with 1 line on 2 filled with NULL elements
  if ( text == NULL)
    return;

}

Here is the caller graph for this function:

static void end_board ( void  ) [static]

Definition at line 168 of file read_colors.c.

                         {

  if(gcomprisBoard!=NULL){
    pause_board(TRUE);
    gc_score_end();
    read_colors_destroy_all_items();
    // free list
    while (g_list_length(listColors) > 0)
      listColors = g_list_remove(listColors, g_list_nth_data(listColors,0));

    g_list_free(listColors);
    listColors = NULL;
  }
  gcomprisBoard = NULL;
}

Here is the call graph for this function:

static void game_won ( void  ) [static]

Definition at line 312 of file read_colors.c.

                       {
  gcomprisBoard->sublevel++;

  listColors = g_list_remove(listColors, g_list_nth_data(listColors,0));

  if( g_list_length(listColors) <= 0 ) { // the current board is finished : bail out
    gc_bonus_end_display(BOARD_FINISHED_TUXLOCO);
    return;
  }

  read_colors_next_level();
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void highlight_selected ( int  c) [static]

Definition at line 390 of file read_colors.c.

                                      {
  int x, y;

  g_assert(c>=0 && c<LAST_COLOR);

  x = X[c];
  y = Y[c];

  x -= highlight_width/2;
  y -= highlight_height/2;
  gnome_canvas_item_show(highlight_image_item);
  gc_item_absolute_move(highlight_image_item, x, y);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void init_xml ( void  ) [static]

Definition at line 406 of file read_colors.c.

{
  char *filename;

  filename = gc_file_find_absolute("%s/board1.xml",
                               gcomprisBoard->boarddir);

  g_assert(read_xml_file(filename)== TRUE);

  g_free(filename);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static gboolean is_our_board ( GcomprisBoard *  gcomprisBoard) [static]

Definition at line 187 of file read_colors.c.

                                                            {
  if (gcomprisBoard) {
    if(g_strcasecmp(gcomprisBoard->type, "read_colors")==0) {
      /* Set the plugin entry */
      gcomprisBoard->plugin=&menu_bp;
      return TRUE;
    }
  }
  return FALSE;
}
static gint item_event ( GnomeCanvasItem *  item,
GdkEvent *  event,
gpointer  data 
) [static]

Definition at line 351 of file read_colors.c.

                                                                              {
  double x, y;
  int i, clicked;

  x = event->button.x;
  y = event->button.y;

  if (!gcomprisBoard || board_paused)
    return FALSE;

  switch (event->type)
    {
    case GDK_BUTTON_PRESS:
      gnome_canvas_c2w (gcomprisBoard->canvas, x, y, &x, &y);
      clicked = -1;
      for (i=0; i<LAST_COLOR; i++) {
       if (hypot((double) (X[i]-x),(double)(Y[i]-y)) < RADIUS) {
         clicked = i;
         break;
       }
      }

      if (clicked >= 0) {
       board_paused = TRUE;
       highlight_selected(clicked);
       gamewon = (clicked == GPOINTER_TO_INT(g_list_nth_data(listColors,0)));
       process_ok();
      }
      break;

    default:
      break;
    }
  return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void parse_doc ( xmlDocPtr  doc) [static]

Definition at line 454 of file read_colors.c.

{
  xmlNodePtr node;

  for(node = doc->children->children; node != NULL; node = node->next) {
    if ( g_strcasecmp((gchar *)node->name, "Board") == 0 )
      add_xml_data(doc, node,NULL);
  }

}

Here is the call graph for this function:

Here is the caller graph for this function:

static void pause_board ( gboolean  pause) [static]

Definition at line 112 of file read_colors.c.

{
  if(gcomprisBoard==NULL)
    return;

  gc_bar_hide(FALSE);
  if(gamewon == TRUE && pause == FALSE) /* the game is won */
    game_won();

  board_paused = pause;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void process_ok ( void  ) [static]

Definition at line 342 of file read_colors.c.

                         {
  gc_bar_hide(TRUE);
  // leave time to display the right answer
  g_timeout_add(TIME_CLICK_TO_BONUS, process_ok_timeout, NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static gboolean process_ok_timeout ( ) [static]

Definition at line 327 of file read_colors.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static GnomeCanvasItem * read_colors_create_item ( GnomeCanvasGroup *  parent) [static]

Definition at line 233 of file read_colors.c.

                                                                          {
  GdkPixbuf *highlight_pixmap = NULL;
  char *str = NULL;

  boardRootItem = GNOME_CANVAS_GROUP(
                                 gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
                                                     gnome_canvas_group_get_type (),
                                                     "x", (double) 0,
                                                     "y", (double) 0,
                                                     NULL));

  str = g_strdup_printf("%s/%s", gcomprisBoard->boarddir, "read_colors_highlight.png");
  highlight_pixmap = gc_pixmap_load(str);

  highlight_image_item = gnome_canvas_item_new (boardRootItem,
                                          gnome_canvas_pixbuf_get_type (),
                                          "pixbuf", highlight_pixmap,
                                          "x", (double) 0,
                                          "y", (double) 0,
                                          "width", (double) gdk_pixbuf_get_width(highlight_pixmap),
                                          "height", (double) gdk_pixbuf_get_height(highlight_pixmap),
                                          "width_set", TRUE,
                                          "height_set", TRUE,
                                          NULL);

  highlight_width = gdk_pixbuf_get_width(highlight_pixmap);
  highlight_height = gdk_pixbuf_get_height(highlight_pixmap);

  g_free(str);
  gnome_canvas_item_hide(highlight_image_item);

  gdk_pixbuf_unref(highlight_pixmap);

  /* setup the clock */
  str = g_strdup_printf("%s%d.png", "gcompris/timers/clock",errors);
  clock_pixmap = gc_pixmap_load(str);

  clock_image_item = gnome_canvas_item_new (boardRootItem,
                                       gnome_canvas_pixbuf_get_type (),
                                       "pixbuf", clock_pixmap,
                                       "x", (double) CLOCK_X,
                                       "y", (double) CLOCK_Y,
                                       "width", (double) gdk_pixbuf_get_width(clock_pixmap),
                                       "height", (double) gdk_pixbuf_get_height(clock_pixmap),
                                       "width_set", TRUE,
                                       "height_set", TRUE,
                                       NULL);

  g_free(str);

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void read_colors_destroy_all_items ( void  ) [static]

Definition at line 223 of file read_colors.c.

                                            {
  if(boardRootItem!=NULL)
    gtk_object_destroy (GTK_OBJECT(boardRootItem));

  boardRootItem = NULL;
}

Here is the caller graph for this function:

static void read_colors_next_level ( void  ) [static]

Definition at line 200 of file read_colors.c.

                                     {

  read_colors_destroy_all_items();
  gamewon = FALSE;

  /* Try the next level */
  read_colors_create_item(gnome_canvas_root(gcomprisBoard->canvas));

  /* show text of color to find */
  color_item = gnome_canvas_item_new (boardRootItem,
                                  gnome_canvas_text_get_type (),
                                  "text", colors[GPOINTER_TO_INT(g_list_nth_data(listColors,0))],
                                  "font", gc_skin_font_board_title_bold,
                                  "x", (double) (color_x1+color_x2)/2,
                                  "y", (double) (color_y1+color_y2)/2,
                                  "anchor", GTK_ANCHOR_CENTER,
                                  "fill_color", "darkblue",
                                  NULL);

}

Here is the call graph for this function:

Here is the caller graph for this function:

static gboolean read_xml_file ( char *  fname) [static]

Definition at line 467 of file read_colors.c.

{
  /* pointer to the new doc */
  xmlDocPtr doc;

  g_return_val_if_fail(fname!=NULL,FALSE);

  /* parse the new file and put the result into newdoc */
  doc = gc_net_load_xml(fname);

  /* in case something went wrong */
  if(!doc)
    return FALSE;

  if(/* if there is no root element */
     !doc->children ||
     /* if it doesn't have a name */
     !doc->children->name ||
     /* if it isn't a ImageId node */
     g_strcasecmp((char *)doc->children->name,"ReadColors")!=0) {
    xmlFreeDoc(doc);
    return FALSE;
  }

  parse_doc(doc);
  xmlFreeDoc(doc);
  return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void start_board ( GcomprisBoard *  agcomprisBoard) [static]

Definition at line 127 of file read_colors.c.

                                                        {
  GList * list = NULL;
  int * item;
  int i;

  if(agcomprisBoard!=NULL) {
    gcomprisBoard=agcomprisBoard;
    gc_set_background(gnome_canvas_root(gcomprisBoard->canvas), "read_colors/read_colors_background.png");
    gcomprisBoard->level=1;
    gcomprisBoard->maxlevel=1;
    gc_bar_set(0);

    gamewon = FALSE;
    errors = MAX_ERRORS;

    init_xml();

    // we generate a list of color indexes in a random order
    while (g_list_length(listColors) > 0)
      listColors = g_list_remove(listColors, g_list_nth_data(listColors,0));

    for (i=0; i<LAST_COLOR; i++)
      list = g_list_append(list, GINT_TO_POINTER(i));

    while ((g_list_length(list) > 0)) {
      i = RAND(0,g_list_length(list)-1);
      item = g_list_nth_data(list, i);
      listColors = g_list_append(listColors, item);
      list = g_list_remove(list, item);
    }
    g_list_free(list);

    gtk_signal_connect(GTK_OBJECT(gcomprisBoard->canvas), "event",  (GtkSignalFunc) item_event, NULL);
    read_colors_next_level();
    pause_board(FALSE);
  }
}

Here is the call graph for this function:

static void update_clock ( ) [static]

Definition at line 288 of file read_colors.c.

                           {
  char *str = g_strdup_printf("%s%d.png", "gcompris/timers/clock",errors);

  gtk_object_destroy (GTK_OBJECT(clock_image_item));

  clock_pixmap = gc_pixmap_load(str);

  clock_image_item = gnome_canvas_item_new (boardRootItem,
                                       gnome_canvas_pixbuf_get_type (),
                                       "pixbuf", clock_pixmap,
                                       "x", (double) CLOCK_X,
                                       "y", (double) CLOCK_Y,
                                       "width", (double) gdk_pixbuf_get_width(clock_pixmap),
                                       "height", (double) gdk_pixbuf_get_height(clock_pixmap),
                                       "width_set", TRUE,
                                       "height_set", TRUE,
                                       NULL);

  gdk_pixbuf_unref(clock_pixmap);
  g_free(str);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

gboolean board_paused = TRUE [static]

Definition at line 31 of file read_colors.c.

GnomeCanvasGroup* boardRootItem = NULL [static]

Definition at line 47 of file read_colors.c.

GnomeCanvasItem* clock_image_item = NULL [static]

Definition at line 50 of file read_colors.c.

GdkPixbuf* clock_pixmap = NULL [static]

Definition at line 51 of file read_colors.c.

GnomeCanvasItem* color_item = NULL [static]

Definition at line 49 of file read_colors.c.

int color_x1 = 240 [static]

Definition at line 74 of file read_colors.c.

int color_x2 = 570 [static]

Definition at line 74 of file read_colors.c.

int color_y1 = 395 [static]

Definition at line 75 of file read_colors.c.

int color_y2 = 490 [static]

Definition at line 75 of file read_colors.c.

char* colors[LAST_COLOR] [static]

Definition at line 67 of file read_colors.c.

int errors [static]

Definition at line 59 of file read_colors.c.

int gamewon [static]

Definition at line 37 of file read_colors.c.

GcomprisBoard* gcomprisBoard = NULL [static]

Definition at line 30 of file read_colors.c.

int highlight_height [static]

Definition at line 59 of file read_colors.c.

GnomeCanvasItem* highlight_image_item = NULL [static]

Definition at line 48 of file read_colors.c.

int highlight_width [static]

Definition at line 59 of file read_colors.c.

GList* listColors = NULL [static]

Definition at line 60 of file read_colors.c.

BoardPlugin menu_bp [static]
Initial value:
  {
    NULL,
    NULL,
    "Read Colors",
    "Click on the corresponding color",
    "Pascal Georges pascal.georges1@free.fr>",
    NULL,
    NULL,
    NULL,
    NULL,
    start_board,
    pause_board,
    end_board,
    is_our_board,
    NULL,
    process_ok,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
  }

Definition at line 80 of file read_colors.c.

int X[] = {100,300,500,700,200,400,600,100,300,500,700} [static]

Definition at line 69 of file read_colors.c.

int Y[] = {90,90,90,90,180,180,180,270,270,270,270} [static]

Definition at line 70 of file read_colors.c.