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

@ -124,6 +124,9 @@ void DrawCamera::update_main(World& w) {
isAccurateZooming = newIsAccurateZooming;
if(isAccurateZooming && startZoomVal != WorldScalar(0)) {
WorldScalar zoomFactor(std::pow(1.0 + w.main.toolbar.dragZoomSpeed, w.main.toolbar.flipZoomToolDirection ? w.main.input.mouse.move.y() : -w.main.input.mouse.move.y()));
if(zoomFactor < WorldScalar(0.000001))
zoomFactor = WorldScalar(0.000001);
c.scale(zoomFactor);
if(c.inverseScale < WorldScalar(0.0001))
c.inverseScale = WorldScalar(0.0001);
@ -139,6 +142,10 @@ void DrawCamera::update_main(World& w) {
// Ignore the value of scrollAmount, since that leads to problems on macOS
WorldScalar zoomFactor(1.0 + w.main.toolbar.scrollZoomSpeed);
if(zoomFactor < WorldScalar(0.000001))
zoomFactor = WorldScalar(0.000001);
if(zoomFactor != WorldScalar(0)) {
if(w.main.input.mouse.scrollAmount.y() < 0.0)
zoomFactor = WorldScalar(1) / zoomFactor;

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;

View file

@ -43,7 +43,7 @@ DrawingProgram::DrawingProgram(World& initWorld):
if(addToCompCacheOnInsert)
compCache.add_component(c);
DrawComponentType t = c->obj->get_type();
if(t == DRAWCOMPONENT_IMAGE || t == DRAWCOMPONENT_TEXTBOX)
if(t == DRAWCOMPONENT_IMAGE)
updateableComponents.emplace(c->obj);
};
components.clientEraseCallback = [&](const CollabListType::ObjectInfoPtr& c) {

View file

@ -178,7 +178,7 @@ void DrawingProgramSelection::commit_transform_selection() {
}
DrawComponent::client_send_transform_many(drawP, transformsToSend);
drawP.world.undo.push(UndoManager::UndoRedoPair{
[&, transformsFrom = transformsFrom]() {
[isSingleThread, &drawP = drawP, transformsFrom = transformsFrom]() {
std::vector<std::pair<ServerClientID, CoordSpaceHelper>> transformsToSend;
for(auto& [comp, coords] : transformsFrom) {
auto lockObjInfo = comp->obj->collabListInfo.lock();
@ -188,7 +188,6 @@ void DrawingProgramSelection::commit_transform_selection() {
return false;
}
bool isSingleThread = a.size() < DrawingProgramCache::MINIMUM_COMPONENTS_TO_START_REBUILD;
parallel_loop_container(transformsFrom, [&](auto& p) {
auto& [comp, coords] = p;
comp->obj->coords = coords;
@ -202,7 +201,7 @@ void DrawingProgramSelection::commit_transform_selection() {
return true;
},
[&, transformsTo = transformsTo]() {
[isSingleThread, &drawP = drawP, transformsTo = transformsTo]() {
std::vector<std::pair<ServerClientID, CoordSpaceHelper>> transformsToSend;
for(auto& [comp, coords] : transformsTo) {
auto lockObjInfo = comp->obj->collabListInfo.lock();
@ -212,7 +211,6 @@ void DrawingProgramSelection::commit_transform_selection() {
return false;
}
bool isSingleThread = a.size() < DrawingProgramCache::MINIMUM_COMPONENTS_TO_START_REBUILD;
parallel_loop_container(transformsTo, [&](auto& p) {
auto& [comp, coords] = p;
comp->obj->coords = coords;

View file

@ -60,13 +60,13 @@ void EllipseDrawEditTool::commit_edit_updates(const std::shared_ptr<DrawComponen
DrawEllipse::Data pData = std::any_cast<DrawEllipse::Data>(prevData);
DrawEllipse::Data cData = a->d;
drawP.world.undo.push(UndoManager::UndoRedoPair{
[&, a, pData]() {
[&drawP = drawP, a, pData]() {
a->d = pData;
a->client_send_update(drawP, true);
a->commit_update(drawP);
return true;
},
[&, a, cData]() {
[&drawP = drawP, a, cData]() {
a->d = cData;
a->client_send_update(drawP, true);
a->commit_update(drawP);

View file

@ -57,13 +57,13 @@ void RectDrawEditTool::commit_edit_updates(const std::shared_ptr<DrawComponent>&
DrawRectangle::Data pData = std::any_cast<DrawRectangle::Data>(prevData);
DrawRectangle::Data cData = a->d;
drawP.world.undo.push(UndoManager::UndoRedoPair{
[&, a, pData]() {
[&drawP = drawP, a, pData]() {
a->d = pData;
a->client_send_update(drawP, true);
a->commit_update(drawP);
return true;
},
[&, a, cData]() {
[&drawP = drawP, a, cData]() {
a->d = cData;
a->client_send_update(drawP, true);
a->commit_update(drawP);

View file

@ -16,17 +16,20 @@ bool TextBoxEditTool::edit_gui(const std::shared_ptr<DrawComponent>& comp) {
Toolbar& t = drawP.world.main.toolbar;
t.gui.push_id("edit tool text");
t.gui.text_label_centered("Edit Text");
t.gui.slider_scalar_field("Text Relative Size", "Text Size", &a->d.textSize, 5.0f, 100.0f);
t.gui.left_to_right_line_layout([&]() {
CLAY({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(40), .height = CLAY_SIZING_FIXED(40)}}}) {
if(t.gui.color_button("Text Color", &a->d.textColor, &a->d.textColor == t.colorRight))
t.color_selector_right(&a->d.textColor == t.colorRight ? nullptr : &a->d.textColor);
}
t.gui.text_label("Text Color");
});
//t.gui.slider_scalar_field("Text Relative Size", "Text Size", &a->d.textSize, 5.0f, 100.0f);
//t.gui.left_to_right_line_layout([&]() {
// CLAY({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(40), .height = CLAY_SIZING_FIXED(40)}}}) {
// if(t.gui.color_button("Text Color", &a->d.textColor, &a->d.textColor == t.colorRight))
// t.color_selector_right(&a->d.textColor == t.colorRight ? nullptr : &a->d.textColor);
// }
// t.gui.text_label("Text Color");
//});
t.gui.pop_id();
bool editHappened = (!oldData.has_value()) || (a->d != oldData);
oldData = a->d;
TextBoxEditToolAllData newData = get_all_data(a);
bool editHappened = newData != oldData;
if(editHappened)
oldData = newData;
return editHappened;
}
@ -34,20 +37,21 @@ void TextBoxEditTool::commit_edit_updates(const std::shared_ptr<DrawComponent>&
std::shared_ptr<DrawTextBox> a = std::static_pointer_cast<DrawTextBox>(comp);
a->d.editing = false;
DrawTextBox::Data pData = std::any_cast<DrawTextBox::Data>(prevData);
DrawTextBox::Data cData = a->d;
auto pData = std::any_cast<TextBoxEditToolAllData>(prevData);
TextBoxEditToolAllData cData = get_all_data(a);
RichTextBox::RichTextData richText = a->textBox->get_rich_text_data();
drawP.world.undo.push(UndoManager::UndoRedoPair{
[&, a, pData]() {
a->d = pData;
a->set_textbox_string(pData.currentText);
[&drawP = drawP, a, pData]() {
a->d = pData.textboxData;
a->textBox->set_rich_text_data(pData.richText);
a->d.editing = false;
a->client_send_update(drawP, true);
a->commit_update(drawP);
return true;
},
[&, a, cData]() {
a->d = cData;
a->set_textbox_string(cData.currentText);
[&drawP = drawP, a, cData]() {
a->d = cData.textboxData;
a->textBox->set_rich_text_data(cData.richText);
a->d.editing = false;
a->client_send_update(drawP, true);
a->commit_update(drawP);
@ -56,6 +60,13 @@ void TextBoxEditTool::commit_edit_updates(const std::shared_ptr<DrawComponent>&
});
}
TextBoxEditTool::TextBoxEditToolAllData TextBoxEditTool::get_all_data(const std::shared_ptr<DrawTextBox>& a) {
return {
.textboxData = a->d,
.richText = a->textBox->get_rich_text_data()
};
}
void TextBoxEditTool::edit_start(EditTool& editTool, const std::shared_ptr<DrawComponent>& comp, std::any& prevData) {
std::shared_ptr<DrawTextBox> a = std::static_pointer_cast<DrawTextBox>(comp);
auto& cur = a->cursor;
@ -63,7 +74,8 @@ void TextBoxEditTool::edit_start(EditTool& editTool, const std::shared_ptr<DrawC
cur = std::make_shared<RichTextBox::Cursor>();
Vector2f textSelectPos = a->get_mouse_pos(drawP);
textbox->process_mouse_left_button(*cur, textSelectPos, true, false, false);
prevData = a->d;
oldData = get_all_data(a);
prevData = oldData;
a->d.editing = true;
a->client_send_update(drawP, false);
editTool.add_point_handle({&a->d.p1, nullptr, &a->d.p2});
@ -88,15 +100,3 @@ bool TextBoxEditTool::edit_update(const std::shared_ptr<DrawComponent>& comp) {
return true;
}
// Passing a function in, just in case we have to do something before editing the text
void TextBoxEditTool::edit_text(std::function<void()> toRun, const std::shared_ptr<DrawTextBox>& textBox) {
toRun();
textBox->update_contained_string(drawP);
update_textbox_network(textBox);
}
void TextBoxEditTool::update_textbox_network(const std::shared_ptr<DrawTextBox>& textBox) {
textBox->client_send_update(drawP, false);
textBox->commit_update(drawP);
}

View file

@ -15,8 +15,13 @@ class TextBoxEditTool : public DrawingProgramEditToolBase {
virtual bool edit_update(const std::shared_ptr<DrawComponent>& a) override;
virtual bool edit_gui(const std::shared_ptr<DrawComponent>& a) override;
private:
void edit_text(std::function<void()> toRun, const std::shared_ptr<DrawTextBox>& textBox);
void update_textbox_network(const std::shared_ptr<DrawTextBox>& textBox);
struct TextBoxEditToolAllData {
DrawTextBox::Data textboxData;
RichTextBox::RichTextData richText;
bool operator==(const TextBoxEditToolAllData& o) const = default;
bool operator!=(const TextBoxEditToolAllData& o) const = default;
};
TextBoxEditToolAllData get_all_data(const std::shared_ptr<DrawTextBox>& a);
std::optional<DrawTextBox::Data> oldData;
TextBoxEditToolAllData oldData;
};

View file

@ -39,14 +39,13 @@ void TextBoxTool::tool_update() {
controls.endAt = controls.startAt;
controls.intermediateItem->d.p1 = controls.intermediateItem->d.p2 = controls.startAt;
controls.intermediateItem->d.p2 = ensure_points_have_distance(controls.intermediateItem->d.p1, controls.intermediateItem->d.p2, MINIMUM_DISTANCE_BETWEEN_BOUNDS);
controls.intermediateItem->d.textColor = drawP.controls.foregroundColor;
controls.intermediateItem->d.textSize = controls.textRelativeSize;
controls.intermediateItem->d.editing = true;
controls.intermediateItem->coords = drawP.world.drawData.cam.c;
controls.intermediateItem->lastUpdateTime = std::chrono::steady_clock::now();
uint64_t placement = drawP.components.client_list().size();
auto objAdd = drawP.components.client_insert(placement, controls.intermediateItem);
controls.intermediateItem->client_send_place(drawP);
controls.intermediateItem->commit_update(drawP);
drawP.add_undo_place_component(objAdd);
controls.drawStage = 1;
}

View file

@ -216,6 +216,24 @@ RichTextBox::TextPosition RichTextBox::get_text_pos_closest_to_point(Vector2f po
return TextPosition{0, pIndex};
}
RichTextBox::RichTextData RichTextBox::get_rich_text_data() {
return {get_string()};
}
void RichTextBox::clear_text() {
paragraphs.clear();
paragraphs.emplace_back();
}
void RichTextBox::set_string(const std::string& str) {
clear_text();
insert({0, 0}, str);
}
void RichTextBox::set_rich_text_data(const RichTextData& richText) {
set_string(richText.text);
}
std::string RichTextBox::get_string() {
return get_text_between({0, 0}, move(RichTextBox::Movement::END, {0, 0}));
}
@ -244,8 +262,6 @@ std::string RichTextBox::get_text_between(TextPosition p1, TextPosition p2) {
}
RichTextBox::TextPosition RichTextBox::move(Movement movement, TextPosition pos, std::optional<float>* previousX, bool flipDependingOnTextDirection) {
rebuild();
if(pos.fParagraphIndex >= paragraphs.size()) {
pos.fParagraphIndex = paragraphs.size() - 1;
pos.fTextByteIndex = paragraphs[pos.fParagraphIndex].text.size();
@ -254,15 +270,18 @@ RichTextBox::TextPosition RichTextBox::move(Movement movement, TextPosition pos,
if(pos.fTextByteIndex >= paragraphs[pos.fParagraphIndex].text.size())
pos.fTextByteIndex = paragraphs[pos.fParagraphIndex].text.size();
if(flipDependingOnTextDirection && paragraphs[pos.fParagraphIndex].pStyle.getTextDirection() == skia::textlayout::TextDirection::kRtl) {
if(movement == Movement::LEFT)
movement = Movement::RIGHT;
else if(movement == Movement::RIGHT)
movement = Movement::LEFT;
else if(movement == Movement::LEFT_WORD)
movement = Movement::RIGHT_WORD;
else if(movement == Movement::RIGHT_WORD)
movement = Movement::LEFT_WORD;
if(movement != Movement::NOWHERE && movement != Movement::HOME && movement != Movement::END) {
rebuild();
if(flipDependingOnTextDirection && paragraphs[pos.fParagraphIndex].pStyle.getTextDirection() == skia::textlayout::TextDirection::kRtl) {
if(movement == Movement::LEFT)
movement = Movement::RIGHT;
else if(movement == Movement::RIGHT)
movement = Movement::LEFT;
else if(movement == Movement::LEFT_WORD)
movement = Movement::RIGHT_WORD;
else if(movement == Movement::RIGHT_WORD)
movement = Movement::LEFT_WORD;
}
}
switch (movement) {
@ -430,8 +449,10 @@ void RichTextBox::set_allow_newlines(bool allow) {
}
void RichTextBox::set_font_collection(const sk_sp<skia::textlayout::FontCollection>& fC) {
fontCollection = fC;
needsRebuild = true;
if(fontCollection != fC) {
fontCollection = fC;
needsRebuild = true;
}
}
void RichTextBox::rebuild() {

View file

@ -47,6 +47,18 @@ class RichTextBox {
std::optional<Cursor> cursor;
};
struct RichTextData {
std::string text;
bool operator==(const RichTextData& o) const = default;
bool operator!=(const RichTextData& o) const = default;
template <typename Archive> void save(Archive& a) const {
a(text);
}
template <typename Archive> void load(Archive& a) {
a(text);
}
};
TextPosition move(Movement movement, TextPosition pos, std::optional<float>* previousX = nullptr, bool flipDependingOnTextDirection = false);
void set_width(float newWidth);
TextPosition insert(TextPosition pos, std::string_view textToInsert);
@ -69,6 +81,11 @@ class RichTextBox {
SELECT_ALL
};
RichTextData get_rich_text_data();
void set_rich_text_data(const RichTextData& richText);
void clear_text();
void set_string(const std::string& str);
void process_mouse_left_button(Cursor& cur, const Vector2f& pos, bool clicked, bool held, bool shift);
void process_key_input(Cursor& cur, InputKey in, bool ctrl, bool shift);
std::string process_copy(Cursor& cur);