-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
111 lines (103 loc) · 3.15 KB
/
window.cpp
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
108
109
110
111
/*
* window.cpp
*
* Created on: 9 ago 2022
* Author: Francesco Antonetti Lamorgese Passeri
* Version: 1.0
* License: CC-BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
* This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
*/
#ifndef WINDOW_HPP_
#include "window.hpp"
#endif
#include "Arduino.h"
window::window(UTFT& tft,int* size,int* position) : tft{tft},position{position[0],position[1]},size{size[0],size[1]},fontSize{tft.getFontXsize(),tft.getFontYsize()}
{
this->drawBorders();
}
window::window(UTFT& tft,int sizeX,int sizeY,int positionX,int positionY) : tft{tft},position{positionX,positionY},size{sizeX,sizeY},fontSize{tft.getFontXsize(),tft.getFontYsize()}
{
this->drawBorders();
}
int* window::getSize()
{
return this->size;
}
int* window::getPosition()
{
return this->position;
}
void window::drawBorders()
{
this->tft.drawRect(this->position[0],this->position[1],this->size[0]+this->position[0],this->size[1]+this->position[1]);
}
void window::clearWindow()
{
int previousColor = tft.getColor();
this->tft.setColor(0);
this->tft.fillRect(this->position[0],this->position[1],this->size[0]+this->position[0],this->size[1]+this->position[1]);
this->tft.setColor(previousColor);
this->drawBorders();
}
void window::text(String text)
{
int textLength;
int textPositionX;
int textPositionY;
int fontSizeX = this->tft.getFontXsize();
int maxLineChars = this->size[0]/fontSizeX;
const int numLines = (this->size[1]-2)/this->tft.getFontYsize();
int filledLines = 0;
if((text.length() < maxLineChars) && (text.indexOf("\n") < 0))
{
textPositionX = (this->position[0]+(this->size[0]-text.length()*this->tft.getFontXsize())/2);
textPositionY = (this->position[1]+(this->size[1]-this->tft.getFontYsize())/2);
this->tft.print(text,textPositionX,textPositionY);
}
else
{
String lines[numLines];
for(int textIndex=0,lineNumber=0;lineNumber < numLines;++lineNumber)
{
String line = text.substring(textIndex,(textIndex+maxLineChars < text.length()) ? textIndex+maxLineChars : textIndex+text.length());
int newLine = line.indexOf("\n");
if((newLine > -1) && (newLine <= text.length()))
{
line = line.substring(0,newLine);
text.remove(text.indexOf("\n"),1);
line.remove(line.indexOf("\n"),1);
if(line.length())
lines[lineNumber] = line;
else
--lineNumber;
if(textIndex < text.length())
textIndex += newLine;
else
{
filledLines = lineNumber;
break;
}
continue;
}
lines[lineNumber] = line;
if(textIndex < text.length())
(textIndex+maxLineChars < text.length()) ? textIndex += maxLineChars : textIndex = text.length();
else
{
filledLines = lineNumber;
break;
}
}
for(int i=0;i<filledLines;i++)
{
if(lines[i]!="")
{
textPositionX = this->position[0]+1;
textPositionY = this->position[1]+i*this->tft.getFontYsize()+1;
this->tft.print(lines[i],textPositionX,textPositionY);
}
else
break;
}
}
}