static void setup_canvas (GtkWidget *canvas) { GooCanvasItem *root, *path G_GNUC_UNUSED; root = goo_canvas_get_root_item (GOO_CANVAS (canvas)); g_signal_connect (root, "button_press_event", G_CALLBACK (on_background_button_press), NULL); /* Test the simple commands like moveto and lineto: MmZzLlHhVv. */ path1 = goo_canvas_path_new (root, "M 20 20 L 40 40", NULL); path = goo_canvas_path_new (root, "M30 20 l20, 20", NULL); path = goo_canvas_path_new (root, "M 60 20 H 80", NULL); path = goo_canvas_path_new (root, "M60 40 h20", NULL); path = goo_canvas_path_new (root, "M 100,20 V 40", NULL); path = goo_canvas_path_new (root, "M 120 20 v 20", NULL); path = goo_canvas_path_new (root, "M 140 20 h20 v20 h-20 z", NULL); path = goo_canvas_path_new (root, "M 180 20 h20 v20 h-20 z m 5,5 h10 v10 h-10 z", "fill-color", "red", "fill-rule", CAIRO_FILL_RULE_EVEN_ODD, NULL); path = goo_canvas_path_new (root, "M 220 20 L 260 20 L 240 40 z", "fill-color", "red", "stroke-color", "blue", "line-width", 3.0, NULL); /* Test the bezier curve commands: CcSsQqTt. */ path = goo_canvas_path_new (root, "M20,100 C20,50 100,50 100,100 S180,150 180,100", NULL); path = goo_canvas_path_new (root, "M220,100 c0,-50 80,-50 80,0 s80,50 80,0", NULL); path = goo_canvas_path_new (root, "M20,200 Q60,130 100,200 T180,200", NULL); path = goo_canvas_path_new (root, "M220,200 q40,-70 80,0 t80,0", NULL); /* Test the elliptical arc commands: Aa. */ path = goo_canvas_path_new (root, "M200,500 h-150 a150,150 0 1,0 150,-150 z", "fill-color", "red", "stroke-color", "blue", "line-width", 5.0, NULL); path = goo_canvas_path_new (root, "M175,475 v-150 a150,150 0 0,0 -150,150 z", "fill-color", "yellow", "stroke-color", "blue", "line-width", 5.0, NULL); path = goo_canvas_path_new (root, "M400,600 l 50,-25 " "a25,25 -30 0,1 50,-25 l 50,-25 " "a25,50 -30 0,1 50,-25 l 50,-25 " "a25,75 -30 0,1 50,-25 l 50,-25 " "a25,100 -30 0,1 50,-25 l 50,-25", "stroke-color", "red", "line-width", 5.0, NULL); path = goo_canvas_path_new (root, "M 525,75 a100,50 0 0,0 100,50", "stroke-color", "red", "line-width", 5.0, NULL); path = goo_canvas_path_new (root, "M 725,75 a100,50 0 0,1 100,50", "stroke-color", "red", "line-width", 5.0, NULL); path = goo_canvas_path_new (root, "M 525,200 a100,50 0 1,0 100,50", "stroke-color", "red", "line-width", 5.0, NULL); path = goo_canvas_path_new (root, "M 725,200 a100,50 0 1,1 100,50", "stroke-color", "red", "line-width", 5.0, NULL); }
/* ==================================== */ static GooCanvasItem * hanoi_create_item(GooCanvasItem *parent) { int i,j; double gap_x, gap_y; double baseline; GooCanvasItem *item = NULL; guint color_to_place; guint used_colors[NUMBER_OF_COLOR]; guint w; boardRootItem = \ goo_canvas_group_new (parent, NULL); if (gcomprisBoard->level == 1) { item = goo_canvas_svg_new (boardRootItem, gc_skin_rsvg_get(), "svg-id", "#BAR_BG", NULL); SET_ITEM_LOCATION_CENTER(item, BOARDWIDTH/2, 50); goo_canvas_text_new (boardRootItem, _("Build the same tower in the empty area as the one you see on the right-hand side."), BOARDWIDTH/2, 50, -1, GTK_ANCHOR_CENTER, "font", gc_skin_font_board_medium, "fill_color_rgba", gc_skin_color_text_button, NULL); } /*----------------------------------------*/ /* Empty the solution */ for(i=0; i<(number_of_item_x+2); i++) { for(j=0; j<number_of_item_y; j++) { position[i][j] = g_malloc(sizeof(PieceItem)); position[i][j]->color = -1; position[i][j]->i = i; position[i][j]->j = j; position[i][j]->on_top = FALSE; } } /* Clear the used colors list */ for(i=0; i<NUMBER_OF_COLOR; i++) used_colors[i] = FALSE; /* Initialize a random goal and store the color index in position[number_of_item_x] */ for(i=0; i<(number_of_item_y); i++) { guint color = (guint)g_random_int_range(0, NUMBER_OF_COLOR-1); position[number_of_item_x+1][i]->color = color; used_colors[color] = TRUE; } /* Randomly place the solution */ for (color_to_place=0; color_to_place<number_of_item_y; color_to_place++) { gboolean done; do { done = FALSE; i = (guint)g_random_int_range(0, number_of_item_x); /* Restrict the goal to lowest items */ j = (guint)g_random_int_range(0, 2); if(position[i][j]->color == -1) { done = TRUE; position[i][j]->color = position[number_of_item_x+1][color_to_place]->color; } } while(!done); } /* Initialize the left open positions */ for(i=0; i<(number_of_item_x); i++) { for(j=0; j<number_of_item_y-1; j++) { if(position[i][j]->color == -1) { /* Take only a color that is not part of the goal */ guint color = (guint)g_random_int_range(0, NUMBER_OF_COLOR-1); while(used_colors[color]) { color++; if(color >= NUMBER_OF_COLOR) color = 0; } position[i][j]->color = color; } } } //dump_solution(); /* Mark the top pieces */ for(i=0; i<(number_of_item_x); i++) { position[i][number_of_item_y-2]->on_top = TRUE; } /*----------------------------------------*/ /* Display it now */ item_width = BOARDWIDTH / (number_of_item_x + 2); item_height = 30; gap_x = item_width * 0.1; gap_y = item_height * 0.25; baseline = BOARDHEIGHT/2 + item_height * number_of_item_y/2; number_of_item = 0; for(i=0; i<(number_of_item_x+2); i++) { if(i == number_of_item_x+1) { /* Create the backgound for the target */ goo_canvas_rect_new (boardRootItem, item_width * i + gap_x/2, baseline - item_height * number_of_item_y - gap_y - 50, item_width - gap_x, item_height * number_of_item_y + gap_y*2 + 100, "fill_color_rgba", 0x036ED8FF, "stroke-color", "black", "line-width", (double)1, NULL); } else if (i == number_of_item_x) { /* Create the backgound for the empty area */ goo_canvas_rect_new (boardRootItem, item_width * i + gap_x/2, baseline - item_height * number_of_item_y - gap_y - 50, item_width - gap_x, item_height * number_of_item_y + gap_y*2 + 100, "fill_color_rgba", 0x48AAF1FF, "stroke-color", "black", "line-width", (double)1, NULL); } /* Create the vertical line */ w = 10; goo_canvas_rect_new (boardRootItem, item_width * i + item_width/2 - w, baseline - item_height * number_of_item_y - gap_y, w*2, (item_height + gap_y/2 - 2) * number_of_item_y, "fill_color_rgba", 0xFF1030FF, "stroke-color", "black", "line-width", (double)1, NULL); /* And the base line */ item = goo_canvas_path_new (boardRootItem, "M 43,19 A 22,20 0 1 1 -1,19 L 20,19 z", "fill_color_rgba", 0xFF1030FF, "stroke-color", "black", "line-width", 1.0, NULL); goo_canvas_item_translate(item, item_width * i + item_width/2 - 20, baseline - 25); for(j=0; j<number_of_item_y; j++) { position[i][j]->x = item_width * i + gap_x; position[i][j]->y = baseline - item_height * j - item_height + gap_y; if(position[i][j]->color != -1) { char car[2]; GooCanvasItem *group = goo_canvas_group_new(boardRootItem, NULL); goo_canvas_item_translate(group, position[i][j]->x, position[i][j]->y); position[i][j]->group = group; item = goo_canvas_rect_new (group, 0, 0, item_width - gap_x * 2, item_height - gap_y, "fill_color_rgba", colorlist[position[i][j]->color], "stroke-color", "black", "line-width", (double)1, NULL); car[0] = symbollist[position[i][j]->color]; car[1] = '\0'; goo_canvas_text_new (group, (char *)car, 20, 0, -1, GTK_ANCHOR_NORTH, "font", "sans bold 14", "fill-color", "white", NULL); if(i != number_of_item_x+1) { g_signal_connect(item, "button_press_event", (GCallback)gc_drag_event, position[i][j]); g_signal_connect(item, "button_release_event", (GCallback)gc_drag_event, position[i][j]); g_signal_connect(item, "enter_notify_event", (GCallback) item_event, position[i][j]); g_signal_connect(item, "leave_notify_event", (GCallback) item_event, position[i][j]); } } } } return NULL; }