Skip to content

Commit

Permalink
Added an ability to draw a text using TextBox
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriiKoniushenko committed Jul 31, 2023
1 parent 75de47f commit b5cc138
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
6 changes: 3 additions & 3 deletions game/source/VaKon2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ void VaKon2D::start()
texture.setMagAndMinFilter(Gl::Texture::MagFilter::Linear, Gl::Texture::MinFilter::LinearMipmapLinear);

Font font("assets/fonts/Roboto-Medium.ttf");
LineText text;
text.setText("Hello\nworld");
TextBox text;
text.setFont(font);
text.setText("Hello\nworld");
// text.setTexture(texture);
text.setColor({ 255, 0, 0 });
// text.setColor({ 255, 0, 0 });
text.prepare(shaderPack);
// text.setPosition({100.f, 50.f});
// text.prepare(shaderPack);
Expand Down
2 changes: 2 additions & 0 deletions lib/core/shapes/include/LineText.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class LineText : public Widget
void setText(const std::string& text);

_NODISCARD float getTextWidth() const;
_NODISCARD float getTextHeight() const;
_NODISCARD float getFontSize() const;
void setFontSize(float size);

Expand All @@ -77,6 +78,7 @@ class LineText : public Widget
std::string text_;
std::string lastSavedText_;
mutable float textWidth_ = -1.f;
mutable float textHeight_ = -1.f;
Vbo vbo_;
Vao vao_;
float fontSize_ = 24.f;
Expand Down
31 changes: 31 additions & 0 deletions lib/core/shapes/source/LineText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void LineText::updateCache()
const float ypos = position.y - static_cast<float>(ch.size.y - ch.bearing.y) * scale - offsetY * scale;
const float w = static_cast<float>(ch.size.x) * scale;
const float h = static_cast<float>(ch.size.y) * scale;
textHeight_ = h;

// clang-format off
std::vector<float> vertices = {
Expand Down Expand Up @@ -232,6 +233,36 @@ float LineText::getTextWidth() const
return textWidth_;
}

float LineText::getTextHeight() const
{
if (lastSavedText_ == text_)
{
return textHeight_;
}

textHeight_ = 0;
const float scale = fontSize_ / Font::defaultRenderSize;

for (auto ch : text_)
{
if (!font_)
{
spdlog::get("core")->warn("Can't get character without font");
BOOST_ASSERT_MSG(false, "Can't get character without font");
break;
}

const auto& oneChar = font_->getCharacter(ch);
auto height = static_cast<float>(oneChar.bearing.y) * scale;
if (height > textHeight_)
{
textHeight_ = height;
}
}

return textHeight_;
}

void LineText::setColor(const Color& color)
{
color_ = color;
Expand Down
16 changes: 15 additions & 1 deletion lib/core/shapes/source/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Font.h"
#include "LineText.h"
#include "ShaderPack.h"
#include "Logger.h"

#include <boost/algorithm/string.hpp>

Expand All @@ -35,6 +36,13 @@ std::string TextBox::getComponentName() const

void TextBox::setText(const std::string& text)
{
if (!font_)
{
spdlog::get("core")->critical("Can't set text without a font");
BOOST_ASSERT_MSG(font_, "Can't set text without a font");
return;
}

std::vector<std::string> strings;
boost::split(strings, text, boost::is_any_of("\n"));

Expand All @@ -43,7 +51,13 @@ void TextBox::setText(const std::string& text)
std::size_t i = 0;
for (auto& lineText : rows_)
{
lineText.setText(strings[i++]);
lineText.setText(strings[i]);
lineText.setFont(*font_);
if (i != 0)
{
lineText.move({0.f, lineText.getTextHeight()});
}
++i;
}
}

Expand Down

0 comments on commit b5cc138

Please sign in to comment.