-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewPort.cpp
More file actions
126 lines (107 loc) · 2.97 KB
/
ViewPort.cpp
File metadata and controls
126 lines (107 loc) · 2.97 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* see ViewPort.h */
#include "ViewPort.h"
#include <stdint.h>
#include <string.h>
ViewPort_line::ViewPort_line(const char* l, text_align a, ViewPort_line* n)
: line(l), align(a), next(n) {}
ViewPort::ViewPort(uint8_t dx, uint8_t dy, void (*sc)(uint8_t x, uint8_t y), void (*pr)(char ch))
: first_line(nullptr), vx_offset(0), vy_offset(0), dmaxx(dx), dmaxy(dy), set_cursor(sc), print(pr),
text_width(dx), text_height(dy) {}
void ViewPort::append_line(ViewPort_line* line) {
ViewPort_line** last_line = &first_line;
while (*last_line != nullptr) {
last_line = &((*last_line)->next);
}
(*last_line) = line;
}
void ViewPort::clear() {
first_line = nullptr;
refresh();
}
void ViewPort::scroll_down() {
if (text_height - vy_offset > dmaxy) {
vy_offset++;
refresh();
}
}
void ViewPort::scroll_up() {
if (vy_offset) {
vy_offset--;
refresh();
}
}
void ViewPort::scroll_right() {
if (text_width - vx_offset > dmaxx) {
vx_offset++;
refresh();
}
}
void ViewPort::scroll_left() {
if (vx_offset) {
vx_offset--;
refresh();
}
}
void ViewPort::refresh() {
ViewPort_line* cur_line = first_line;
uint8_t cur_text_width = 0;
text_height = 0;
for (uint8_t row = 0; row < vy_offset; row++) {
if (!cur_line) break;
cur_line = cur_line->next;
text_height++;
}
for (uint8_t row = 0; row < dmaxy; row++) {
set_cursor(0,row);
if (!cur_line) {
for (uint8_t col = 0; col < dmaxx; col++) {
print(' ');
}
continue;
}
uint8_t row_text_width = 0;
uint8_t row_padding = 0;
const char* text = cur_line->line;
if (cur_line->align == align_right) {
row_padding = text_width - strlen(text);
}
else if (cur_line->align == align_center) {
row_padding = (text_width - strlen(text)) / 2;
}
for (uint8_t col = 0; col < vx_offset; col++) {
if (!text[0]) break;
if (row_padding) {
row_padding--;
}
else {
text++;
}
row_text_width++;
}
for (uint8_t col = 0; col < dmaxx; col++) {
char ch = ' ';
if (row_padding) {
row_padding--;
}
else if (text && text[0]) {
ch = text[0];
text++;
row_text_width++;
}
print(ch);
}
while (text[0]) {
text++;
row_text_width++;
}
if (row_text_width > text_width) text_width = row_text_width;
cur_line = cur_line->next;
text_height++;
}
while (cur_line) {
uint8_t row_text_width = strlen(cur_line->line);
if (row_text_width > text_width) text_width = row_text_width;
cur_line = cur_line->next;
text_height++;
}
}