From 9cb39234423cdf5d92d171e4517606ac7e71aad0 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 3 Feb 2024 05:56:10 +0300 Subject: [PATCH] gui and Font update --- src/assets/assetload_funcs.cpp | 10 +++++----- src/frontend/gui/controls.cpp | 24 ++++++++++++++++++------ src/frontend/gui/controls.h | 2 ++ src/graphics/Font.cpp | 19 +++++++++++-------- src/graphics/Font.h | 11 +++++++---- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index 2bac2dd7..8336f5a8 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -94,21 +94,21 @@ bool assetload::font(Assets* assets, const ResPaths* paths, const std::string filename, const std::string name) { - std::vector pages; + std::vector> pages; for (size_t i = 0; i <= 4; i++) { std::string name = filename + "_" + std::to_string(i) + ".png"; name = paths->find(name).string(); - Texture* texture = png::load_texture(name); + std::unique_ptr texture (png::load_texture(name)); if (texture == nullptr) { std::cerr << "failed to load bitmap font '" << name; std::cerr << "' (missing page " << std::to_string(i) << ")"; std::cerr << std::endl; return false; } - pages.push_back(texture); + pages.push_back(std::move(texture)); } - Font* font = new Font(pages, pages[0]->height / 16); - assets->store(font, name); + int res = pages[0]->height / 16; + assets->store(new Font(std::move(pages), res, 4), name); return true; } diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 7687a667..d0483d19 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -48,7 +48,10 @@ void Label::draw(const GfxContext* pctx, Assets* assets) { batch->color = getColor(); Font* font = assets->getFont(fontName_); vec2 size = getSize(); - vec2 newsize = vec2(font->calcWidth(text), font->lineHeight()); + vec2 newsize = vec2( + font->calcWidth(text), + font->getLineHeight()+font->getYOffset() + ); vec2 coord = calcCoord(); switch (align) { @@ -61,7 +64,7 @@ void Label::draw(const GfxContext* pctx, Assets* assets) { coord.x += size.x-newsize.x; break; } - + coord.y += (size.y-newsize.y)*0.5f; font->draw(batch, text, coord.x, coord.y); } @@ -103,10 +106,12 @@ Button::Button( vec2 size ) : Panel(size, padding, 0) { - size = vec2( - glm::max(padding.x + padding.z + text.length()*8, size.x), - glm::max(padding.y + padding.w + 16, size.y) - ); + if (size.y < 0.0f) { + size = vec2( + glm::max(padding.x + padding.z + text.length()*8, size.x), + glm::max(padding.y + padding.w + 16, size.y) + ); + } setSize(size); if (action) { @@ -141,6 +146,13 @@ Button* Button::textSupplier(wstringsupplier supplier) { return this; } +void Button::setSize(glm::vec2 size) { + Panel::setSize(size); + if (label) { + label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y)); + } +} + void Button::drawBackground(const GfxContext* pctx, Assets* assets) { vec2 coord = calcCoord(); auto batch = pctx->getBatch2D(); diff --git a/src/frontend/gui/controls.h b/src/frontend/gui/controls.h index edb2e497..c2d7d47c 100644 --- a/src/frontend/gui/controls.h +++ b/src/frontend/gui/controls.h @@ -81,6 +81,8 @@ namespace gui { virtual std::wstring getText() const; virtual Button* textSupplier(wstringsupplier supplier); + + virtual void setSize(glm::vec2 size) override; }; class RichButton : public Container { diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 17b934d9..27548631 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -4,16 +4,19 @@ using glm::vec4; -Font::Font(std::vector pages, int lineHeight) : lineHeight_(lineHeight), pages(pages) { +Font::Font(std::vector> pages, int lineHeight, int yoffset) + : lineHeight(lineHeight), yoffset(yoffset), pages(std::move(pages)) { } Font::~Font(){ - for (Texture* texture : pages) - delete texture; } -int Font::lineHeight() const { - return lineHeight_; +int Font::getYOffset() const { + return yoffset; +} + +int Font::getLineHeight() const { + return lineHeight; } bool Font::isPrintableChar(int c) { @@ -48,11 +51,11 @@ void Font::draw(Batch2D* batch, std::wstring text, int x, int y, int style) { if (isPrintableChar(c)){ int charpage = c >> 8; if (charpage == page){ - Texture* texture = pages[charpage]; + Texture* texture = pages[charpage].get(); if (texture == nullptr){ - texture = pages[0]; + texture = pages[0].get(); } - batch->texture(pages[charpage]); + batch->texture(texture); switch (style){ case STYLE_SHADOW: diff --git a/src/graphics/Font.h b/src/graphics/Font.h index 1acb6cf9..d2800b1c 100644 --- a/src/graphics/Font.h +++ b/src/graphics/Font.h @@ -1,6 +1,7 @@ #ifndef GRAPHICS_FONT_H_ #define GRAPHICS_FONT_H_ +#include #include #include #include "../typedefs.h" @@ -13,13 +14,15 @@ const uint STYLE_SHADOW = 1; const uint STYLE_OUTLINE = 2; class Font { - int lineHeight_; + int lineHeight; + int yoffset; public: - std::vector pages; - Font(std::vector pages, int lineHeight); + std::vector> pages; + Font(std::vector> pages, int lineHeight, int yoffset); ~Font(); - int lineHeight() const; + int getLineHeight() const; + int getYOffset() const; int calcWidth(std::wstring text); // int getGlyphWidth(char c); bool isPrintableChar(int c);