infinipaint/src/DrawingProgram/Tools/EditTools/TextBoxEditTool.cpp

343 lines
19 KiB
C++
Raw Normal View History

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>
#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>
#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-08-02 20:10:33 +03:00
#include "../../../GUIStuff/ElementHelpers/TextLabelHelpers.hpp"
#include "../../../GUIStuff/ElementHelpers/LayoutHelpers.hpp"
#include "../../../GUIStuff/Elements/PositionAdjustingPopupMenu.hpp"
using namespace RichText;
2025-08-02 20:10:33 +03:00
TextBoxEditTool::TextBoxEditTool(DrawingProgram& initDrawP):
DrawingProgramEditToolBase(initDrawP)
{}
void TextBoxEditTool::edit_gui(CanvasComponentContainer::ObjInfo* comp) {
using namespace GUIStuff;
using namespace ElementHelpers;
auto& a = static_cast<TextBoxCanvasComponent&>(comp->obj->get_comp());
2025-08-02 20:10:33 +03:00
Toolbar& t = drawP.world.main.toolbar;
2025-11-05 12:29:31 +03:00
auto& currentMods = *currentModsPtr;
2025-08-02 20:10:33 +03:00
t.gui.push_id("edit tool text");
text_label_centered(t.gui, "Edit Text");
left_to_right_line_layout(t.gui, [&] {
text_label(t.gui, "Font");
//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()}});
// add_undo_if_selecting_area(a, [&]() {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
//left_to_right_line_centered_layout(t.gui, [&] {
// if(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);
// add_undo_if_selecting_area(a, [&]() {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);
// add_undo_if_selecting_area(a, [&]() {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());
// add_undo_if_selecting_area(a, [&]() {a.textBox->set_text_style_modifier_between(a.cursor->selectionBeginPos, a.cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::DECORATION]);});
// }
//});
//t.gui.left_to_right_line_centered_layout([&]() {
// if(t.gui.svg_icon_button("Align left button", "data/icons/RemixIcon/align-left.svg", currentPStyle.textAlignment == skia::textlayout::TextAlign::kLeft))
// add_undo([&](){a.textBox->set_text_alignment_between(a.cursor->selectionBeginPos.fParagraphIndex, a.cursor->selectionEndPos.fParagraphIndex, skia::textlayout::TextAlign::kLeft);});
// if(t.gui.svg_icon_button("Align center button", "data/icons/RemixIcon/align-center.svg", currentPStyle.textAlignment == skia::textlayout::TextAlign::kCenter))
// add_undo([&](){a.textBox->set_text_alignment_between(a.cursor->selectionBeginPos.fParagraphIndex, a.cursor->selectionEndPos.fParagraphIndex, skia::textlayout::TextAlign::kCenter);});
// if(t.gui.svg_icon_button("Align right button", "data/icons/RemixIcon/align-right.svg", currentPStyle.textAlignment == skia::textlayout::TextAlign::kRight))
// add_undo([&](){a.textBox->set_text_alignment_between(a.cursor->selectionBeginPos.fParagraphIndex, a.cursor->selectionEndPos.fParagraphIndex, skia::textlayout::TextAlign::kRight);});
// if(t.gui.svg_icon_button("Align justify button", "data/icons/RemixIcon/align-justify.svg", currentPStyle.textAlignment == skia::textlayout::TextAlign::kJustify))
// add_undo([&](){a.textBox->set_text_alignment_between(a.cursor->selectionBeginPos.fParagraphIndex, a.cursor->selectionEndPos.fParagraphIndex, skia::textlayout::TextAlign::kJustify);});
// if(t.gui.svg_icon_button("Text direction left", "data/icons/RemixIcon/text-direction-l.svg", currentPStyle.textDirection == skia::textlayout::TextDirection::kLtr))
// add_undo([&](){a.textBox->set_text_direction_between(a.cursor->selectionBeginPos.fParagraphIndex, a.cursor->selectionEndPos.fParagraphIndex, skia::textlayout::TextDirection::kLtr);});
// if(t.gui.svg_icon_button("Text direction right", "data/icons/RemixIcon/text-direction-r.svg", currentPStyle.textDirection == skia::textlayout::TextDirection::kRtl))
// add_undo([&](){a.textBox->set_text_direction_between(a.cursor->selectionBeginPos.fParagraphIndex, a.cursor->selectionEndPos.fParagraphIndex, skia::textlayout::TextDirection::kRtl);});
//});
//if(t.gui.slider_scalar_field<uint32_t>("Font Size Slider", "Font Size", &newFontSize, 3, 100)) {
// hold_undo_data("Font Size", a);
// 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]);
//}
//else
// release_undo_data("Font Size");
//// Text color
//t.gui.left_to_right_line_layout([&]() {
// if(t.gui.color_button_big("Text Color", &newTextColor, &newTextColor == t.colorRight))
// t.color_selector_right(&newTextColor == t.colorRight ? nullptr : &newTextColor);
// t.gui.text_label("Text Color");
//});
//if(&newTextColor == t.colorRight)
// hold_undo_data("Text Color", a);
//else
// release_undo_data("Text Color");
//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]);
//}
//// Highlight color
//t.gui.left_to_right_line_layout([&]() {
// if(t.gui.color_button_big("Highlight Color", &newHighlightColor, &newHighlightColor == t.colorRight)) {
// if(newHighlightColor.w() == 0.0f) { // Make highlight appear when the button is pressed
// newHighlightColor.w() = 1.0f;
// currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR] = std::make_shared<HighlightColorTextStyleModifier>(newHighlightColor);
// add_undo_if_selecting_area(a, [&]() {a.textBox->set_text_style_modifier_between(a.cursor->selectionBeginPos, a.cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR]);});
// }
// t.color_selector_right(&newHighlightColor == t.colorRight ? nullptr : &newHighlightColor);
// }
// if(newHighlightColor.w() != 0.0f) {
// if(t.gui.svg_icon_button("Remove Highlight Color", "data/icons/close.svg")) {
// newHighlightColor = {0.0f, 0.0f, 0.0f, 0.0f};
// currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR] = std::make_shared<HighlightColorTextStyleModifier>(newHighlightColor);
// add_undo_if_selecting_area(a, [&]() {a.textBox->set_text_style_modifier_between(a.cursor->selectionBeginPos, a.cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR]);});
// }
// }
// t.gui.text_label("Highlight Color");
//});
//if(&newHighlightColor == t.colorRight)
// hold_undo_data("Highlight Color", a);
//else
// release_undo_data("Highlight Color");
//if((&newHighlightColor == t.colorRight) && t.isUpdatingColorRight) {
// currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR] = std::make_shared<HighlightColorTextStyleModifier>(newHighlightColor);
// a.textBox->set_text_style_modifier_between(a.cursor->selectionBeginPos, a.cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR]);
//}
//t.gui.pop_id();
//if(a.textBox->inputChangedTextBox)
// set_styles_at_selection(a);
//// 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
}
2026-03-07 13:00:10 +03:00
void TextBoxEditTool::input_key_callback(CanvasComponentContainer::ObjInfo* comp, const InputManager::KeyCallbackArgs& key) {
}
void TextBoxEditTool::input_mouse_button_on_canvas_callback(CanvasComponentContainer::ObjInfo* comp, const InputManager::MouseButtonCallbackArgs& button, bool isDraggingPoint) {
if(button.button == InputManager::MouseButton::LEFT && !isDraggingPoint) {
auto& a = static_cast<TextBoxCanvasComponent&>(comp->obj->get_comp());
SCollision::ColliderCollection<float> mousePointCollection;
mousePointCollection.circle.emplace_back(button.pos, 1.0f);
mousePointCollection.recalculate_bounds();
InputManager& input = drawP.world.main.input;
bool collidesWithBox = comp->obj->collides_with_cam_coords(drawP.world.drawData.cam.c, mousePointCollection);
a.textBox->process_mouse_left_button(*a.cursor, a.get_mouse_pos(drawP), (button.down && button.clicks && collidesWithBox) ? button.clicks : 0, button.down, input.key(InputManager::KEY_GENERIC_LSHIFT).held);
}
}
void TextBoxEditTool::input_mouse_motion_callback(CanvasComponentContainer::ObjInfo* comp, const InputManager::MouseMotionCallbackArgs& motion, bool isDraggingPoint) {
auto& a = static_cast<TextBoxCanvasComponent&>(comp->obj->get_comp());
a.init_text_box(drawP);
if(!isDraggingPoint) {
auto& a = static_cast<TextBoxCanvasComponent&>(comp->obj->get_comp());
SCollision::ColliderCollection<float> mousePointCollection;
mousePointCollection.circle.emplace_back(motion.pos, 1.0f);
mousePointCollection.recalculate_bounds();
InputManager& input = drawP.world.main.input;
a.textBox->process_mouse_left_button(*a.cursor, a.get_mouse_pos(drawP), 0, drawP.controls.leftClickHeld, input.key(InputManager::KEY_GENERIC_LSHIFT).held);
}
}
void TextBoxEditTool::right_click_popup_gui(CanvasComponentContainer::ObjInfo* comp, Vector2f popupPos) {
using namespace GUIStuff;
using namespace ElementHelpers;
2025-12-02 09:53:03 +03:00
Toolbar& t = drawP.world.main.toolbar;
auto& a = static_cast<TextBoxCanvasComponent&>(comp->obj->get_comp());
t.gui.element<PositionAdjustingPopupMenu>("Text popup menu", popupPos, [&, popupPos] {
text_label_light(t.gui, "Text menu");
2025-12-02 09:53:03 +03:00
InputManager& input = drawP.world.main.input;
drawP.popup_menu_action_button("Paste", "Paste", [&] {
2025-12-02 09:53:03 +03:00
input.call_text_paste(true);
});
drawP.popup_menu_action_button("Paste without formatting", "Paste without formatting", [&] {
2025-12-02 09:53:03 +03:00
input.call_text_paste(false);
});
if(a.cursor->selectionBeginPos != a.cursor->selectionEndPos) {
drawP.popup_menu_action_button("Copy", "Copy", [&] {
input.set_clipboard_plain_and_richtext_pair(a.textBox->process_copy(*a.cursor));
});
drawP.popup_menu_action_button("Cut", "Cut", [&] {
2025-12-02 09:53:03 +03:00
input.text.do_textbox_operation_with_undo([&]() {
input.set_clipboard_plain_and_richtext_pair(a.textBox->process_cut(*a.cursor));
2025-12-02 09:53:03 +03:00
});
});
2025-12-02 09:53:03 +03:00
}
});
}
void TextBoxEditTool::hold_undo_data(const std::string& undoName, TextBoxCanvasComponent& a) {
2025-11-12 14:54:46 +03:00
auto it = undoHeldData.find(undoName);
if(it != undoHeldData.end()) {
std::pair<RichText::TextBox::Cursor, RichText::TextData>& undoData = it->second;
if(undoData.first != *a.cursor)
2025-11-12 14:54:46 +03:00
undoHeldData.erase(it);
}
else if(a.cursor->selectionBeginPos != a.cursor->selectionEndPos)
undoHeldData.emplace(undoName, std::pair<RichText::TextBox::Cursor, RichText::TextData>{*a.cursor, a.textBox->get_rich_text_data()});
2025-11-12 14:54:46 +03:00
}
void TextBoxEditTool::release_undo_data(const std::string& undoName) {
auto it = undoHeldData.find(undoName);
if(it != undoHeldData.end()) {
std::pair<RichText::TextBox::Cursor, RichText::TextData>& undoData = it->second;
InputManager& input = drawP.world.main.input;
input.text.add_textbox_undo(undoData.first, undoData.second);
undoHeldData.erase(it);
}
}
void TextBoxEditTool::add_undo_if_selecting_area(TextBoxCanvasComponent& a, const std::function<void()>& func) {
if(a.cursor->selectionBeginPos == a.cursor->selectionEndPos)
2025-11-12 14:54:46 +03:00
func();
else {
InputManager& input = drawP.world.main.input;
input.text.do_textbox_operation_with_undo(func);
}
}
void TextBoxEditTool::add_undo(const std::function<void()>& func) {
InputManager& input = drawP.world.main.input;
input.text.do_textbox_operation_with_undo(func);
}
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;
}
void TextBoxEditTool::set_styles_at_selection(TextBoxCanvasComponent& a) {
auto& currentMods = *currentModsPtr;
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)
currentMods = a.textBox->get_mods_used_at_pos(a.textBox->move(TextBox::Movement::LEFT, start));
2025-11-05 14:55:22 +03:00
else
currentMods = a.textBox->get_mods_used_at_pos(start);
2025-11-10 16:49:02 +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();
2025-11-24 17:45:21 +03:00
newHighlightColor = std::static_pointer_cast<HighlightColorTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR])->get_color();
2025-11-10 16:49:02 +03:00
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;
currentPStyle = a.textBox->get_paragraph_style_data_at(start.fParagraphIndex);
2025-11-05 14:55:22 +03:00
}
void TextBoxEditTool::commit_edit_updates(CanvasComponentContainer::ObjInfo* comp, std::any& prevData) {
auto& a = static_cast<TextBoxCanvasComponent&>(comp->obj->get_comp());
a.d.editing = false;
comp->obj->commit_update(drawP);
2026-03-07 13:00:10 +03:00
drawP.world.main.input.remove_rich_text_box_input(a.textBox);
2025-08-02 20:10:33 +03:00
}
TextBoxEditTool::TextBoxEditToolAllData TextBoxEditTool::get_all_data(const TextBoxCanvasComponent& a) {
return {
.textboxData = a.d,
.richText = a.textBox->get_rich_text_data()
};
}
void TextBoxEditTool::edit_start(EditTool& editTool, CanvasComponentContainer::ObjInfo* comp, std::any& prevData) {
auto& a = static_cast<TextBoxCanvasComponent&>(comp->obj->get_comp());
auto& cur = a.cursor;
auto& textbox = a.textBox;
2026-03-07 13:00:10 +03:00
a.init_text_box(drawP);
cur = std::make_shared<TextBox::Cursor>();
Vector2f textSelectPos = a.get_mouse_pos(drawP);
textbox->process_mouse_left_button(*cur, textSelectPos, 1, false, false);
2025-11-04 10:58:26 +03:00
prevData = get_all_data(a);
a.d.editing = true;
a.textBox->inputChangedTextBox = true;
2025-11-05 12:29:31 +03:00
2025-11-10 16:49:02 +03:00
set_styles_at_selection(a);
2025-11-05 12:29:31 +03:00
editTool.add_point_handle({&a.d.p1, nullptr, &a.d.p2});
editTool.add_point_handle({&a.d.p2, &a.d.p1, nullptr});
2026-01-06 12:36:15 +03:00
comp->obj->commit_update(drawP);
2026-03-07 13:00:10 +03:00
drawP.world.main.input.set_rich_text_box_input_back(a.textBox, a.cursor, true, nullptr, {
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_TEXT,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = true,
.multiline = true,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_TEXT
}, currentModsPtr);
2025-08-02 20:10:33 +03:00
}
bool TextBoxEditTool::edit_update(CanvasComponentContainer::ObjInfo* comp) {
2025-08-02 20:10:33 +03:00
return true;
}