diff options
author | Marvin Borner | 2020-10-25 21:42:51 +0100 |
---|---|---|
committer | Marvin Borner | 2020-10-25 21:42:51 +0100 |
commit | 4ae48304b2290b6c835eb3d937bd5e905ce0e5d4 (patch) | |
tree | a1d6edb02667fb76c2ce03193ccc76ada08ea9f4 | |
parent | 4ec7c19e1567f322b1622ad506290e8eb7a4956d (diff) |
Added on-demand font loading
-rw-r--r-- | apps/window.c | 2 | ||||
-rw-r--r-- | apps/wm.c | 8 | ||||
-rw-r--r-- | libgui/gfx.c | 77 | ||||
-rw-r--r-- | libgui/gui.c | 26 | ||||
-rw-r--r-- | libgui/inc/gfx.h | 15 | ||||
-rw-r--r-- | libgui/inc/gui.h | 14 |
6 files changed, 104 insertions, 38 deletions
diff --git a/apps/window.c b/apps/window.c index ffd860d..f8e2723 100644 --- a/apps/window.c +++ b/apps/window.c @@ -19,7 +19,7 @@ int main() struct element *container = gui_init("test", 0, 0); struct element_button *button = - gui_add_button(container, 10, 10, 100, 20, "hallo", COLOR_RED); + gui_add_button(container, 10, 10, FONT_24, "Baum!", COLOR_WHITE, COLOR_BLACK); button->on_click = on_click; @@ -4,6 +4,7 @@ #include <cpu.h> #include <def.h> #include <gfx.h> +#include <gui.h> #include <input.h> #include <keymap.h> #include <list.h> @@ -203,7 +204,6 @@ int main(int argc, char **argv) printf("VBE: %dx%d\n", vbe.width, vbe.height); keymap = keymap_parse("/res/keymaps/en.keymap"); - gfx_init("/font/spleen-16x32.psfu"); contexts = list_new(); new_context(&root, pid, 0, 0, vbe.width, vbe.height, @@ -215,9 +215,9 @@ int main(int argc, char **argv) direct.fb = vbe.fb; list_add(contexts, &root); - gfx_fill(&direct, COLOR_BG); - gfx_write(&direct, 0, 0, COLOR_FG, "Welcome to Melvix!"); - gfx_write(&direct, 0, 32, COLOR_FG, "Loading resources..."); + //gfx_fill(&direct, COLOR_BG); + //gfx_write(&direct, 0, 0, FONT_32, COLOR_FG, "Welcome to Melvix!"); + //gfx_write(&direct, 0, 32, FONT_32, COLOR_FG, "Loading resources..."); gfx_fill(&root, COLOR_FG); //gfx_border(&root, COLOR_FG, 2); diff --git a/libgui/gfx.c b/libgui/gfx.c index 30c10e3..7e52389 100644 --- a/libgui/gfx.c +++ b/libgui/gfx.c @@ -11,9 +11,52 @@ #include <sys.h> #include <vesa.h> -struct font *font; +// TODO: Move to some global config file +#define FONT_COUNT 6 +#define FONT_8_PATH "/font/spleen-5x8.psfu" +#define FONT_12_PATH "/font/spleen-6x12.psfu" +#define FONT_16_PATH "/font/spleen-8x16.psfu" +#define FONT_24_PATH "/font/spleen-12x24.psfu" +#define FONT_32_PATH "/font/spleen-16x32.psfu" +#define FONT_64_PATH "/font/spleen-32x64.psfu" + +struct font *fonts[FONT_COUNT]; + +static void load_font(enum font_type font_type) +{ + if (fonts[font_type]) + return; + + char *path = NULL; + + switch (font_type) { + case FONT_8: + path = FONT_8_PATH; + break; + case FONT_12: + path = FONT_12_PATH; + break; + case FONT_16: + path = FONT_16_PATH; + break; + case FONT_24: + path = FONT_24_PATH; + break; + case FONT_32: + path = FONT_32_PATH; + break; + case FONT_64: + path = FONT_64_PATH; + break; + default: + break; + } + + fonts[font_type] = psf_parse(read(path)); + assert(fonts[font_type]); +} -static void write_char(struct context *ctx, int x, int y, u32 c, char ch) +static void write_char(struct context *ctx, int x, int y, struct font *font, u32 c, char ch) { int bypp = ctx->bpp >> 3; @@ -51,16 +94,26 @@ static void draw_rectangle(struct context *ctx, int x1, int y1, int x2, int y2, } } -void gfx_write_char(struct context *ctx, int x, int y, u32 c, char ch) +// On-demand font loading +struct font *gfx_resolve_font(enum font_type font_type) { - write_char(ctx, x, y, c, ch); + if (!fonts[font_type]) + load_font(font_type); + return fonts[font_type]; +} + +void gfx_write_char(struct context *ctx, int x, int y, enum font_type font_type, u32 c, char ch) +{ + struct font *font = gfx_resolve_font(font_type); + write_char(ctx, x, y, font, c, ch); gfx_redraw(); } -void gfx_write(struct context *ctx, int x, int y, u32 c, char *text) +void gfx_write(struct context *ctx, int x, int y, enum font_type font_type, u32 c, char *text) { + struct font *font = gfx_resolve_font(font_type); for (u32 i = 0; i < strlen(text); i++) { - write_char(ctx, x + i * font->width, y, c, text[i]); + write_char(ctx, x + i * font->width, y, font, c, text[i]); } gfx_redraw(); } @@ -167,18 +220,14 @@ void gfx_border(struct context *ctx, u32 c, u32 width) gfx_redraw(); } -int gfx_font_height() +int gfx_font_height(enum font_type font_type) { + struct font *font = gfx_resolve_font(font_type); return font->height; } -int gfx_font_width() +int gfx_font_width(enum font_type font_type) { + struct font *font = gfx_resolve_font(font_type); return font->width; } - -void gfx_init(char *font_path) -{ - font = psf_parse(read(font_path)); - assert(font); -} diff --git a/libgui/gui.c b/libgui/gui.c index aa5c494..024c5d7 100644 --- a/libgui/gui.c +++ b/libgui/gui.c @@ -7,6 +7,7 @@ #include <list.h> #include <mem.h> #include <print.h> +#include <str.h> #include <sys.h> #define MAX_WINDOWS 10 @@ -72,28 +73,40 @@ static struct element *element_at(struct element *container, int x, int y) return ret; } -struct element_button *gui_add_button(struct element *container, int x, int y, u32 width, - u32 height, const char *text, u32 color) +void gui_sync_button(struct element *elem) +{ + struct element_button *button = elem->data; + gfx_fill(elem->ctx, button->color_bg); + gfx_write(elem->ctx, 0, 0, button->font_type, button->color_fg, button->text); +} + +struct element_button *gui_add_button(struct element *container, int x, int y, + enum font_type font_type, char *text, u32 color_bg, + u32 color_fg) { if (!container || !container->childs) return NULL; + gfx_resolve_font(font_type); + struct element *button = malloc(sizeof(*button)); button->type = GUI_TYPE_BUTTON; button->window_id = container->window_id; button->ctx = malloc(sizeof(*button->ctx)); button->ctx->x = x; button->ctx->y = y; - button->ctx->width = width; - button->ctx->height = height; + button->ctx->width = strlen(text) * gfx_font_width(font_type); + button->ctx->height = gfx_font_height(font_type); button->ctx->flags = WF_RELATIVE; button->childs = list_new(); button->data = malloc(sizeof(struct element_button)); ((struct element_button *)button->data)->text = text; - ((struct element_button *)button->data)->color = color; + ((struct element_button *)button->data)->color_fg = color_fg; + ((struct element_button *)button->data)->color_bg = color_bg; + ((struct element_button *)button->data)->font_type = font_type; gfx_new_ctx(button->ctx); - gfx_fill(button->ctx, color); list_add(container->childs, button); + gui_sync_button(button); merge_elements(container); return button->data; @@ -138,7 +151,6 @@ struct element *gui_init(const char *title, u32 width, u32 height) return NULL; gfx_fill(win->ctx, COLOR_BG); - gfx_init("/font/spleen-12x24.psfu"); struct element *container = malloc(sizeof(*container)); container->type = GUI_TYPE_CONTAINER; diff --git a/libgui/inc/gfx.h b/libgui/inc/gfx.h index 2ce588c..5010650 100644 --- a/libgui/inc/gfx.h +++ b/libgui/inc/gfx.h @@ -5,7 +5,6 @@ #define GFX_H #include <def.h> -#include <gui.h> #include <sys.h> #include <vesa.h> @@ -39,7 +38,9 @@ #define WF_NO_RESIZE (1 << 2) #define WF_RELATIVE (1 << 3) -enum message_type { GFX_NEW_CONTEXT = EVENT_MAX + 1, GFX_REDRAW }; +enum font_type { FONT_8, FONT_12, FONT_16, FONT_24, FONT_32, FONT_64 }; + +enum message_type { GFX_NEW_CONTEXT = EVENT_MAX + 1, GFX_REDRAW, GFX_MAX }; // Generalized font struct struct font { @@ -61,8 +62,9 @@ struct context { int flags; }; -void gfx_write_char(struct context *ctx, int x, int y, u32 c, char ch); -void gfx_write(struct context *ctx, int x, int y, u32 c, char *text); +struct font *gfx_resolve_font(enum font_type font_type); +void gfx_write_char(struct context *ctx, int x, int y, enum font_type font_type, u32 c, char ch); +void gfx_write(struct context *ctx, int x, int y, enum font_type font_type, u32 c, char *text); void gfx_load_image(struct context *ctx, char *path, int x, int y); void gfx_load_wallpaper(struct context *ctx, char *path); void gfx_copy(struct context *dest, struct context *src, int x, int y, u32 width, u32 height); @@ -70,10 +72,9 @@ void gfx_ctx_on_ctx(struct context *dest, struct context *src, int x, int y); void gfx_draw_rectangle(struct context *ctx, int x1, int y1, int x2, int y2, u32 c); void gfx_fill(struct context *ctx, u32 c); void gfx_border(struct context *ctx, u32 c, u32 width); -void gfx_init(char *font_path); -int gfx_font_height(); -int gfx_font_width(); +int gfx_font_height(enum font_type); +int gfx_font_width(enum font_type); /** * Wrappers diff --git a/libgui/inc/gui.h b/libgui/inc/gui.h index e7573f8..8149381 100644 --- a/libgui/inc/gui.h +++ b/libgui/inc/gui.h @@ -11,18 +11,21 @@ #define MAX_CHILDS 100 // TODO: Improve event types (maybe as struct header) -enum window_event_type { GUI_KEYBOARD = 10, GUI_MOUSE, GUI_MAX }; +enum window_event_type { GUI_KEYBOARD = GFX_MAX + 1, GUI_MOUSE, GUI_MAX }; enum element_type { GUI_TYPE_CONTAINER, GUI_TYPE_BUTTON, GUI_TYPE_TEXTBOX }; struct element_button { - const char *text; - u32 color; + char *text; + u32 color_fg; + u32 color_bg; + enum font_type font_type; void (*on_click)(); }; struct element_textbox { const char *text; u32 color; + enum font_type font_type; }; struct element { @@ -56,7 +59,8 @@ struct gui_event_mouse { struct element *gui_init(const char *title, u32 width, u32 height); void gui_event_loop(struct element *container); -struct element_button *gui_add_button(struct element *container, int x, int y, u32 width, - u32 height, const char *text, u32 color); +struct element_button *gui_add_button(struct element *container, int x, int y, + enum font_type font_type, char *text, u32 color_bg, + u32 color_fg); #endif |