summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoralex <alex@ASUS>2018-09-23 01:02:32 +0200
committeralex <alex@ASUS>2018-09-23 01:02:32 +0200
commitd947164aed58efaf5158c55314b9d95ce76688f9 (patch)
tree51183938136a8b783c5243073c538891b2a6710c
parent908059f7cbdd85fee44b6cc0f3c7eb5a0ae0a5c2 (diff)
Add alx_sscan_int() and ...dbl() for use in GUI; Add entries in the GUI menu
-rw-r--r--Makefile4
-rw-r--r--bin/Makefile6
-rw-r--r--libalx/inc/alx_getnum.h12
-rw-r--r--libalx/src/alx_getnum.c76
-rw-r--r--modules/menu/obj/Makefile2
-rw-r--r--modules/menu/src/menu_gui.c174
-rw-r--r--modules/player/src/player_iface.c31
7 files changed, 218 insertions, 87 deletions
diff --git a/Makefile b/Makefile
index 0de5249..3974868 100644
--- a/Makefile
+++ b/Makefile
@@ -269,7 +269,3 @@ help:
################################################################################
######## End of file ###########################################################
################################################################################
-######## End of file ###########################################################
-################################################################################
-######## End of file ###########################################################
-################################################################################
diff --git a/bin/Makefile b/bin/Makefile
index 3a6448c..5f4b88c 100644
--- a/bin/Makefile
+++ b/bin/Makefile
@@ -60,7 +60,7 @@ all: $(ALL)
$(BIN_NAME): $(MAIN_OBJS)
$(Q)$(CC) $^ -o $@ $(LIBS)
- @echo "\tLD $@"
+ @echo "\tBIN $@"
@echo ""
@@ -71,7 +71,3 @@ clean:
################################################################################
######## End of file ###########################################################
################################################################################
-######## End of file ###########################################################
-################################################################################
-######## End of file ###########################################################
-################################################################################
diff --git a/libalx/inc/alx_getnum.h b/libalx/inc/alx_getnum.h
index 17404e0..af569d2 100644
--- a/libalx/inc/alx_getnum.h
+++ b/libalx/inc/alx_getnum.h
@@ -21,6 +21,18 @@
/******************************************************************************
******* functions ************************************************************
******************************************************************************/
+ int alx_sscan_dbl (double *dest,
+ double m,
+ double def,
+ double M,
+ const char *str);
+
+ int alx_sscan_int (int64_t *dest,
+ double m,
+ int64_t def,
+ double M,
+ const char *str);
+
double alx_getdbl (double m,
double def,
double M,
diff --git a/libalx/src/alx_getnum.c b/libalx/src/alx_getnum.c
index 3bfa22f..cf46514 100644
--- a/libalx/src/alx_getnum.c
+++ b/libalx/src/alx_getnum.c
@@ -47,6 +47,54 @@ static void manage_error (int err);
******* main *****************************************************************
******************************************************************************/
/*
+ * Scan a double in the range [m, M].
+ * return: 0 if correct
+ * non 0 if there is an error
+ */
+int alx_sscan_dbl (double *dest, double m, double def, double M, const char *str)
+{
+ int err;
+
+ if (sscanf(str, " %lf", dest) != 1) {
+ err = ERR_SSCANF;
+ } else if ((*dest < m) || (*dest > M)) {
+ err = ERR_RANGE;
+ } else {
+ err = 0;
+ }
+
+ if (err) {
+ *dest = def;
+ }
+
+ return err;
+}
+
+ /*
+ * Scan an int64_t in the range [m, M].
+ * return: 0 if correct
+ * non 0 if there is an error
+ */
+int alx_sscan_int (int64_t *dest, double m, int64_t def, double M, const char *str)
+{
+ int err;
+
+ if (sscanf(str, " %"SCNi64"", dest) != 1) {
+ err = ERR_SSCANF;
+ } else if ((*dest < m) || (*dest > M)) {
+ err = ERR_RANGE;
+ } else {
+ err = 0;
+ }
+
+ if (err) {
+ *dest = def;
+ }
+
+ return err;
+}
+
+ /*
* Ask for a double in the range [m, M].
*
* If the user enters a non valid number, it repeats to ask for
@@ -123,16 +171,16 @@ static double loop_getdbl (double m, double def, double M)
if (x == NULL) {
err = ERR_FGETS;
- } else if (sscanf(buff, " %lf", &R) != 1) {
- err = ERR_SSCANF;
- } else if (R < m || R > M) {
- err = ERR_RANGE;
} else {
- break;
+ err = alx_sscan_dbl(&R, m, def, M, buff);
}
- manage_error(err);
- R = def;
+ if (err) {
+ manage_error(err);
+ R = def;
+ } else {
+ break;
+ }
}
return R;
@@ -151,16 +199,16 @@ static int64_t loop_getint (double m, int64_t def, double M)
if (x == NULL) {
err = ERR_FGETS;
- } else if (sscanf(buff, " %"SCNi64, &Z) != 1) {
- err = ERR_SSCANF;
- } else if (Z < m || Z > M) {
- err = ERR_RANGE;
} else {
- break;
+ err = alx_sscan_int(&Z, m, def, M, buff);
}
- manage_error(err);
- Z = def;
+ if (err) {
+ manage_error(err);
+ Z = def;
+ } else {
+ break;
+ }
}
return Z;
diff --git a/modules/menu/obj/Makefile b/modules/menu/obj/Makefile
index 5b72d54..285cb35 100644
--- a/modules/menu/obj/Makefile
+++ b/modules/menu/obj/Makefile
@@ -94,7 +94,7 @@ MENUTUI_INC_DIRS = -I $(INC_DIR) \
-I $(GAME_INC_DIR) \
-I $(SAVE_INC_DIR)
-MENUGUI_INC_LIBALX = alx_ncur.h
+MENUGUI_INC_LIBALX = alx_getnum.h
MENUGUI_INC_ABOUT = about.h
MENUGUI_INC_CTRL = start.h
MENUGUI_INC_GAME = game_iface.h
diff --git a/modules/menu/src/menu_gui.c b/modules/menu/src/menu_gui.c
index 427534a..70c3e17 100644
--- a/modules/menu/src/menu_gui.c
+++ b/modules/menu/src/menu_gui.c
@@ -22,6 +22,8 @@
/* * * * * * * * * *
* * * Other * * * * * * *
* * * * * * * * * */
+ #include "alx_getnum.h"
+
#include "about.h"
#include "game_iface.h"
#include "save.h"
@@ -55,6 +57,24 @@ struct Label_Data {
char text [LINE_SIZE];
};
+struct Entry_dbl_Data {
+ GtkWidget *ptr;
+ struct Label_Data lbl;
+ double *num;
+ double min;
+ double def;
+ double max;
+};
+
+struct Entry_int_Data {
+ GtkWidget *ptr;
+ struct Label_Data lbl;
+ int *num;
+ double min;
+ int64_t def;
+ double max;
+};
+
/******************************************************************************
******* variables ************************************************************
@@ -82,6 +102,11 @@ static void destroy_window (GtkWidget *widget,
/* Selection */
static void callback_button (GtkWidget *widget,
void *data);
+ /* Entries */
+static void callback_entry_dbl (GtkWidget *widget,
+ void *data);
+static void callback_entry_int (GtkWidget *widget,
+ void *data);
/* Text */
static void menu_gui_disclaim (void);
static void menu_gui_license (void);
@@ -243,8 +268,8 @@ static void destroy_window (GtkWidget *widget,
/* * * * * * * * * *
* * Selection * * * * * *
* * * * * * * * * */
-static void callback_button (GtkWidget *widget,
- void *data)
+static void callback_button (GtkWidget *widget,
+ void *data)
{
struct Button_Data *button;
@@ -256,6 +281,61 @@ static void callback_button (GtkWidget *widget,
}
/* * * * * * * * * *
+ * * Entries * * * * * * *
+ * * * * * * * * * */
+static void callback_entry_dbl (GtkWidget *widget,
+ void *data)
+{
+ struct Entry_dbl_Data *entry;
+ const char *str;
+ int err;
+ double R;
+ char buff [LINE_SIZE];
+
+ entry = ((struct Entry_dbl_Data *)data);
+
+ str = gtk_entry_get_text(GTK_ENTRY(entry->ptr));
+ err = alx_sscan_dbl(&R, entry->min, entry->def, entry->max, str);
+
+ if (err) {
+ snprintf(buff, LINE_SIZE, "Error %i", err);
+ gtk_entry_set_text(GTK_ENTRY(entry->ptr), buff);
+ gtk_editable_select_region(GTK_EDITABLE(entry->ptr),
+ 0, GTK_ENTRY(entry->ptr)->text_length);
+ } else {
+ *(entry->num) = R;
+ }
+
+ gtk_main_quit();
+}
+
+static void callback_entry_int (GtkWidget *widget,
+ void *data)
+{
+ struct Entry_int_Data *entry;
+ const char *str;
+ int err;
+ int64_t Z;
+ char buff [LINE_SIZE];
+
+ entry = ((struct Entry_int_Data *)data);
+
+ str = gtk_entry_get_text(GTK_ENTRY(entry->ptr));
+ err = alx_sscan_int(&Z, entry->min, entry->def, entry->max, str);
+
+ if (err) {
+ snprintf(buff, LINE_SIZE, "Error %i", err);
+ gtk_entry_set_text(GTK_ENTRY(entry->ptr), buff);
+ gtk_editable_select_region(GTK_EDITABLE(entry->ptr),
+ 0, GTK_ENTRY(entry->ptr)->text_length);
+ } else {
+ *(entry->num) = Z;
+ }
+
+ gtk_main_quit();
+}
+
+/* * * * * * * * * *
* * Text * * * * * * *
* * * * * * * * * */
static void menu_gui_disclaim (void)
@@ -714,48 +794,60 @@ static void menu_gui_custom (void)
int sw;
GtkWidget *separator[3];
struct Label_Data label;
- struct Button_Data button [4];
+ struct Button_Data button [1];
+ struct Entry_int_Data entry_int[2];
+ struct Entry_dbl_Data entry_dbl[1];
/* Text */
snprintf(label.text, LINE_SIZE, "Custom");
snprintf(button[0].text, LINE_SIZE, "[_0] Back");
/* Data */
- button[1].num = 1;
- button[2].num = 2;
- button[3].num = 3;
- button[0].num = 0;
- button[1].sw = &sw;
- button[2].sw = &sw;
- button[3].sw = &sw;
- button[0].sw = &sw;
+ entry_int[0].num = &menu_iface_variables.rows;
+ entry_int[0].min = 2;
+ entry_int[0].def = menu_iface_variables.rows;
+ entry_int[0].max = ROWS_GUI_MAX;
+ entry_int[1].num = &menu_iface_variables.cols;
+ entry_int[1].min = 2;
+ entry_int[1].def = menu_iface_variables.cols;
+ entry_int[1].max = COLS_GUI_MAX;
+ entry_dbl[0].num = &menu_iface_variables.p;
+ entry_dbl[0].min = 0;
+ entry_dbl[0].def = menu_iface_variables.p;
+ entry_dbl[0].max = 1;
+ button[0].num = 0;
+ button[0].sw = &sw;
/* Menu loop */
wh = true;
while (wh) {
+
/* Text */
- snprintf(button[1].text, LINE_SIZE, "[_1] Change rows: rows\t\t(%i)", menu_iface_variables.rows);
- snprintf(button[2].text, LINE_SIZE, "[_2] Change columns: cols\t(%i)", menu_iface_variables.cols);
- snprintf(button[3].text, LINE_SIZE, "[_3] Change proportion of mines: p\t(%lf)", menu_iface_variables.p);
+ snprintf(entry_int[0].lbl.text, LINE_SIZE, "Change rows: rows\t\t(%i)", menu_iface_variables.rows);
+ snprintf(entry_int[1].lbl.text, LINE_SIZE, "Change columns: cols\t(%i)", menu_iface_variables.cols);
+ snprintf(entry_dbl[0].lbl.text, LINE_SIZE, "Change proportion of mines: p\t(%lf)", menu_iface_variables.p);
/* Generate widgets */
- box = gtk_vbox_new(false, 0);
- label.ptr = gtk_label_new(label.text);
- separator[0] = gtk_hseparator_new();
- button[1].ptr = gtk_button_new_with_mnemonic(button[1].text);
- button[2].ptr = gtk_button_new_with_mnemonic(button[2].text);
- separator[1] = gtk_hseparator_new();
- button[3].ptr = gtk_button_new_with_mnemonic(button[3].text);
- separator[2] = gtk_hseparator_new();
- button[0].ptr = gtk_button_new_with_mnemonic(button[0].text);
+ box = gtk_vbox_new(false, 0);
+ label.ptr = gtk_label_new(label.text);
+ separator[0] = gtk_hseparator_new();
+ entry_int[0].lbl.ptr = gtk_label_new(entry_int[0].lbl.text);
+ entry_int[0].ptr = gtk_entry_new();
+ entry_int[1].lbl.ptr = gtk_label_new(entry_int[1].lbl.text);
+ entry_int[1].ptr = gtk_entry_new();
+ separator[1] = gtk_hseparator_new();
+ entry_dbl[0].lbl.ptr = gtk_label_new(entry_dbl[0].lbl.text);
+ entry_dbl[0].ptr = gtk_entry_new();
+ separator[2] = gtk_hseparator_new();
+ button[0].ptr = gtk_button_new_with_mnemonic(button[0].text);
/* Events */
- g_signal_connect(button[1].ptr, "clicked",
- G_CALLBACK(callback_button), (void *)&button[1]);
- g_signal_connect(button[2].ptr, "clicked",
- G_CALLBACK(callback_button), (void *)&button[2]);
- g_signal_connect(button[3].ptr, "clicked",
- G_CALLBACK(callback_button), (void *)&button[3]);
+ g_signal_connect(entry_int[0].ptr, "activate",
+ G_CALLBACK(callback_entry_int), (void *)&entry_int[0]);
+ g_signal_connect(entry_int[1].ptr, "activate",
+ G_CALLBACK(callback_entry_int), (void *)&entry_int[1]);
+ g_signal_connect(entry_dbl[0].ptr, "activate",
+ G_CALLBACK(callback_entry_dbl), (void *)&entry_dbl[0]);
g_signal_connect(button[0].ptr, "clicked",
G_CALLBACK(callback_button), (void *)&button[0]);
@@ -765,10 +857,13 @@ static void menu_gui_custom (void)
/* Box */
gtk_box_pack_start(GTK_BOX(box), label.ptr, false, false, 0);
gtk_box_pack_start(GTK_BOX(box), separator[0], false, false, 5);
- gtk_box_pack_start(GTK_BOX(box), button[1].ptr, true, true, 0);
- gtk_box_pack_start(GTK_BOX(box), button[2].ptr, true, true, 0);
+ gtk_box_pack_start(GTK_BOX(box), entry_int[0].lbl.ptr, true, true, 0);
+ gtk_box_pack_start(GTK_BOX(box), entry_int[0].ptr, true, true, 0);
+ gtk_box_pack_start(GTK_BOX(box), entry_int[1].lbl.ptr, true, true, 0);
+ gtk_box_pack_start(GTK_BOX(box), entry_int[1].ptr, true, true, 0);
gtk_box_pack_start(GTK_BOX(box), separator[1], false, false, 5);
- gtk_box_pack_start(GTK_BOX(box), button[3].ptr, true, true, 0);
+ gtk_box_pack_start(GTK_BOX(box), entry_dbl[0].lbl.ptr, true, true, 0);
+ gtk_box_pack_start(GTK_BOX(box), entry_dbl[0].ptr, true, true, 0);
gtk_box_pack_start(GTK_BOX(box), separator[2], false, false, 5);
gtk_box_pack_start(GTK_BOX(box), button[0].ptr, true, true, 0);
@@ -776,7 +871,7 @@ static void menu_gui_custom (void)
gtk_widget_show_all(window_gui);
/* GTK loop */
- sw = 0;
+ sw = 1;
gtk_main();
/* Selection */
@@ -784,17 +879,8 @@ static void menu_gui_custom (void)
case 0:
wh = false;
break;
- case 1:
-// menu_iface_variables.rows = alx_w_getint(w2, r2,
-// txt[sw - 1], 2, menu_iface_variables.rows, ROWS_TUI_MAX, NULL);
- break;
- case 2:
-// menu_iface_variables.cols = alx_w_getint(w2, r2,
-// txt[sw - 1], 2, menu_iface_variables.cols, COLS_TUI_MAX, NULL);
- break;
- case 3:
-// menu_iface_variables.p = alx_w_getdbl(w2, r2,
-// txt[sw - 1], 0, menu_iface_variables.p, 1, NULL);
+
+ default:
break;
}
diff --git a/modules/player/src/player_iface.c b/modules/player/src/player_iface.c
index 357594c..f736f2e 100644
--- a/modules/player/src/player_iface.c
+++ b/modules/player/src/player_iface.c
@@ -53,16 +53,12 @@ static int player_action;
******* static functions *****************************************************
******************************************************************************/
/* Actions */
-static void player_iface_act (struct Game_Iface_In *game_iface_in,
- int action);
+static void player_iface_act (struct Game_Iface_In *game_iface_in);
/* Actions: game iface */
-static void player_iface_act_start (int action);
-
-static void player_iface_act_play (struct Game_Iface_In *game_iface_in,
- int action);
-static void player_iface_act_game (struct Game_Iface_In *game_iface_in,
- int action);
+static void player_iface_act_start (void);
+static void player_iface_act_play (struct Game_Iface_In *game_iface_in);
+static void player_iface_act_game (struct Game_Iface_In *game_iface_in);
/* Actions: player iface */
static void player_iface_move_up (void);
@@ -127,7 +123,7 @@ int player_iface_start (int *pos_row, int *pos_col)
break;
}
- player_iface_act_start(player_action);
+ player_iface_act_start();
} while (player_action != PLAYER_IFACE_ACT_STEP &&
player_action != PLAYER_IFACE_ACT_QUIT);
@@ -206,7 +202,7 @@ void player_iface (const struct Game_Iface_Out *game_iface_out,
break;
}
- player_iface_act(game_iface_in, player_action);
+ player_iface_act(game_iface_in);
}
void player_iface_save_name (const char *filepath, char *filename, int destsize)
@@ -267,15 +263,14 @@ void player_iface_cleanup (void)
/* * * * * * * * * *
* * * Actions * * * * * *
* * * * * * * * * */
-static void player_iface_act (struct Game_Iface_In *game_iface_in,
- int player_action)
+static void player_iface_act (struct Game_Iface_In *game_iface_in)
{
switch (player_action) {
case PLAYER_IFACE_ACT_STEP:
case PLAYER_IFACE_ACT_FLAG:
case PLAYER_IFACE_ACT_FLAG_POSSIBLE:
case PLAYER_IFACE_ACT_RM_FLAG:
- player_iface_act_play(game_iface_in, player_action);
+ player_iface_act_play(game_iface_in);
break;
case PLAYER_IFACE_ACT_PAUSE:
@@ -286,7 +281,7 @@ static void player_iface_act (struct Game_Iface_In *game_iface_in,
case PLAYER_IFACE_ACT_XYZZY_P:
case PLAYER_IFACE_ACT_XYZZY_NP:
case PLAYER_IFACE_ACT_QUIT:
- player_iface_act_game(game_iface_in, player_action);
+ player_iface_act_game(game_iface_in);
break;
case PLAYER_IFACE_ACT_MOVE_UP:
@@ -311,7 +306,7 @@ static void player_iface_act (struct Game_Iface_In *game_iface_in,
}
}
-static void player_iface_act_start (int player_action)
+static void player_iface_act_start (void)
{
switch (player_action) {
case PLAYER_IFACE_ACT_STEP:
@@ -343,8 +338,7 @@ static void player_iface_act_start (int player_action)
/* * * * * * * * * *
* * * Actions: game iface * * * * *
* * * * * * * * * */
-static void player_iface_act_play (struct Game_Iface_In *game_iface_in,
- int player_action)
+static void player_iface_act_play (struct Game_Iface_In *game_iface_in)
{
switch (player_action) {
case PLAYER_IFACE_ACT_STEP:
@@ -371,8 +365,7 @@ static void player_iface_act_play (struct Game_Iface_In *game_iface_in,
game_iface_in->action = GAME_IFACE_ACT_PLAY;
}
-static void player_iface_act_game (struct Game_Iface_In *game_iface_in,
- int player_action)
+static void player_iface_act_game (struct Game_Iface_In *game_iface_in)
{
switch (player_action) {
case PLAYER_IFACE_ACT_PAUSE: