2026-04-03 23:02:33 +03:00
|
|
|
#include "RichTextUserInput.hpp"
|
2026-04-04 17:52:43 +03:00
|
|
|
#include "CustomEvents.hpp"
|
2026-04-03 23:02:33 +03:00
|
|
|
|
|
|
|
|
template <typename T> std::optional<T> shared_ptr_to_opt(const std::shared_ptr<T> o) {
|
|
|
|
|
if(o)
|
|
|
|
|
return *o;
|
|
|
|
|
else
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 17:52:43 +03:00
|
|
|
RichTextUserInput::RichTextUserInput(const std::shared_ptr<RichText::TextBox>& initTextBox, const std::shared_ptr<RichText::TextBox::Cursor>& initCursor, const std::shared_ptr<RichText::TextStyleModifier::ModifierMap>& initModMap):
|
2026-04-03 23:02:33 +03:00
|
|
|
textBox(initTextBox),
|
|
|
|
|
cursor(initCursor),
|
2026-04-04 17:52:43 +03:00
|
|
|
modMap(initModMap)
|
2026-04-03 23:02:33 +03:00
|
|
|
{}
|
|
|
|
|
|
2026-04-11 16:36:24 +03:00
|
|
|
bool RichTextUserInput::input_paste_callback(const CustomEvents::PasteEvent& paste) {
|
|
|
|
|
if(paste.type == CustomEvents::PasteEvent::DataType::TEXT) {
|
2026-04-04 17:52:43 +03:00
|
|
|
do_textbox_operation_with_undo([&]() {
|
|
|
|
|
if(paste.richText.has_value())
|
|
|
|
|
textBox->process_rich_text_input(*cursor, paste.richText.value());
|
|
|
|
|
else
|
|
|
|
|
textBox->process_text_input(*cursor, paste.data, shared_ptr_to_opt(modMap));
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 23:02:33 +03:00
|
|
|
void RichTextUserInput::add_text_to_textbox(const std::string& inputText) {
|
|
|
|
|
do_textbox_operation_with_undo([&]() {
|
|
|
|
|
textBox->process_text_input(*cursor, inputText, shared_ptr_to_opt(modMap));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RichTextUserInput::add_textbox_undo(const RichText::TextBox::Cursor& prevCursor, const RichText::TextData& prevRichText) {
|
|
|
|
|
textboxUndo.push({[&, prevCursor = prevCursor, prevRichText = prevRichText]() {
|
|
|
|
|
textBox->set_rich_text_data_for_undo_redo(prevRichText);
|
|
|
|
|
cursor->selectionBeginPos = cursor->selectionEndPos = cursor->pos = std::max(prevCursor.selectionEndPos, prevCursor.selectionBeginPos);
|
|
|
|
|
cursor->previousX = std::nullopt;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
[&, currentCursor = *cursor, currentRichText = textBox->get_rich_text_data()]() {
|
|
|
|
|
textBox->set_rich_text_data_for_undo_redo(currentRichText);
|
|
|
|
|
cursor->selectionBeginPos = cursor->selectionEndPos = cursor->pos = std::max(currentCursor.selectionEndPos, currentCursor.selectionBeginPos);
|
|
|
|
|
cursor->previousX = std::nullopt;
|
|
|
|
|
return true;
|
|
|
|
|
}});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RichTextUserInput::do_textbox_operation_with_undo(const std::function<void()>& func) {
|
|
|
|
|
auto prevRichText = textBox->get_rich_text_data();
|
|
|
|
|
auto prevCursor = *cursor;
|
|
|
|
|
func();
|
|
|
|
|
add_textbox_undo(prevCursor, prevRichText);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 17:52:43 +03:00
|
|
|
RichTextUserInput::Changes RichTextUserInput::input_key_callback(InputManager& input, const InputManager::KeyCallbackArgs& key) {
|
2026-04-03 23:02:33 +03:00
|
|
|
std::optional<RichText::TextStyleModifier::ModifierMap> modMapOpt = shared_ptr_to_opt(modMap);
|
|
|
|
|
|
2026-04-04 17:52:43 +03:00
|
|
|
Changes toRet;
|
|
|
|
|
|
2026-04-03 23:02:33 +03:00
|
|
|
if(key.down) {
|
2026-04-04 17:52:43 +03:00
|
|
|
auto oldCursor = *cursor;
|
|
|
|
|
|
2026-04-03 23:02:33 +03:00
|
|
|
switch(key.key) {
|
|
|
|
|
case InputManager::KEY_GENERIC_UP:
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::UP, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_GENERIC_DOWN:
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::DOWN, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_GENERIC_LEFT:
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::LEFT, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_GENERIC_RIGHT:
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::RIGHT, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_BACKSPACE:
|
|
|
|
|
do_textbox_operation_with_undo([&] {
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::BACKSPACE, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
});
|
2026-04-04 17:52:43 +03:00
|
|
|
toRet.textEdited = true;
|
2026-04-03 23:02:33 +03:00
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_DELETE:
|
|
|
|
|
do_textbox_operation_with_undo([&] {
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::DEL, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
});
|
2026-04-04 17:52:43 +03:00
|
|
|
toRet.textEdited = true;
|
2026-04-03 23:02:33 +03:00
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_HOME:
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::HOME, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_END:
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::END, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_COPY:
|
|
|
|
|
input.set_clipboard_plain_and_richtext_pair(textBox->process_copy(*cursor));
|
|
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_CUT:
|
|
|
|
|
do_textbox_operation_with_undo([&] {
|
|
|
|
|
input.set_clipboard_plain_and_richtext_pair(textBox->process_cut(*cursor));
|
|
|
|
|
});
|
2026-04-04 17:52:43 +03:00
|
|
|
toRet.textEdited = true;
|
2026-04-03 23:02:33 +03:00
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_PASTE:
|
2026-04-11 16:36:24 +03:00
|
|
|
input.call_paste(CustomEvents::PasteEvent::DataType::TEXT);
|
2026-04-03 23:02:33 +03:00
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_SELECTALL:
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::SELECT_ALL, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_UNDO:
|
|
|
|
|
textboxUndo.undo();
|
2026-04-04 17:52:43 +03:00
|
|
|
toRet.textEdited = true;
|
2026-04-03 23:02:33 +03:00
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_REDO:
|
|
|
|
|
textboxUndo.redo();
|
2026-04-04 17:52:43 +03:00
|
|
|
toRet.textEdited = true;
|
2026-04-03 23:02:33 +03:00
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_GENERIC_ENTER:
|
|
|
|
|
do_textbox_operation_with_undo([&] {
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::ENTER, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
});
|
2026-04-04 17:52:43 +03:00
|
|
|
toRet.textEdited = true;
|
2026-04-03 23:02:33 +03:00
|
|
|
break;
|
|
|
|
|
case InputManager::KEY_TEXT_TAB:
|
|
|
|
|
do_textbox_operation_with_undo([&] {
|
|
|
|
|
textBox->process_key_input(*cursor, RichText::TextBox::InputKey::TAB, input.ctrl_or_meta_held(), input.key(InputManager::KEY_GENERIC_LSHIFT).held, modMapOpt);
|
|
|
|
|
});
|
2026-04-04 17:52:43 +03:00
|
|
|
toRet.textEdited = true;
|
2026-04-03 23:02:33 +03:00
|
|
|
break;
|
|
|
|
|
default:
|
2026-04-04 17:52:43 +03:00
|
|
|
return toRet;
|
2026-04-03 23:02:33 +03:00
|
|
|
}
|
2026-04-04 17:52:43 +03:00
|
|
|
toRet.cursorChanged = oldCursor != *cursor;
|
2026-04-03 23:02:33 +03:00
|
|
|
}
|
2026-04-04 17:52:43 +03:00
|
|
|
return toRet;
|
2026-04-03 23:02:33 +03:00
|
|
|
}
|