Fixed more RichTextBox issues, fixed segfault from zooming in/out too fast, fixed segfault from undoing an edit

This commit is contained in:
Yousef Khadadeh 2025-10-25 17:31:55 +03:00
parent 4db5838a64
commit 011f8ba0b4
12 changed files with 113 additions and 88 deletions

View file

@ -19,14 +19,13 @@ DrawComponentType DrawTextBox::get_type() const {
}
void DrawTextBox::save(cereal::PortableBinaryOutputArchive& a) const {
a(d.editing, d.p1, d.p2, d.textColor, d.textSize, *cursor, d.currentText);
a(d.editing, d.p1, d.p2, textBox->get_rich_text_data(), *cursor);
}
void DrawTextBox::load(cereal::PortableBinaryInputArchive& a) {
a(d.editing, d.p1, d.p2, d.textColor, d.textSize, *cursor, d.currentText);
#ifndef IS_SERVER
textboxUpdate = true;
#endif
RichTextBox::RichTextData richText;
a(d.editing, d.p1, d.p2, richText, *cursor);
textBox->set_rich_text_data(richText);
}
#ifndef IS_SERVER
@ -34,7 +33,7 @@ std::shared_ptr<DrawComponent> DrawTextBox::copy() const {
auto a = std::make_shared<DrawTextBox>();
a->d = d;
a->coords = coords;
a->textboxUpdate = true;
a->textBox->set_rich_text_data(textBox->get_rich_text_data());
return a;
}
@ -42,7 +41,7 @@ std::shared_ptr<DrawComponent> DrawTextBox::deep_copy() const {
auto a = std::make_shared<DrawTextBox>();
a->d = d;
a->coords = coords;
a->textboxUpdate = false;
a->textBox->set_rich_text_data(textBox->get_rich_text_data());
a->collisionTree = collisionTree;
return a;
}
@ -50,13 +49,11 @@ std::shared_ptr<DrawComponent> DrawTextBox::deep_copy() const {
void DrawTextBox::update_from_delayed_ptr(const std::shared_ptr<DrawComponent>& delayedUpdatePtr) {
std::shared_ptr<DrawTextBox> newPtr = std::static_pointer_cast<DrawTextBox>(delayedUpdatePtr);
d = newPtr->d;
textboxUpdate = true;
}
void DrawTextBox::init_text_box(DrawingProgram& drawP) {
textBox->set_font_collection(drawP.world.main.fonts.collection);
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());
textBox->insert({0, 0}, d.currentText);
}
void DrawTextBox::draw(SkCanvas* canvas, const DrawData& drawData) {
@ -85,19 +82,6 @@ void DrawTextBox::draw(SkCanvas* canvas, const DrawData& drawData) {
}
void DrawTextBox::update(DrawingProgram& drawP) {
if(textboxUpdate)
set_textbox_string(d.currentText);
}
void DrawTextBox::set_textbox_string(const std::string& str) {
d.currentText = str;
textBox->remove({0, 0}, textBox->move(RichTextBox::Movement::END, {0, 0}));
textBox->insert({0, 0}, d.currentText);
textboxUpdate = false;
}
void DrawTextBox::update_contained_string(DrawingProgram& drawP) {
d.currentText = textBox->get_string();
}
Vector2f DrawTextBox::get_mouse_pos(DrawingProgram& drawP) {

View file

@ -17,12 +17,9 @@ class DrawTextBox : public DrawComponent {
// User input data
struct Data {
Vector4f textColor;
float textSize = -100000.0f;
Vector2f p1;
Vector2f p2;
std::string currentText;
bool editing = false;
bool editing = false; // Should probably move this out of the Data struct
bool operator==(const Data& o) const = default;
bool operator!=(const Data& o) const = default;
} d;
@ -38,13 +35,10 @@ class DrawTextBox : public DrawComponent {
void create_collider();
virtual void update_from_delayed_ptr(const std::shared_ptr<DrawComponent>& delayedUpdatePtr) override;
void init_text_box(DrawingProgram& drawP);
void update_contained_string(DrawingProgram& drawP);
void set_textbox_string(const std::string& str);
Vector2f get_mouse_pos(DrawingProgram& drawP);
virtual SCollision::AABB<float> get_obj_coord_bounds() const override;
bool textboxUpdate = false;
std::shared_ptr<RichTextBox> textBox;
std::shared_ptr<RichTextBox::Cursor> cursor;
SCollision::BVHContainer<float> collisionTree;