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

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

    Text = text;

    Font = font;
    Position = position;

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

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

    Pixmap = Osd->CreatePixmap(Layer, Position, drawPort);
    Pixmap->Fill( colorBg );
    Draw();
}

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

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

void cTextScroll::Draw(void) {
    if( !Pixmap )
        return;
    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) {
    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);

    StartScrolling();
}

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();
    }
}