primitive button functions

This commit is contained in:
Alexandre 2025-02-17 22:29:10 +01:00
parent dab216810e
commit bb39b62df2
4 changed files with 122 additions and 12 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

View File

@ -42,7 +42,7 @@ void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, in
scale[0][0] = w; scale[0][0] = w;
scale[1][1] = h; scale[1][1] = h;
scale[2][2] = 0.5f+incr; scale[2][2] = 0.5f+incr;
incr += 0.001f; incr += 0.000001f;
glm_translate(slide, (vec3){x+w/2, y+h/2, 0.0f}); glm_translate(slide, (vec3){x+w/2, y+h/2, 0.0f});
@ -111,7 +111,7 @@ void gl_drawDigit(unsigned int fragShader, int n, float x, float y, float size,
// 7-segment display // 7-segment display
// y is the middle of the text // y is the middle of the text
// side can be -1 (aligned right) or 1 (aligned left) // side can be -1 (aligned right), 0 (centered) or 1 (aligned left)
void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side) { void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side) {
int left = n; int left = n;
float curx = x; float curx = x;
@ -120,6 +120,8 @@ void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size
} }
if(side == 1) { if(side == 1) {
curx += (((n==0)+ln_baseN(abs(n), 10)-1)*(size+4*width)); curx += (((n==0)+ln_baseN(abs(n), 10)-1)*(size+4*width));
} else if(side == 0) {
curx += (((n==0)+ln_baseN(abs(n), 10)-1)*(size+4*width))/2.0;
} }
while(left > 0) { while(left > 0) {
gl_drawDigit(fragShader, left%10, curx, y, size, r, g, b, width); gl_drawDigit(fragShader, left%10, curx, y, size, r, g, b, width);
@ -135,6 +137,22 @@ void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size
} }
} }
void gl_drawFloat(unsigned int fragShader, float n, float x, float y, float size, int r, int g, int b, float width, int side) {
gl_drawInteger(fragShader, (int)(n*1000.0f), x, y, size, r, g, b, width, side);
int left = n;
float curx = x;
if(left < 0) {
left *= (-1);
}
if(side == 1) {
curx += (((n==0)+ln_baseN(abs((int)n), 10)-1)*(size+4*width));
} else if(side == 0) {
curx += (((n==0)+ln_baseN(abs((int)n), 10)-1)*(size+4*width))/2.0;
}
curx -= (size+4*width)*3; // 0.001 rounding
gl_drawRect(fragShader, curx+size/2+2*width, y-size-width, 3*width, 3*width, r, g, b);
}
void gl_drawChar(unsigned int fragShader, char ch, float x, float y, float size, int r, int g, int b, float width) { void gl_drawChar(unsigned int fragShader, char ch, float x, float y, float size, int r, int g, int b, float width) {
if(ch == 'a' || ch == 'A') { if(ch == 'a' || ch == 'A') {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
@ -364,6 +382,8 @@ static int intListId;
static int* current_interface; static int* current_interface;
static bool noMousePoll;
int mn_get_color(int r, int g, int b) { int mn_get_color(int r, int g, int b) {
if(r+g+b <= 384) { if(r+g+b <= 384) {
return 255; return 255;
@ -403,7 +423,7 @@ void display_interface(int int_id, unsigned int fragShader, float mx, float my)
gl_drawString(fragShader, interfaceList[int_id].title, interfaceList[int_id].x+interfaceList[int_id].w/2.0f, interfaceList[int_id].y+interfaceList[int_id].h/2.0f, 0.7f*size, retcol, retcol, retcol, 0.7f*size/10.0f, 0); gl_drawString(fragShader, interfaceList[int_id].title, interfaceList[int_id].x+interfaceList[int_id].w/2.0f, interfaceList[int_id].y+interfaceList[int_id].h/2.0f, 0.7f*size, retcol, retcol, retcol, 0.7f*size/10.0f, 0);
for(int k = 0; k < interfaceList[int_id].nButtons; k++) { for(int k = 0; k < interfaceList[int_id].nButtons; k++) {
if(is_hovering_button(interfaceList[int_id].buttons[k], mx, my)) { if(((int)(sim_time*10.0))%2 == 0 && is_hovering_button(interfaceList[int_id].buttons[k], mx, my)) {
gl_drawRect(fragShader, buttonList[interfaceList[int_id].buttons[k]].x-0.015f, buttonList[interfaceList[int_id].buttons[k]].y-0.015f, buttonList[interfaceList[int_id].buttons[k]].w+0.03f, buttonList[interfaceList[int_id].buttons[k]].h+0.03f, 255, 255, 255); gl_drawRect(fragShader, buttonList[interfaceList[int_id].buttons[k]].x-0.015f, buttonList[interfaceList[int_id].buttons[k]].y-0.015f, buttonList[interfaceList[int_id].buttons[k]].w+0.03f, buttonList[interfaceList[int_id].buttons[k]].h+0.03f, 255, 255, 255);
} }
display_button(interfaceList[int_id].buttons[k], fragShader); display_button(interfaceList[int_id].buttons[k], fragShader);
@ -412,11 +432,12 @@ void display_interface(int int_id, unsigned int fragShader, float mx, float my)
} }
bool some_bool = true; bool some_bool = true;
unsigned int fShader;
void menu_actions(GLFWwindow *window, int button, int action, int mods) { void menu_actions(GLFWwindow *window, int button, int action, int mods) {
if(*current_interface < 0 || *current_interface > intListId) { if(*current_interface < 0 || *current_interface > intListId) {
//fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", *current_interface); //fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", *current_interface);
} else{ } else{
if(some_bool) { if(some_bool && !noMousePoll) {
some_bool = false; some_bool = false;
double mx = 0.0; double mx = 0.0;
double my = 0.0; double my = 0.0;
@ -424,20 +445,103 @@ void menu_actions(GLFWwindow *window, int button, int action, int mods) {
mx = 2.0*mx/1500.0 -1.0; mx = 2.0*mx/1500.0 -1.0;
my = 1.0 -2.0*my/1000.0; my = 1.0 -2.0*my/1000.0;
for(int k = 0; k < interfaceList[*current_interface].nButtons; k++) { for(int k = 0; k < interfaceList[*current_interface].nButtons; k++) {
if(is_hovering_button(interfaceList[*current_interface].buttons[k], (float)mx, (float)my)) { int but_id = interfaceList[*current_interface].buttons[k];
if(buttonList[interfaceList[*current_interface].buttons[k]].onClick != NULL) { if(is_hovering_button(but_id, (float)mx, (float)my)) {
(*buttonList[interfaceList[*current_interface].buttons[k]].onClick)(buttonList[interfaceList[*current_interface].buttons[k]].arg); if(buttonList[but_id].onClick != NULL) {
(*buttonList[but_id].onClick)(buttonList[but_id].arg);
} }
switch (buttonList[interfaceList[*current_interface].buttons[k]].type) { //float prevIncr = incr+0.0001f;
float size = minf(interfaceList[*current_interface].h/2.1f, (interfaceList[*current_interface].w)/(get_string_len(interfaceList[*current_interface].title)));
int retcol = mn_get_color(interfaceList[*current_interface].red, interfaceList[*current_interface].green, interfaceList[*current_interface].blue);
switch (buttonList[but_id].type) {
case WARP: case WARP:
*current_interface = *((int*)buttonList[interfaceList[*current_interface].buttons[k]].metadata); *current_interface = *((int*)buttonList[but_id].metadata);
break; break;
case NONE: case NONE:
break; break;
case SET_VAR: case SET_VAR:
/* TODO */ bool halt = false;
bool pressed = false;
bool clicked = false;
double toad = 0.0;
double mult = 1.0;
noMousePoll = true;
while(!halt) {
toad = 0.0;
mult = 1.0;
if(glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) {
pressed = true;
if(!clicked) {
clicked = true;
toad = 1.0;
}
}
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
pressed = true;
if(!clicked) {
clicked = true;
toad = -1.0;
}
}
if(glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
pressed = true;
if(!clicked) {
clicked = true;
mult = 2.0;
}
}
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
pressed = true;
if(!clicked) {
clicked = true;
mult = 0.5;
}
}
if(glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) {
halt = true;
}
if(!pressed) {
clicked = false;
}
fflush(stdout);
gl_drawRect(fShader, buttonList[but_id].x, buttonList[but_id].y, buttonList[but_id].w, buttonList[but_id].h, buttonList[but_id].red, buttonList[but_id].green, buttonList[but_id].blue);
switch (buttonList[but_id].cast) {
case INT:
int* valuei = (int*)buttonList[but_id].metadata;
*valuei = ((int)toad) + ((int)mult)*(*valuei);
gl_drawInteger(fShader, *valuei, buttonList[but_id].x+buttonList[but_id].w/2.0f, buttonList[but_id].y+buttonList[but_id].h/2.0f, size*0.7f, retcol, retcol, retcol, size/10.0f*0.7f, 0);
break;
case FLOAT:
float* valuef = (float*)buttonList[but_id].metadata;
*valuef = ((float)toad) + ((float)mult)*(*valuef);
gl_drawFloat(fShader, *valuef, buttonList[but_id].x+buttonList[but_id].w/2.0f, buttonList[but_id].y+buttonList[but_id].h/2.0f, size*0.7f, retcol, retcol, retcol, size/10.0f*0.7f, 0);
break;
case DOUBLE:
double* valued = (double*)buttonList[but_id].metadata;
*valued = ((double)toad) + ((double)mult)*(*valued);
printf("(%lf, %lf) %lf\n", toad, mult, *valued);
gl_drawFloat(fShader, (float)(*valued), buttonList[but_id].x+buttonList[but_id].w/2.0f, buttonList[but_id].y+buttonList[but_id].h/2.0f, size*0.7f, retcol, retcol, retcol, size/10.0f*0.7f, 0);
break;
default:
break;
}
glfwSwapBuffers(window);
glfwPollEvents();
pressed = false;
//incr = prevIncr;
//usleep(1000000/60);
}
printf("EXIT\n");
noMousePoll = false;
break; break;
case EXIT: case EXIT:
@ -447,6 +551,7 @@ void menu_actions(GLFWwindow *window, int button, int action, int mods) {
default: default:
break; break;
} }
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0); // just in case FOV is changed
break; break;
} }
} }
@ -457,6 +562,7 @@ void menu_actions(GLFWwindow *window, int button, int action, int mods) {
} }
bool isInMenu(GLFWwindow *win, unsigned int fragShader) { bool isInMenu(GLFWwindow *win, unsigned int fragShader) {
fShader = fragShader;
if(*current_interface == -1) { if(*current_interface == -1) {
return false; return false;
} else { } else {
@ -489,6 +595,7 @@ void init_interf(GLFWwindow *window) {
intListId = 0; intListId = 0;
current_interface = malloc(sizeof(int)); current_interface = malloc(sizeof(int));
*current_interface = -1; *current_interface = -1;
noMousePoll = false;
glfwSetMouseButtonCallback(window, menu_actions); glfwSetMouseButtonCallback(window, menu_actions);
} }
@ -610,13 +717,15 @@ void build_all_menus() {
interface_link_button(welcome_start_i, welcome_start_settings); interface_link_button(welcome_start_i, welcome_start_settings);
int settings_speed = button_create_onoff("speed", 0, 192, 192, -0.25f, 0.2f, 0.5f, 0.3f, SET_VAR, &speed, DOUBLE, NULL, NULL); int settings_speed = button_create_onoff("speed", 0, 192, 192, -0.25f, 0.55f, 0.5f, 0.3f, SET_VAR, &speed, DOUBLE, NULL, NULL);
int settings_sensitivity = button_create_onoff("sensibility", 192, 192, 0, -0.25f, -0.15f, 0.5f, 0.3f, SET_VAR, &sensitivity, DOUBLE, NULL, NULL); int settings_sensitivity = button_create_onoff("sensibility", 192, 192, 0, -0.25f, 0.2f, 0.5f, 0.3f, SET_VAR, &sensitivity, DOUBLE, NULL, NULL);
int settings_fov = button_create_onoff("fov", 192, 0, 192, -0.25f, -0.15f, 0.5f, 0.3f, SET_VAR, &fov, DOUBLE, NULL, NULL);
int settings_exit = button_create_onoff("back", 192, 64, 64, -0.25f, -0.5f, 0.5f, 0.3f, WARP, &zero, INT, NULL, NULL); int settings_exit = button_create_onoff("back", 192, 64, 64, -0.25f, -0.5f, 0.5f, 0.3f, WARP, &zero, INT, NULL, NULL);
int settings_i = interface_create("Settings", 128, 128, 128, -0.4f , 0.7f, 0.8f, 0.25f); int settings_i = interface_create("Settings", 128, 128, 128, -0.4f , 0.7f, 0.8f, 0.25f);
interface_link_button(settings_i, settings_speed); interface_link_button(settings_i, settings_speed);
interface_link_button(settings_i, settings_sensitivity); interface_link_button(settings_i, settings_sensitivity);
interface_link_button(settings_i, settings_fov);
interface_link_button(settings_i, settings_exit); interface_link_button(settings_i, settings_exit);
interface_set(welcome_start_i); interface_set(welcome_start_i);

View File

@ -42,6 +42,7 @@ void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, in
void gl_initDrawRect(unsigned int shaderProgram); void gl_initDrawRect(unsigned int shaderProgram);
void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side); void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side);
void gl_drawFloat(unsigned int fragShader, float n, float x, float y, float size, int r, int g, int b, float width, int side);
void gl_drawString(unsigned int fragShader, char* str, float x, float y, float size, int r, int g, int b, float width, int side); void gl_drawString(unsigned int fragShader, char* str, float x, float y, float size, int r, int g, int b, float width, int side);
void gl_printf(unsigned int fragShader, float x, float y, float size, float width, int r, int g, int b, const char* str, ...); void gl_printf(unsigned int fragShader, float x, float y, float size, float width, int r, int g, int b, const char* str, ...);