mirror of
https://github.com/MeowLynxSea/ceditor.git
synced 2025-07-09 10:54:37 +00:00
修复了原先Text类中分行存在的异常;
优化了渲染效率
This commit is contained in:
parent
22617d26ce
commit
7086956872
@ -29,28 +29,33 @@ public:
|
|||||||
|
|
||||||
SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(color_)) | FrontColorToWinColor(getFrontColor(color_)));
|
SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(color_)) | FrontColorToWinColor(getFrontColor(color_)));
|
||||||
|
|
||||||
|
std::string outputTarget = "|";
|
||||||
for(int i = 1; i < height - 1 && top + i < csbi.dwSize.Y - 1; i++) {
|
for(int i = 1; i < height - 1 && top + i < csbi.dwSize.Y - 1; i++) {
|
||||||
if(left >= 0) {
|
if(left >= 0) {
|
||||||
SetConsoleCursorPosition(hConsole, {static_cast<short>(left - 1), static_cast<short>(top + i - 1)});
|
SetConsoleCursorPosition(hConsole, {static_cast<short>(left - 1), static_cast<short>(top + i - 1)});
|
||||||
putchar('|');
|
WriteConsoleA(hConsole, outputTarget.c_str(), outputTarget.length(), NULL, NULL);
|
||||||
}
|
}
|
||||||
if(left + width - 1 < csbi.dwSize.X) {
|
if(left + width - 1 < csbi.dwSize.X) {
|
||||||
SetConsoleCursorPosition(hConsole, {static_cast<short>(left + width - 2), static_cast<short>(top + i - 1)});
|
SetConsoleCursorPosition(hConsole, {static_cast<short>(left + width - 2), static_cast<short>(top + i - 1)});
|
||||||
putchar('|');
|
WriteConsoleA(hConsole, outputTarget.c_str(), outputTarget.length(), NULL, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
outputTarget = "-";
|
||||||
if(top >= 0) {
|
if(top >= 0) {
|
||||||
for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) {
|
for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) {
|
||||||
if(i > 1 && i < strlen(title_.plainText().c_str()) + 2) continue;
|
if(i > 1 && i < strlen(title_.plainText().c_str()) + 2) continue;
|
||||||
SetConsoleCursorPosition(hConsole, {static_cast<short>(left + i - 1), static_cast<short>(top - 1)});
|
SetConsoleCursorPosition(hConsole, {static_cast<short>(left + i - 1), static_cast<short>(top - 1)});
|
||||||
putchar('-');
|
WriteConsoleA(hConsole, outputTarget.c_str(), outputTarget.length(), NULL, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(top + height - 1 < csbi.dwSize.Y) {
|
if(top + height - 1 < csbi.dwSize.Y) {
|
||||||
|
outputTarget = "";
|
||||||
for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) {
|
for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) {
|
||||||
SetConsoleCursorPosition(hConsole, {static_cast<short>(left + i - 1), static_cast<short>(top + height - 2)});
|
outputTarget += "-";
|
||||||
putchar('-');
|
|
||||||
}
|
}
|
||||||
|
SetConsoleCursorPosition(hConsole, {static_cast<short>(left - 1), static_cast<short>(top + height - 2)});
|
||||||
|
WriteConsoleA(hConsole, outputTarget.c_str(), outputTarget.length(), NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
//绘制标题
|
//绘制标题
|
||||||
|
BIN
components/Text
Normal file
BIN
components/Text
Normal file
Binary file not shown.
@ -6,6 +6,7 @@
|
|||||||
#include "../mystl/my_vector.h"
|
#include "../mystl/my_vector.h"
|
||||||
#include <conio.h>
|
#include <conio.h>
|
||||||
#include "../utils/RichText.h"
|
#include "../utils/RichText.h"
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
class Text : public BaseComponent {
|
class Text : public BaseComponent {
|
||||||
private:
|
private:
|
||||||
@ -31,15 +32,19 @@ public:
|
|||||||
lines_.clear();
|
lines_.clear();
|
||||||
RichText line;
|
RichText line;
|
||||||
maxLineWidth_ = 0;
|
maxLineWidth_ = 0;
|
||||||
|
// std::ofstream out("log.txt");
|
||||||
for(auto part : text.getParts()) {
|
for(auto part : text.getParts()) {
|
||||||
if(part.text.find('\n') == std::string::npos) {
|
if(part.text.find('\n') == std::string::npos) {
|
||||||
line += part;
|
line += part;
|
||||||
// printf("Add: %s\n", part.text.c_str());
|
// printf("Add: %s\n", part.text.c_str());
|
||||||
} else {
|
} else {
|
||||||
size_t begin = 0;
|
size_t begin = 0;
|
||||||
while((begin = part.text.find('\n', begin)) != std::string::npos) {
|
while((begin = part.text.find('\n', 0)) != std::string::npos) {
|
||||||
line += StringPart(part.text.substr(0, begin), part.color);
|
line += StringPart(part.text.substr(0, begin), part.color);
|
||||||
// printf("Add: %s\n", part.text.substr(0, begin).c_str());
|
// printf("Add: %s\n", part.text.substr(0, begin).c_str());
|
||||||
|
if(line.plainText() == "\n" || line.plainText() == "\r\n" || line.plainText() == "\r"){
|
||||||
|
line = "";
|
||||||
|
}
|
||||||
lines_.push_back(line);
|
lines_.push_back(line);
|
||||||
if(maxLineWidth_ < line.length()) {
|
if(maxLineWidth_ < line.length()) {
|
||||||
maxLineWidth_ = line.length();
|
maxLineWidth_ = line.length();
|
||||||
@ -53,10 +58,16 @@ public:
|
|||||||
// printf("Add: %s\n", part.text.c_str());
|
// printf("Add: %s\n", part.text.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(line.plainText() == "\n") {
|
||||||
|
line = "";
|
||||||
|
}
|
||||||
lines_.push_back(line);
|
lines_.push_back(line);
|
||||||
if(maxLineWidth_ < line.length()) {
|
if(maxLineWidth_ < line.length()) {
|
||||||
maxLineWidth_ = line.length();
|
maxLineWidth_ = line.length();
|
||||||
}
|
}
|
||||||
|
// for(int i = 0; i < lines_.size(); i++) {
|
||||||
|
// out << lines_[i].plainText() << std::endl << "-------------------------------------------" << std::endl;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
RichText getText() {
|
RichText getText() {
|
||||||
@ -71,17 +82,34 @@ public:
|
|||||||
|
|
||||||
for(int i = viewTop_; i < viewTop_ + height && i < lines_.size(); i++) {
|
for(int i = viewTop_; i < viewTop_ + height && i < lines_.size(); i++) {
|
||||||
RichText drawTarget = lines_[i].substr(viewLeft_, std::min(width, csbi.dwSize.X - left));
|
RichText drawTarget = lines_[i].substr(viewLeft_, std::min(width, csbi.dwSize.X - left));
|
||||||
SetConsoleCursorPosition(hConsole, {static_cast<short>(left - 1), static_cast<short>(top + i - viewTop_ - 1)});
|
int currentLength = 0;
|
||||||
|
CHAR_INFO buffer[width * 1];
|
||||||
for(auto part : drawTarget.getParts()) {
|
for(auto part : drawTarget.getParts()) {
|
||||||
SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(part.color)) | FrontColorToWinColor(getFrontColor(part.color)));
|
// SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(part.color)) | FrontColorToWinColor(getFrontColor(part.color)));
|
||||||
printf("%s", part.text.c_str());
|
// printf("%s", part.text.c_str());
|
||||||
|
// SetConsoleCursorPosition(hConsole, {static_cast<short>(left + currentLength - 1), static_cast<short>(top + i - viewTop_ - 1)});
|
||||||
|
// WriteConsoleA(hConsole, part.text.c_str(), part.text.length(), NULL, NULL);
|
||||||
|
for(int j = 0; j < part.text.length(); j++) {
|
||||||
|
buffer[currentLength + j].Char.UnicodeChar = part.text[j];
|
||||||
|
buffer[currentLength + j].Attributes = BackgroundColorToWinColor(getBackColor(part.color)) | FrontColorToWinColor(getFrontColor(part.color));
|
||||||
|
}
|
||||||
|
currentLength += part.text.length();
|
||||||
}
|
}
|
||||||
CONSOLE_SCREEN_BUFFER_INFO currentCSBI;
|
CONSOLE_SCREEN_BUFFER_INFO currentCSBI;
|
||||||
GetConsoleScreenBufferInfo(hConsole, ¤tCSBI);
|
GetConsoleScreenBufferInfo(hConsole, ¤tCSBI);
|
||||||
for(int cx = currentCSBI.dwCursorPosition.X; cx < left + width - 1; cx++) {
|
// std::string space = "";
|
||||||
SetConsoleCursorPosition(hConsole, {static_cast<short>(cx), static_cast<short>(currentCSBI.dwCursorPosition.Y)});
|
for(int j = 0; j < width - drawTarget.plainText().length(); j++) {
|
||||||
printf(" ");
|
// space += " ";
|
||||||
|
buffer[currentLength + j].Char.UnicodeChar = ' ';
|
||||||
|
buffer[currentLength + j].Attributes = currentCSBI.wAttributes;
|
||||||
}
|
}
|
||||||
|
// SetConsoleCursorPosition(hConsole, {static_cast<short>(left + drawTarget.plainText().length() - 1), static_cast<short>(top + i - viewTop_ - 1)});
|
||||||
|
// WriteConsoleA(hConsole, space.c_str(), space.length(), NULL, NULL);
|
||||||
|
SetConsoleCursorPosition(hConsole, {static_cast<short>(left), static_cast<short>(top + i - viewTop_)});
|
||||||
|
COORD bufferSize = {static_cast<short>(width), 1};
|
||||||
|
COORD bufferCoord = {0, 0};
|
||||||
|
SMALL_RECT rect = {static_cast<short>(left - 1), static_cast<short>(top + i - viewTop_ - 1), static_cast<short>(left + width - 1), static_cast<short>(top + i - viewTop_ + 1)};
|
||||||
|
WriteConsoleOutput(hConsole, buffer, bufferSize, bufferCoord, &rect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ int main() {
|
|||||||
richText += StringPart(" with colors!\n", COLOR_RED);
|
richText += StringPart(" with colors!\n", COLOR_RED);
|
||||||
richText += "And more text.\n";
|
richText += "And more text.\n";
|
||||||
richText += StringPart("And more more more text.", COLOR_GREEN);
|
richText += StringPart("And more more more text.", COLOR_GREEN);
|
||||||
richText += " And more more more more more text.\n";
|
richText += " And more more more more more text.\n\n111\n\n";
|
||||||
richText += StringPart("And more more more more more more text.", COLOR_BLUE);
|
richText += StringPart("And more more more more more more text.", COLOR_BLUE);
|
||||||
richText += " And more more more more more more more text.\n";
|
richText += " And more more more more more more more text.\n";
|
||||||
richText += StringPart(" With some spaces before And more more more more more more more more text.\n", COLOR_YELLOW);
|
richText += StringPart(" With some spaces before And more more more more more more more more text.\n", COLOR_YELLOW);
|
||||||
@ -73,7 +73,6 @@ int main() {
|
|||||||
// // 切换到后台缓冲区,显示绘制的内容
|
// // 切换到后台缓冲区,显示绘制的内容
|
||||||
// SetConsoleActiveScreenBuffer(hConsole);
|
// SetConsoleActiveScreenBuffer(hConsole);
|
||||||
}
|
}
|
||||||
Sleep(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
22
components/log.txt
Normal file
22
components/log.txt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
Hello, World with colors!
|
||||||
|
-------------------------------------------
|
||||||
|
And more text.
|
||||||
|
-------------------------------------------
|
||||||
|
And more more more text. And more more more more more text.
|
||||||
|
-------------------------------------------
|
||||||
|
|
||||||
|
-------------------------------------------
|
||||||
|
111
|
||||||
|
-------------------------------------------
|
||||||
|
|
||||||
|
-------------------------------------------
|
||||||
|
And more more more more more more text. And more more more more more more more text.
|
||||||
|
-------------------------------------------
|
||||||
|
With some spaces before And more more more more more more more more text.
|
||||||
|
-------------------------------------------
|
||||||
|
And more more more more more more more more more text.
|
||||||
|
-------------------------------------------
|
||||||
|
And more more more more more more more more more more text.
|
||||||
|
-------------------------------------------
|
||||||
|
Last Key: 113
|
||||||
|
-------------------------------------------
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SyntaxHighlighter highlighter = SyntaxHighlighter("cpp");
|
SyntaxHighlighter highlighter = SyntaxHighlighter("cpp");
|
||||||
TextArea textArea = TextArea(1, 1, 100, 28);
|
TextArea textArea = TextArea(1, 1, 100, 50);
|
||||||
std::string rawText;
|
std::string rawText;
|
||||||
// read in SyntaxHighlighter_test.cpp
|
// read in SyntaxHighlighter_test.cpp
|
||||||
std::ifstream file("SyntaxHighlighter_test.cpp");
|
std::ifstream file("SyntaxHighlighter_test.cpp");
|
||||||
@ -33,6 +33,7 @@ int main() {
|
|||||||
|
|
||||||
textArea.setTitle(RichText("SyntaxHighlighter Test", COLOR_LIGHTRED));
|
textArea.setTitle(RichText("SyntaxHighlighter Test", COLOR_LIGHTRED));
|
||||||
textArea.setText(richText);
|
textArea.setText(richText);
|
||||||
|
// textArea.setText(RichText(rawText, COLOR_WHITE));
|
||||||
textArea.draw();
|
textArea.draw();
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
|
78
utils/log.txt
Normal file
78
utils/log.txt
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#include "SyntaxHighlighter.h"
|
||||||
|
#include "../components/TextArea.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
SyntaxHighlighter highlighter = SyntaxHighlighter("cpp");
|
||||||
|
TextArea textArea = TextArea(1, 1, 100, 50);
|
||||||
|
std::string rawText;
|
||||||
|
// read in SyntaxHighlighter_test.cpp
|
||||||
|
std::ifstream file("SyntaxHighlighter_test.cpp");
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(file, line)) {
|
||||||
|
rawText += line + "\n";
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
RichText richText = highlighter.highlight(rawText);
|
||||||
|
|
||||||
|
// // 创建后台缓冲区
|
||||||
|
// HANDLE hBackBuffer = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
|
||||||
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
|
||||||
|
// // 清空后台缓冲区
|
||||||
|
// CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||||
|
// GetConsoleScreenBufferInfo(hConsole, &csbi);
|
||||||
|
// DWORD dwBytesWritten;
|
||||||
|
// FillConsoleOutputCharacter(hBackBuffer, ' ', csbi.dwSize.X * csbi.dwSize.Y, {0, 0}, &dwBytesWritten);
|
||||||
|
// FillConsoleOutputAttribute(hBackBuffer, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, {0, 0}, &dwBytesWritten);
|
||||||
|
CONSOLE_CURSOR_INFO cci;
|
||||||
|
cci.bVisible = false;
|
||||||
|
cci.dwSize = 1;
|
||||||
|
//SetConsoleCursorInfo(hBackBuffer, &cci);
|
||||||
|
SetConsoleCursorInfo(hConsole, &cci);
|
||||||
|
|
||||||
|
textArea.setTitle(RichText("SyntaxHighlighter Test", COLOR_LIGHTRED));
|
||||||
|
textArea.setText(richText);
|
||||||
|
// textArea.setText(RichText(rawText, COLOR_WHITE));
|
||||||
|
textArea.draw();
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
if (_kbhit()) {
|
||||||
|
// SetConsoleActiveScreenBuffer(hBackBuffer);
|
||||||
|
char opt = _getch();
|
||||||
|
|
||||||
|
switch(opt) {
|
||||||
|
case 72:
|
||||||
|
textArea.moveUp();
|
||||||
|
break;
|
||||||
|
case 80:
|
||||||
|
textArea.moveDown();
|
||||||
|
break;
|
||||||
|
case 75:
|
||||||
|
textArea.moveLeft();
|
||||||
|
break;
|
||||||
|
case 77:
|
||||||
|
textArea.moveRight();
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
// SetConsoleActiveScreenBuffer(hConsole);
|
||||||
|
// CloseHandle(hBackBuffer);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// // 清空后台缓冲区
|
||||||
|
// FillConsoleOutputCharacter(hBackBuffer, ' ', csbi.dwSize.X * csbi.dwSize.Y, {0, 0}, &dwBytesWritten);
|
||||||
|
// FillConsoleOutputAttribute(hBackBuffer, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, {0, 0}, &dwBytesWritten);
|
||||||
|
|
||||||
|
// // 在后台缓冲区中绘制
|
||||||
|
textArea.draw();
|
||||||
|
|
||||||
|
// // 切换到后台缓冲区,显示绘制的内容
|
||||||
|
// SetConsoleActiveScreenBuffer(hConsole);
|
||||||
|
}
|
||||||
|
Sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user