diff --git a/components/BaseComponent.h b/components/BaseComponent.h index 954edcb..f88b56b 100644 --- a/components/BaseComponent.h +++ b/components/BaseComponent.h @@ -44,7 +44,7 @@ public: return focusStatus; } - void setPosition(int top, int left) { + void setPosition(int left, int top) { this->top = top; this->left = left; } diff --git a/components/Cursor.h b/components/Cursor.h new file mode 100644 index 0000000..d3f0fec --- /dev/null +++ b/components/Cursor.h @@ -0,0 +1,98 @@ +#ifndef CURSOR_H +#define CURSOR_H + +#include "BaseComponent.h" +#include "../utils/Color.h" +#include +#include + +class Cursor : public BaseComponent { +private: + bool visibility_; + const int cursorAttributes_ = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; + int lastAttributes_; + bool posHasChanged_ = true; + int maxLeft_, maxTop_; + COORD lastPos_ = {0, 0}; + +public: + Cursor() : BaseComponent(1, 1, 1, 1) { visibility_ = true; } + Cursor(int x, int y) : BaseComponent(x, y, 1, 1) { visibility_ = true; }; + Cursor(int x, int y, bool visibility) : BaseComponent(x, y, 1, 1) { visibility_ = visibility; } + ~Cursor() override = default; + + void draw() override { + if (!visibility_) return; + // ReadConsoleOutput + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + COORD bufferSize = {static_cast(1), static_cast(1)}; + COORD bufferPos = {0, 0}; + if(posHasChanged_) { + SMALL_RECT oldReat = {static_cast(lastPos_.X - 1), static_cast(lastPos_.Y - 1), static_cast(lastPos_.X), static_cast(lastPos_.Y)}; + CHAR_INFO buffer[1]; + if(ReadConsoleOutput(hConsole, buffer, bufferSize, bufferPos, &oldReat) == 0) { + // std::cout << GetLastError() << std::endl; + } + buffer[0].Attributes = lastAttributes_; + if(WriteConsoleOutput(hConsole, buffer, bufferSize, bufferPos, &oldReat) == 0) { + // std::cout << GetLastError() << std::endl; + } + } + SMALL_RECT rect = {static_cast(left - 1), static_cast(top - 1), static_cast(left), static_cast(top)}; + CHAR_INFO buffer[1]; + if(ReadConsoleOutput(hConsole, buffer, bufferSize, bufferPos, &rect) == 0) { + // std::cout << GetLastError() << std::endl; + } + if(posHasChanged_) { + lastAttributes_ = buffer[0].Attributes; + } + buffer[0].Attributes = cursorAttributes_; + if(WriteConsoleOutput(hConsole, buffer, bufferSize, bufferPos, &rect) == 0) { + // std::cout << GetLastError() << std::endl; + } + posHasChanged_ = false; + } + + void setPosition(int x, int y) { + posHasChanged_ = true; + lastPos_ = {static_cast(left), static_cast(top)}; + BaseComponent::setPosition(x, y); + } + + void setBounds(int maxLeft, int maxTop) { + maxLeft_ = maxLeft; + maxTop_ = maxTop; + } + + void moveUp() { + if(top > 1) { + setPosition(left, top - 1); + } + } + + void moveDown() { + if(top < maxTop_) { + setPosition(left, top + 1); + } + } + + void moveLeft() { + if(left > 1) { + setPosition(left - 1, top); + } + } + + void moveRight() { + if(left < maxLeft_) { + setPosition(left + 1, top); + } + } + + void onKeyPress(int key) override { return; } + + void setVisibility(bool visibility) { visibility_ = visibility; } + + bool isVisible() { return visibility_; } +}; + +#endif \ No newline at end of file diff --git a/components/Cursor_test.cpp b/components/Cursor_test.cpp new file mode 100644 index 0000000..8377324 --- /dev/null +++ b/components/Cursor_test.cpp @@ -0,0 +1,51 @@ +#include "Cursor.h" +#include +#include +#include + +int main() { + for(int i = 0; i < 20; i++) { + for(int j = 0; j < 20; j++) { + std::cout << char('a' + j); + } + std::cout << std::endl; + } + + // Test case 1: Cursor is created at the origin + Cursor cursor1; + cursor1.draw(); + assert(cursor1.getLeft() == 1); + assert(cursor1.getTop() == 1); + + // Test case 2: Cursor is moved to a positive coordinate + cursor1.setPosition(5, 10); + cursor1.draw(); + assert(cursor1.getLeft() == 5); + + cursor1.setBounds(20, 20); + + while(true) { + if (_kbhit()) { + char opt = _getch(); + switch(opt) { + case 72: + cursor1.moveUp(); + break; + case 80: + cursor1.moveDown(); + break; + case 75: + cursor1.moveLeft(); + break; + case 77: + cursor1.moveRight(); + break; + case 'q': + return 0; + } + cursor1.draw(); + } + } + + return 0; +} \ No newline at end of file diff --git a/components/Editor.h b/components/Editor.h new file mode 100644 index 0000000..c08804d --- /dev/null +++ b/components/Editor.h @@ -0,0 +1,12 @@ +#ifndef EDITOR_H +#define EDITOR_H + +#include "BaseComponent.h" +#include "TextArea.h" + +class Editor : public BaseComponent { +private: + TextArea* textArea; +}; + +#endif \ No newline at end of file diff --git a/components/Text b/components/Text deleted file mode 100644 index a115d1a..0000000 Binary files a/components/Text and /dev/null differ