Files
SAE11_2025/src/menu_view.c
T

237 lines
7.4 KiB
C
Raw Normal View History

2025-12-06 19:44:44 +01:00
/* src/menu_view.c
* Implémentation complète de l'interface de configuration (Menu)
*/
2025-11-30 15:40:46 +01:00
#include "menu_view.h"
2025-12-06 15:49:10 +01:00
#include "defs.h"
2025-11-30 15:40:46 +01:00
#include <graph.h>
2025-12-06 15:49:10 +01:00
#include <stdio.h>
2025-12-04 18:48:28 +01:00
#include <stdlib.h>
2025-12-06 15:49:10 +01:00
#include <string.h>
2025-11-30 15:40:46 +01:00
2025-12-06 19:44:44 +01:00
/* Constantes de positionnement */
2025-11-30 15:40:46 +01:00
#define IMG_PREVIEW_SIZE 150
2025-12-06 19:44:44 +01:00
#define IMG_START_X 250
2025-11-30 15:40:46 +01:00
#define IMG_START_Y 150
#define IMG_SPACING 200
#define BUTTON_SIZE 30
#define DIM_START_Y 350
2025-12-06 19:44:44 +01:00
/* Variables globales de la bibliothèque IUT pour la souris */
2025-11-30 15:40:46 +01:00
extern int _X;
extern int _Y;
2025-12-06 19:44:44 +01:00
/* --- PROTOTYPES DES UTILITAIRES PRIVÉS --- */
2025-11-30 15:40:46 +01:00
static int is_in_rect(int x_click, int y_click, int rx, int ry, int rw, int rh);
static void draw_button(int x, int y, const char *text, couleur C_BG);
static void draw_text_centered(int x, int y, int w, int h, const char *text, int size);
2025-12-06 19:44:44 +01:00
/* --- IMPLÉMENTATION DES FONCTIONS PUBLIQUES (API du Menu) --- */
2025-11-30 15:40:46 +01:00
2025-12-06 19:44:44 +01:00
/* Initialise l'état du menu */
2025-11-30 15:40:46 +01:00
void menu_init(menu_state_t *state)
{
2025-12-06 19:44:44 +01:00
state->selected_image = 0; /* Image 1 sélectionnée par défaut */
/* Ces constantes sont maintenant trouvées dans menu_view.h */
2025-11-30 15:40:46 +01:00
state->rows = MIN_DIMENSION;
state->cols = MIN_DIMENSION;
state->ready_to_play = 0;
}
2025-12-06 19:44:44 +01:00
/* Dessine l'écran de configuration du menu */
2025-11-30 15:40:46 +01:00
void menu_render(const menu_state_t *state)
{
int i;
couleur C_FOND = CouleurParNom("white");
couleur C_SELECTION = CouleurParNom("red");
couleur C_TEXTE = CouleurParNom("black");
couleur C_BOUTON = CouleurParNom("dark gray");
couleur C_START = CouleurParNom("green");
char buffer[50];
2025-12-06 15:49:10 +01:00
char path_buffer[30];
2025-11-30 15:40:46 +01:00
EffacerEcran(C_FOND);
ChoisirCouleurDessin(C_TEXTE);
2025-12-06 19:44:44 +01:00
/* 1. Titre et Section Image */
2025-11-30 15:40:46 +01:00
EcrireTexte(IMG_START_X, 50, "CONFIGURATION DU JEU TAQUIN", 2);
EcrireTexte(IMG_START_X, 100, "1. Choisir l'image :", 1);
2025-12-06 19:44:44 +01:00
/* BOUCLE : Affichage des 3 aperçus */
2025-11-30 15:40:46 +01:00
for (i = 0; i < NUM_IMAGES; i++) {
int x = IMG_START_X + i * IMG_SPACING;
int y = IMG_START_Y;
2025-12-06 19:44:44 +01:00
/* Définition du chemin */
if (i == 0) {
strcpy(path_buffer, "assets/spiderman.jpg");
} else if (i == 1) {
strcpy(path_buffer, "assets/capy.jpg");
} else {
strcpy(path_buffer, "assets/iut.jpg");
}
2025-11-30 15:40:46 +01:00
2025-12-06 19:44:44 +01:00
/* Affiche l'image */
ChargerImage(path_buffer,
x, y,
0, 0,
IMG_PREVIEW_SIZE, IMG_PREVIEW_SIZE);
2025-11-30 15:40:46 +01:00
2025-12-06 19:44:44 +01:00
/* Encadrement si sélectionné */
2025-11-30 15:40:46 +01:00
if (state->selected_image == i) {
ChoisirCouleurDessin(C_SELECTION);
DessinerRectangle(x - 5, y - 5, IMG_PREVIEW_SIZE + 10, IMG_PREVIEW_SIZE + 10);
}
}
ChoisirCouleurDessin(C_TEXTE);
2025-12-06 19:44:44 +01:00
/* 2. Section Dimensions */
2025-11-30 15:40:46 +01:00
int dim_section_x = IMG_START_X;
int button_l_offset = 180;
int button_c_offset = 400;
EcrireTexte(dim_section_x, DIM_START_Y, "2. Choisir les dimensions (3x3 a 8x8) :", 1);
2025-12-06 19:44:44 +01:00
/* Affichage de la taille LIGNES */
2025-11-30 15:40:46 +01:00
EcrireTexte(dim_section_x, DIM_START_Y + 40, "Lignes :", 0);
sprintf(buffer, "%d", state->rows);
EcrireTexte(dim_section_x + 100, DIM_START_Y + 40, buffer, 0);
2025-12-06 19:44:44 +01:00
/* Affichage de la taille COLONNES */
2025-11-30 15:40:46 +01:00
EcrireTexte(dim_section_x + 250, DIM_START_Y + 40, "Colonnes :", 0);
sprintf(buffer, "%d", state->cols);
EcrireTexte(dim_section_x + 350, DIM_START_Y + 40, buffer, 0);
2025-12-06 19:44:44 +01:00
/* Boutons de contrôle LIGNES */
2025-11-30 15:40:46 +01:00
draw_button(dim_section_x + button_l_offset, DIM_START_Y + 30, "+", C_BOUTON);
draw_button(dim_section_x + button_l_offset + 40, DIM_START_Y + 30, "-", C_BOUTON);
2025-12-06 19:44:44 +01:00
/* Boutons de contrôle COLONNES */
2025-11-30 15:40:46 +01:00
draw_button(dim_section_x + button_c_offset, DIM_START_Y + 30, "+", C_BOUTON);
draw_button(dim_section_x + button_c_offset + 40, DIM_START_Y + 30, "-", C_BOUTON);
2025-12-06 19:44:44 +01:00
/* 3. Bouton DÉMARRER LA PARTIE */
2025-11-30 15:40:46 +01:00
ChoisirCouleurDessin(C_START);
RemplirRectangle(300, 500, 200, 50);
ChoisirCouleurDessin(CouleurParNom("white"));
draw_text_centered(300, 500, 200, 50, "DEMARRER", 2);
}
2025-12-06 19:44:44 +01:00
/* Gère l'interaction après un clic de souris */
2025-11-30 15:40:46 +01:00
void menu_handle_mouse_click(menu_state_t *state)
{
int i;
int x_click = _X;
int y_click = _Y;
int btn_size = BUTTON_SIZE;
int dim_section_x = IMG_START_X;
int button_l_offset = 180;
int button_c_offset = 400;
2025-12-06 19:44:44 +01:00
/* 1. Clic sur les images */
2025-11-30 15:40:46 +01:00
for (i = 0; i < NUM_IMAGES; i++) {
int x_img = IMG_START_X + i * IMG_SPACING;
int y_img = IMG_START_Y;
if (is_in_rect(x_click, y_click, x_img, y_img, IMG_PREVIEW_SIZE, IMG_PREVIEW_SIZE)) {
state->selected_image = i;
return;
}
}
2025-12-06 19:44:44 +01:00
/* 2. Clic sur les boutons de Lignes (+/-) */
2025-12-06 15:49:10 +01:00
2025-12-06 19:44:44 +01:00
/* + Lignes */
2025-12-06 15:49:10 +01:00
if (is_in_rect(x_click, y_click, dim_section_x + button_l_offset, DIM_START_Y + 30, btn_size, btn_size)) {
2025-11-30 15:40:46 +01:00
if (state->rows < MAX_DIMENSION) state->rows++;
return;
}
2025-12-06 19:44:44 +01:00
/* - Lignes */
2025-11-30 15:40:46 +01:00
if (is_in_rect(x_click, y_click, dim_section_x + button_l_offset + 40, DIM_START_Y + 30, btn_size, btn_size)) {
if (state->rows > MIN_DIMENSION) state->rows--;
return;
}
2025-12-06 19:44:44 +01:00
/* 3. Clic sur les boutons de Colonnes (+/-) */
2025-11-30 15:40:46 +01:00
2025-12-06 19:44:44 +01:00
/* + Colonnes */
2025-11-30 15:40:46 +01:00
if (is_in_rect(x_click, y_click, dim_section_x + button_c_offset, DIM_START_Y + 30, btn_size, btn_size)) {
if (state->cols < MAX_DIMENSION) state->cols++;
return;
}
2025-12-06 19:44:44 +01:00
/* - Colonnes */
2025-11-30 15:40:46 +01:00
if (is_in_rect(x_click, y_click, dim_section_x + button_c_offset + 40, DIM_START_Y + 30, btn_size, btn_size)) {
if (state->cols > MIN_DIMENSION) state->cols--;
return;
}
2025-12-06 19:44:44 +01:00
/* 4. Clic sur DÉMARRER */
2025-11-30 15:40:46 +01:00
if (is_in_rect(x_click, y_click, 300, 500, 200, 50)) {
state->ready_to_play = 1;
return;
}
}
2025-12-06 19:44:44 +01:00
/* Gère l'interaction clavier */
2025-11-30 15:40:46 +01:00
void menu_handle_key(menu_state_t *state)
{
if (ToucheEnAttente()) {
int key = Touche();
if (key == XK_Escape) {
state->ready_to_play = -1;
}
else if (key == XK_Up) {
if (state->rows < MAX_DIMENSION) state->rows++;
}
else if (key == XK_Down) {
if (state->rows > MIN_DIMENSION) state->rows--;
}
else if (key == XK_Right) {
if (state->cols < MAX_DIMENSION) state->cols++;
}
else if (key == XK_Left) {
if (state->cols > MIN_DIMENSION) state->cols--;
}
else if (key == XK_Return) {
state->ready_to_play = 1;
}
}
}
2025-12-06 19:44:44 +01:00
/* --- IMPLÉMENTATION DES UTILITAIRES PRIVÉS --- */
2025-11-30 15:40:46 +01:00
2025-12-06 19:44:44 +01:00
/* Vérifie si (x_click, y_click) est dans le rectangle */
2025-11-30 15:40:46 +01:00
static int is_in_rect(int x_click, int y_click, int rx, int ry, int rw, int rh)
{
return (x_click >= rx && x_click <= rx + rw && y_click >= ry && y_click <= ry + rh);
}
2025-12-06 19:44:44 +01:00
/* Dessine un bouton rectangulaire rempli avec du texte centré */
2025-11-30 15:40:46 +01:00
static void draw_button(int x, int y, const char *text, couleur C_BG)
{
ChoisirCouleurDessin(C_BG);
RemplirRectangle(x, y, BUTTON_SIZE, BUTTON_SIZE);
ChoisirCouleurDessin(CouleurParNom("black"));
draw_text_centered(x, y, BUTTON_SIZE, BUTTON_SIZE, (char*)text, 1);
}
2025-12-06 19:44:44 +01:00
/* Centre le texte dans une zone (x, y, w, h) */
2025-11-30 15:40:46 +01:00
static void draw_text_centered(int x, int y, int w, int h, const char *text, int size)
{
int text_w = TailleChaineEcran((char*)text, size);
int text_h_sup = TailleSupPolice(size);
2025-12-06 19:44:44 +01:00
/* Calcul de la position de départ pour centrer le texte */
2025-11-30 15:40:46 +01:00
int draw_x = x + (w - text_w) / 2;
int draw_y = y + (h + text_h_sup) / 2;
EcrireTexte(draw_x, draw_y, (char*)text, size);
}