void ei_geomnotify_button(ei_widget_t *widget, ei_rect_t rect) { *(widget->content_rect) = ei_rect( ei_point_add( rect.top_left, ei_point( *ei_button(widget)->border_width, *ei_button(widget)->border_width)), ei_size( rect.size.width - 2 * *ei_button(widget)->border_width, rect.size.height - 2 * *ei_button(widget)->border_width)); }
/* Modifie l'Etat du bouton*/ void set_button_press(ei_widget_t * button) { ei_geomnotify_button(button, ei_rect( ei_point_add( button->screen_location.top_left, ei_point( EI_BUTTON_SINK_LENGHT, EI_BUTTON_SINK_LENGHT)), button->screen_location.size)); *ei_button(button)->relief = ei_relief_sunken; ei_app_invalidate_rect(&button->screen_location); if(*ei_button(button)->callback) ei_bind(ei_ev_mouse_buttonup, button, NULL, *ei_button(button)->callback, ei_button(button)->user_param); }
ei_bool_t handle_tile_press(ei_widget_t* widget, ei_event_t* event, void* user_param) { tile_t* tile = (tile_t*)user_param; puzzle_t* puzzle = tile->puzzle; ei_point_t offsets[] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}}; ei_point_t current = tile->current_position; ei_point_t swap_pos; ei_point_t swap_coordinates; int i; for (i = 0; i < 4; i++) { swap_pos = ei_point_add(current, offsets[i]); if (valid(puzzle, swap_pos) && tile_at(puzzle, swap_pos) == NULL) { puzzle->current[index_at(puzzle, current)] = NULL; puzzle->current[index_at(puzzle, swap_pos)] = tile; tile->current_position = swap_pos; swap_coordinates = place_coordinates(swap_pos); ei_place(tile->button, NULL, &(swap_coordinates.x), &(swap_coordinates.y), NULL, NULL, NULL, NULL, NULL, NULL); } } return EI_TRUE; }
// Dessine l'image d'un widget void ei_draw_widget_img( ei_surface_t surface, ei_widget_t* widget, ei_surface_t img, ei_rect_t* img_rect, ei_anchor_t anchor, int button_border_width ) { // Paramètres du widget ei_point_t wtopleft = ei_point_add( widget->screen_location.top_left, ei_point(2*button_border_width, 2*button_border_width) ); ei_size_t wsize = ei_size_sub( widget->screen_location.size, ei_size(4*button_border_width, 4*button_border_width) ); // Variables à calculer ei_rect_t dst_widget_rect; ei_rect_t src_image_rect; /***********************************************/ /* ETAPE 1 : CALCUL DE LA TAILLE DE DST ET SRC */ /***********************************************/ ei_size_t img_size; if (img_rect != NULL) { img_size = img_rect->size; } else { img_size = hw_surface_get_size(img); } // Calcul de la largeur de la zone de copie if (img_size.width > wsize.width) { src_image_rect.size.width = wsize.width; dst_widget_rect.size.width = wsize.width; } else { src_image_rect.size.width = img_size.width; dst_widget_rect.size.width = img_size.width; } // Calcul de la hauteur de la zone de copie if (img_size.height > wsize.height) { src_image_rect.size.height = wsize.height; dst_widget_rect.size.height = wsize.height; } else { src_image_rect.size.height = img_size.height; dst_widget_rect.size.height = img_size.height; } /*************************************/ /* ETAPE 2 : CALCUL DES DEUX TOPLEFT */ /*************************************/ // Calcul du topleft de la destination ei_switch_anchor(anchor, &(dst_widget_rect.top_left), dst_widget_rect.size, wtopleft, wsize); // Calcul du topleft de la source if (img_rect != NULL) { src_image_rect.top_left = img_rect->top_left; // fprintf(stderr, "x %i\n", img_rect->top_left.x); // fprintf(stderr, "y %i\n", img_rect->top_left.y); } else { // Par défaut je prend le coin en haut à gauche de l'image ei_rect_t rect_total = hw_surface_get_rect(img); src_image_rect.top_left = rect_total.top_left; } /*************************************************************/ /* ETAPE 2bis : RECALCUL SI SORTIE DU CADRE PRINCIPAL (ROOT) */ /*************************************************************/ ei_surface_t display_surface = ei_app_root_surface(); ei_rect_t display_rect = hw_surface_get_rect(display_surface); int cut; // Cas 1 : sortie à gauche cut = display_rect.top_left.x - dst_widget_rect.top_left.x; if ( cut > 0 ) { // Changement de DST dst_widget_rect.top_left.x = display_rect.top_left.x; dst_widget_rect.size.width = dst_widget_rect.size.width - cut; // Changement de SRC src_image_rect.top_left.x = src_image_rect.top_left.x + cut; src_image_rect.size = dst_widget_rect.size; } // Cas 2 : sortie à droite cut = dst_widget_rect.top_left.x + dst_widget_rect.size.width - display_rect.top_left.x - display_rect.size.width; if ( cut > 0 ) { // Changement de DST dst_widget_rect.size.width = dst_widget_rect.size.width - cut; // Changement de SRC src_image_rect.size = dst_widget_rect.size; } // Cas 3 : sortie en haut cut = display_rect.top_left.y - dst_widget_rect.top_left.y; if ( cut > 0 ) { // Changement de DST dst_widget_rect.top_left.y = display_rect.top_left.y; dst_widget_rect.size.height = dst_widget_rect.size.height - cut; // Changement de SRC src_image_rect.top_left.y = src_image_rect.top_left.y + cut; src_image_rect.size = dst_widget_rect.size; } // Cas 4 : sortie en bas cut = dst_widget_rect.top_left.y + dst_widget_rect.size.height - display_rect.top_left.y - display_rect.size.height; if ( cut > 0 ) { // Changement de DST dst_widget_rect.size.height = dst_widget_rect.size.height - cut; // Changement de SRC src_image_rect.size = dst_widget_rect.size; } /*****************************************/ /* ETAPE 3 : COPIE DE L'IMAGE A AFFICHEE */ /*****************************************/ hw_surface_lock(img); ei_copy_surface(surface, &dst_widget_rect, img, &src_image_rect, EI_FALSE); hw_surface_unlock(img); }
void create_puzzle_window(char* image_filename) { /*ei_widget_t* toplevel; ei_surface_t image; ei_size_t image_size; int x, y; int border_width = 2; ei_point_t place_coords; ei_point_t current_position; ei_size_t n; ei_size_t toplevel_size; ei_size_t image_rect_size; ei_size_t tile_size = ei_size(k_tile_size, k_tile_size); char* title = "Puzzle"; ei_color_t toplevel_bg = {0xff, 0xff, 0xff, 0x60}; ei_axis_set_t resizable = ei_axis_none;*/ ei_widget_t* frame; ei_surface_t image; ei_size_t image_size; int x, y; int border_width = 2; ei_point_t place_coords; ei_point_t current_position; ei_size_t n; ei_size_t toplevel_size; ei_size_t image_rect_size; ei_size_t tile_size = ei_size(k_tile_size, k_tile_size); ei_color_t toplevel_bg = {0xff, 0xff, 0xff, 0x60}; ei_axis_set_t resizable = ei_axis_none; ei_widget_t* button; ei_color_t grey = {0x88, 0x88, 0x88, 0xff}; int corner_radius = 0; ei_relief_t relief = ei_relief_raised; ei_rect_t img_rect; ei_rect_t* img_rect_ptr = &img_rect; ei_callback_t callback = handle_tile_press; puzzle_t* puzzle; tile_t* tile; image = hw_image_load(image_filename, ei_app_root_surface()); image_size = hw_surface_get_size(image); n = ei_size(image_size.width / k_tile_size, image_size.height / k_tile_size); frame = ei_widget_create("frame", ei_app_root_widget()); toplevel_size = ei_size(n.width*k_tile_size, n.height*k_tile_size); border_width = 0; ei_frame_configure(frame, &toplevel_size, &toplevel_bg, &border_width, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); ei_place(frame, NULL, &(g_toplevel_position.x), &(g_toplevel_position.y), NULL, NULL, NULL, NULL, NULL, NULL); g_toplevel_position = ei_point_add(g_toplevel_position, ei_point(12, 12)); puzzle = (puzzle_t*)malloc(sizeof(*puzzle)); puzzle->n = n; puzzle->tiles = (tile_t*) malloc(n.width*n.height*sizeof(tile_t)); puzzle->current = (tile_t**)malloc(n.width*n.height*sizeof(tile_t*)); border_width = 2; image_rect_size = ei_size_sub(tile_size, ei_size(2*border_width, 2*border_width)); for (y = 0; y < n.height; y++) { for (x = 0; x < n.width; x++) { current_position = ei_point(x, y); if ((x == n.width - 1) && (y == n.height - 1)) { puzzle->current[index_at(puzzle, current_position)] = NULL; continue; } button = ei_widget_create("button", frame); img_rect = ei_rect(ei_point(x*k_tile_size+border_width, y*k_tile_size+border_width), image_rect_size); tile = tile_memory_at(puzzle, ei_point(x, y)); printf("puzzle.c WIDTH::%i\n",img_rect_ptr->size.width); ei_button_configure(button, &tile_size, &grey, &border_width, &corner_radius, &relief, NULL, NULL, NULL, NULL, &image, &img_rect_ptr, NULL, &callback, (void*)&tile); place_coords = place_coordinates(current_position); ei_place(button, NULL, &(place_coords.x), &(place_coords.y), NULL, NULL, NULL, NULL, NULL, NULL); tile->puzzle = puzzle; tile->button = button; tile->original_position = current_position; tile->current_position = current_position; puzzle->current[index_at(puzzle, current_position)] = tile; } } }
void ei_runfunc_placer(struct ei_widget_t* widget) { // we load the location of the father widget ei_rect_t* container = widget->parent->content_rect; ei_geometrymanager_param_placer_t* prm_placer = (ei_geometrymanager_param_placer_t*)widget->geom_params; float width, height ; assert(prm_placer); assert(prm_placer->rel_width); width = container->size.width * *(prm_placer->rel_width) + *(prm_placer->width); height = container->size.height * *(prm_placer->rel_height) + *(prm_placer->height); ei_point_t pos_rel; switch (*(prm_placer->anchor)) { case ei_anc_none: case ei_anc_northwest: pos_rel = ei_point_zero(); break; case ei_anc_center: pos_rel = ei_point(width / 2, height / 2); break; case ei_anc_north: pos_rel = ei_point(width / 2, 0); break; case ei_anc_northeast: pos_rel = ei_point(width , 0); break; case ei_anc_east: pos_rel = ei_point(width, height / 2); break; case ei_anc_southeast: pos_rel = ei_point(width, height ); break; case ei_anc_south: pos_rel = ei_point(width / 2, height); break; case ei_anc_southwest: pos_rel = ei_point(0 , height ); break; case ei_anc_west: pos_rel = ei_point(0 , height / 2 ); break; } ei_point_t pos_abs = ei_point( container->size.width * *(prm_placer->rel_x) + *(prm_placer->x), container->size.height * *(prm_placer->rel_y) + *(prm_placer->y)); // ei_app_invalidate_rect(&widget->screen_location); widget->screen_location = ei_rect( ei_point_add(ei_point_sub(pos_abs, pos_rel),container->top_left), ei_size(width, height)); ei_app_invalidate_rect(&widget->screen_location); widget->wclass->geomnotifyfunc(widget, widget->screen_location); ei_widget_t * current = widget->children_head; while(current) { if(current->geom_params) { current->geom_params->manager->runfunc(current); } current = current->next_sibling; } }