diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-12-02 14:20:48 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-12-02 14:29:13 +0100 |
commit | 14770f42df291753a240def91fb488a904a909c1 (patch) | |
tree | c1ef52c408cb365890da4aeb5536256040bb96fd /src |
Initial commit, set up skeleton with list and button
Diffstat (limited to 'src')
-rw-r--r-- | src/alloc.c | 25 | ||||
-rw-r--r-- | src/common.c | 6 | ||||
-rw-r--r-- | src/mgui/button.c | 55 | ||||
-rw-r--r-- | src/mgui/list.c | 65 | ||||
-rw-r--r-- | src/mgui/widget.c | 48 | ||||
-rw-r--r-- | src/resource_loader.c | 82 |
6 files changed, 281 insertions, 0 deletions
diff --git a/src/alloc.c b/src/alloc.c new file mode 100644 index 0000000..90682b5 --- /dev/null +++ b/src/alloc.c @@ -0,0 +1,25 @@ +#include "../include/alloc.h" +#include <stdlib.h> +#include <stdio.h> + +void* mgui_alloc(size_t size) { + void *mem = malloc(size); + if(!mem) { + fprintf(stderr, "mgui error: mgui_alloc failed to allocate %zu bytes\n", size); + abort(); + } + return mem; +} + +void* mgui_realloc(void *mem, size_t new_size) { + void *new_mem = realloc(mem, new_size); + if(!new_mem) { + fprintf(stderr, "mgui error: mgui_realloc failed to reallocate %p to %zu bytes\n", mem, new_size); + abort(); + } + return new_mem; +} + +void mgui_free(void *mem) { + free(mem); +} diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..74dcf44 --- /dev/null +++ b/src/common.c @@ -0,0 +1,6 @@ +#include "../include/common.h" + +bool mgui_rectangle_contains(mgl_vec2f rectangle_pos, mgl_vec2f rectangle_size, mgl_vec2f point) { + return point.x >= rectangle_pos.x && point.x <= rectangle_pos.x + rectangle_size.x + && point.y >= rectangle_pos.y && point.y <= rectangle_pos.y + rectangle_size.y; +} diff --git a/src/mgui/button.c b/src/mgui/button.c new file mode 100644 index 0000000..d8d0c58 --- /dev/null +++ b/src/mgui/button.c @@ -0,0 +1,55 @@ +#include "../../include/mgui/button.h" +#include "../../include/resource_loader.h" +#include "../../include/common.h" +#include "../../include/alloc.h" +#include <mgl/mgl.h> +#include <mgl/window/event.h> +#include <assert.h> + +mgui_button* mgui_button_create() { + mgui_button *button = mgui_alloc(sizeof(mgui_button)); + mgui_widget_init(&button->widget, MGUI_WIDGET_BUTTON); + button->background.position = (mgl_vec2f){ 0.0f, 0.0f }; + button->background.size = (mgl_vec2f){ 0.0f, 0.0f }; + button->background.color = (mgl_color){ 45, 45, 45, 255 }; + mgl_text_init(&button->label, mgui_get_font(MGUI_FONT_LATIN, 32), "Label", 5); + return button; +} + +mgui_widget* mgui_button_to_widget(mgui_button *list) { + return &list->widget; +} + +mgui_button* mgui_widget_to_button(mgui_widget *widget) { + assert(widget->type == MGUI_WIDGET_BUTTON); + return (mgui_button*)widget; +} + +void mgui_button_set_position(mgui_button *self, mgl_vec2i position) { + const mgl_vec2f position_f = (mgl_vec2f){ position.x, position.y }; + self->background.position = position_f; + mgl_text_set_position(&self->label, position_f); +} + +void mgui_button_on_event(mgui_button *self, mgl_window *window, mgl_event *event) { + // TODO: Implement + (void)window; + if(event->type == MGL_EVENT_MOUSE_MOVED) { + if(mgui_rectangle_contains(self->background.position, self->background.size, (mgl_vec2f){ event->mouse_move.x, event->mouse_move.y })) { + self->background.color = (mgl_color){ 70, 70, 70, 255 }; + } else { + self->background.color = (mgl_color){ 45, 45, 45, 255 }; + } + } else if(event->type == MGL_EVENT_MOUSE_BUTTON_PRESSED && event->mouse_button.button == MGL_BUTTON_LEFT) { + if(mgui_rectangle_contains(self->background.position, self->background.size, (mgl_vec2f){ event->mouse_button.x, event->mouse_button.y })) { + + } + } +} + +void mgui_button_draw(mgui_button *self, mgl_window *window) { + (void)window; + self->background.size = mgl_text_get_bounds(&self->label); + mgl_rectangle_draw(mgl_get_context(), &self->background); + mgl_text_draw(mgl_get_context(), &self->label); +} diff --git a/src/mgui/list.c b/src/mgui/list.c new file mode 100644 index 0000000..8dc4283 --- /dev/null +++ b/src/mgui/list.c @@ -0,0 +1,65 @@ +#include "../../include/mgui/list.h" +#include "../../include/alloc.h" +#include <mgl/mgl.h> +#include <assert.h> + +mgui_list* mgui_list_create(mgui_list_direction direction) { + mgui_list *list = mgui_alloc(sizeof(mgui_list)); + mgui_widget_init(&list->widget, MGUI_WIDGET_LIST); + list->direction = direction; + list->position = (mgl_vec2i){ 0, 0 }; + list->items = NULL; + list->num_items = 0; + list->items_capacity = 0; + return list; +} + +mgui_widget* mgui_list_to_widget(mgui_list *list) { + return &list->widget; +} + +mgui_list* mgui_widget_to_list(mgui_widget *widget) { + assert(widget->type == MGUI_WIDGET_LIST); + return (mgui_list*)widget; +} + +void mgui_list_set_position(mgui_list *self, mgl_vec2i position) { + self->position = position; +} + +static void mgui_list_ensure_capacity(mgui_list *self, size_t new_capacity) { + if(self->items_capacity >= new_capacity) + return; + + size_t capacity = self->items_capacity; + if(capacity == 0) + capacity = 8; + + while(capacity < new_capacity) { + capacity = capacity + (capacity >> 1); /* capacity *= 1.5 */ + } + + self->items = mgui_realloc(self->items, capacity); + self->items_capacity = capacity; +} + +void mgui_list_append(mgui_list *self, mgui_widget *widget) { + mgui_list_ensure_capacity(self, (self->num_items + 1) * sizeof(mgui_widget*)); + self->items[self->num_items] = widget; + ++self->num_items; +} + +void mgui_list_on_event(mgui_list *self, mgl_window *window, mgl_event *event) { + for(size_t i = 0; i < self->num_items; ++i) { + mgui_widget_on_event(self->items[i], window, event); + } +} + +void mgui_list_draw(mgui_list *self, mgl_window *window) { + mgl_context *context = mgl_get_context(); + context->gl.glTranslatef(self->position.x, self->position.y, 0.0f); + for(size_t i = 0; i < self->num_items; ++i) { + mgui_widget_draw(self->items[i], window); + } + context->gl.glLoadIdentity(); /* TODO: Remove, but what about glRotatef above */ +} diff --git a/src/mgui/widget.c b/src/mgui/widget.c new file mode 100644 index 0000000..242174d --- /dev/null +++ b/src/mgui/widget.c @@ -0,0 +1,48 @@ +#include "../../include/mgui/widget.h" +#include "../../include/mgui/list.h" +#include "../../include/mgui/button.h" + +void mgui_widget_init(mgui_widget *self, mgui_widget_type type) { + self->type = type; + mgui_widget_set_margin(self, 0, 0, 0, 0); +} + +void mgui_widget_set_margin(mgui_widget *self, int left, int top, int right, int bottom) { + self->margin.left = left; + self->margin.top = top; + self->margin.right = right; + self->margin.bottom = bottom; +} + +void mgui_widget_set_position(mgui_widget *self, mgl_vec2i position) { + switch(self->type) { + case MGUI_WIDGET_LIST: + mgui_list_set_position(mgui_widget_to_list(self), position); + break; + case MGUI_WIDGET_BUTTON: + mgui_button_set_position(mgui_widget_to_button(self), position); + break; + } +} + +void mgui_widget_on_event(mgui_widget *self, mgl_window *window, mgl_event *event) { + switch(self->type) { + case MGUI_WIDGET_LIST: + mgui_list_on_event(mgui_widget_to_list(self), window, event); + break; + case MGUI_WIDGET_BUTTON: + mgui_button_on_event(mgui_widget_to_button(self), window, event); + break; + } +} + +void mgui_widget_draw(mgui_widget *self, mgl_window *window) { + switch(self->type) { + case MGUI_WIDGET_LIST: + mgui_list_draw(mgui_widget_to_list(self), window); + break; + case MGUI_WIDGET_BUTTON: + mgui_button_draw(mgui_widget_to_button(self), window); + break; + } +} diff --git a/src/resource_loader.c b/src/resource_loader.c new file mode 100644 index 0000000..d589373 --- /dev/null +++ b/src/resource_loader.c @@ -0,0 +1,82 @@ +#include "../include/resource_loader.h" +#include "../include/alloc.h" +#include <mgl/system/fileutils.h> +#include <mgl/graphics/font.h> +#include <stdio.h> + +/* Note: max font size: 100 */ +static mgl_memory_mapped_file *font_file_cache[4]; +static mgl_font *font_cache[4][100]; + +/* TODO: If font fails to load, then create a new font with only a squares that represents all glyphs */ +/* TODO: Load default system font from fontconfig files */ +mgl_font* mgui_get_font(mgui_font_type type, unsigned int character_size) { + if(character_size > 0) + --character_size; + + /* TODO: Make this work for character size >= 100 */ + if(character_size >= 100) + character_size = 99; + + mgl_memory_mapped_file *font_file = font_file_cache[type]; + if(!font_file) { + const char *font_paths[2]; + size_t num_font_paths = 0; + + switch(type) { + case MGUI_FONT_LATIN: { + font_paths[0] = "/usr/share/fonts/noto/NotoSans-Regular.ttf"; + font_paths[1] = "/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf"; + num_font_paths = 2; + break; + } + case MGUI_FONT_LATIN_BOLD: { + font_paths[0] = "/usr/share/fonts/noto/NotoSans-Bold.ttf"; + font_paths[1] = "/usr/share/fonts/truetype/noto/NotoSans-Bold.ttf"; + num_font_paths = 2; + break; + } + case MGUI_FONT_CJK: { + font_paths[0] = "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttf"; + font_paths[1] = "/usr/share/fonts/truetype/noto-cjk/NotoSansCJK-Regular.ttf"; + num_font_paths = 2; + break; + } + } + + mgl_memory_mapped_file *new_font_file = mgui_alloc(sizeof(mgl_memory_mapped_file)); + + mgl_memory_mapped_file_load_options load_options = { + .readable = true, + .writable = false + }; + + bool successfully_loaded_file = false; + for(size_t i = 0; i < num_font_paths; ++i) { + if(mgl_mapped_file_load(font_paths[i], new_font_file, &load_options) == 0) { + successfully_loaded_file = true; + break; + } + } + + if(!successfully_loaded_file) + fprintf(stderr, "mgui warning: mgui_get_font failed to load font %d\n", (int)type); + + font_file_cache[type] = new_font_file; + font_file = new_font_file; + } + + if(!font_file) + return NULL; + + mgl_font *font = font_cache[type][character_size]; + if(!font) { + mgl_font *new_font = mgui_alloc(sizeof(mgl_font)); + if(mgl_font_load_from_file(new_font, font_file, character_size) != 0) + fprintf(stderr, "mgui warning: mgui_get_font failed to load font %d\n", (int)type); + + font_cache[type][character_size] = new_font; + font = new_font; + } + return font; +} |