summaryrefslogtreecommitdiff
path: root/textscroller.c
blob: 37cf31f0d2af0c0576406edcf555299704025aac (plain)
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
#include "textscroller.h"

void cTextScroll::SetText(const char *text, cRect position, tColor colorFg, tColor colorBg, cFont *font, tColor colorExtraTextFg) {
    if( Osd == NULL )
        return;

    Text = text;

    Font = font;
    Position = position;

    ColorFg = colorFg; ColorBg = colorBg; ColorExtraTextFg = colorExtraTextFg;
    cRect drawPort(0, 0, font->Width(Text.c_str()), Position.Height());

    if( Osd && Pixmap )
        Osd->DestroyPixmap(Pixmap);

    Pixmap = Osd->CreatePixmap(Layer, Position, drawPort);
    dsyslog("skinflatplus: TextScrollerPixmap left: %d top: %d width: %d height: %d", Position.Left(), Position.Top(), Position.Width(), Position.Height());
    dsyslog("skinflatplus: TextScrollerPixmap drawPort left: %d top: %d width: %d height: %d", drawPort.Left(), drawPort.Top(), drawPort.Width(), drawPort.Height());
    Pixmap->Fill( colorBg );
    Draw();
}

void cTextScroll::UpdateViewPortWidth(int w) {
    cRect viewPort = Pixmap->ViewPort();
    viewPort.SetWidth(viewPort.Width() - w);
    Pixmap->SetViewPort(viewPort);
}

void cTextScroll::Reset(void) {
    if( !Pixmap )
        return;

    Pixmap->SetDrawPortPoint(cPoint(0, 0));
    waitSteps = WAITSTEPS;
}

void cTextScroll::Draw(void) {
    if( !Pixmap )
        return;

    if( ColorExtraTextFg ) {
        std::string tilde = Text;
        size_t found = tilde.find(" ~ ");
        size_t found2 = tilde.find("~");
        if( found != std::string::npos ) {
            std::string first = tilde.substr(0, found);
            std::string second = tilde.substr(found +2, tilde.length() );

            Pixmap->DrawText(cPoint(0, 0), first.c_str(), ColorFg, ColorBg, Font);
            int l = Font->Width( first.c_str() );
            Pixmap->DrawText(cPoint(l, 0), second.c_str(), ColorExtraTextFg, ColorBg, Font);
        } else if ( found2 != std::string::npos ) {
            std::string first = tilde.substr(0, found2);
            std::string second = tilde.substr(found2 +1, tilde.length() );

            Pixmap->DrawText(cPoint(0, 0), first.c_str(), ColorFg, ColorBg, Font);
            int l = Font->Width( first.c_str() );
            l += Font->Width("X");
            Pixmap->DrawText(cPoint(l, 0), second.c_str(), ColorExtraTextFg, ColorBg, Font);
        } else
            Pixmap->DrawText(cPoint(0, 0), Text.c_str(), ColorFg, ColorBg, Font);
    } else {
        Pixmap->DrawText(cPoint(0, 0), Text.c_str(), ColorFg, ColorBg, Font);
    }
}

void cTextScroll::DoStep(void) {
    if( !Pixmap )
        return;

    // wait at the beginning for better read
    if( waitSteps > 0 ) {
        waitSteps--;
        return;
    }
    // wait after return to the front
    if( ResetX ) {
        ResetX = false;
        Pixmap->SetDrawPortPoint(cPoint(0, 0));
        waitSteps = WAITSTEPS;
        return;
    }

    int drawPortX = Pixmap->DrawPort().X();

    if( isReserveStep )
        drawPortX += PixelsPerStep;
    else
        drawPortX -= PixelsPerStep;

    int maxX = Pixmap->DrawPort().Width() - Pixmap->ViewPort().Width();
    maxX *= -1;

    if( ScrollType == 0 ) {
        if( drawPortX <= maxX ) {
            drawPortX += PixelsPerStep;
            ResetX = true;
            waitSteps = WAITSTEPS;
        }
    } else if( ScrollType == 1 ) {
        if( drawPortX <= maxX ) {
            isReserveStep = true;
            waitSteps = WAITSTEPS;
        } else if( drawPortX > 0 ) {
            isReserveStep = false;
            waitSteps = WAITSTEPS;
        }
    }

    Pixmap->SetDrawPortPoint(cPoint(drawPortX, 0));
}

cTextScrollers::cTextScrollers() {
    Layer = 2;
}

cTextScrollers::~cTextScrollers() {
}

void cTextScrollers::Clear(void) {
    Cancel(-1);
    while( Active() )
        cCondWait::SleepMs(10);

    std::vector<cTextScroll *>::iterator it;
    for( it = Scrollers.begin(); it != Scrollers.end(); it++) {
        delete *it;
    }

    Scrollers.clear();
}

void cTextScrollers::AddScroller(const char *text, cRect position, tColor colorFg, tColor colorBg, cFont *font, tColor ColorExtraTextFg) {
    Cancel(-1);
    while( Active() )
        cCondWait::SleepMs(10);

    Scrollers.push_back( new cTextScroll(Osd, scrollType, scrollStep, (int)((double)WAITDELAY / (double)scrollDelay), Layer) );
    Scrollers.back()->SetText(text, position, colorFg, colorBg, font, ColorExtraTextFg);

    StartScrolling();
}

void cTextScrollers::UpdateViewPortWidth(int w) {
    std::vector<cTextScroll *>::iterator it;
    for( it = Scrollers.begin(); it != Scrollers.end(); it++) {
        cPixmap::Lock();
        (*it)->UpdateViewPortWidth(w);
        cPixmap::Unlock();
    }
}

void cTextScrollers::StartScrolling(void) {
    if( !Running() && Scrollers.size() > 0 ) {
        Start();
    }
}

void cTextScrollers::Action(void) {
    // wait 1 second so the osd is finished
    for(int i = 0; i < 100 && Running(); i++ ) {
        cCondWait::SleepMs(10);
    }

    if( !Running() )
        return;

    std::vector<cTextScroll *>::iterator it;
    for( it = Scrollers.begin(); it != Scrollers.end(); it++) {
        if( !Running() )
            return;
        cPixmap::Lock();
        (*it)->Reset();
        cPixmap::Unlock();
    }

    while( Running() ) {
        if (Running())
            cCondWait::SleepMs(scrollDelay);

        std::vector<cTextScroll *>::iterator it;
        for( it = Scrollers.begin(); it != Scrollers.end(); it++) {
            if( !Running() )
                return;
            cPixmap::Lock();
            (*it)->DoStep();
            cPixmap::Unlock();
        }

        if( Running() )
            Osd->Flush();
    }
}