world_of_cow/ui.c
2023-08-09 18:27:55 +03:00

53 lines
2.2 KiB
C

#include "ui.h"
#include "vec.h"
#include <GL/freeglut_std.h>
#include <GL/freeglut_ext.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
// originally i also had a ui button, but the right click menu made it much simpler, but that was to allow easily changing the colors for both buttons and sliders
#define BACK_COLOR 0.3, 0.35, 0.35
void ui_slider_draw(ui_slider* s) {
int wWidth = glutGet(GLUT_WINDOW_WIDTH);
int wHeight = glutGet(GLUT_WINDOW_HEIGHT);
// calculate position based on the different ui_pos items
vec3 pos = vec3_add(s->position.absolute, vec3_new(s->position.relative.x * wWidth, s->position.relative.y * wHeight, 0.0));
vec3 ext = s->size;
// center position
vec3 slider = vec3_add(pos, vec3_new(ext.x * s->value, ext.y * 0.5, 0.0));
glBegin(GL_QUADS);
// draw background line
glColor3f(BACK_COLOR);
glVertexVec(pos);
glVertex3f(pos.x + ext.x, pos.y, pos.z);
glVertex3f(pos.x + ext.x, pos.y + ext.y, pos.z);
glVertex3f(pos.x, pos.y + ext.y, pos.z);
// Draw slider button thing
glColor3f(1.0, 1.0, 1.0);
glVertex3f(slider.x - ext.z, slider.y - ext.z, 0.0);
glVertex3f(slider.x + ext.z, slider.y - ext.z, 0.0);
glVertex3f(slider.x + ext.z, slider.y + ext.z, 0.0);
glVertex3f(slider.x - ext.z, slider.y + ext.z, 0.0);
glEnd();
}
// checks if the mouse is hovering over the slider
char ui_slider_mouse_over(ui_slider* s, int mouseX, int mouseY) {
int wWidth = glutGet(GLUT_WINDOW_WIDTH);
int wHeight = glutGet(GLUT_WINDOW_HEIGHT);
vec3 pos = vec3_add(s->position.absolute, vec3_new(s->position.relative.x * wWidth, s->position.relative.y * wHeight, 0.0));
vec3 ext = s->size;
vec3 m = vec3_sub(vec3_new(mouseX, wHeight - mouseY - 0.5 * ext.y, 0.0), pos);
return m.x > 0.0 && m.x < ext.x && m.y > -ext.z && m.y < ext.z;
}
void ui_slider_onclick(ui_slider* s, int mouseX, int mouseY) {
// we assume mouse is already hovering
int wWidth = glutGet(GLUT_WINDOW_WIDTH);
int wHeight = glutGet(GLUT_WINDOW_HEIGHT);
vec3 pos = vec3_add(s->position.absolute, vec3_new(s->position.relative.x * wWidth, s->position.relative.y * wHeight, 0.0));
s->value = ((float)mouseX - pos.x) / s->size.x;
}