Loading [MathJax]/extensions/tex2jax.js
ProtoTracer  1.0
Real-time 3D rendering and animation engine
All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Pages
TextEngine.tpp
Go to the documentation of this file.
1#include "TextEngine.h"
2
3template<uint8_t lineCount, uint8_t characterWidth>
4TextEngine<lineCount, characterWidth>::TextEngine(bool isEfficient) {
5 this->isEfficient = isEfficient;
6
7 ClearText(); //init to spaces
8}
9
10template<uint8_t lineCount, uint8_t characterWidth>
11TextEngine<lineCount, characterWidth>::TextEngine(Vector2D size, Vector2D position, uint16_t blinkTime, bool isEfficient) {
12 this->size = size;
13 this->positionOffset = position;
14 this->blinkTime = blinkTime;
15 this->isEfficient = isEfficient;
16
17 ClearText(); //init to spaces
18}
19
20template<uint8_t lineCount, uint8_t characterWidth>
21void TextEngine<lineCount, characterWidth>::SetMaterial(Material* material) {
22 this->material = material;
23}
24
25template<uint8_t lineCount, uint8_t characterWidth>
26void TextEngine<lineCount, characterWidth>::SetSize(Vector2D size) {
27 this->size = size;
28}
29
30template<uint8_t lineCount, uint8_t characterWidth>
31void TextEngine<lineCount, characterWidth>::SetPositionOffset(Vector2D positionOffset) {
32 this->positionOffset = positionOffset;
33}
34
35template<uint8_t lineCount, uint8_t characterWidth>
36void TextEngine<lineCount, characterWidth>::SetRotationOffset(Vector2D rotationOffset) {
37 this->rotationOffset = rotationOffset;
38}
39
40template<uint8_t lineCount, uint8_t characterWidth>
41void TextEngine<lineCount, characterWidth>::SetRotationAngle(float rotationAngle) {
42 this->rotationAngle = rotationAngle;
43}
44
45template<uint8_t lineCount, uint8_t characterWidth>
46void TextEngine<lineCount, characterWidth>::SetBlinkTime(uint16_t blinkTime) {
47 this->blinkTime = blinkTime;
48}
49
50template<uint8_t lineCount, uint8_t characterWidth>
51void TextEngine<lineCount, characterWidth>::SetText(uint8_t line, String value, bool centerText) {
52 uint8_t length = value.length();
53 int spacing = centerText ? (length - characterWidth) / 2 : 0;
54
55 if (line > lineCount || spacing < 0 || length > characterWidth) return; //break when outside of bounds
56
57 for (int i = spacing; i < length + spacing; i++) {
58 lines[line][i] = value[i - spacing];
59 }
60}
61
62template<uint8_t lineCount, uint8_t characterWidth>
63void TextEngine<lineCount, characterWidth>::ClearText() {
64 for (uint8_t i = 0; i < lineCount; i++) {
65 for (uint8_t j = 0; j < characterWidth; j++) {
66 lines[i][j] = ' ';
67 }
68 }
69}
70
71template<uint8_t lineCount, uint8_t characterWidth>
72RGBColor TextEngine<lineCount, characterWidth>::GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) {
73 Vector3D positionL = position;
74
75 if(rotationAngle != 0 && !isEfficient){
76 positionL = positionL - Vector3D(rotationOffset.X, rotationOffset.Y, 0);
77
78 Quaternion temp = Rotation(EulerAngles(Vector3D(0, 0, rotationAngle), EulerConstants::EulerOrderXYZS)).GetQuaternion();
79
80 positionL = temp.RotateVector(positionL);// rotate
81
82 positionL = positionL + Vector3D(rotationOffset.X, rotationOffset.Y, 0);
83 }
84 else{
85 Vector2D tempPos = positionL;
86
87 if(Mathematics::IsClose(int(rotationAngle) % 360, 90.0f, 45.0f)){
88 positionL.X = tempPos.Y;
89 positionL.Y = -tempPos.X;
90 }
91 else if (Mathematics::IsClose(int(rotationAngle) % 360, 180.0f, 45.0f)){
92 positionL.X = -tempPos.X;
93 positionL.Y = -tempPos.Y;
94 }
95 else if (Mathematics::IsClose(int(rotationAngle) % 360, 270.0f, 45.0f)){
96 positionL.X = -tempPos.Y;
97 positionL.Y = tempPos.X;
98 }
99 }
100
101 positionL = positionL - Vector3D(positionOffset.X, positionOffset.Y, 0);//offset position
102
103 int x = floorf(Mathematics::Map(positionL.X, 0.0f, size.X, characterWidth * 10.0f, 0.0f));
104 int y = floorf(Mathematics::Map(positionL.Y, 0.0f, size.Y, lineCount * 10.0f, 0.0f));
105
106 if(x < 0 || x >= characterWidth * 10 || y < 0 || y >= lineCount * 10) return black;
107
108 //bit
109 uint8_t charXBit = 9 - (x % 10);
110 uint8_t charYBit = y % 10;
111
112 char searchChar = lines[y / 10][x / 10];
113 bool blink = millis() % (blinkTime * 2) > blinkTime;
114
115 if(charYBit == 0 || charYBit == 9 || charXBit == 0 || charXBit == 9){//margin
116 if (searchChar > 90 && blink) {
117 return material->GetRGB(positionL, normal, uvw).HueShift(180);
118 }
119 else return black;
120 }
121 else{
122 uint8_t yCharacter = Characters::GetCharacter(searchChar)[charYBit - 1];//get character
123
124 bool xBit = 1 & (yCharacter >> (charXBit - 1));
125
126 if (searchChar > 90 && blink){
127 return xBit ? black : material->GetRGB(positionL, normal, uvw).HueShift(180);
128 }
129 else if (searchChar > 90){
130 return xBit ? material->GetRGB(positionL, normal, uvw).HueShift(180) : black;
131 }
132 else {
133 return xBit ? material->GetRGB(positionL, normal, uvw) : black;
134 }
135 }
136}