From c34b9191f258ddc15c5b45c000cd0266aed9dead Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Mon, 29 Jul 2024 21:26:17 -0400 Subject: window borders --- libraries/daguerre/include/daguerre/image.hpp | 7 ++++ .../daguerre/include/daguerre/impl/fixed-font.hpp | 1 + libraries/daguerre/include/daguerre/impl/image.hpp | 20 +++++++++-- libraries/pake/include/pake/widget.hpp | 8 +++++ libraries/pake/include/pake/widgets/fixed-text.hpp | 5 ++- libraries/pake/source/widgets/fixed-text.cpp | 42 +++++++++++++++++----- 6 files changed, 71 insertions(+), 12 deletions(-) (limited to 'libraries') diff --git a/libraries/daguerre/include/daguerre/image.hpp b/libraries/daguerre/include/daguerre/image.hpp index a55f43b..3c9c902 100644 --- a/libraries/daguerre/include/daguerre/image.hpp +++ b/libraries/daguerre/include/daguerre/image.hpp @@ -110,6 +110,13 @@ namespace daguerre { param_converter_t *conversion = &default_conversion); + //does not check bounds or wrap text + template + void render_text( + const fixed_font &font, int x, int y, const char *text, + converter_t *conversion = + &default_conversion); + }; template diff --git a/libraries/daguerre/include/daguerre/impl/fixed-font.hpp b/libraries/daguerre/include/daguerre/impl/fixed-font.hpp index 1a5f3d8..61a27fc 100644 --- a/libraries/daguerre/include/daguerre/impl/fixed-font.hpp +++ b/libraries/daguerre/include/daguerre/impl/fixed-font.hpp @@ -35,6 +35,7 @@ namespace daguerre { glyphs[i] = std::move(other.glyphs[i]); other.glyph_width = 0; other.glyph_height = 0; + return *this; } template diff --git a/libraries/daguerre/include/daguerre/impl/image.hpp b/libraries/daguerre/include/daguerre/impl/image.hpp index 6cf2ca9..c91cc7d 100644 --- a/libraries/daguerre/include/daguerre/impl/image.hpp +++ b/libraries/daguerre/include/daguerre/impl/image.hpp @@ -107,7 +107,7 @@ namespace daguerre { void image::fill( const color_t &color, int start_x, int start_y, int width, int height) { for (int y = start_y; y < start_y + height; ++y) - for (int x = 0; x < start_x + width; ++x) + for (int x = start_x; x < start_x + width; ++x) buffer[y * buffer_pitch + x] = color; } @@ -170,7 +170,7 @@ namespace daguerre { void image::convert_from( const image &other, int to_x, int to_y, converter_t *conversion) { - convert_from(other, to_x, to_y, 0, 0, other.width, other.y, conversion); + convert_from(other, to_x, to_y, 0, 0, other.width, other.height, conversion); } template @@ -213,6 +213,22 @@ namespace daguerre { } + template + template + void image::render_text( + const fixed_font &font, int x, int y, const char *text, + converter_t *conversion) { + + while (*text) { + int ch = *text; + if (ch >= 0 && ch < 128) + convert_from(font.glyphs[ch], x, y, conversion); + ++text; + x += font.glyph_width; + } + + } + template void swap(image &a, image &b) { std::swap(a.delete_buffer_on_destruct, b.delete_buffer_on_destruct); diff --git a/libraries/pake/include/pake/widget.hpp b/libraries/pake/include/pake/widget.hpp index dad5651..466fb80 100644 --- a/libraries/pake/include/pake/widget.hpp +++ b/libraries/pake/include/pake/widget.hpp @@ -4,6 +4,14 @@ namespace pake { + enum class halign { + left, center, right + }; + + enum class valign { + top, center, bottom + }; + class widget { public: diff --git a/libraries/pake/include/pake/widgets/fixed-text.hpp b/libraries/pake/include/pake/widgets/fixed-text.hpp index c6dafab..dc2e277 100644 --- a/libraries/pake/include/pake/widgets/fixed-text.hpp +++ b/libraries/pake/include/pake/widgets/fixed-text.hpp @@ -12,6 +12,8 @@ namespace pake::widgets { std::string text; int width, height; + halign ha; + valign va; public: //TODO: look up font in some kind of catalogue @@ -19,7 +21,8 @@ namespace pake::widgets { std::string &&text, const daguerre::fixed_font *font, daguerre::hilbert_color bg, - daguerre::hilbert_color fg); + daguerre::hilbert_color fg, + halign ha, valign va); virtual void render( dirtiable_image &onto, int x_off, int y_off, bool force) override; diff --git a/libraries/pake/source/widgets/fixed-text.cpp b/libraries/pake/source/widgets/fixed-text.cpp index 9ae55dc..c3a9a10 100644 --- a/libraries/pake/source/widgets/fixed-text.cpp +++ b/libraries/pake/source/widgets/fixed-text.cpp @@ -12,24 +12,48 @@ namespace pake::widgets { std::string &&text, const daguerre::fixed_font *font, daguerre::hilbert_color bg, - daguerre::hilbert_color fg) - : font(font), bg(bg), fg(fg), text(std::move(text)) {} + daguerre::hilbert_color fg, + halign ha, valign va) + : font(font), bg(bg), fg(fg), + text(std::move(text)), ha(ha), va(va) {} void fixed_text::render( dirtiable_image &onto, int x_off, int y_off, bool force) { if (force) { - onto.image.fill( bg, x_off, y_off, width, height); - onto.dirty.fill(true, x_off, y_off, width, height); - //TODO: have options for alignment + //TODO: check overflow + + onto.dirty.fill(true, x_off, y_off, width, height); + + onto.image.fill( bg, x_off, y_off, width, height); + + switch (ha) { + case halign::left: + break; + case halign::center: + x_off = width / 2 - text.size() * font->glyph_width / 2; + break; + case halign::right: + x_off = width - text.size() * font->glyph_width; + break; + } + + switch (va) { + case valign::top: + break; + case valign::center: + y_off = height / 2 - font->glyph_height / 2; + break; + case valign::bottom: + y_off = height - font->glyph_width; + break; + } + onto.image.render_text( *font, fg, x_off, y_off, text.data(), draw_if_true); - onto.dirty.fill( - true, x_off, y_off, - font->glyph_width * text.size(), - font->glyph_height); + } } -- cgit v1.2.3