Print messages using SkParagraph and new TextParagraph element

This commit is contained in:
Yousef Khadadeh 2025-10-15 16:33:24 +03:00
parent 9ceb4e1b8a
commit 921e111255
9 changed files with 147 additions and 16 deletions

View file

@ -5,6 +5,7 @@
#include <Eigen/Dense>
#include <include/core/SkFont.h>
#include <include/core/SkFontMgr.h>
#include <modules/skparagraph/include/FontCollection.h>
#include <modules/svg/include/SkSVGDOM.h>
#include <Helpers/SCollision.hpp>
#include <Helpers/Serializers.hpp>
@ -95,6 +96,7 @@ struct UpdateInputData {
bool copy = false;
bool paste = false;
bool cut = false;
bool escape = false;
} key;
struct {
@ -107,6 +109,8 @@ struct UpdateInputData {
uint16_t fontSize = 20;
sk_sp<SkTypeface> textTypeface;
sk_sp<skia::textlayout::FontCollection> fontCollection;
std::string textInput;
bool hoverObstructed = false;
bool acceptingTextInput = false;

View file

@ -0,0 +1,28 @@
#include "TextParagraph.hpp"
#include <include/core/SkPath.h>
#include <iostream>
namespace GUIStuff {
void TextParagraph::update(UpdateInputData& io, std::unique_ptr<skia::textlayout::Paragraph> paragraph, float width, const std::function<void()>& elemUpdate) {
data = std::move(paragraph);
data->layout(width);
CLAY({
.layout = {
.sizing = {.width = CLAY_SIZING_FIXED(width), .height = CLAY_SIZING_FIXED(data->getHeight())}
},
.custom = { .customData = this }
}) {
if(elemUpdate)
elemUpdate();
}
}
void TextParagraph::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command) {
bb = get_bb(command);
if(data)
data->paint(canvas, bb.min.x(), bb.min.y());
}
}

View file

@ -0,0 +1,16 @@
#pragma once
#include "Element.hpp"
#include <modules/skparagraph/include/Paragraph.h>
namespace GUIStuff {
class TextParagraph : public Element {
public:
void update(UpdateInputData& io, std::unique_ptr<skia::textlayout::Paragraph> paragraph, float width, const std::function<void()>& elemUpdate);
virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command) override;
private:
SCollision::AABB<float> bb = SCollision::AABB<float>({0.0f, 0.0f}, {0.0f, 0.0f});
std::unique_ptr<skia::textlayout::Paragraph> data;
};
}