summaryrefslogtreecommitdiff
path: root/displaymessage.c
blob: 8d85f8b214895b7e395b186d4c63a2746ccc9148 (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
#include "displaymessage.h"
#include "config.h"
#include "imageloader.h"
#include "helpers.h"

cNopacityDisplayMessage::cNopacityDisplayMessage(cImageCache *imgCache) {
    this->imgCache = imgCache;
    FadeTime = config.GetValue("messageFadeTime");
    FrameTime = FadeTime / 10;
    int top = geoManager->osdTop + geoManager->osdHeight - geoManager->messageHeight - config.GetValue("messageBorderBottom");
    int left = (geoManager->osdLeft + geoManager->osdWidth - geoManager->messageWidth) / 2;
    osd = CreateOsd(left, top, geoManager->messageWidth, geoManager->messageHeight);
    pixmap = osd->CreatePixmap(2, cRect(0, 0, geoManager->messageWidth, geoManager->messageHeight));
    pixmapBackground = osd->CreatePixmap(1, cRect(0, 0, geoManager->messageWidth, geoManager->messageHeight));
    if (FadeTime) {
        pixmap->SetAlpha(0);
        pixmapBackground->SetAlpha(0);
    }
}

cNopacityDisplayMessage::~cNopacityDisplayMessage() {
    Cancel(-1);
    while (Active())
        cCondWait::SleepMs(10);
    osd->DestroyPixmap(pixmap);
    osd->DestroyPixmap(pixmapBackground);
    delete osd;
}

void cNopacityDisplayMessage::SetMessage(eMessageType Type, const char *Text) {
    tColor col = Theme.Color(clrMessageStatus);
    tColor colFont = Theme.Color(clrMessageFontStatus);
    eSkinElementType seType = seMessageStatus;
    switch (Type) {
        case mtStatus:
            col = Theme.Color(clrMessageStatus);
            colFont = Theme.Color(clrMessageFontStatus);
            seType = seMessageStatus;
            break;
        case mtInfo:
            col = Theme.Color(clrMessageInfo);
            colFont = Theme.Color(clrMessageFontInfo);
            seType = seMessageInfo;
            break;
        case mtWarning:
            col = Theme.Color(clrMessageWarning);
            colFont = Theme.Color(clrMessageFontWarning);
            seType = seMessageWarning;
            break;
        case mtError:
            col = Theme.Color(clrMessageError);
            colFont = Theme.Color(clrMessageFontError);
            seType = seMessageError;
            break;
    }

    pixmap->Fill(clrTransparent);
    if (config.GetValue("displayType") == dtGraphical) {
        pixmapBackground->Fill(clrTransparent);
        cImage *imgBack = imgCache->GetSkinElement(seType);
        if (imgBack) {
            pixmapBackground->DrawImage(cPoint(0, 0), *imgBack);
        }
    } else {
        pixmapBackground->Fill(col);
        if (config.GetValue("displayType") == dtBlending) {
            cImage imgBack = imgCache->GetBackground(Theme.Color(clrMessageBlend), col, geoManager->messageWidth-2, geoManager->messageHeight-2, true);
            pixmapBackground->DrawImage(cPoint(1, 1), imgBack);
        }
        if (config.GetValue("roundedCorners")) {
            DrawRoundedCornersWithBorder(pixmapBackground, col, config.GetValue("cornerRadius"), geoManager->messageWidth, geoManager->messageHeight);
        }
    }
    int textWidth = fontManager->messageText->Width(Text);
    pixmap->DrawText(cPoint((geoManager->messageWidth - textWidth) / 2,
                            (geoManager->messageHeight - fontManager->messageText->Height()) / 2),
                            Text,
                            colFont,
                            clrTransparent,
                            fontManager->messageText);
    if (FadeTime)
        Start();
}

void cNopacityDisplayMessage::Flush(void) {
    osd->Flush();
}

void cNopacityDisplayMessage::Action(void) {
    uint64_t Start = cTimeMs::Now();
    while (Running()) {
        uint64_t Now = cTimeMs::Now();
        cPixmap::Lock();
        double t = min(double(Now - Start) / FadeTime, 1.0);
        int Alpha = t * ALPHA_OPAQUE;
        pixmapBackground->SetAlpha(Alpha);
        pixmap->SetAlpha(Alpha);
        cPixmap::Unlock();
        if (Running())
            osd->Flush();
        int Delta = cTimeMs::Now() - Now;
        if (Running() && (Delta < FrameTime))
            cCondWait::SleepMs(FrameTime - Delta);
        if ((int)(Now - Start) > FadeTime)
            break;
    }
}