forked from vdr-projects/vdr-plugin-skinflatplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextscroller.c
221 lines (176 loc) · 6.1 KB
/
textscroller.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Skin flatPlus: A plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include "./textscroller.h"
void cTextScroll::SetText(const char *text, cRect position, tColor colorFg, tColor colorBg, cFont *font,
tColor colorExtraTextFg) {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cTextScroll::SetText()");
#endif
// if (!m_Osd) return;
m_Text = text;
m_Font = font;
m_Position = position;
ColorFg = colorFg; ColorBg = colorBg; ColorExtraTextFg = colorExtraTextFg;
const cRect DrawPort(0, 0, font->Width(text), position.Height());
m_Osd->DestroyPixmap(Pixmap);
Pixmap = CreatePixmap(m_Osd, "Pixmap", m_Layer, position, DrawPort);
#ifdef DEBUGFUNCSCALL
dsyslog(" Pixmap left %d, top %d, width %d, height %d", m_Position.Left(), m_Position.Top(), m_Position.Width(),
m_Position.Height());
dsyslog(" DrawPort left %d, top %d, width %d, height %d", DrawPort.Left(), DrawPort.Top(), DrawPort.Width(),
DrawPort.Height());
#endif
PixmapFill(Pixmap, colorBg);
Draw();
}
void cTextScroll::UpdateViewPortWidth(int w) {
// if (!Pixmap) return; // Check in 'Draw()'. Try to reduce load
cRect ViewPort {Pixmap->ViewPort()};
ViewPort.SetWidth(ViewPort.Width() - w);
Pixmap->SetViewPort(ViewPort);
}
void cTextScroll::Reset() {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cTextScroll::Reset()");
#endif
// if (!Pixmap) return; // Check in 'Draw()'. Try to reduce load
Pixmap->SetDrawPortPoint(cPoint(0, 0));
m_WaitSteps = m_WAITSTEPS;
}
void cTextScroll::Draw() {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cTextScroll::Draw()");
#endif
if (!Pixmap) return;
const char *Text {*m_Text};
const char *TildePos {strchr(Text, '~')};
if (TildePos && ColorExtraTextFg) {
std::string_view sv1 {Text, static_cast<size_t>(TildePos - Text)};
std::string_view sv2 {TildePos + 1};
const std::string first {rtrim(sv1)};
const std::string second {ltrim(sv2)};
Pixmap->DrawText(cPoint(0, 0), first.c_str(), ColorFg, ColorBg, m_Font);
const int l {m_Font->Width(first.c_str()) + m_Font->Width('X')};
Pixmap->DrawText(cPoint(l, 0), second.c_str(), ColorExtraTextFg, ColorBg, m_Font);
} else {
Pixmap->DrawText(cPoint(0, 0), Text, ColorFg, ColorBg, m_Font);
}
}
void cTextScroll::DoStep() {
// if (!Pixmap) return; // Try to reduce load
if (m_WaitSteps > 0) { // Wait at the beginning for better read
--m_WaitSteps;
return;
}
if (m_ResetX) { // Wait after return to the front
m_ResetX = false;
Pixmap->SetDrawPortPoint(cPoint(0, 0));
m_WaitSteps = m_WAITSTEPS;
return;
}
int DrawPortX {Pixmap->DrawPort().X() + (m_IsReserveStep ? m_PixelsPerStep : -m_PixelsPerStep)};
int maxX {Pixmap->DrawPort().Width() - Pixmap->ViewPort().Width()};
maxX *= -1;
if (m_ScrollType == 0) {
if (DrawPortX <= maxX) {
DrawPortX += m_PixelsPerStep;
m_ResetX = true;
m_WaitSteps = m_WAITSTEPS;
}
} else if (m_ScrollType == 1) {
if (DrawPortX <= maxX) {
m_IsReserveStep = true;
m_WaitSteps = m_WAITSTEPS;
} else if (DrawPortX > 0) {
m_IsReserveStep = false;
m_WaitSteps = m_WAITSTEPS;
}
}
Pixmap->SetDrawPortPoint(cPoint(DrawPortX, 0));
}
cTextScrollers::cTextScrollers() {
m_Layer = 2;
Scrollers.reserve(8);
}
cTextScrollers::~cTextScrollers() {}
void cTextScrollers::Clear() {
#ifdef DEBUGFUNCSCALL
if (!Scrollers.empty())
dsyslog("flatPlus: cTextScrollers::Clear() Size %ld", Scrollers.size());
#endif
Cancel(-1);
while (Active())
cCondWait::SleepMs(10);
std::vector<cTextScroll *>::iterator it, end = Scrollers.end();
for (it = Scrollers.begin(); it != end; ++it) {
delete *it;
}
Scrollers.clear();
}
void cTextScrollers::AddScroller(const char *text, cRect position, tColor colorFg, tColor colorBg, cFont *Font,
tColor ColorExtraTextFg) {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cTextScrollers::AddScroller()");
#endif
Cancel(-1);
while (Active())
cCondWait::SleepMs(10);
if (m_ScrollDelay == 0) { // Avoid DIV/0
esyslog("flatPlus: Error in cTextScrollers::AddScroller() m_ScrollDelay is 0!");
return;
}
Scrollers.emplace_back(new cTextScroll(m_Osd, m_ScrollType, m_ScrollStep,
static_cast<int>(WAITDELAY * 1.0 / m_ScrollDelay), m_Layer));
Scrollers.back()->SetText(text, position, colorFg, colorBg, Font, ColorExtraTextFg);
StartScrolling();
}
void cTextScrollers::UpdateViewPortWidth(int w) {
std::vector<cTextScroll *>::iterator it, end = Scrollers.end();
for (it = Scrollers.begin(); it != end; ++it) {
cPixmap::Lock();
(*it)->UpdateViewPortWidth(w);
cPixmap::Unlock();
}
}
void cTextScrollers::StartScrolling() {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cTextScrollers::StartScrolling()");
#endif
if (!Running() && Scrollers.size() > 0)
Start();
}
void cTextScrollers::Action() {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cTextScrollers::Action()");
#endif
// Wait 1 second so the osd is finished
for (uint i {0}; i < 10 && Running(); ++i) {
cCondWait::SleepMs(100);
}
if (!Running()) return;
std::vector<cTextScroll *>::iterator it, end = Scrollers.end();
for (it = Scrollers.begin(); it != end; ++it) {
if (!Running()) return;
cPixmap::Lock();
(*it)->Reset();
cPixmap::Unlock();
}
while (Running()) {
// if (Running()) //? Check needed here?
cCondWait::SleepMs(m_ScrollDelay);
// std::vector<cTextScroll *>::iterator it, end = Scrollers.end(); // Reuse iterator above
for (it = Scrollers.begin(); it != end; ++it) {
if (!Running()) return;
cPixmap::Lock();
(*it)->DoStep();
cPixmap::Unlock();
}
if (Running())
m_Osd->Flush();
}
}