infinipaint/src/DrawComponents/DrawTextBox.cpp

131 lines
4.3 KiB
C++
Raw Normal View History

2025-07-06 20:43:53 +03:00
#include "DrawTextBox.hpp"
#include "Helpers/ConvertVec.hpp"
#include <cereal/types/string.hpp>
#ifndef IS_SERVER
#include "../MainProgram.hpp"
#include "Helpers/SCollision.hpp"
#include "../DrawingProgram/DrawingProgram.hpp"
#include "../DrawCollision.hpp"
#endif
DrawTextBox::DrawTextBox():
textBox(std::make_shared<RichTextBox>()),
cursor(std::make_shared<RichTextBox::Cursor>())
{
skia::textlayout::TextStyle tStyle;
tStyle.setFontFamilies({SkString{"Roboto"}});
tStyle.setFontSize(16.0f);
tStyle.setForegroundPaint(SkPaint{SkColor4f{0.0f, 1.0f, 1.0f, 1.0f}});
textBox->set_initial_text_style(tStyle);
auto boldMod = std::make_shared<WeightTextStyleModifier>();
boldMod->set_weight(SkFontStyle::kNormal_Weight);
textBox->set_initial_text_style_modifier(boldMod);
auto italicMod = std::make_shared<SlantTextStyleModifier>();
italicMod->set_slant(SkFontStyle::Slant::kUpright_Slant);
textBox->set_initial_text_style_modifier(italicMod);
auto colorMod = std::make_shared<ColorTextStyleModifier>();
colorMod->color = {1.0f, 1.0f, 1.0f, 1.0f};
textBox->set_initial_text_style_modifier(colorMod);
}
2025-07-06 20:43:53 +03:00
DrawComponentType DrawTextBox::get_type() const {
return DRAWCOMPONENT_TEXTBOX;
}
void DrawTextBox::save(cereal::PortableBinaryOutputArchive& a) const {
a(d.editing, d.p1, d.p2, textBox->get_rich_text_data(), *cursor);
2025-07-06 20:43:53 +03:00
}
void DrawTextBox::load(cereal::PortableBinaryInputArchive& a) {
RichTextBox::RichTextData richText;
a(d.editing, d.p1, d.p2, richText, *cursor);
textBox->set_rich_text_data(richText);
2025-07-06 20:43:53 +03:00
}
#ifndef IS_SERVER
std::shared_ptr<DrawComponent> DrawTextBox::copy() const {
auto a = std::make_shared<DrawTextBox>();
a->d = d;
a->coords = coords;
a->textBox->set_rich_text_data(textBox->get_rich_text_data());
2025-07-06 20:43:53 +03:00
return a;
}
std::shared_ptr<DrawComponent> DrawTextBox::deep_copy() const {
auto a = std::make_shared<DrawTextBox>();
a->d = d;
a->coords = coords;
a->textBox->set_rich_text_data(textBox->get_rich_text_data());
a->collisionTree = collisionTree;
return a;
}
2025-07-31 09:34:17 +03:00
void DrawTextBox::update_from_delayed_ptr(const std::shared_ptr<DrawComponent>& delayedUpdatePtr) {
2025-07-06 20:43:53 +03:00
std::shared_ptr<DrawTextBox> newPtr = std::static_pointer_cast<DrawTextBox>(delayedUpdatePtr);
d = newPtr->d;
}
void DrawTextBox::init_text_box(DrawingProgram& drawP) {
textBox->set_font_collection(drawP.world.main.fonts.collection); // Getting a segfault relating to the paragraph cache means that the font collection hasn't been set yet
textBox->set_width(d.p2.x() - d.p1.x());
2025-07-06 20:43:53 +03:00
}
void DrawTextBox::draw(SkCanvas* canvas, const DrawData& drawData) {
if(drawSetupData.shouldDraw) {
canvas->save();
canvas_do_calculated_transform(canvas);
SkRect clipR = SkRect::MakeLTRB(d.p1.x(), d.p1.y(), d.p2.x(), d.p2.y());
if(d.editing) {
SkPaint p;
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(0.0f);
p.setColor4f({0.5f, 0.5f, 0.5f, 1.0f});
canvas->drawRect(clipR, p);
}
canvas->clipRect(clipR);
canvas->translate(d.p1.x(), d.p1.y());
RichTextBox::PaintOpts paintOpts;
paintOpts.cursorColor = {0.7f, 0.7f, 1.0f};
if(d.editing && cursor)
paintOpts.cursor = *cursor;
2025-07-06 20:43:53 +03:00
textBox->paint(canvas, paintOpts);
2025-07-06 20:43:53 +03:00
canvas->restore();
}
}
void DrawTextBox::update(DrawingProgram& drawP) {
}
Vector2f DrawTextBox::get_mouse_pos(DrawingProgram& drawP) {
return coords.get_mouse_pos(drawP.world) - d.p1;
}
void DrawTextBox::initialize_draw_data(DrawingProgram& drawP) {
init_text_box(drawP);
2025-07-31 09:34:17 +03:00
create_collider();
2025-07-06 20:43:53 +03:00
}
2025-07-28 14:25:28 +03:00
bool DrawTextBox::collides_within_coords(const SCollision::ColliderCollection<float>& checkAgainst) {
2025-07-06 20:43:53 +03:00
return collisionTree.is_collide(checkAgainst);
}
2025-07-28 14:25:28 +03:00
void DrawTextBox::create_collider() {
2025-07-06 20:43:53 +03:00
using namespace SCollision;
ColliderCollection<float> strokeObjects;
std::array<Vector2f, 4> newT = triangle_from_rect_points(d.p1, d.p2);
strokeObjects.triangle.emplace_back(newT[0], newT[1], newT[2]);
strokeObjects.triangle.emplace_back(newT[2], newT[3], newT[0]);
collisionTree.clear();
collisionTree.calculate_bvh_recursive(strokeObjects);
}
SCollision::AABB<float> DrawTextBox::get_obj_coord_bounds() const {
return collisionTree.objects.bounds;
}
#endif