diff --git a/components/BaseComponent.h b/components/BaseComponent.h index f0ab972..bc663e9 100644 --- a/components/BaseComponent.h +++ b/components/BaseComponent.h @@ -4,6 +4,7 @@ class BaseComponent { protected: int top, left, width, height; + bool focused; public: BaseComponent(int top, int left, int width, int height) : top(top), left(left), width(width), height(height) {}; @@ -35,6 +36,10 @@ public: return height; } + void setFocused(bool focused) { + this->focused = focused; + } + void setPosition(int top, int left) { this->top = top; this->left = left; diff --git a/components/Rect.h b/components/Rect.h index b30a894..34fdd47 100644 --- a/components/Rect.h +++ b/components/Rect.h @@ -9,6 +9,7 @@ class Rect : public BaseComponent { private: + RichText title_; MColor color_ = COLOR_WHITE; public: @@ -38,20 +39,34 @@ public: putchar('|'); } } - for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) { - if(top >= 0) { + if(top >= 0) { + for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) { + if(i > 1 && i < strlen(title_.plainText().c_str()) + 2) continue; SetConsoleCursorPosition(hConsole, {static_cast(left + i - 1), static_cast(top - 1)}); putchar('-'); } - if(top + height - 1 < csbi.dwSize.Y) { + } + if(top + height - 1 < csbi.dwSize.Y) { + for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) { SetConsoleCursorPosition(hConsole, {static_cast(left + i - 1), static_cast(top + height - 2)}); putchar('-'); } } + //绘制标题 + SetConsoleCursorPosition(hConsole, {static_cast(left + 1), static_cast(top - 1)}); + for(auto part : title_.getParts()) { + SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(part.color)) | FrontColorToWinColor(getFrontColor(part.color))); + printf("%s", part.text.c_str()); + } + SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(COLOR_BLACK) | FrontColorToWinColor(COLOR_WHITE)); } + void setTitle(const RichText title) { + title_ = title; + } + void onKeyPress(int key) override { //不处理按键 } diff --git a/components/Text.h b/components/Text.h index 9f9d4ee..859e868 100644 --- a/components/Text.h +++ b/components/Text.h @@ -3,7 +3,7 @@ #include "BaseComponent.h" #include -#include +#include "../mystl/my_vector.h" #include #include "../utils/RichText.h" @@ -11,7 +11,7 @@ class Text : public BaseComponent { private: RichText text_; int viewLeft_ = 0, viewTop_ = 0; - std::vector lines_; + MyVector lines_; int maxLineWidth_ = 0; public: diff --git a/components/TextArea b/components/TextArea deleted file mode 100644 index 7cc76b7..0000000 Binary files a/components/TextArea and /dev/null differ diff --git a/components/TextArea.h b/components/TextArea.h index 34a2d60..8ae0a03 100644 --- a/components/TextArea.h +++ b/components/TextArea.h @@ -3,7 +3,6 @@ #include "BaseComponent.h" #include -#include #include #include "../utils/RichText.h" #include "Rect.h" @@ -12,7 +11,6 @@ class TextArea : public BaseComponent { private: Text text_ = Text(0, 0, 0, 0); - RichText title_; Rect border_ = Rect(0, 0, 0, 0); public: @@ -35,13 +33,6 @@ public: //获得缓冲区信息 CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole, &csbi); - - //绘制标题 - SetConsoleCursorPosition(hConsole, {static_cast(left + 1), static_cast(top - 1)}); - for(auto part : title_.getParts()) { - SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(part.color)) | FrontColorToWinColor(getFrontColor(part.color))); - printf("%s", part.text.c_str()); - } text_.draw(); @@ -81,7 +72,7 @@ public: } void setTitle(const RichText& newTitle) { - this->title_ = newTitle; + border_.setTitle(newTitle); } }; diff --git a/components/TextArea_test.cpp b/components/TextArea_test.cpp index 54fe2a9..ed75877 100644 --- a/components/TextArea_test.cpp +++ b/components/TextArea_test.cpp @@ -18,27 +18,28 @@ int main() { TextArea textArea(3, 3, 64, 7, richText); - // 创建后台缓冲区 - HANDLE hBackBuffer = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL); - HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + // // 创建后台缓冲区 + // 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); + // // 清空后台缓冲区 + // 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("TextArea Demo", COLOR_RED)); textArea.draw(); while(true) { - if (_kbhit()) {SetConsoleActiveScreenBuffer(hBackBuffer); + if (_kbhit()) { + // SetConsoleActiveScreenBuffer(hBackBuffer); char opt = _getch(); textArea.setText(richText + StringPart("Last Key: " + std::to_string(opt), COLOR_CYAN)); switch(opt) { @@ -55,22 +56,22 @@ int main() { textArea.moveRight(); break; case 'q': - SetConsoleActiveScreenBuffer(hConsole); - CloseHandle(hBackBuffer); + // 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); + // // 清空后台缓冲区 + // 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); + // // 切换到后台缓冲区,显示绘制的内容 + // SetConsoleActiveScreenBuffer(hConsole); } Sleep(1); } diff --git a/utils/InputManager.h b/utils/InputManager.h new file mode 100644 index 0000000..c19ab40 --- /dev/null +++ b/utils/InputManager.h @@ -0,0 +1,4 @@ +#ifndef INPUTMANAGER_H +#define INPUTMANAGER_H + +#endif \ No newline at end of file