1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include "Text.h"
#include "UTFTFactory.h"
RText::RText(uint8_t numberOFLine,Alignement alignement,font* textfont){
this->_numberOfLine = numberOFLine;
this->_alignement = alignement;
this->_numberOfLineCounter=0;
this->_textlines = new char*[_numberOfLine];
this->_interligne = 5;
for (int i = 0; i < _numberOfLine; i++)
{
this->_textlines[i] = "";
}
RUTFT* tft = UTFTFactory::GetInstance();
if( textfont == NULL)
this->_font = new font(tft->getFont(),new RGB(255,255,255));
else
this->_font = textfont;
}
void RText::AddLine(char* line){
if( this->_numberOfLineCounter< this->_numberOfLine){
_textlines[_numberOfLineCounter] = line;
_numberOfLineCounter++;
}
}
void RText::Write(uint8_t x,uint8_t y,uint8_t width){
RUTFT* tft = UTFTFactory::GetInstance();
tft->setFont(this-> _font->_font);
tft->setColor(this->_font->fontColor->R,this->_font->fontColor->G,this->_font->fontColor->B);
uint8_t text_x,text_y;
text_y = y + tft->getFontYsize()+_interligne;
double Charbyline = ceil(width / tft->getFontXsize());
for (int i = 0; i < _numberOfLine; i++)
{
if( strlen(_textlines[i] )<= Charbyline){
this->WriteLine(tft,_textlines[i],x,text_y,width);
}else{
double iteration = ceil(strlen(_textlines[i] )/Charbyline);
String str = String(_textlines[i]);
for (int i = 0; i < iteration; i=i+Charbyline)
{
String part = str.substring(i*Charbyline,((i+1)*Charbyline -1));
this-> WriteLine(tft,part,x,text_y,width);
text_y = text_y+tft->getFontYsize()+_interligne;
}
str = NULL;
}
text_y = text_y+tft->getFontYsize()+_interligne;
}
}
void RText::RefreshLine(uint8_t index, char* text){
if(index < this->_numberOfLine){
this->_textlines[index] = text;
}
}
void RText::WriteLine(UTFT* tft,String text,uint8_t x ,uint8_t y,uint8_t width){
char *buf= new char[text.length()+1];
text.toCharArray(buf, text.length()+1);
WriteLine(tft,buf,x,y,width);
free(buf);
}
void RText::WriteLine(UTFT* tft,char* text,uint8_t x ,uint8_t y,uint8_t width){
switch (_alignement)
{
case Alignement_LEFT:
break;
case Alignement_CENTER:
x= ((width/2) - ((strlen(text) * tft->getFontXsize())/2)) + x;
break;
case Alignement_RIGHT:
x = (uint8_t)(width-strlen(text)-1);
break;
default:
break;
}
tft->print(text,x,y);
free(text);
}
RText::~RText(void)
{
Serial.println("unloading text");
for (int i = 0; i < _numberOfLine; i++)
{
Serial.print("unloading text line");Serial.println(i);
delete [] _textlines[i];
Serial.print("unloaded text line");Serial.println(i);
}
delete [] _textlines;
Serial.println("unloaded text");
} |
Partager