You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.8 KiB
C++
111 lines
2.8 KiB
C++
#include "textwindow.h"
|
|
#include "cga.h"
|
|
#include "../utils/string.h"
|
|
#include "../debug/output.h"
|
|
|
|
TextWindow::TextWindow(unsigned from_col, unsigned to_col, unsigned from_row,
|
|
unsigned to_row, bool use_cursor) {
|
|
this->from_col = from_col;
|
|
this->to_col = to_col;
|
|
this->from_row = from_row;
|
|
this->to_row = to_row;
|
|
this->use_cursor = use_cursor;
|
|
|
|
setPos(0,0);
|
|
}
|
|
|
|
extern unsigned int testx, testy;
|
|
void TextWindow::setPos(unsigned rel_x, unsigned rel_y) {
|
|
unsigned abs_x = from_col + rel_x;
|
|
unsigned abs_y = from_row + rel_y;
|
|
|
|
if(abs_x >= CGA::COLUMNS)
|
|
return;
|
|
if(abs_y >= CGA::ROWS)
|
|
return;
|
|
|
|
if(use_cursor){
|
|
testx = abs_x;
|
|
testy = abs_y;
|
|
CGA::setCursor(abs_x, abs_y);
|
|
}else{
|
|
pos_x = abs_x;
|
|
pos_y = abs_y;
|
|
}
|
|
}
|
|
|
|
void TextWindow::getPos(unsigned& rel_x, unsigned& rel_y) const {
|
|
if(use_cursor){
|
|
CGA::getCursor(rel_x, rel_y);
|
|
rel_x -= from_col;
|
|
rel_y -= from_row;
|
|
}
|
|
else {
|
|
rel_x = pos_x - from_col;
|
|
rel_y = pos_y - from_row;
|
|
}
|
|
}
|
|
|
|
void TextWindow::setPos(int rel_x, int rel_y) {
|
|
if(rel_x < 0)
|
|
rel_x = to_col - from_col + rel_x;
|
|
if(rel_y < 0)
|
|
rel_y = to_row - from_row + rel_y;
|
|
|
|
setPos((unsigned) rel_x, (unsigned) rel_y);
|
|
}
|
|
|
|
void TextWindow::getPos(int& rel_x, int& rel_y) const {
|
|
unsigned x, y;
|
|
getPos(x,y);
|
|
rel_x = x;
|
|
rel_y = y;
|
|
}
|
|
|
|
void TextWindow::scrollUp(){
|
|
for(uint8_t row = from_row; row < to_row-1; row++){
|
|
memmove(&CGA::TEXT_BUFFER_BASE[row*CGA::COLUMNS + from_col], &CGA::TEXT_BUFFER_BASE[(row+1)*CGA::COLUMNS + from_col], 2*(to_col-from_col));
|
|
}
|
|
memset(&CGA::TEXT_BUFFER_BASE[(to_row-1)*CGA::COLUMNS + from_col] , 0, 2*(to_col-from_col));
|
|
}
|
|
|
|
void TextWindow::print(const char* str, size_t length, CGA::Attribute attrib) {
|
|
for(unsigned i=0; i<length; i++){
|
|
unsigned x_now, y_now;
|
|
getPos(x_now, y_now);
|
|
if(str[i] == '\n'){
|
|
x_now = 0;
|
|
if(from_row + y_now >= to_row-1){
|
|
scrollUp();
|
|
}
|
|
else{
|
|
y_now++;
|
|
}
|
|
setPos(x_now, y_now);
|
|
return;
|
|
}
|
|
|
|
CGA::show(from_col + x_now, from_row + y_now, str[i], attrib);
|
|
if(from_col+x_now >= to_col-1){
|
|
x_now = 0;
|
|
if(from_row + y_now >= to_row-1){
|
|
scrollUp();
|
|
}
|
|
else{
|
|
y_now++;
|
|
}
|
|
}
|
|
else {
|
|
x_now++;
|
|
}
|
|
setPos(x_now, y_now);
|
|
}
|
|
}
|
|
|
|
void TextWindow::reset(char character, CGA::Attribute attrib) {
|
|
for(unsigned i=from_row; i<to_row; i++)
|
|
for(unsigned j=from_col; j<to_col; j++)
|
|
CGA::show(j, i, character, attrib);
|
|
setPos(0, 0);
|
|
}
|