2025-08-02 20:10:33 +03:00
|
|
|
#include "TextBoxEditTool.hpp"
|
|
|
|
|
#include "DrawingProgram.hpp"
|
|
|
|
|
#include "../MainProgram.hpp"
|
|
|
|
|
#include "../DrawData.hpp"
|
2025-11-08 20:50:23 +03:00
|
|
|
#include "Helpers/ConvertVec.hpp"
|
2025-08-02 20:10:33 +03:00
|
|
|
#include "Helpers/SCollision.hpp"
|
|
|
|
|
#include <cereal/types/vector.hpp>
|
2025-11-04 17:31:23 +03:00
|
|
|
#include <include/core/SkFontStyle.h>
|
2025-11-08 20:50:23 +03:00
|
|
|
#include <modules/skparagraph/include/DartTypes.h>
|
|
|
|
|
#include <modules/skparagraph/include/ParagraphStyle.h>
|
2025-11-09 16:24:40 +03:00
|
|
|
#include <modules/skparagraph/include/TextStyle.h>
|
2025-11-08 20:50:23 +03:00
|
|
|
#include <modules/skparagraph/src/ParagraphBuilderImpl.h>
|
|
|
|
|
#include <modules/skunicode/include/SkUnicode_icu.h>
|
2025-08-02 20:10:33 +03:00
|
|
|
#include <memory>
|
|
|
|
|
#include "EditTool.hpp"
|
|
|
|
|
|
2025-11-10 14:45:51 +03:00
|
|
|
using namespace RichText;
|
|
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
TextBoxEditTool::TextBoxEditTool(DrawingProgram& initDrawP):
|
|
|
|
|
DrawingProgramEditToolBase(initDrawP)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
bool TextBoxEditTool::edit_gui(const std::shared_ptr<DrawComponent>& comp) {
|
2025-11-10 14:45:51 +03:00
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
std::shared_ptr<DrawTextBox> a = std::static_pointer_cast<DrawTextBox>(comp);
|
|
|
|
|
Toolbar& t = drawP.world.main.toolbar;
|
2025-11-05 12:29:31 +03:00
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
t.gui.push_id("edit tool text");
|
|
|
|
|
t.gui.text_label_centered("Edit Text");
|
2025-11-08 20:50:23 +03:00
|
|
|
|
|
|
|
|
t.gui.left_to_right_line_layout([&]() {
|
|
|
|
|
t.gui.text_label("Font");
|
2025-11-09 16:24:40 +03:00
|
|
|
if(t.gui.font_picker("font picker", &newFontName)) {
|
|
|
|
|
currentMods[TextStyleModifier::ModifierType::FONT_FAMILIES] = std::make_shared<FontFamiliesTextStyleModifier>(std::vector<SkString>{SkString{newFontName.c_str(), newFontName.size()}});
|
|
|
|
|
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::FONT_FAMILIES]);
|
|
|
|
|
}
|
2025-11-08 20:50:23 +03:00
|
|
|
});
|
2025-11-05 12:29:31 +03:00
|
|
|
|
2025-11-05 18:21:49 +03:00
|
|
|
t.gui.left_to_right_line_centered_layout([&]() {
|
2025-11-09 16:24:40 +03:00
|
|
|
if(t.gui.svg_icon_button("Bold button", "data/icons/RemixIcon/bold.svg", newIsBold)) {
|
|
|
|
|
newIsBold = !newIsBold;
|
|
|
|
|
currentMods[TextStyleModifier::ModifierType::WEIGHT] = std::make_shared<WeightTextStyleModifier>(newIsBold ? SkFontStyle::Weight::kBold_Weight : SkFontStyle::Weight::kNormal_Weight);
|
|
|
|
|
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::WEIGHT]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(t.gui.svg_icon_button("Italic button", "data/icons/RemixIcon/italic.svg", newIsItalic)) {
|
|
|
|
|
newIsItalic = !newIsItalic;
|
|
|
|
|
currentMods[TextStyleModifier::ModifierType::SLANT] = std::make_shared<SlantTextStyleModifier>(newIsItalic ? SkFontStyle::Slant::kItalic_Slant : SkFontStyle::Slant::kUpright_Slant);
|
|
|
|
|
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::SLANT]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool decorationEdited = false;
|
|
|
|
|
if(t.gui.svg_icon_button("Underline button", "data/icons/RemixIcon/underline.svg", newIsUnderlined)) {
|
|
|
|
|
newIsUnderlined = !newIsUnderlined;
|
|
|
|
|
decorationEdited = true;
|
|
|
|
|
}
|
|
|
|
|
if(t.gui.svg_icon_button("Strikethrough button", "data/icons/RemixIcon/strikethrough.svg", newIsLinethrough)) {
|
|
|
|
|
newIsLinethrough = !newIsLinethrough;
|
|
|
|
|
decorationEdited = true;
|
|
|
|
|
}
|
|
|
|
|
if(t.gui.svg_icon_button("Overline button", "data/icons/RemixIcon/overline.svg", newIsOverline)) {
|
|
|
|
|
newIsOverline = !newIsOverline;
|
|
|
|
|
decorationEdited = true;
|
|
|
|
|
}
|
|
|
|
|
if(decorationEdited) {
|
|
|
|
|
currentMods[TextStyleModifier::ModifierType::DECORATION] = std::make_shared<DecorationTextStyleModifier>(get_new_decoration_value());
|
|
|
|
|
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::DECORATION]);
|
|
|
|
|
}
|
2025-11-05 18:21:49 +03:00
|
|
|
});
|
|
|
|
|
|
2025-11-09 16:24:40 +03:00
|
|
|
if(t.gui.slider_scalar_field<uint32_t>("Font Size Slider", "Font Size", &newFontSize, 3, 100)) {
|
|
|
|
|
currentMods[TextStyleModifier::ModifierType::SIZE] = std::make_shared<SizeTextStyleModifier>(newFontSize);
|
|
|
|
|
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::SIZE]);
|
|
|
|
|
}
|
2025-11-09 15:01:57 +03:00
|
|
|
|
2025-11-05 18:21:49 +03:00
|
|
|
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", &newTextColor, &newTextColor == t.colorRight)) {
|
|
|
|
|
t.color_selector_right(&newTextColor == t.colorRight ? nullptr : &newTextColor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
t.gui.text_label("Text Color");
|
|
|
|
|
});
|
2025-11-09 16:24:40 +03:00
|
|
|
if((&newTextColor == t.colorRight) && t.isUpdatingColorRight) {
|
|
|
|
|
currentMods[TextStyleModifier::ModifierType::COLOR] = std::make_shared<ColorTextStyleModifier>(newTextColor);
|
|
|
|
|
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::COLOR]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 14:55:22 +03:00
|
|
|
|
2025-11-08 20:50:23 +03:00
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
t.gui.pop_id();
|
2025-10-25 17:31:55 +03:00
|
|
|
|
2025-11-09 16:24:40 +03:00
|
|
|
if(a->textBox->inputChangedTextBox) {
|
2025-11-05 14:55:22 +03:00
|
|
|
set_mods_at_selection(a);
|
2025-11-09 16:24:40 +03:00
|
|
|
newFontName = std::static_pointer_cast<FontFamiliesTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::FONT_FAMILIES])->get_families().back().c_str();
|
|
|
|
|
newFontSize = std::static_pointer_cast<SizeTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::SIZE])->get_size();
|
|
|
|
|
newTextColor = std::static_pointer_cast<ColorTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::COLOR])->get_color();
|
|
|
|
|
newIsBold = std::static_pointer_cast<WeightTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::WEIGHT])->get_weight() == SkFontStyle::Weight::kBold_Weight;
|
|
|
|
|
newIsItalic = std::static_pointer_cast<SlantTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::SLANT])->get_slant() == SkFontStyle::Slant::kItalic_Slant;
|
|
|
|
|
newIsUnderlined = std::static_pointer_cast<DecorationTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::DECORATION])->get_decoration_value() & skia::textlayout::TextDecoration::kUnderline;
|
|
|
|
|
newIsLinethrough = std::static_pointer_cast<DecorationTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::DECORATION])->get_decoration_value() & skia::textlayout::TextDecoration::kLineThrough;
|
|
|
|
|
newIsOverline = std::static_pointer_cast<DecorationTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::DECORATION])->get_decoration_value() & skia::textlayout::TextDecoration::kOverline;
|
|
|
|
|
}
|
2025-11-05 12:29:31 +03:00
|
|
|
|
|
|
|
|
// NOTE: There should be a system that periodically sends updates even if no changes are made, since unreliable channels can drop update data
|
|
|
|
|
bool oldInputChangedTextBox = a->textBox->inputChangedTextBox;
|
|
|
|
|
a->textBox->inputChangedTextBox = false;
|
|
|
|
|
return oldInputChangedTextBox;
|
2025-08-02 20:10:33 +03:00
|
|
|
}
|
|
|
|
|
|
2025-11-09 16:24:40 +03:00
|
|
|
uint8_t TextBoxEditTool::get_new_decoration_value() {
|
|
|
|
|
uint8_t toRet = skia::textlayout::TextDecoration::kNoDecoration;
|
|
|
|
|
if(newIsUnderlined)
|
|
|
|
|
toRet |= skia::textlayout::TextDecoration::kUnderline;
|
|
|
|
|
if(newIsLinethrough)
|
|
|
|
|
toRet |= skia::textlayout::TextDecoration::kLineThrough;
|
|
|
|
|
if(newIsOverline)
|
|
|
|
|
toRet |= skia::textlayout::TextDecoration::kOverline;
|
|
|
|
|
return toRet;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 14:55:22 +03:00
|
|
|
void TextBoxEditTool::set_mods_at_selection(const std::shared_ptr<DrawTextBox>& a) {
|
2025-11-10 14:45:51 +03:00
|
|
|
TextPosition start = std::min(a->cursor->selectionBeginPos, a->cursor->selectionEndPos);
|
|
|
|
|
TextPosition end = std::max(a->cursor->selectionBeginPos, a->cursor->selectionEndPos);
|
2025-11-05 14:55:22 +03:00
|
|
|
if(start == end)
|
2025-11-10 14:45:51 +03:00
|
|
|
currentMods = a->textBox->get_mods_used_at_pos(a->textBox->move(TextBox::Movement::LEFT, start));
|
2025-11-05 14:55:22 +03:00
|
|
|
else
|
2025-11-09 16:24:40 +03:00
|
|
|
currentMods = a->textBox->get_mods_used_at_pos(start);
|
2025-11-05 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
void TextBoxEditTool::commit_edit_updates(const std::shared_ptr<DrawComponent>& comp, std::any& prevData) {
|
|
|
|
|
std::shared_ptr<DrawTextBox> a = std::static_pointer_cast<DrawTextBox>(comp);
|
|
|
|
|
|
|
|
|
|
a->d.editing = false;
|
2025-10-25 17:31:55 +03:00
|
|
|
auto pData = std::any_cast<TextBoxEditToolAllData>(prevData);
|
|
|
|
|
TextBoxEditToolAllData cData = get_all_data(a);
|
2025-11-10 14:45:51 +03:00
|
|
|
TextData richText = a->textBox->get_rich_text_data();
|
2025-08-02 20:10:33 +03:00
|
|
|
drawP.world.undo.push(UndoManager::UndoRedoPair{
|
2025-10-25 17:31:55 +03:00
|
|
|
[&drawP = drawP, a, pData]() {
|
|
|
|
|
a->d = pData.textboxData;
|
|
|
|
|
a->textBox->set_rich_text_data(pData.richText);
|
2025-08-02 20:10:33 +03:00
|
|
|
a->d.editing = false;
|
2025-10-01 08:59:44 +03:00
|
|
|
a->client_send_update(drawP, true);
|
2025-08-02 20:10:33 +03:00
|
|
|
a->commit_update(drawP);
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2025-10-25 17:31:55 +03:00
|
|
|
[&drawP = drawP, a, cData]() {
|
|
|
|
|
a->d = cData.textboxData;
|
|
|
|
|
a->textBox->set_rich_text_data(cData.richText);
|
2025-08-02 20:10:33 +03:00
|
|
|
a->d.editing = false;
|
2025-10-01 08:59:44 +03:00
|
|
|
a->client_send_update(drawP, true);
|
2025-08-02 20:10:33 +03:00
|
|
|
a->commit_update(drawP);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 17:31:55 +03:00
|
|
|
TextBoxEditTool::TextBoxEditToolAllData TextBoxEditTool::get_all_data(const std::shared_ptr<DrawTextBox>& a) {
|
|
|
|
|
return {
|
|
|
|
|
.textboxData = a->d,
|
|
|
|
|
.richText = a->textBox->get_rich_text_data()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
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);
|
2025-10-20 17:30:36 +03:00
|
|
|
auto& cur = a->cursor;
|
2025-08-02 20:10:33 +03:00
|
|
|
auto& textbox = a->textBox;
|
2025-11-10 14:45:51 +03:00
|
|
|
cur = std::make_shared<TextBox::Cursor>();
|
2025-08-02 20:10:33 +03:00
|
|
|
Vector2f textSelectPos = a->get_mouse_pos(drawP);
|
2025-10-20 17:30:36 +03:00
|
|
|
textbox->process_mouse_left_button(*cur, textSelectPos, true, false, false);
|
2025-11-04 10:58:26 +03:00
|
|
|
prevData = get_all_data(a);
|
2025-08-02 20:10:33 +03:00
|
|
|
a->d.editing = true;
|
2025-11-05 12:29:31 +03:00
|
|
|
a->textBox->inputChangedTextBox = true;
|
|
|
|
|
|
|
|
|
|
a->commit_update(drawP);
|
2025-10-01 08:59:44 +03:00
|
|
|
a->client_send_update(drawP, false);
|
2025-11-05 12:29:31 +03:00
|
|
|
|
2025-11-05 14:55:22 +03:00
|
|
|
set_mods_at_selection(a);
|
2025-11-05 12:29:31 +03:00
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
editTool.add_point_handle({&a->d.p1, nullptr, &a->d.p2});
|
|
|
|
|
editTool.add_point_handle({&a->d.p2, &a->d.p1, nullptr});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TextBoxEditTool::edit_update(const std::shared_ptr<DrawComponent>& comp) {
|
|
|
|
|
std::shared_ptr<DrawTextBox> a = std::static_pointer_cast<DrawTextBox>(comp);
|
|
|
|
|
|
|
|
|
|
SCollision::ColliderCollection<float> mousePointCollection;
|
|
|
|
|
mousePointCollection.circle.emplace_back(drawP.world.main.input.mouse.pos, 1.0f);
|
|
|
|
|
mousePointCollection.recalculate_bounds();
|
|
|
|
|
|
2025-10-20 17:30:36 +03:00
|
|
|
a->textBox->set_width(std::abs(a->d.p2.x() - a->d.p1.x()));
|
2025-08-02 20:10:33 +03:00
|
|
|
|
|
|
|
|
InputManager& input = drawP.world.main.input;
|
|
|
|
|
|
2025-10-20 17:30:36 +03:00
|
|
|
bool collidesWithBox = a->collides_with_cam_coords(drawP.world.drawData.cam.c, mousePointCollection);
|
2025-08-02 20:10:33 +03:00
|
|
|
|
2025-11-09 16:24:40 +03:00
|
|
|
input.text.set_rich_text_box_input(a->textBox, a->cursor, currentMods);
|
2025-10-20 17:30:36 +03:00
|
|
|
a->textBox->process_mouse_left_button(*a->cursor, a->get_mouse_pos(drawP), drawP.controls.leftClick && collidesWithBox, drawP.controls.leftClickHeld, input.key(InputManager::KEY_GENERIC_LSHIFT).held);
|
2025-08-02 20:10:33 +03:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|