fixing buttons
This commit is contained in:
parent
cc7f169340
commit
cb5ee0de8a
BIN
obj/main.o
BIN
obj/main.o
Binary file not shown.
BIN
obj/menus.o
BIN
obj/menus.o
Binary file not shown.
|
@ -172,7 +172,7 @@ void processInput(GLFWwindow *window, float dtime) {
|
||||||
|
|
||||||
// reset
|
// reset
|
||||||
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
|
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
|
||||||
if(!rPress) {
|
if(!isMenuOpen() && !rPress) {
|
||||||
rPress = true;
|
rPress = true;
|
||||||
reset_everything(7, "templates/");
|
reset_everything(7, "templates/");
|
||||||
}
|
}
|
||||||
|
@ -246,11 +246,14 @@ int main_alt() {
|
||||||
glCullFace(GL_FRONT);
|
glCullFace(GL_FRONT);
|
||||||
glDepthFunc(GL_LESS);
|
glDepthFunc(GL_LESS);
|
||||||
|
|
||||||
|
//glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||||
|
|
||||||
init_csts();
|
init_csts();
|
||||||
init_hashtbl();
|
init_hashtbl();
|
||||||
init_ent_generator(10);
|
init_ent_generator(10);
|
||||||
init_proj();
|
init_proj();
|
||||||
init_interf();
|
init_interf();
|
||||||
|
build_all_menus();
|
||||||
parse_rooms(7, "templates/");
|
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_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
|
||||||
gl_drawData(shaderProgramR);
|
gl_drawData(shaderProgramR);
|
||||||
|
|
||||||
if(!isInMenu(shaderProgramR)) {
|
if(!isInMenu(window, shaderProgramR)) {
|
||||||
processInput(window, delta);
|
processInput(window, delta);
|
||||||
if(gamemode == 0) {
|
if(gamemode == 0) {
|
||||||
movePlayerG(delta);
|
movePlayerG(delta);
|
||||||
|
|
65
src/menus.c
65
src/menus.c
|
@ -356,7 +356,7 @@ static int MAX_INTERFACE_SIZE = 192;
|
||||||
static int bListId;
|
static int bListId;
|
||||||
static int intListId;
|
static int intListId;
|
||||||
|
|
||||||
static int* current_interface = NULL;
|
static int* current_interface;
|
||||||
|
|
||||||
void display_button(int but_id, unsigned int fragShader) {
|
void display_button(int but_id, unsigned int fragShader) {
|
||||||
if(but_id < 0 || but_id > bListId) {
|
if(but_id < 0 || but_id > bListId) {
|
||||||
|
@ -392,24 +392,39 @@ 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) {
|
if(int_id < 0 || int_id > intListId) {
|
||||||
fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", int_id);
|
fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", int_id);
|
||||||
} else if(false /* TODO : mouse is clicked */) {
|
} else if(
|
||||||
float mx = 0.0f;
|
glfwGetKey(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS
|
||||||
float my = 0.0f;
|
) {
|
||||||
/* TODO : get mouse position and state */
|
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++) {
|
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) {
|
switch (buttonList[interfaceList[int_id].buttons[k]].type) {
|
||||||
case WARP:
|
case WARP:
|
||||||
*current_interface = *buttonList[interfaceList[int_id].buttons[k]].metadata;
|
*current_interface = *buttonList[interfaceList[int_id].buttons[k]].metadata;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case NONE:
|
||||||
|
break;
|
||||||
|
|
||||||
case SET_VAR:
|
case SET_VAR:
|
||||||
/* TODO */
|
/* TODO */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EXIT:
|
case EXIT:
|
||||||
current_interface = NULL;
|
*current_interface = -1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -420,16 +435,21 @@ void menu_actions(int int_id) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isInMenu(unsigned int fragShader) {
|
bool isInMenu(GLFWwindow *win, unsigned int fragShader) {
|
||||||
if(current_interface == NULL) {
|
if(*current_interface == -1) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
//printf("displaying %d\n", *current_interface);
|
||||||
display_interface(*current_interface, fragShader);
|
display_interface(*current_interface, fragShader);
|
||||||
menu_actions(*current_interface);
|
menu_actions(win, *current_interface);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isMenuOpen() {
|
||||||
|
return (*current_interface != -1);
|
||||||
|
}
|
||||||
|
|
||||||
void init_interf() {
|
void init_interf() {
|
||||||
buttonList = malloc(sizeof(onoff_button)*MAX_BUTTON_SIZE);
|
buttonList = malloc(sizeof(onoff_button)*MAX_BUTTON_SIZE);
|
||||||
for(int k = 0; k < MAX_BUTTON_SIZE; k++) {
|
for(int k = 0; k < MAX_BUTTON_SIZE; k++) {
|
||||||
|
@ -441,10 +461,14 @@ void init_interf() {
|
||||||
}
|
}
|
||||||
bListId = 0;
|
bListId = 0;
|
||||||
intListId = 0;
|
intListId = 0;
|
||||||
|
current_interface = malloc(sizeof(int));
|
||||||
|
*current_interface = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the ID of the new button (-1 if error)
|
// 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) {
|
if(bListId < MAX_BUTTON_SIZE) {
|
||||||
|
|
||||||
buttonList[bListId].id = bListId;
|
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].type = actn;
|
||||||
buttonList[bListId].metadata = val;
|
buttonList[bListId].metadata = val;
|
||||||
|
|
||||||
|
buttonList[bListId].onClick = onClick;
|
||||||
|
buttonList[bListId].arg = arg;
|
||||||
|
|
||||||
bListId += 1;
|
bListId += 1;
|
||||||
return (bListId-1);
|
return (bListId-1);
|
||||||
} else {
|
} else {
|
||||||
|
@ -538,9 +565,23 @@ void interface_set(int interface_id) {
|
||||||
*current_interface = 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() {
|
void free_interf() {
|
||||||
free(buttonList);
|
free(buttonList);
|
||||||
free(interfaceList);
|
free(interfaceList);
|
||||||
|
free(current_interface);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
14
src/menus.h
14
src/menus.h
|
@ -1,7 +1,7 @@
|
||||||
#ifndef MENUS_H
|
#ifndef MENUS_H
|
||||||
#define 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 {
|
typedef struct onoff_button {
|
||||||
int id;
|
int id;
|
||||||
|
@ -15,6 +15,11 @@ typedef struct onoff_button {
|
||||||
|
|
||||||
// the value to change (if SET_VAR) or the destination interface (if WARP)
|
// the value to change (if SET_VAR) or the destination interface (if WARP)
|
||||||
int* metadata;
|
int* metadata;
|
||||||
|
|
||||||
|
// is called upon clicking button
|
||||||
|
// this function is executed before metadata is used
|
||||||
|
void (*onClick)(void* args);
|
||||||
|
void* arg;
|
||||||
} onoff_button;
|
} onoff_button;
|
||||||
|
|
||||||
typedef struct interface {
|
typedef struct interface {
|
||||||
|
@ -40,14 +45,17 @@ void gl_printf(unsigned int fragShader, float x, float y, float size, float widt
|
||||||
|
|
||||||
void init_interf();
|
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);
|
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_link_button(int interface_id, int button_id);
|
||||||
void interface_unlink_button(int interface_id, int button_id);
|
void interface_unlink_button(int interface_id, int button_id);
|
||||||
void interface_set(int interface_id);
|
void interface_set(int interface_id);
|
||||||
|
|
||||||
|
void build_all_menus();
|
||||||
|
|
||||||
void free_interf();
|
void free_interf();
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue