From 43540603c5699e735ac8e1d09ea942e74028c70c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=A6=E5=87=8C=E6=B1=90?= Date: Thu, 19 Dec 2024 13:45:36 +0800 Subject: [PATCH] t --- components/Editor.h | 48 +++++++++++++++++++++++++++++++++++++- components/Editor_test.cpp | 3 ++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/components/Editor.h b/components/Editor.h index c68ae3d..8e075ac 100644 --- a/components/Editor.h +++ b/components/Editor.h @@ -18,6 +18,13 @@ private: std::string ruleName_; MyVector lineedContent_; ActionManager actionmanager_; + enum class InputStatus { + Insert, + Delete, + Command + } inputstatus_ = InputStatus::Insert; + static const int MAX_DIVISION_TIME = 1200; // 1.2s + int lastInputTime_ = 0; MyVector split(std::string content) { MyVector lineedContent; @@ -47,6 +54,7 @@ public: cursor_.setBounds(left + width - 2, top + height - 2); content_ = ""; cursor_.setVisibility(false); + actionmanager_.setOriginContent(content_); }; void setRuleName(std::string ruleName) { ruleName_ = ruleName; } @@ -57,6 +65,7 @@ public: coloredContent_ = SyntaxHighlighter(ruleName_).highlight(content_); textArea_.setText(coloredContent_); lineedContent_ = split(content_); + actionmanager_.setOriginContent(content_); } std::string getContent() { return content_; } @@ -138,6 +147,10 @@ public: } if(key == 8) { // backspace + if(inputstatus_ != InputStatus::Delete) { + inputstatus_ = InputStatus::Delete; + actionmanager_.updateContent(content_); + } // 如果是第一个字符,则把当前行内容加到上一行末尾 if(currentChar == 0) { if(currentLine > 0) { @@ -157,6 +170,10 @@ public: setContent(assemble(lineedContent_)); } } else if(key == 127 || key == 83 + 256) { // delete + if(inputstatus_ != InputStatus::Delete) { + inputstatus_ = InputStatus::Delete; + actionmanager_.updateContent(content_); + } if(currentChar == lineLength) { if(currentLine < lineedContent_.size() - 1) { lineedContent_[currentLine] += lineedContent_[currentLine + 1]; @@ -168,14 +185,41 @@ public: lineedContent_[currentLine] = lineedContent_[currentLine].substr(0, currentChar) + lineedContent_[currentLine].substr(currentChar + 1); setContent(assemble(lineedContent_)); } - } else if(key == 13) { // enter + } else if(key == 13) { // enter + if(inputstatus_ != InputStatus::Insert) { + inputstatus_ = InputStatus::Insert; + actionmanager_.updateContent(content_); + } lineedContent_[currentLine].insert(currentChar, "\n"); setContent(assemble(lineedContent_)); moveDown(); for(int i = 0; i < currentChar; i++) { moveLeft(); } + } else if(key >= 512 && key < 768) { // ctrl + + if(inputstatus_ != InputStatus::Command) { + inputstatus_ = InputStatus::Command; + actionmanager_.updateContent(content_); + } + key -= 512; + switch (key) { + case 26: // ctrl + z + actionmanager_.undo(); + setContent(actionmanager_.getContent()); + break; + case 25: // ctrl + y + actionmanager_.redo(); + setContent(actionmanager_.getContent()); + break; + } } else { + if(inputstatus_ != InputStatus::Insert) { + inputstatus_ = InputStatus::Insert; + actionmanager_.updateContent(content_); + } + if(key == 32 || key == 9) { // space + actionmanager_.updateContent(content_); + } if(key == 9) { // tab lineedContent_[currentLine].insert(currentChar, " "); moveRight(3); @@ -186,6 +230,8 @@ public: moveRight(); } } + + draw(); } }; diff --git a/components/Editor_test.cpp b/components/Editor_test.cpp index b4c9156..5f9e1dd 100644 --- a/components/Editor_test.cpp +++ b/components/Editor_test.cpp @@ -33,6 +33,8 @@ int main() { int scan = _getch(), opt; if(scan == 224) { opt = _getch() + 256; + } else if(scan == 26) { + opt = 26 + 512; } else { opt = scan; } @@ -47,7 +49,6 @@ int main() { editor.setFocus(true); } } - editor.draw(); } }