fixing buttons

This commit is contained in:
Alexandre 2025-02-17 10:42:25 +01:00
parent cc7f169340
commit cb5ee0de8a
6 changed files with 80 additions and 28 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -172,7 +172,7 @@ void processInput(GLFWwindow *window, float dtime) {
// reset
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
if(!rPress) {
if(!isMenuOpen() && !rPress) {
rPress = true;
reset_everything(7, "templates/");
}
@ -246,11 +246,14 @@ int main_alt() {
glCullFace(GL_FRONT);
glDepthFunc(GL_LESS);
//glfwSetMouseButtonCallback(window, mouse_button_callback);
init_csts();
init_hashtbl();
init_ent_generator(10);
init_proj();
init_interf();
build_all_menus();
parse_rooms(7, "templates/");
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
@ -412,7 +415,7 @@ int main_alt() {
gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
gl_drawData(shaderProgramR);
if(!isInMenu(shaderProgramR)) {
if(!isInMenu(window, shaderProgramR)) {
processInput(window, delta);
if(gamemode == 0) {
movePlayerG(delta);

View File

@ -356,7 +356,7 @@ static int MAX_INTERFACE_SIZE = 192;
static int bListId;
static int intListId;
static int* current_interface = NULL;
static int* current_interface;
void display_button(int but_id, unsigned int fragShader) {
if(but_id < 0 || but_id > bListId) {
@ -392,44 +392,64 @@ bool is_button_pressed(int but_id, float mx, float my) {
}
}
void menu_actions(int int_id) {
void menu_actions(GLFWwindow *window, int int_id) {
printf("%d\n", glfwGetKey(window, GLFW_MOUSE_BUTTON_LEFT));
if(int_id < 0 || int_id > intListId) {
fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", int_id);
} else if(false /* TODO : mouse is clicked */) {
float mx = 0.0f;
float my = 0.0f;
/* TODO : get mouse position and state */
} else if(
glfwGetKey(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS
) {
double mx = 0.0;
double my = 0.0;
glfwGetCursorPos(window, &mx, &my);
mx = 2.0*mx/1500.0 -1.0;
my = 2.0*my/1000.0 -1.0;
//printf("EE %lf, %lf\n", mx, my);
//fflush(stdout);
for(int k = 0; k < interfaceList[int_id].nButtons; k++) {
if(is_button_pressed(interfaceList[int_id].buttons[k], mx, my)) {
if(is_button_pressed(interfaceList[int_id].buttons[k], (float)mx, (float)my)) {
if(buttonList[interfaceList[int_id].buttons[k]].onClick != NULL) {
(*buttonList[interfaceList[int_id].buttons[k]].onClick)(buttonList[interfaceList[int_id].buttons[k]].arg);
}
switch (buttonList[interfaceList[int_id].buttons[k]].type) {
case WARP:
*current_interface = *buttonList[interfaceList[int_id].buttons[k]].metadata;
break;
case SET_VAR:
/* TODO */
break;
case EXIT:
current_interface = NULL;
break;
case WARP:
*current_interface = *buttonList[interfaceList[int_id].buttons[k]].metadata;
break;
default:
break;
case NONE:
break;
case SET_VAR:
/* TODO */
break;
case EXIT:
*current_interface = -1;
break;
default:
break;
}
}
}
}
}
bool isInMenu(unsigned int fragShader) {
if(current_interface == NULL) {
bool isInMenu(GLFWwindow *win, unsigned int fragShader) {
if(*current_interface == -1) {
return false;
} else {
//printf("displaying %d\n", *current_interface);
display_interface(*current_interface, fragShader);
menu_actions(*current_interface);
menu_actions(win, *current_interface);
return true;
}
}
bool isMenuOpen() {
return (*current_interface != -1);
}
void init_interf() {
buttonList = malloc(sizeof(onoff_button)*MAX_BUTTON_SIZE);
for(int k = 0; k < MAX_BUTTON_SIZE; k++) {
@ -441,10 +461,14 @@ void init_interf() {
}
bListId = 0;
intListId = 0;
current_interface = malloc(sizeof(int));
*current_interface = -1;
}
// returns the ID of the new button (-1 if error)
int button_create_onoff(char* text, int red, int green, int blue, float x, float y, float w, float h, button_action actn, int* val) {
// actn can be within {NONE, WARP, SET_VAR, EXIT}
// if actn is WARP or SET_VAR then val shall not be NULL
int button_create_onoff(char* text, int red, int green, int blue, float x, float y, float w, float h, button_action actn, int* val, void (*onClick)(void*), void* arg) {
if(bListId < MAX_BUTTON_SIZE) {
buttonList[bListId].id = bListId;
@ -461,6 +485,9 @@ int button_create_onoff(char* text, int red, int green, int blue, float x, float
buttonList[bListId].type = actn;
buttonList[bListId].metadata = val;
buttonList[bListId].onClick = onClick;
buttonList[bListId].arg = arg;
bListId += 1;
return (bListId-1);
} else {
@ -538,9 +565,23 @@ void interface_set(int interface_id) {
*current_interface = interface_id;
}
/*
*/
// build and link everything here
void build_all_menus() {
int welcome_start_b = button_create_onoff("start", 32, 255, 255, -0.25f, -0.25f, 0.5f, 0.5f, EXIT, NULL, NULL, NULL);
int welcome_start_i = interface_create("Welcome", 128, 128, 128, -0.4f, 0.7f, 0.8f, 0.25f);
//printf("%d %d\n", welcome_start_i, welcome_start_b);
interface_link_button(welcome_start_i, welcome_start_b);
interface_set(welcome_start_i);
//printf("------------- %d\n", *current_interface);
}
void free_interf() {
free(buttonList);
free(interfaceList);
free(current_interface);
}
/*

View File

@ -1,7 +1,7 @@
#ifndef MENUS_H
#define MENUS_H
typedef enum button_action {WARP, SET_VAR, EXIT} button_action ;
typedef enum button_action {NONE, WARP, SET_VAR, EXIT} button_action ;
typedef struct onoff_button {
int id;
@ -15,6 +15,11 @@ typedef struct onoff_button {
// the value to change (if SET_VAR) or the destination interface (if WARP)
int* metadata;
// is called upon clicking button
// this function is executed before metadata is used
void (*onClick)(void* args);
void* arg;
} onoff_button;
typedef struct interface {
@ -40,14 +45,17 @@ void gl_printf(unsigned int fragShader, float x, float y, float size, float widt
void init_interf();
bool isInMenu(unsigned int fragShader);
bool isInMenu(GLFWwindow *win, unsigned int fragShader);
bool isMenuOpen();
int button_create_onoff(char* text, int red, int green, int blue, float x, float y, float w, float h, button_action actn, int* val);
int button_create_onoff(char* text, int red, int green, int blue, float x, float y, float w, float h, button_action actn, int* val, void (*onClick)(void*), void* arg);
int interface_create(char* title, int red, int green, int blue, float x, float y, float w, float h);
void interface_link_button(int interface_id, int button_id);
void interface_unlink_button(int interface_id, int button_id);
void interface_set(int interface_id);
void build_all_menus();
void free_interf();
#endif