Added undo and redo to textbox
This commit is contained in:
parent
f5d496bc45
commit
94438c7b97
3 changed files with 76 additions and 9 deletions
|
|
@ -89,8 +89,10 @@ void InputManager::Text::set_rich_text_box_input(const std::shared_ptr<RichText:
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputManager::Text::add_text_to_textbox(const std::string& inputText) {
|
void InputManager::Text::add_text_to_textbox(const std::string& inputText) {
|
||||||
if(textBox)
|
if(textBox && !inputText.empty())
|
||||||
|
do_textbox_operation_with_undo([&]() {
|
||||||
textBox->process_text_input(*cursor, inputText, modMap);
|
textBox->process_text_input(*cursor, inputText, modMap);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputManager::Text::set_accepting_input() {
|
void InputManager::Text::set_accepting_input() {
|
||||||
|
|
@ -101,6 +103,32 @@ bool InputManager::Text::get_accepting_input() {
|
||||||
return acceptingInput;
|
return acceptingInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InputManager::Text::add_textbox_undo(const RichText::TextBox::Cursor& prevCursor, const RichText::TextData& prevRichText) {
|
||||||
|
if(textBox) {
|
||||||
|
textboxUndo.push({[textBox = textBox, cursor = cursor, prevCursor = prevCursor, prevRichText = prevRichText]() {
|
||||||
|
textBox->set_rich_text_data(prevRichText);
|
||||||
|
*cursor = prevCursor;
|
||||||
|
cursor->previousX = std::nullopt;
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
[textBox = textBox, cursor = cursor, currentCursor = *cursor, currentRichText = textBox->get_rich_text_data()]() {
|
||||||
|
textBox->set_rich_text_data(currentRichText);
|
||||||
|
*cursor = currentCursor;
|
||||||
|
cursor->previousX = std::nullopt;
|
||||||
|
return true;
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputManager::Text::do_textbox_operation_with_undo(const std::function<void()>& func) {
|
||||||
|
if(textBox) {
|
||||||
|
auto prevRichText = textBox->get_rich_text_data();
|
||||||
|
auto prevCursor = *cursor;
|
||||||
|
func();
|
||||||
|
add_textbox_undo(prevCursor, prevRichText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool InputManager::get_clipboard_paste_happened() {
|
bool InputManager::get_clipboard_paste_happened() {
|
||||||
#ifdef __EMSCRIPTEN__
|
#ifdef __EMSCRIPTEN__
|
||||||
return clipboardPasteEventHappened;
|
return clipboardPasteEventHappened;
|
||||||
|
|
@ -291,12 +319,16 @@ void InputManager::backend_key_down_update(const SDL_KeyboardEvent& e) {
|
||||||
case SDLK_BACKSPACE:
|
case SDLK_BACKSPACE:
|
||||||
set_key_down(e, KEY_TEXT_BACKSPACE);
|
set_key_down(e, KEY_TEXT_BACKSPACE);
|
||||||
if(text.textBox && key(KEY_TEXT_BACKSPACE).repeat)
|
if(text.textBox && key(KEY_TEXT_BACKSPACE).repeat)
|
||||||
|
text.do_textbox_operation_with_undo([&]() {
|
||||||
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::BACKSPACE, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::BACKSPACE, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
case SDLK_DELETE:
|
case SDLK_DELETE:
|
||||||
set_key_down(e, KEY_TEXT_DELETE);
|
set_key_down(e, KEY_TEXT_DELETE);
|
||||||
if(text.textBox && key(KEY_TEXT_DELETE).repeat)
|
if(text.textBox && key(KEY_TEXT_DELETE).repeat)
|
||||||
|
text.do_textbox_operation_with_undo([&]() {
|
||||||
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::DELETE, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::DELETE, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
case SDLK_HOME:
|
case SDLK_HOME:
|
||||||
set_key_down(e, KEY_TEXT_HOME);
|
set_key_down(e, KEY_TEXT_HOME);
|
||||||
|
|
@ -334,13 +366,17 @@ void InputManager::backend_key_down_update(const SDL_KeyboardEvent& e) {
|
||||||
if((kMod & SDL_KMOD_GUI) || (kMod & SDL_KMOD_CTRL))
|
if((kMod & SDL_KMOD_GUI) || (kMod & SDL_KMOD_CTRL))
|
||||||
set_key_down(e, KEY_TEXT_CUT);
|
set_key_down(e, KEY_TEXT_CUT);
|
||||||
if(text.textBox && key(KEY_TEXT_CUT).repeat)
|
if(text.textBox && key(KEY_TEXT_CUT).repeat)
|
||||||
|
text.do_textbox_operation_with_undo([&]() {
|
||||||
set_clipboard_plain_and_richtext_pair(text.textBox->process_cut(*text.cursor));
|
set_clipboard_plain_and_richtext_pair(text.textBox->process_cut(*text.cursor));
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
case SDLK_V:
|
case SDLK_V:
|
||||||
if((kMod & SDL_KMOD_GUI) || (kMod & SDL_KMOD_CTRL))
|
if((kMod & SDL_KMOD_GUI) || (kMod & SDL_KMOD_CTRL))
|
||||||
set_key_down(e, KEY_TEXT_PASTE);
|
set_key_down(e, KEY_TEXT_PASTE);
|
||||||
if(text.textBox && key(KEY_TEXT_PASTE).repeat)
|
if(text.textBox && key(KEY_TEXT_PASTE).repeat)
|
||||||
|
text.do_textbox_operation_with_undo([&]() {
|
||||||
process_text_paste();
|
process_text_paste();
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
case SDLK_A:
|
case SDLK_A:
|
||||||
if((kMod & SDL_KMOD_GUI) || (kMod & SDL_KMOD_CTRL))
|
if((kMod & SDL_KMOD_GUI) || (kMod & SDL_KMOD_CTRL))
|
||||||
|
|
@ -348,16 +384,32 @@ void InputManager::backend_key_down_update(const SDL_KeyboardEvent& e) {
|
||||||
if(text.textBox && key(KEY_TEXT_SELECTALL).repeat)
|
if(text.textBox && key(KEY_TEXT_SELECTALL).repeat)
|
||||||
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::SELECT_ALL, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::SELECT_ALL, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
||||||
break;
|
break;
|
||||||
|
case SDLK_Z:
|
||||||
|
if((kMod & SDL_KMOD_GUI) || (kMod & SDL_KMOD_CTRL))
|
||||||
|
set_key_down(e, KEY_TEXT_UNDO);
|
||||||
|
if(text.textBox && key(KEY_TEXT_UNDO).repeat)
|
||||||
|
text.textboxUndo.undo();
|
||||||
|
break;
|
||||||
|
case SDLK_R:
|
||||||
|
if((kMod & SDL_KMOD_GUI) || (kMod & SDL_KMOD_CTRL))
|
||||||
|
set_key_down(e, KEY_TEXT_REDO);
|
||||||
|
if(text.textBox && key(KEY_TEXT_REDO).repeat)
|
||||||
|
text.textboxUndo.redo();
|
||||||
|
break;
|
||||||
case SDLK_RETURN: {
|
case SDLK_RETURN: {
|
||||||
set_key_down(e, KEY_TEXT_ENTER);
|
set_key_down(e, KEY_TEXT_ENTER);
|
||||||
if(text.textBox && key(KEY_TEXT_ENTER).repeat)
|
if(text.textBox && key(KEY_TEXT_ENTER).repeat)
|
||||||
|
text.do_textbox_operation_with_undo([&]() {
|
||||||
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::ENTER, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::ENTER, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDLK_TAB: {
|
case SDLK_TAB: {
|
||||||
set_key_down(e, KEY_TEXT_TAB);
|
set_key_down(e, KEY_TEXT_TAB);
|
||||||
if(text.textBox && key(KEY_TEXT_TAB).repeat)
|
if(text.textBox && key(KEY_TEXT_TAB).repeat)
|
||||||
|
text.do_textbox_operation_with_undo([&]() {
|
||||||
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::TAB, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
text.textBox->process_key_input(*text.cursor, RichText::TextBox::InputKey::TAB, ctrl_or_meta_held(), key(KEY_GENERIC_LSHIFT).held, text.modMap);
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
|
|
@ -446,6 +498,12 @@ void InputManager::backend_key_up_update(const SDL_KeyboardEvent& e) {
|
||||||
case SDLK_A:
|
case SDLK_A:
|
||||||
set_key_up(e, KEY_TEXT_SELECTALL);
|
set_key_up(e, KEY_TEXT_SELECTALL);
|
||||||
break;
|
break;
|
||||||
|
case SDLK_Z:
|
||||||
|
set_key_up(e, KEY_TEXT_UNDO);
|
||||||
|
break;
|
||||||
|
case SDLK_R:
|
||||||
|
set_key_up(e, KEY_TEXT_REDO);
|
||||||
|
break;
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
set_key_up(e, KEY_TEXT_ENTER);
|
set_key_up(e, KEY_TEXT_ENTER);
|
||||||
break;
|
break;
|
||||||
|
|
@ -483,6 +541,8 @@ void InputManager::frame_reset(const Vector2i& windowSize) {
|
||||||
mouse.middleClicks = 0;
|
mouse.middleClicks = 0;
|
||||||
text.acceptingInput = text.acceptingInputNew;
|
text.acceptingInput = text.acceptingInputNew;
|
||||||
text.acceptingInputNew = false;
|
text.acceptingInputNew = false;
|
||||||
|
if(text.cursor != text.newCursor || text.textBox != text.newTextBox)
|
||||||
|
text.textboxUndo.clear();
|
||||||
text.cursor = text.newCursor;
|
text.cursor = text.newCursor;
|
||||||
text.textBox = text.newTextBox;
|
text.textBox = text.newTextBox;
|
||||||
text.modMap = text.newModMap;
|
text.modMap = text.newModMap;
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include "UndoManager.hpp"
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3/SDL_events.h>
|
#include <SDL3/SDL_events.h>
|
||||||
|
|
@ -92,6 +93,9 @@ struct InputManager {
|
||||||
void add_text_to_textbox(const std::string& inputText);
|
void add_text_to_textbox(const std::string& inputText);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void add_textbox_undo(const RichText::TextBox::Cursor& prevCursor, const RichText::TextData& prevRichText);
|
||||||
|
void do_textbox_operation_with_undo(const std::function<void()>& func);
|
||||||
|
|
||||||
std::shared_ptr<RichText::TextBox> textBox;
|
std::shared_ptr<RichText::TextBox> textBox;
|
||||||
std::shared_ptr<RichText::TextBox::Cursor> cursor;
|
std::shared_ptr<RichText::TextBox::Cursor> cursor;
|
||||||
std::optional<RichText::TextStyleModifier::ModifierMap> modMap;
|
std::optional<RichText::TextStyleModifier::ModifierMap> modMap;
|
||||||
|
|
@ -100,6 +104,8 @@ struct InputManager {
|
||||||
std::shared_ptr<RichText::TextBox::Cursor> newCursor;
|
std::shared_ptr<RichText::TextBox::Cursor> newCursor;
|
||||||
std::optional<RichText::TextStyleModifier::ModifierMap> newModMap;
|
std::optional<RichText::TextStyleModifier::ModifierMap> newModMap;
|
||||||
|
|
||||||
|
UndoManager textboxUndo;
|
||||||
|
|
||||||
bool acceptingInput = false;
|
bool acceptingInput = false;
|
||||||
bool acceptingInputNew = false;
|
bool acceptingInputNew = false;
|
||||||
|
|
||||||
|
|
@ -172,6 +178,8 @@ struct InputManager {
|
||||||
KEY_TEXT_CTRL,
|
KEY_TEXT_CTRL,
|
||||||
KEY_TEXT_META,
|
KEY_TEXT_META,
|
||||||
KEY_TEXT_SELECTALL,
|
KEY_TEXT_SELECTALL,
|
||||||
|
KEY_TEXT_UNDO,
|
||||||
|
KEY_TEXT_REDO,
|
||||||
KEY_GENERIC_ESCAPE,
|
KEY_GENERIC_ESCAPE,
|
||||||
KEY_GENERIC_LSHIFT,
|
KEY_GENERIC_LSHIFT,
|
||||||
KEY_GENERIC_LALT,
|
KEY_GENERIC_LALT,
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,6 @@ void UndoManager::redo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void UndoManager::clear() {
|
void UndoManager::clear() {
|
||||||
std::cout << "[UndoManager::clear] Clearing undo queue" << std::endl;
|
|
||||||
redoQueue.clear();
|
redoQueue.clear();
|
||||||
undoQueue.clear();
|
undoQueue.clear();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue