OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

statusbar_gui.cpp
Go to the documentation of this file.
1 /* $Id: statusbar_gui.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
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 */
9
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"
28
29 #include "widgets/statusbar_widget.h"
30
31 #include "table/strings.h"
32 #include "table/sprites.h"
33
34 #include "safeguards.h"
35
36 static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
37 {
38 CopyInDParam(0, ni->params, lengthof(ni->params));
39 StringID str = ni->string_id;
40
41 char buf[512];
42 GetString(buf, str, lastof(buf));
43 const char *s = buf;
44
45 char buffer[256];
46 char *d = buffer;
47 const char *last = lastof(buffer);
48
49 for (;;) {
50 WChar c = Utf8Consume(&s);
51 if (c == 0) {
52 break;
53 } else if (c == '\n') {
54 if (d + 4 >= last) break;
55 d[0] = d[1] = d[2] = d[3] = ' ';
56 d += 4;
57 } else if (IsPrintable(c)) {
58 if (d + Utf8CharLen(c) >= last) break;
59 d += Utf8Encode(d, c);
60 }
61 }
62 *d = '\0';
63
64 DrawPixelInfo tmp_dpi;
65 if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
66
67 int width = GetStringBoundingBox(buffer).width;
68 int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
69
70 DrawPixelInfo *old_dpi = _cur_dpi;
71 _cur_dpi = &tmp_dpi;
72 DrawString(pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
73 _cur_dpi = old_dpi;
74
75 return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
76 }
77
78 struct StatusBarWindow : Window {
79 bool saving;
80 int ticker_scroll;
81 int reminder_timeout;
82
83 static const int TICKER_STOP = 1640;
84 static const int REMINDER_START = 91;
85 static const int REMINDER_STOP = 0;
86 static const int COUNTER_STEP = 2;
87
88 StatusBarWindow(WindowDesc *desc) : Window(desc)
89 {
90 this->ticker_scroll = TICKER_STOP;
91 this->reminder_timeout = REMINDER_STOP;
92
93 this->InitNested();
94 CLRBITS(this->flags, WF_WHITE_BORDER);
95 PositionStatusbar(this);
96 }
97
98 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
99 {
100 Point pt = { 0, _screen.height - sm_height };
101 return pt;
102 }
103
104 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
105 {
106 Dimension d;
107 switch (widget) {
108 case WID_S_LEFT:
109 SetDParamMaxValue(0, MAX_YEAR * DAYS_IN_YEAR);
110 d = GetStringBoundingBox(STR_WHITE_DATE_LONG);
111 break;
112
113 case WID_S_RIGHT: {
114 int64 max_money = UINT32_MAX;
115 const Company *c;
116 FOR_ALL_COMPANIES(c) max_money = max<int64>(c->money, max_money);
117 SetDParam(0, 100LL * max_money);
118 d = GetStringBoundingBox(STR_COMPANY_MONEY);
119 break;
120 }
121
122 default:
123 return;
124 }
125
126 d.width += padding.width;
127 d.height += padding.height;
128 *size = maxdim(d, *size);
129 }
130
131 virtual void DrawWidget(const Rect &r, int widget) const
132 {
133 switch (widget) {
134 case WID_S_LEFT:
135 /* Draw the date */
136 SetDParam(0, _date);
137 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);
138 break;
139
140 case WID_S_RIGHT: {
141 /* Draw company money, if any */
142 const Company *c = Company::GetIfValid(_local_company);
143 if (c != NULL) {
144 SetDParam(0, c->money);
145 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_COMPANY_MONEY, TC_FROMSTRING, SA_HOR_CENTER);
146 }
147 break;
148 }
149
150 case WID_S_MIDDLE:
151 /* Draw status bar */
152 if (this->saving) { // true when saving is active
153 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);
154 } else if (_do_autosave) {
155 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_HOR_CENTER);
156 } else if (_pause_mode != PM_UNPAUSED) {
157 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_PAUSED, TC_FROMSTRING, SA_HOR_CENTER);
158 } else if (this->ticker_scroll < TICKER_STOP && FindWindowById(WC_NEWS_WINDOW, 0) == NULL && _statusbar_news_item != NULL && _statusbar_news_item->string_id != 0) {
159 /* Draw the scrolling news text */
160 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)) {
161 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
162 if (Company::IsValidID(_local_company)) {
163 /* This is the default text */
164 SetDParam(0, _local_company);
165 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 }
167 }
168 } else {
169 if (Company::IsValidID(_local_company)) {
170 /* This is the default text */
171 SetDParam(0, _local_company);
172 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);
173 }
174 }
175
176 if (this->reminder_timeout > 0) {
177 Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS);
178 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);
179 }
180 break;
181 }
182 }
183
189 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
190 {
191 if (!gui_scope) return;
192 switch (data) {
193 default: NOT_REACHED();
194 case SBI_SAVELOAD_START: this->saving = true; break;
195 case SBI_SAVELOAD_FINISH: this->saving = false; break;
196 case SBI_SHOW_TICKER: this->ticker_scroll = 0; break;
197 case SBI_SHOW_REMINDER: this->reminder_timeout = REMINDER_START; break;
198 case SBI_NEWS_DELETED:
199 this->ticker_scroll = TICKER_STOP; // reset ticker ...
200 this->reminder_timeout = REMINDER_STOP; // ... and reminder
201 break;
202 }
203 }
204
205 virtual void OnClick(Point pt, int widget, int click_count)
206 {
207 switch (widget) {
208 case WID_S_MIDDLE: ShowLastNewsMessage(); break;
209 case WID_S_RIGHT: if (_local_company != COMPANY_SPECTATOR) ShowCompanyFinances(_local_company); break;
210 default: ResetObjectToPlace();
211 }
212 }
213
214 virtual void OnTick()
215 {
216 if (_pause_mode != PM_UNPAUSED) return;
217
218 if (this->ticker_scroll < TICKER_STOP) { // Scrolling text
219 this->ticker_scroll += COUNTER_STEP;
220 this->SetWidgetDirty(WID_S_MIDDLE);
221 }
222
223 if (this->reminder_timeout > REMINDER_STOP) { // Red blot to show there are new unread newsmessages
224 this->reminder_timeout -= COUNTER_STEP;
225 } else if (this->reminder_timeout < REMINDER_STOP) {
226 this->reminder_timeout = REMINDER_STOP;
227 this->SetWidgetDirty(WID_S_MIDDLE);
228 }
229 }
230 };
231
232 static const NWidgetPart _nested_main_status_widgets[] = {
233 NWidget(NWID_HORIZONTAL),
234 NWidget(WWT_PANEL, COLOUR_GREY, WID_S_LEFT), SetMinimalSize(140, 12), EndContainer(),
235 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_MIDDLE), SetMinimalSize(40, 12), SetDataTip(0x0, STR_STATUSBAR_TOOLTIP_SHOW_LAST_NEWS), SetResize(1, 0),
236 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_RIGHT), SetMinimalSize(140, 12),
237 EndContainer(),
238 };
239
240 static WindowDesc _main_status_desc(
241 WDP_MANUAL, NULL, 640, 12,
242 WC_STATUS_BAR, WC_NONE,
243 WDF_NO_FOCUS,
244 _nested_main_status_widgets, lengthof(_nested_main_status_widgets)
245 );
246
250 bool IsNewsTickerShown()
251 {
252 const StatusBarWindow *w = dynamic_cast<StatusBarWindow*>(FindWindowById(WC_STATUS_BAR, 0));
253 return w != NULL && w->ticker_scroll < StatusBarWindow::TICKER_STOP;
254 }
255
259 void ShowStatusBar()
260 {
261 new StatusBarWindow(&_main_status_desc);
262 }