2023-07-05 08:37:27 +00:00
|
|
|
#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>
|
|
|
|
|
2023-07-05 09:24:14 +00:00
|
|
|
#define BACK_COLOR 0.3, 0.35, 0.35
|
2023-07-06 08:24:46 +00:00
|
|
|
#define BACK_CLICKED_COLOR 0.25, 0.3, 0.3
|
2023-07-05 09:24:14 +00:00
|
|
|
|
2023-07-05 08:37:27 +00:00
|
|
|
void ui_button_draw(ui_button* b) {
|
|
|
|
int wWidth = glutGet(GLUT_WINDOW_WIDTH);
|
|
|
|
int wHeight = glutGet(GLUT_WINDOW_HEIGHT);
|
|
|
|
|
|
|
|
vec3 pos = vec3_add(b->position.absolute, vec3_new(b->position.relative.x * wWidth, b->position.relative.y * wHeight, 0.0));
|
|
|
|
vec3 ext = b->size;
|
2023-07-06 08:24:46 +00:00
|
|
|
glColorVec(b->clicked ? vec3_new(BACK_CLICKED_COLOR) : vec3_new(BACK_COLOR));
|
2023-07-05 08:37:27 +00:00
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glVertexVec(pos);
|
|
|
|
glVertex3f(pos.x + ext.x, pos.y, pos.z);
|
|
|
|
glVertexVec(vec3_add(pos, ext));
|
|
|
|
glVertex3f(pos.x, pos.y + ext.y, pos.z);
|
|
|
|
glEnd();
|
2023-07-05 09:24:14 +00:00
|
|
|
glColor3f(0.7, 0.7, 0.8);
|
|
|
|
glLineWidth(2.0);
|
|
|
|
glBegin(GL_LINE_LOOP);
|
|
|
|
glVertexVec(pos);
|
|
|
|
glVertex3f(pos.x + ext.x, pos.y, pos.z);
|
|
|
|
glVertexVec(vec3_add(pos, ext));
|
|
|
|
glVertex3f(pos.x, pos.y + ext.y, pos.z);
|
|
|
|
glEnd();
|
2023-07-05 08:37:27 +00:00
|
|
|
// Draw text if needed
|
|
|
|
if(b->text != NULL) {
|
2023-07-05 09:24:14 +00:00
|
|
|
glColor3f(1.0, 1.0, 1.0);
|
2023-07-05 08:37:27 +00:00
|
|
|
// Get text size
|
|
|
|
int textWidth = glutBitmapLength(GLUT_BITMAP_TIMES_ROMAN_24, (unsigned char*)b->text);
|
|
|
|
int textHeight = glutBitmapHeight(GLUT_BITMAP_TIMES_ROMAN_24);
|
|
|
|
// set raster position - not gonna take into account multiple line text buttons
|
|
|
|
vec3 rPos = vec3_add(pos, vec3_mult(ext, 0.5));
|
|
|
|
rPos.x -= textWidth * 0.5;
|
|
|
|
rPos.y -= textHeight * 0.25;
|
|
|
|
glRasterPos3f(rPos.x, rPos.y, rPos.z);
|
|
|
|
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (unsigned char*)b->text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:24:14 +00:00
|
|
|
char ui_button_mouse_over(ui_button* b, int mouseX, int mouseY) {
|
|
|
|
int wWidth = glutGet(GLUT_WINDOW_WIDTH);
|
|
|
|
int wHeight = glutGet(GLUT_WINDOW_HEIGHT);
|
|
|
|
|
|
|
|
vec3 pos = vec3_add(b->position.absolute, vec3_new(b->position.relative.x * wWidth, b->position.relative.y * wHeight, 0.0));
|
|
|
|
vec3 ext = b->size;
|
|
|
|
vec3 m = vec3_sub(vec3_new(mouseX, wHeight - mouseY, 0.0), pos);
|
|
|
|
return m.x > 0.0 && m.x < ext.x && m.y > 0.0 && m.y < ext.y;
|
2023-07-05 08:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ui_slider_draw(ui_slider* s) {
|
2023-07-05 09:24:14 +00:00
|
|
|
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 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);
|
2023-07-06 10:10:45 +00:00
|
|
|
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);
|
2023-07-05 09:24:14 +00:00
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-07-11 10:20:38 +00:00
|
|
|
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;
|
2023-07-05 09:24:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 08:24:46 +00:00
|
|
|
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);
|
2023-07-05 08:37:27 +00:00
|
|
|
|
2023-07-06 08:24:46 +00:00
|
|
|
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;
|
2023-07-05 08:37:27 +00:00
|
|
|
}
|