From f6caaabef2c3fc8c10d5ac584e4258dc24186f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 20 Jun 2021 21:19:26 +0200 Subject: [PATCH] feat: add gui text component --- text-viewer/include/gui_component_text.h | 21 ++++++++++++++++ text-viewer/src/gui_component_text.c | 32 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 text-viewer/include/gui_component_text.h create mode 100644 text-viewer/src/gui_component_text.c diff --git a/text-viewer/include/gui_component_text.h b/text-viewer/include/gui_component_text.h new file mode 100644 index 0000000000000000000000000000000000000000..e106bfd8f544a87c0ea0e58dfb0049ab95f3981a --- /dev/null +++ b/text-viewer/include/gui_component_text.h @@ -0,0 +1,21 @@ +#ifndef __GUI_COMPONENT_TEXT_H__ +#define __GUI_COMPONENT_TEXT_H__ + +#include "display_utils.h" +#include "gui.h" + +typedef struct { + char *line; + font_t *font; + display_pixel_t color; +} text_t; + +component_t gui_text_create(text_t *text, int16_t x, int16_t y, + int16_t w, int16_t h); + +void gui_text_render(container_t *container, component_t *component, + gui_t *gui); +void gui_text_update(container_t *container, component_t *component, + gui_t *gui); + +#endif // __GUI_COMPONENT_TEXT_H__ diff --git a/text-viewer/src/gui_component_text.c b/text-viewer/src/gui_component_text.c new file mode 100644 index 0000000000000000000000000000000000000000..3530f0d6ced8ddb24104368df4ca859efb601686 --- /dev/null +++ b/text-viewer/src/gui_component_text.c @@ -0,0 +1,32 @@ +#include "gui_component_text.h" +#include "font.h" +#include "renderer.h" + +component_t gui_text_create(text_t *text, int16_t x, int16_t y, int16_t w, + int16_t h) { + component_t comp = + gui_component_create(x, y, w, h, gui_text_render, gui_text_update); + comp.state = text; + + if (w == 0 || h == 0) { + size2d_t measured = font_measure_text(text->font, text->line); + + comp.width = comp.width != 0 ? comp.width : measured.x; + comp.height = comp.height != 0 ? comp.height : measured.y; + } + + return comp; +} + +void gui_text_render(container_t *container, component_t *component, + gui_t *gui) { + if (gui_is_component_visible(gui, container, component)) { + text_t *state = (text_t*)component->state; + renderer_write_string(gui->renderer, component->x, component->y, 0, state->font, state->line, state->color); + } +} + +void gui_text_update(container_t *container, component_t *component, + gui_t *gui) { + // do nothing -- maybe horizontal scroll? +}