Silence uninitialised variable warnings in SSE blitters
[openttd/fttd.git] / src / statusbar_gui.cpp
blob62799e18bfa97bab37830d94714745cfb5aab4f7
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file statusbar_gui.cpp The GUI for the bottom status bar. */
12 #include "stdafx.h"
13 #include "date_func.h"
14 #include "gfx_func.h"
15 #include "news_func.h"
16 #include "company_func.h"
17 #include "string_func.h"
18 #include "strings_func.h"
19 #include "company_base.h"
20 #include "tilehighlight_func.h"
21 #include "news_gui.h"
22 #include "company_gui.h"
23 #include "window_gui.h"
24 #include "saveload/saveload.h"
25 #include "window_func.h"
26 #include "statusbar_gui.h"
27 #include "core/geometry_func.hpp"
29 #include "widgets/statusbar_widget.h"
31 #include "table/strings.h"
32 #include "table/sprites.h"
34 static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
36 CopyInDParam(0, ni->params, lengthof(ni->params));
37 StringID str = ni->string_id;
39 char buf[512];
40 GetString(buf, str, lastof(buf));
41 const char *s = buf;
43 char buffer[256];
44 char *d = buffer;
45 const char *last = lastof(buffer);
47 for (;;) {
48 WChar c = Utf8Consume(&s);
49 if (c == 0) {
50 break;
51 } else if (c == '\n') {
52 if (d + 4 >= last) break;
53 d[0] = d[1] = d[2] = d[3] = ' ';
54 d += 4;
55 } else if (IsPrintable(c)) {
56 if (d + Utf8CharLen(c) >= last) break;
57 d += Utf8Encode(d, c);
60 *d = '\0';
62 DrawPixelInfo tmp_dpi;
63 if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
65 int width = GetStringBoundingBox(buffer).width;
66 int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
68 DrawPixelInfo *old_dpi = _cur_dpi;
69 _cur_dpi = &tmp_dpi;
70 DrawString(pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
71 _cur_dpi = old_dpi;
73 return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
76 struct StatusBarWindow : Window {
77 bool saving;
78 int ticker_scroll;
79 int reminder_timeout;
81 static const int TICKER_STOP = 1640; ///< scrolling is finished when counter reaches this value
82 static const int REMINDER_START = 91; ///< initial value of the reminder counter (right dot on the right)
83 static const int REMINDER_STOP = 0; ///< reminder disappears when counter reaches this value
84 static const int COUNTER_STEP = 2; ///< this is subtracted from active counters every tick
86 StatusBarWindow(WindowDesc *desc) : Window(desc)
88 this->ticker_scroll = TICKER_STOP;
89 this->reminder_timeout = REMINDER_STOP;
91 this->InitNested();
92 CLRBITS(this->flags, WF_WHITE_BORDER);
93 PositionStatusbar(this);
96 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
98 Point pt = { 0, _screen.height - sm_height };
99 return pt;
102 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
104 Dimension d;
105 switch (widget) {
106 case WID_S_LEFT:
107 SetDParamMaxValue(0, MAX_YEAR * DAYS_IN_YEAR);
108 d = GetStringBoundingBox(STR_WHITE_DATE_LONG);
109 break;
111 case WID_S_RIGHT: {
112 int64 max_money = UINT32_MAX;
113 const Company *c;
114 FOR_ALL_COMPANIES(c) max_money = max<int64>(c->money, max_money);
115 SetDParam(0, 100LL * max_money);
116 d = GetStringBoundingBox(STR_COMPANY_MONEY);
117 break;
120 default:
121 return;
124 d.width += padding.width;
125 d.height += padding.height;
126 *size = maxdim(d, *size);
129 virtual void DrawWidget(const Rect &r, int widget) const
131 switch (widget) {
132 case WID_S_LEFT:
133 /* Draw the date */
134 SetDParam(0, _date);
135 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_WHITE_DATE_LONG, TC_FROMSTRING, SA_HOR_CENTER);
136 break;
138 case WID_S_RIGHT: {
139 /* Draw company money, if any */
140 const Company *c = Company::GetIfValid(_local_company);
141 if (c != NULL) {
142 SetDParam(0, c->money);
143 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_COMPANY_MONEY, TC_FROMSTRING, SA_HOR_CENTER);
145 break;
148 case WID_S_MIDDLE:
149 /* Draw status bar */
150 if (this->saving) { // true when saving is active
151 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_SAVING_GAME, TC_FROMSTRING, SA_HOR_CENTER);
152 } else if (_do_autosave) {
153 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_HOR_CENTER);
154 } else if (_pause_mode != PM_UNPAUSED) {
155 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_PAUSED, TC_FROMSTRING, SA_HOR_CENTER);
156 } else if (this->ticker_scroll < TICKER_STOP && FindWindowById(WC_NEWS_WINDOW, 0) == NULL && _statusbar_news_item != NULL && _statusbar_news_item->string_id != 0) {
157 /* Draw the scrolling news text */
158 if (!DrawScrollingStatusText(_statusbar_news_item, this->ticker_scroll, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom)) {
159 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
160 if (Company::IsValidID(_local_company)) {
161 /* This is the default text */
162 SetDParam(0, _local_company);
163 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_HOR_CENTER);
166 } else {
167 if (Company::IsValidID(_local_company)) {
168 /* This is the default text */
169 SetDParam(0, _local_company);
170 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_HOR_CENTER);
174 if (this->reminder_timeout > 0) {
175 Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS);
176 DrawSprite(SPR_UNREAD_NEWS, PAL_NONE, r.right - WD_FRAMERECT_RIGHT - icon_size.width, r.top + WD_FRAMERECT_TOP + (int)(FONT_HEIGHT_NORMAL - icon_size.height) / 2);
178 break;
183 * Some data on this window has become invalid.
184 * @param data Information about the changed data.
185 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
187 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
189 if (!gui_scope) return;
190 switch (data) {
191 default: NOT_REACHED();
192 case SBI_SAVELOAD_START: this->saving = true; break;
193 case SBI_SAVELOAD_FINISH: this->saving = false; break;
194 case SBI_SHOW_TICKER: this->ticker_scroll = 0; break;
195 case SBI_SHOW_REMINDER: this->reminder_timeout = REMINDER_START; break;
196 case SBI_NEWS_DELETED:
197 this->ticker_scroll = TICKER_STOP; // reset ticker ...
198 this->reminder_timeout = REMINDER_STOP; // ... and reminder
199 break;
203 virtual void OnClick(Point pt, int widget, int click_count)
205 switch (widget) {
206 case WID_S_MIDDLE: ShowLastNewsMessage(); break;
207 case WID_S_RIGHT: if (_local_company != COMPANY_SPECTATOR) ShowCompanyFinances(_local_company); break;
208 default: ResetObjectToPlace();
212 virtual void OnTick()
214 if (_pause_mode != PM_UNPAUSED) return;
216 if (this->ticker_scroll < TICKER_STOP) { // Scrolling text
217 this->ticker_scroll += COUNTER_STEP;
218 this->SetWidgetDirty(WID_S_MIDDLE);
221 if (this->reminder_timeout > REMINDER_STOP) { // Red blot to show there are new unread newsmessages
222 this->reminder_timeout -= COUNTER_STEP;
223 } else if (this->reminder_timeout < REMINDER_STOP) {
224 this->reminder_timeout = REMINDER_STOP;
225 this->SetWidgetDirty(WID_S_MIDDLE);
230 static const NWidgetPart _nested_main_status_widgets[] = {
231 NWidget(NWID_HORIZONTAL),
232 NWidget(WWT_PANEL, COLOUR_GREY, WID_S_LEFT), SetMinimalSize(140, 12), EndContainer(),
233 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_MIDDLE), SetMinimalSize(40, 12), SetDataTip(0x0, STR_STATUSBAR_TOOLTIP_SHOW_LAST_NEWS), SetResize(1, 0),
234 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_RIGHT), SetMinimalSize(140, 12),
235 EndContainer(),
238 static WindowDesc _main_status_desc(
239 WDP_MANUAL, NULL, 640, 12,
240 WC_STATUS_BAR, WC_NONE,
241 WDF_NO_FOCUS,
242 _nested_main_status_widgets, lengthof(_nested_main_status_widgets)
246 * Checks whether the news ticker is currently being used.
248 bool IsNewsTickerShown()
250 const StatusBarWindow *w = dynamic_cast<StatusBarWindow*>(FindWindowById(WC_STATUS_BAR, 0));
251 return w != NULL && w->ticker_scroll < StatusBarWindow::TICKER_STOP;
255 * Show our status bar.
257 void ShowStatusBar()
259 new StatusBarWindow(&_main_status_desc);