WIP(zephr): window icon. basic ui button

- zephr: added ability to assign an icon to a window
- zephr: added a basic ui button with hover and click states and cursor
  change
This commit is contained in:
2023-12-17 03:52:35 +02:00
parent 499591162e
commit 2fc8ac6456
9 changed files with 8202 additions and 27 deletions
+57 -4
View File
@@ -9,10 +9,7 @@ Shader ui_shader;
unsigned int ui_vao;
unsigned int ui_vbo;
int init_ui(const char* font_path, Size window_size) {
zephr_ctx->window.size = window_size;
zephr_ctx->projection = orthographic_projection_2d(0.f, window_size.width, window_size.height, 0.f);
int init_ui(const char* font_path) {
int res = init_fonts(font_path);
if (res == -1) {
printf("[ERROR]: could not initialize freetype library\n");
@@ -122,6 +119,11 @@ void apply_constraints(UIConstraints *constraints, Vec2f *pos, Sizef *size) {
*size = (Sizef){constraints->width, constraints->height};
}
void apply_constraints_rect(UIConstraints *constraints, Rect *rect) {
rect->pos = (Vec2f){constraints->x, constraints->y};
rect->size = (Sizef){constraints->width, constraints->height};
}
void apply_alignment(Alignment align, UIConstraints *constraints, Vec2f *pos, Sizef size) {
// if we don't have a parent then we're a top level element and we should align against the window
Sizef parent_size = (Sizef){zephr_ctx->window.size.width, zephr_ctx->window.size.height};
@@ -269,6 +271,57 @@ void draw_triangle(UIConstraints *constraints, const Color color, Alignment alig
glBindVertexArray(0);
}
bool inside_rect(Rect *rect, Vec2 *point) {
if (point->x >= rect->pos.x && point->x <= rect->pos.x + rect->size.width &&
point->y >= rect->pos.y && point->y <= rect->pos.y + rect->size.height) {
return true;
}
return false;
}
bool draw_button(UIConstraints *constraints, const Color color, const char *text, f32 radius, Alignment align) {
Rect rect = {0};
apply_constraints(constraints, &rect.pos, &rect.size);
apply_alignment(align, constraints, &rect.pos, rect.size);
if (inside_rect(&rect, &zephr_ctx->mouse.pos)) {
zephr_set_cursor(ZEPHR_CURSOR_HAND);
if (zephr_ctx->mouse.pressed) {
Color new_color = mult_color(color, 0.8f);
draw_quad(constraints, new_color, radius, align);
} else {
Color new_color = mult_color(color, 0.9f);
draw_quad(constraints, new_color, radius, align);
}
} else {
zephr_set_cursor(ZEPHR_CURSOR_ARROW);
draw_quad(constraints, color, radius, align);
}
if (text) {
UIConstraints text_constraints = {0};
set_parent_constraint(&text_constraints, constraints);
set_x_constraint(&text_constraints, 0, UI_CONSTRAINT_RELATIVE_PIXELS);
set_y_constraint(&text_constraints, 0, UI_CONSTRAINT_RELATIVE_PIXELS);
set_width_constraint(&text_constraints, 1.5, UI_CONSTRAINT_RELATIVE_PIXELS);
u8 font_size = (u8)(constraints->width * 0.2 / text_constraints.width * 0.2 * 100.f);
// TODO: need to adjust the font size based on the text height
draw_text(text, font_size, text_constraints, COLOR_BLACK, ALIGN_CENTER);
}
if (inside_rect(&rect, &zephr_ctx->mouse.pos) && zephr_ctx->mouse.released && zephr_ctx->mouse.button == ZEPHR_MOUSE_BUTTON_LEFT) {
return true;
}
return false;
}
/* enum Element { */
/* Rect, */
/* Circle, */