Skip to content

Commit

Permalink
Don't use Drawable as a base class for Line
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Oct 4, 2024
1 parent 1c127e3 commit dfb2db4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions src/jngl/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,38 @@
#include "screen.hpp"

namespace jngl {
class Line : public Drawable {
class Text::Line {
public:
Line(std::string text, std::shared_ptr<FontImpl> font) : text(std::move(text)) {
setFont(std::move(font));
}
void setFont(std::shared_ptr<FontImpl> font) {
width = static_cast<float>(font->getTextWidth(text));
height = static_cast<float>(font->getLineHeight());
width = font->getTextWidth(text);
height = font->getLineHeight();
this->font = std::move(font);
}
void step() override {
void draw(Mat3 modelview) const {
font->print(modelview.translate(position), text);
}
void draw() const override {
font->print(ScaleablePixels(getX()), ScaleablePixels(getY()), text);
double getWidth() const {
return static_cast<double>(static_cast<ScaleablePixels>(width));
}
double getHeight() const {
return static_cast<double>(static_cast<ScaleablePixels>(height));
}
void setX(double x) {
position.x = x;
}
void setY(double y) {
position.y = y;
}

private:
std::string text;
std::shared_ptr<FontImpl> font;
Vec2 position;
Pixels width{ -1 };
Pixels height{ -1 };
};

Text::Text(const std::string& text) : font(pWindow->getFontImpl()) {
Expand Down Expand Up @@ -87,11 +100,10 @@ void Text::step() {
}

void Text::draw() const {
jngl::pushMatrix();
jngl::translate(static_cast<int>(getX()), static_cast<int>(getY()));
auto mv = modelview().translate({ static_cast<double>(static_cast<int>(getX())),
static_cast<double>(static_cast<int>(getY())) });
for (auto& line : lines) {
line->draw();
line->draw(mv);
}
jngl::popMatrix();
}
} // namespace jngl
2 changes: 1 addition & 1 deletion src/jngl/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ enum class Alignment : uint8_t { LEFT, RIGHT, CENTER };

class Font;
class FontImpl;
class Line;

/// Rectangle shaped text block
class Text : public Drawable {
Expand All @@ -41,6 +40,7 @@ class Text : public Drawable {
void draw() const override;

private:
class Line;
std::vector<std::shared_ptr<Line>> lines;
std::shared_ptr<FontImpl> font;
Alignment align = Alignment::LEFT;
Expand Down

0 comments on commit dfb2db4

Please sign in to comment.