2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file statusbar_gui.cpp The GUI for the bottom status bar. */
11 #include "date_func.h"
13 #include "news_func.h"
14 #include "company_func.h"
15 #include "string_func.h"
16 #include "strings_func.h"
17 #include "company_base.h"
18 #include "tilehighlight_func.h"
20 #include "company_gui.h"
21 #include "window_gui.h"
22 #include "saveload/saveload.h"
23 #include "window_func.h"
24 #include "statusbar_gui.h"
25 #include "toolbar_gui.h"
26 #include "core/geometry_func.hpp"
27 #include "guitimer_func.h"
28 #include "zoom_func.h"
30 #include "widgets/statusbar_widget.h"
32 #include "table/strings.h"
33 #include "table/sprites.h"
35 #include "safeguards.h"
37 static bool DrawScrollingStatusText(const NewsItem
*ni
, int scroll_pos
, int left
, int right
, int top
, int bottom
)
39 CopyInDParam(0, ni
->params
, lengthof(ni
->params
));
40 StringID str
= ni
->string_id
;
43 GetString(buf
, str
, lastof(buf
));
48 const char *last
= lastof(buffer
);
51 WChar c
= Utf8Consume(&s
);
54 } else if (c
== '\n') {
55 if (d
+ 4 >= last
) break;
56 d
[0] = d
[1] = d
[2] = d
[3] = ' ';
58 } else if (IsPrintable(c
)) {
59 if (d
+ Utf8CharLen(c
) >= last
) break;
60 d
+= Utf8Encode(d
, c
);
65 DrawPixelInfo tmp_dpi
;
66 if (!FillDrawPixelInfo(&tmp_dpi
, left
, top
, right
- left
, bottom
)) return true;
68 int width
= GetStringBoundingBox(buffer
).width
;
69 int pos
= (_current_text_dir
== TD_RTL
) ? (scroll_pos
- width
) : (right
- scroll_pos
- left
);
71 DrawPixelInfo
*old_dpi
= _cur_dpi
;
73 DrawString(pos
, INT16_MAX
, 0, buffer
, TC_LIGHT_BLUE
, SA_LEFT
| SA_FORCE
);
76 return (_current_text_dir
== TD_RTL
) ? (pos
< right
- left
) : (pos
+ width
> 0);
79 struct StatusBarWindow
: Window
{
82 GUITimer ticker_timer
;
83 GUITimer reminder_timeout
;
85 static const int TICKER_STOP
= 1640; ///< scrolling is finished when counter reaches this value
86 static const int REMINDER_START
= 1350; ///< time in ms for reminder notification (red dot on the right) to stay
87 static const int REMINDER_STOP
= 0; ///< reminder disappears when counter reaches this value
88 static const int COUNTER_STEP
= 2; ///< this is subtracted from active counters every tick
90 StatusBarWindow(WindowDesc
*desc
) : Window(desc
)
92 this->ticker_scroll
= TICKER_STOP
;
93 this->ticker_timer
.SetInterval(15);
94 this->reminder_timeout
.SetInterval(REMINDER_STOP
);
97 CLRBITS(this->flags
, WF_WHITE_BORDER
);
98 PositionStatusbar(this);
101 Point
OnInitialPosition(int16 sm_width
, int16 sm_height
, int window_number
) override
103 Point pt
= { 0, _screen
.height
- sm_height
};
107 void FindWindowPlacementAndResize(int def_width
, int def_height
) override
109 Window::FindWindowPlacementAndResize(_toolbar_width
, def_height
);
112 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
117 SetDParamMaxValue(0, MAX_YEAR
* DAYS_IN_YEAR
);
118 d
= GetStringBoundingBox(STR_WHITE_DATE_LONG
);
122 int64 max_money
= UINT32_MAX
;
123 for (const Company
*c
: Company::Iterate()) max_money
= std::max
<int64
>(c
->money
, max_money
);
124 SetDParam(0, 100LL * max_money
);
125 d
= GetStringBoundingBox(STR_COMPANY_MONEY
);
133 d
.width
+= padding
.width
;
134 d
.height
+= padding
.height
;
135 *size
= maxdim(d
, *size
);
138 void DrawWidget(const Rect
&r
, int widget
) const override
140 int text_offset
= std::max(0, ((int)(r
.bottom
- r
.top
+ 1) - FONT_HEIGHT_NORMAL
) / 2); // Offset for rendering the text vertically centered
141 int text_top
= r
.top
+ text_offset
;
146 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, text_top
, STR_WHITE_DATE_LONG
, TC_FROMSTRING
, SA_HOR_CENTER
);
150 if (_local_company
== COMPANY_SPECTATOR
) {
151 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, text_top
, STR_STATUSBAR_SPECTATOR
, TC_FROMSTRING
, SA_HOR_CENTER
);
153 /* Draw company money, if any */
154 const Company
*c
= Company::GetIfValid(_local_company
);
156 SetDParam(0, c
->money
);
157 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, text_top
, STR_COMPANY_MONEY
, TC_FROMSTRING
, SA_HOR_CENTER
);
164 /* Draw status bar */
165 if (this->saving
) { // true when saving is active
166 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, text_top
, STR_STATUSBAR_SAVING_GAME
, TC_FROMSTRING
, SA_HOR_CENTER
| SA_VERT_CENTER
);
167 } else if (_do_autosave
) {
168 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, text_top
, STR_STATUSBAR_AUTOSAVE
, TC_FROMSTRING
, SA_HOR_CENTER
);
169 } else if (_pause_mode
!= PM_UNPAUSED
) {
170 StringID msg
= (_pause_mode
& PM_PAUSED_LINK_GRAPH
) ? STR_STATUSBAR_PAUSED_LINK_GRAPH
: STR_STATUSBAR_PAUSED
;
171 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, text_top
, msg
, TC_FROMSTRING
, SA_HOR_CENTER
);
172 } else if (this->ticker_scroll
< TICKER_STOP
&& _statusbar_news_item
!= nullptr && _statusbar_news_item
->string_id
!= 0) {
173 /* Draw the scrolling news text */
174 if (!DrawScrollingStatusText(_statusbar_news_item
, ScaleGUITrad(this->ticker_scroll
), r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
, r
.bottom
)) {
175 InvalidateWindowData(WC_STATUS_BAR
, 0, SBI_NEWS_DELETED
);
176 if (Company::IsValidID(_local_company
)) {
177 /* This is the default text */
178 SetDParam(0, _local_company
);
179 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, text_top
, STR_STATUSBAR_COMPANY_NAME
, TC_FROMSTRING
, SA_HOR_CENTER
);
183 if (Company::IsValidID(_local_company
)) {
184 /* This is the default text */
185 SetDParam(0, _local_company
);
186 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, text_top
, STR_STATUSBAR_COMPANY_NAME
, TC_FROMSTRING
, SA_HOR_CENTER
);
190 if (!this->reminder_timeout
.HasElapsed()) {
191 Dimension icon_size
= GetSpriteSize(SPR_UNREAD_NEWS
);
192 DrawSprite(SPR_UNREAD_NEWS
, PAL_NONE
, r
.right
- WD_FRAMERECT_RIGHT
- icon_size
.width
, r
.top
+ std::max(0, ((int)(r
.bottom
- r
.top
+ 1) - (int)icon_size
.height
) / 2));
199 * Some data on this window has become invalid.
200 * @param data Information about the changed data.
201 * @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.
203 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
205 if (!gui_scope
) return;
207 default: NOT_REACHED();
208 case SBI_SAVELOAD_START
: this->saving
= true; break;
209 case SBI_SAVELOAD_FINISH
: this->saving
= false; break;
210 case SBI_SHOW_TICKER
: this->ticker_scroll
= 0; break;
211 case SBI_SHOW_REMINDER
: this->reminder_timeout
.SetInterval(REMINDER_START
); break;
212 case SBI_NEWS_DELETED
:
213 this->ticker_scroll
= TICKER_STOP
; // reset ticker ...
214 this->reminder_timeout
.SetInterval(REMINDER_STOP
); // ... and reminder
219 void OnClick(Point pt
, int widget
, int click_count
) override
222 case WID_S_MIDDLE
: ShowLastNewsMessage(); break;
223 case WID_S_RIGHT
: if (_local_company
!= COMPANY_SPECTATOR
) ShowCompanyFinances(_local_company
); break;
224 default: ResetObjectToPlace();
228 void OnRealtimeTick(uint delta_ms
) override
230 if (_pause_mode
!= PM_UNPAUSED
) return;
232 if (this->ticker_scroll
< TICKER_STOP
) { // Scrolling text
233 uint count
= this->ticker_timer
.CountElapsed(delta_ms
);
235 this->ticker_scroll
+= count
;
236 this->SetWidgetDirty(WID_S_MIDDLE
);
240 // Red blot to show there are new unread newsmessages
241 if (this->reminder_timeout
.Elapsed(delta_ms
)) {
242 this->SetWidgetDirty(WID_S_MIDDLE
);
247 static const NWidgetPart _nested_main_status_widgets
[] = {
248 NWidget(NWID_HORIZONTAL
),
249 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_S_LEFT
), SetMinimalSize(140, 12), EndContainer(),
250 NWidget(WWT_PUSHBTN
, COLOUR_GREY
, WID_S_MIDDLE
), SetMinimalSize(40, 12), SetDataTip(0x0, STR_STATUSBAR_TOOLTIP_SHOW_LAST_NEWS
), SetResize(1, 0),
251 NWidget(WWT_PUSHBTN
, COLOUR_GREY
, WID_S_RIGHT
), SetMinimalSize(140, 12),
255 static WindowDesc
_main_status_desc(
256 WDP_MANUAL
, nullptr, 0, 0,
257 WC_STATUS_BAR
, WC_NONE
,
259 _nested_main_status_widgets
, lengthof(_nested_main_status_widgets
)
263 * Checks whether the news ticker is currently being used.
265 bool IsNewsTickerShown()
267 const StatusBarWindow
*w
= dynamic_cast<StatusBarWindow
*>(FindWindowById(WC_STATUS_BAR
, 0));
268 return w
!= nullptr && w
->ticker_scroll
< StatusBarWindow::TICKER_STOP
;
272 * Show our status bar.
276 new StatusBarWindow(&_main_status_desc
);