From 273ecc77b07633579b123bebf381cefbd0ea6e18 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 17:54:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E6=92=A4=E5=9B=9E?= =?UTF-8?q?=E3=80=81=E9=87=8D=E5=81=9A=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Editor.h | 34 +++++++++++++++--------- components/Editor_test.cpp | 6 ++--- utils/ActionManager.h | 51 +++++++++++++++++++++++------------- utils/ActionManager_test.cpp | 11 ++++++++ 4 files changed, 69 insertions(+), 33 deletions(-) diff --git a/components/Editor.h b/components/Editor.h index e9bc01e..1fa9aa5 100644 --- a/components/Editor.h +++ b/components/Editor.h @@ -22,7 +22,8 @@ private: Insert, Delete, Command - } inputstatus_ = InputStatus::Insert; + }; + InputStatus inputstatus_; static const int MAX_DIVISION_TIME = 1200; // 1.2s int lastInputTime_ = 0; @@ -55,18 +56,20 @@ public: content_ = ""; cursor_.setVisibility(false); actionmanager_.setOriginContent(content_); + inputstatus_ = InputStatus::Insert; }; void setRuleName(std::string ruleName) { ruleName_ = ruleName; } std::string getRuleName() { return ruleName_; } - void setContent(std::string content) { + void setContent(std::string content, bool firstTime = false) { content_ = content; coloredContent_ = SyntaxHighlighter(ruleName_).highlight(content_); textArea_.setText(coloredContent_); lineedContent_ = split(content_); - actionmanager_.setOriginContent(content_); + if(firstTime) actionmanager_.setOriginContent(content_); } + std::string getContent() { return content_; } void setTitle(std::string title) { @@ -131,14 +134,20 @@ public: int currentChar = cursor_.getLeft() - left - 1 + textArea_.getViewLeft(); int lineLength = lineedContent_[currentLine].size(); - if(key == 72 + 256) { // up - moveUp(); - } else if(key == 80 + 256) { // down - moveDown(); - } else if(key == 75 + 256) { // left - moveLeft(); - } else if(key == 77 + 256) { // right - moveRight(); + if(key >= 256 && key < 512) { + if(inputstatus_ != InputStatus::Command) { + inputstatus_ = InputStatus::Command; + actionmanager_.updateContent(content_); + } + if (key == 72 + 256) { // up + moveUp(); + } else if(key == 80 + 256) { // down + moveDown(); + } else if(key == 75 + 256) { // left + moveLeft(); + } else if(key == 77 + 256) { // right + moveRight(); + } } else { if(key == 224) return; if(currentChar > lineLength) { @@ -151,7 +160,8 @@ public: if(key == 8) { // backspace if(inputstatus_ != InputStatus::Delete) { inputstatus_ = InputStatus::Delete; - actionmanager_.updateContent(content_); + std::string rst = actionmanager_.updateContent(getContent()) ? "success" : "fail"; + // MessageBoxA(NULL, rst.c_str(), "Update Content", MB_OK); } // 如果是第一个字符,则把当前行内容加到上一行末尾 if(currentChar == 0) { diff --git a/components/Editor_test.cpp b/components/Editor_test.cpp index 5f9e1dd..7e6dc85 100644 --- a/components/Editor_test.cpp +++ b/components/Editor_test.cpp @@ -24,7 +24,7 @@ int main() { content = content.substr(0, content.size() - 1); // remove last newline editor.setRuleName("cpp"); - editor.setContent(content); + editor.setContent(content, true); editor.draw(); @@ -33,8 +33,8 @@ int main() { int scan = _getch(), opt; if(scan == 224) { opt = _getch() + 256; - } else if(scan == 26) { - opt = 26 + 512; + } else if(scan == 26 || scan == 25) { + opt = scan + 512; } else { opt = scan; } diff --git a/utils/ActionManager.h b/utils/ActionManager.h index e2156fd..ada75d4 100644 --- a/utils/ActionManager.h +++ b/utils/ActionManager.h @@ -4,6 +4,7 @@ #include "../mystl/my_stack.h" #include #include +#include enum class EditActiontype { Initialize, @@ -23,6 +24,18 @@ private: MyStack> redoStack_; std::string content_; + std::string applyEditAction(MyVector &actions, std::string &content) { + std::string newContent = content; + for(int i = 0; i < actions.size(); i++) { + if(actions[i].type == EditActiontype::Insert) { + newContent.insert(actions[i].pos, actions[i].content); + } else if(actions[i].type == EditActiontype::Delete) { + newContent.erase(actions[i].pos, actions[i].content.size()); + } + } + return newContent; + } + public: MyVector calculateEditActions(const std::string &str1, const std::string &str2) { // 规定Editor只会在删除或者插入时提交,因此只会同时存在一种操作 int m = str1.length(); @@ -89,54 +102,56 @@ public: return actions; } - std::string applyEditAction(MyVector &actions, std::string &content) { - std::string newContent = content; - for(int i = 0; i < actions.size(); i++) { - if(actions[i].type == EditActiontype::Insert) { - newContent.insert(actions[i].pos, actions[i].content); - } else if(actions[i].type == EditActiontype::Delete) { - newContent.erase(actions[i].pos, actions[i].content.size()); - } - } - return newContent; - } - ActionManager() { } - ActionManager(const std::string &content) { + ActionManager(std::string content) { content_ = content; } - void updateContent(const std::string &content) { + bool updateContent(const std::string content) { + // std::string oldContentLength = std::to_string(content_.length()); + // std::string newContentLength = std::to_string(content.length()); + // std::string message = "Length of old content is " + oldContentLength + " and new content is " + newContentLength; + // MessageBoxA(0, message.c_str(), "Update Content", MB_OK); + if(content == content_) { + return false; + } redoStack_.clear(); MyVector actions = calculateEditActions(content, content_); content_ = content; undoStack_.push(actions); + return true; } - void undo() { + bool undo() { if (undoStack_.empty()) { - return; + return false; } MyVector actions = undoStack_.top(); undoStack_.pop(); std::string newContent = applyEditAction(actions, content_); redoStack_.push(calculateEditActions(newContent, content_)); content_ = newContent; + return true; } - void redo() { + bool redo() { if (redoStack_.empty()) { - return; + return false; } MyVector actions = redoStack_.top(); redoStack_.pop(); std::string newContent = applyEditAction(actions, content_); undoStack_.push(calculateEditActions(newContent, content_)); content_ = newContent; + return true; } void setOriginContent(std::string content) { + // std::string oldContentLength = std::to_string(content_.length()); + // std::string newContentLength = std::to_string(content.length()); + // std::string message = "Length of old content is " + oldContentLength + " and new content is " + newContentLength; + // MessageBoxA(0, message.c_str(), "Set Origin Content", MB_OK); content_ = content; undoStack_.clear(); redoStack_.clear(); diff --git a/utils/ActionManager_test.cpp b/utils/ActionManager_test.cpp index 71746b7..a58ee12 100644 --- a/utils/ActionManager_test.cpp +++ b/utils/ActionManager_test.cpp @@ -6,6 +6,17 @@ using namespace std; int main() { ActionManager am; + + std::string s_ = "hello,world!"; + am.setOriginContent(s_); + s_ = s_.substr(0, 5); + printf("%s\n", s_.c_str()); + printf("%s\n", am.getContent().c_str()); + am.updateContent(s_); + printf("%s\n", am.getContent().c_str()); + am.undo(); + printf("%s\n", am.getContent().c_str()); + MyVector v; std::string s0 = "i can see you, but u dont love me"; v = am.calculateEditActions("i can see you, but u dont love me", "i cant hug u, but i love you");