1 /*
2  *  This file is part of Dune Legacy.
3  *
4  *  Dune Legacy is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  Dune Legacy is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with Dune Legacy.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <GUI/dune/NewsTicker.h>
19 
20 #include <globals.h>
21 
22 #include <FileClasses/GFXManager.h>
23 #include <FileClasses/FontManager.h>
24 
25 #define MESSAGETIME 440
26 #define SLOWDOWN timer/55
27 
NewsTicker()28 NewsTicker::NewsTicker() : Widget() {
29     enableResizing(false,false);
30 
31     timer = -MESSAGETIME/2;
32     pBackground = pGFXManager->getUIGraphic(UI_MessageBox);
33     pCurrentMessageTexture = nullptr;
34 
35     resize(getTextureSize(pBackground));
36 }
37 
~NewsTicker()38 NewsTicker::~NewsTicker() {
39     if(pCurrentMessageTexture != nullptr) {
40         SDL_DestroyTexture(pCurrentMessageTexture);
41         pCurrentMessageTexture = nullptr;
42     }
43 }
44 
addMessage(const std::string & msg)45 void NewsTicker::addMessage(const std::string& msg)
46 {
47     bool found = false;
48 
49     /*if message is already there, do nothing*/
50     std::queue<std::string> msgcpy(messages);
51     while(!msgcpy.empty()) {
52         if(msgcpy.front() == msg) {
53             found = true;
54         }
55         msgcpy.pop();
56     }
57 
58     if(!found && messages.size() < 3) {
59         messages.push(msg);
60     }
61 }
62 
addUrgentMessage(const std::string & msg)63 void NewsTicker::addUrgentMessage(const std::string& msg)
64 {
65     while(!messages.empty()) {
66         messages.pop();
67     }
68 
69     messages.push(msg);
70 }
71 
draw(Point position)72 void NewsTicker::draw(Point position) {
73     if(isVisible() == false) {
74         return;
75     }
76 
77     //draw background
78     if(pBackground == nullptr) {
79         return;
80     }
81 
82     SDL_Rect dest = calcDrawingRect(pBackground, position.x, position.y);
83     SDL_RenderCopy(renderer, pBackground, nullptr, &dest);
84 
85     // draw message
86     if(!messages.empty()) {
87         if(timer++ == (MESSAGETIME/3)) {
88             timer = -MESSAGETIME/2;
89             // delete first message
90             messages.pop();
91 
92             // if no more messages leave
93             if(messages.empty()) {
94                 timer = -MESSAGETIME/2;
95                 return;
96             };
97         };
98 
99         //draw text
100         SDL_Rect textLocation = { position.x + 10, position.y + 5, 0, 0 };
101         if(timer>0) {
102             textLocation.y -= SLOWDOWN;
103         }
104 
105         if(currentMessage != messages.front()) {
106             if(pCurrentMessageTexture != nullptr) {
107                 SDL_DestroyTexture(pCurrentMessageTexture);
108                 pCurrentMessageTexture = nullptr;
109             }
110             currentMessage = messages.front();
111             pCurrentMessageTexture = pFontManager->createTextureWithText(currentMessage, COLOR_BLACK, FONT_STD10);
112         }
113 
114         if(pCurrentMessageTexture != nullptr) {
115 
116             SDL_Rect cut = { 0, 0, 0, 0 };
117             if(timer>0) {
118                 cut.y = 3*SLOWDOWN;
119             }
120 
121             textLocation.w = cut.w = getWidth(pCurrentMessageTexture);
122             textLocation.h = cut.h = getHeight(pCurrentMessageTexture) - cut.y;
123             SDL_RenderCopy(renderer, pCurrentMessageTexture, &cut, &textLocation);
124         }
125     };
126 }
127