新增了Cursor

This commit is contained in:
梦凌汐 2024-12-15 23:53:57 +08:00
parent 99e5843f0d
commit 9fc69a783c
5 changed files with 162 additions and 1 deletions

View File

@ -44,7 +44,7 @@ public:
return focusStatus; return focusStatus;
} }
void setPosition(int top, int left) { void setPosition(int left, int top) {
this->top = top; this->top = top;
this->left = left; this->left = left;
} }

98
components/Cursor.h Normal file
View File

@ -0,0 +1,98 @@
#ifndef CURSOR_H
#define CURSOR_H
#include "BaseComponent.h"
#include "../utils/Color.h"
#include <windows.h>
#include <iostream>
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<short>(1), static_cast<short>(1)};
COORD bufferPos = {0, 0};
if(posHasChanged_) {
SMALL_RECT oldReat = {static_cast<short>(lastPos_.X - 1), static_cast<short>(lastPos_.Y - 1), static_cast<short>(lastPos_.X), static_cast<short>(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<short>(left - 1), static_cast<short>(top - 1), static_cast<short>(left), static_cast<short>(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<short>(left), static_cast<short>(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

View File

@ -0,0 +1,51 @@
#include "Cursor.h"
#include <iostream>
#include <cassert>
#include <conio.h>
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;
}

12
components/Editor.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef EDITOR_H
#define EDITOR_H
#include "BaseComponent.h"
#include "TextArea.h"
class Editor : public BaseComponent {
private:
TextArea* textArea;
};
#endif

Binary file not shown.