2009-08-21 21:21:05 +01:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 16:11:33 +01:00
|
|
|
/** @file statusbar_gui.cpp The GUI for the bottom status bar. */
|
2008-04-18 16:13:45 +01:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
2023-01-26 18:21:36 +00:00
|
|
|
#include "core/backup_type.hpp"
|
2008-04-18 16:13:45 +01:00
|
|
|
#include "gfx_func.h"
|
|
|
|
#include "news_func.h"
|
2008-09-30 21:51:04 +01:00
|
|
|
#include "company_func.h"
|
2008-04-18 16:13:45 +01:00
|
|
|
#include "string_func.h"
|
|
|
|
#include "strings_func.h"
|
2008-09-30 21:51:04 +01:00
|
|
|
#include "company_base.h"
|
2008-05-08 14:29:35 +01:00
|
|
|
#include "tilehighlight_func.h"
|
2008-04-18 16:13:45 +01:00
|
|
|
#include "news_gui.h"
|
2008-09-30 21:51:04 +01:00
|
|
|
#include "company_gui.h"
|
2008-04-18 16:13:45 +01:00
|
|
|
#include "window_gui.h"
|
2010-07-19 16:44:49 +01:00
|
|
|
#include "saveload/saveload.h"
|
2008-05-06 23:17:12 +01:00
|
|
|
#include "window_func.h"
|
2008-05-16 08:08:04 +01:00
|
|
|
#include "statusbar_gui.h"
|
2015-02-13 21:13:45 +00:00
|
|
|
#include "toolbar_gui.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/geometry_func.hpp"
|
2019-04-23 20:25:40 +01:00
|
|
|
#include "zoom_func.h"
|
2023-05-04 14:14:12 +01:00
|
|
|
#include "date_type.h"
|
2023-04-13 16:18:27 +01:00
|
|
|
#include "timer/timer.h"
|
2023-04-13 12:56:00 +01:00
|
|
|
#include "timer/timer_game_calendar.h"
|
2023-04-13 16:18:27 +01:00
|
|
|
#include "timer/timer_window.h"
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/statusbar_widget.h"
|
|
|
|
|
2008-04-18 16:13:45 +01:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/sprites.h"
|
|
|
|
|
2014-04-23 21:13:33 +01:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-05-12 21:01:39 +01:00
|
|
|
static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
|
2008-04-18 16:13:45 +01:00
|
|
|
{
|
2008-05-13 11:17:04 +01:00
|
|
|
CopyInDParam(0, ni->params, lengthof(ni->params));
|
|
|
|
StringID str = ni->string_id;
|
2008-04-18 16:13:45 +01:00
|
|
|
|
|
|
|
char buf[512];
|
|
|
|
GetString(buf, str, lastof(buf));
|
|
|
|
const char *s = buf;
|
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
char *d = buffer;
|
|
|
|
const char *last = lastof(buffer);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
WChar c = Utf8Consume(&s);
|
|
|
|
if (c == 0) {
|
|
|
|
break;
|
2009-03-02 21:01:19 +00:00
|
|
|
} else if (c == '\n') {
|
2008-04-18 16:13:45 +01:00
|
|
|
if (d + 4 >= last) break;
|
|
|
|
d[0] = d[1] = d[2] = d[3] = ' ';
|
|
|
|
d += 4;
|
|
|
|
} else if (IsPrintable(c)) {
|
|
|
|
if (d + Utf8CharLen(c) >= last) break;
|
|
|
|
d += Utf8Encode(d, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
|
|
|
|
DrawPixelInfo tmp_dpi;
|
2009-05-12 21:01:39 +01:00
|
|
|
if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2009-05-12 23:28:18 +01:00
|
|
|
int width = GetStringBoundingBox(buffer).width;
|
2010-11-13 09:56:25 +00:00
|
|
|
int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
|
2009-05-12 23:28:18 +01:00
|
|
|
|
2023-01-26 18:21:36 +00:00
|
|
|
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
|
2009-05-12 23:28:18 +01:00
|
|
|
DrawString(pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2010-11-13 09:56:25 +00:00
|
|
|
return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
|
2008-04-18 16:13:45 +01:00
|
|
|
}
|
|
|
|
|
2008-05-16 08:24:21 +01:00
|
|
|
struct StatusBarWindow : Window {
|
|
|
|
bool saving;
|
|
|
|
int ticker_scroll;
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2010-05-13 11:14:29 +01:00
|
|
|
static const int TICKER_STOP = 1640; ///< scrolling is finished when counter reaches this value
|
|
|
|
static const int COUNTER_STEP = 2; ///< this is subtracted from active counters every tick
|
2023-04-13 16:18:27 +01:00
|
|
|
static constexpr auto REMINDER_START = std::chrono::milliseconds(1350); ///< time in ms for reminder notification (red dot on the right) to stay
|
2009-01-09 23:49:46 +00:00
|
|
|
|
2013-05-26 20:23:42 +01:00
|
|
|
StatusBarWindow(WindowDesc *desc) : Window(desc)
|
2008-05-16 08:24:21 +01:00
|
|
|
{
|
2018-05-20 09:58:36 +01:00
|
|
|
this->ticker_scroll = TICKER_STOP;
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2013-05-26 20:23:42 +01:00
|
|
|
this->InitNested();
|
2011-12-15 19:54:23 +00:00
|
|
|
CLRBITS(this->flags, WF_WHITE_BORDER);
|
2010-11-13 23:40:36 +00:00
|
|
|
PositionStatusbar(this);
|
2008-05-16 08:24:21 +01:00
|
|
|
}
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
|
2009-11-02 10:15:48 +00:00
|
|
|
{
|
2010-11-13 23:40:36 +00:00
|
|
|
Point pt = { 0, _screen.height - sm_height };
|
2009-11-02 10:15:48 +00:00
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void FindWindowPlacementAndResize(int def_width, int def_height) override
|
2015-02-13 21:13:45 +00:00
|
|
|
{
|
|
|
|
Window::FindWindowPlacementAndResize(_toolbar_width, def_height);
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
|
2009-11-02 10:15:48 +00:00
|
|
|
{
|
2009-11-23 20:25:14 +00:00
|
|
|
Dimension d;
|
|
|
|
switch (widget) {
|
2011-12-16 18:21:13 +00:00
|
|
|
case WID_S_LEFT:
|
2012-12-08 17:18:51 +00:00
|
|
|
SetDParamMaxValue(0, MAX_YEAR * DAYS_IN_YEAR);
|
2023-04-25 09:57:55 +01:00
|
|
|
d = GetStringBoundingBox(STR_JUST_DATE_LONG);
|
2009-11-23 20:25:14 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-16 18:21:13 +00:00
|
|
|
case WID_S_RIGHT: {
|
2009-11-23 20:25:14 +00:00
|
|
|
int64 max_money = UINT32_MAX;
|
2021-01-08 10:16:18 +00:00
|
|
|
for (const Company *c : Company::Iterate()) max_money = std::max<int64>(c->money, max_money);
|
2009-11-23 20:25:14 +00:00
|
|
|
SetDParam(0, 100LL * max_money);
|
2023-04-25 09:51:19 +01:00
|
|
|
d = GetStringBoundingBox(STR_JUST_CURRENCY_LONG);
|
2010-08-01 19:53:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-11-23 20:25:14 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d.width += padding.width;
|
|
|
|
d.height += padding.height;
|
|
|
|
*size = maxdim(d, *size);
|
2009-11-02 10:15:48 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void DrawWidget(const Rect &r, int widget) const override
|
2009-10-04 16:38:16 +01:00
|
|
|
{
|
2022-09-23 09:36:22 +01:00
|
|
|
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect, RectPadding::zero);
|
2022-10-15 16:55:47 +01:00
|
|
|
tr.top = CenterBounds(r.top, r.bottom, FONT_HEIGHT_NORMAL);
|
2009-10-04 16:38:16 +01:00
|
|
|
switch (widget) {
|
2011-12-16 18:21:13 +00:00
|
|
|
case WID_S_LEFT:
|
2009-10-04 16:38:16 +01:00
|
|
|
/* Draw the date */
|
2023-04-24 16:56:01 +01:00
|
|
|
SetDParam(0, TimerGameCalendar::date);
|
2023-04-25 09:57:55 +01:00
|
|
|
DrawString(tr, STR_JUST_DATE_LONG, TC_WHITE, SA_HOR_CENTER);
|
2009-10-04 16:38:16 +01:00
|
|
|
break;
|
2008-05-16 08:24:21 +01:00
|
|
|
|
2011-12-16 18:21:13 +00:00
|
|
|
case WID_S_RIGHT: {
|
2021-08-14 09:19:32 +01:00
|
|
|
if (_local_company == COMPANY_SPECTATOR) {
|
2022-10-15 16:55:47 +01:00
|
|
|
DrawString(tr, STR_STATUSBAR_SPECTATOR, TC_FROMSTRING, SA_HOR_CENTER);
|
2021-08-14 09:19:32 +01:00
|
|
|
} else {
|
|
|
|
/* Draw company money, if any */
|
|
|
|
const Company *c = Company::GetIfValid(_local_company);
|
|
|
|
if (c != nullptr) {
|
|
|
|
SetDParam(0, c->money);
|
2023-04-25 09:51:19 +01:00
|
|
|
DrawString(tr, STR_JUST_CURRENCY_LONG, TC_WHITE, SA_HOR_CENTER);
|
2021-08-14 09:19:32 +01:00
|
|
|
}
|
2009-10-04 16:38:16 +01:00
|
|
|
}
|
2010-08-01 19:53:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-10-04 16:38:16 +01:00
|
|
|
|
2011-12-16 18:21:13 +00:00
|
|
|
case WID_S_MIDDLE:
|
2009-10-04 16:38:16 +01:00
|
|
|
/* Draw status bar */
|
|
|
|
if (this->saving) { // true when saving is active
|
2022-10-15 16:55:47 +01:00
|
|
|
DrawString(tr, STR_STATUSBAR_SAVING_GAME, TC_FROMSTRING, SA_HOR_CENTER | SA_VERT_CENTER);
|
2009-10-04 16:38:16 +01:00
|
|
|
} else if (_do_autosave) {
|
2022-10-15 16:55:47 +01:00
|
|
|
DrawString(tr, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_HOR_CENTER);
|
2009-10-04 16:38:16 +01:00
|
|
|
} else if (_pause_mode != PM_UNPAUSED) {
|
2019-01-22 18:31:08 +00:00
|
|
|
StringID msg = (_pause_mode & PM_PAUSED_LINK_GRAPH) ? STR_STATUSBAR_PAUSED_LINK_GRAPH : STR_STATUSBAR_PAUSED;
|
2022-10-15 16:55:47 +01:00
|
|
|
DrawString(tr, msg, TC_FROMSTRING, SA_HOR_CENTER);
|
2019-05-29 23:11:20 +01:00
|
|
|
} else if (this->ticker_scroll < TICKER_STOP && _statusbar_news_item != nullptr && _statusbar_news_item->string_id != 0) {
|
2009-10-04 16:38:16 +01:00
|
|
|
/* Draw the scrolling news text */
|
2022-09-23 09:36:22 +01:00
|
|
|
if (!DrawScrollingStatusText(_statusbar_news_item, ScaleGUITrad(this->ticker_scroll), tr.left, tr.right, tr.top, tr.bottom)) {
|
2009-10-04 16:38:16 +01:00
|
|
|
InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
|
|
|
|
if (Company::IsValidID(_local_company)) {
|
|
|
|
/* This is the default text */
|
|
|
|
SetDParam(0, _local_company);
|
2022-10-15 16:55:47 +01:00
|
|
|
DrawString(tr, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_HOR_CENTER);
|
2009-10-04 16:38:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Company::IsValidID(_local_company)) {
|
|
|
|
/* This is the default text */
|
|
|
|
SetDParam(0, _local_company);
|
2022-10-15 16:55:47 +01:00
|
|
|
DrawString(tr, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_HOR_CENTER);
|
2009-10-04 16:38:16 +01:00
|
|
|
}
|
2008-04-18 16:13:45 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 16:18:27 +01:00
|
|
|
if (!this->reminder_timeout.HasFired()) {
|
2010-09-02 21:00:48 +01:00
|
|
|
Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS);
|
2022-10-15 16:55:47 +01:00
|
|
|
DrawSprite(SPR_UNREAD_NEWS, PAL_NONE, tr.right - icon_size.width, CenterBounds(r.top, r.bottom, icon_size.height));
|
2010-09-02 20:34:44 +01:00
|
|
|
}
|
2009-10-04 16:38:16 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-05-16 08:24:21 +01:00
|
|
|
}
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2011-03-13 21:31:29 +00:00
|
|
|
/**
|
|
|
|
* Some data on this window has become invalid.
|
|
|
|
* @param data Information about the changed data.
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnInvalidateData(int data = 0, bool gui_scope = true) override
|
2008-05-16 08:24:21 +01:00
|
|
|
{
|
2011-03-13 21:31:29 +00:00
|
|
|
if (!gui_scope) return;
|
2008-05-16 08:24:21 +01:00
|
|
|
switch (data) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case SBI_SAVELOAD_START: this->saving = true; break;
|
|
|
|
case SBI_SAVELOAD_FINISH: this->saving = false; break;
|
2009-05-12 23:28:18 +01:00
|
|
|
case SBI_SHOW_TICKER: this->ticker_scroll = 0; break;
|
2023-04-13 16:18:27 +01:00
|
|
|
case SBI_SHOW_REMINDER: this->reminder_timeout.Reset(); break;
|
2009-01-09 20:42:17 +00:00
|
|
|
case SBI_NEWS_DELETED:
|
2009-01-09 23:49:46 +00:00
|
|
|
this->ticker_scroll = TICKER_STOP; // reset ticker ...
|
2023-04-13 16:18:27 +01:00
|
|
|
this->reminder_timeout.Abort(); // ... and reminder
|
2009-01-09 20:42:17 +00:00
|
|
|
break;
|
2008-05-16 08:24:21 +01:00
|
|
|
}
|
|
|
|
}
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnClick(Point pt, int widget, int click_count) override
|
2008-05-16 08:24:21 +01:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 18:21:13 +00:00
|
|
|
case WID_S_MIDDLE: ShowLastNewsMessage(); break;
|
|
|
|
case WID_S_RIGHT: if (_local_company != COMPANY_SPECTATOR) ShowCompanyFinances(_local_company); break;
|
2008-05-16 08:24:21 +01:00
|
|
|
default: ResetObjectToPlace();
|
|
|
|
}
|
|
|
|
}
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2023-04-13 16:18:27 +01:00
|
|
|
/** Move information on the ticker slowly from one side to the other. */
|
|
|
|
IntervalTimer<TimerWindow> ticker_scroll_interval = {std::chrono::milliseconds(15), [this](uint count) {
|
2009-05-06 16:06:57 +01:00
|
|
|
if (_pause_mode != PM_UNPAUSED) return;
|
2008-05-16 08:24:21 +01:00
|
|
|
|
2023-04-13 16:18:27 +01:00
|
|
|
if (this->ticker_scroll < TICKER_STOP) {
|
|
|
|
this->ticker_scroll += count;
|
2011-12-16 18:21:13 +00:00
|
|
|
this->SetWidgetDirty(WID_S_MIDDLE);
|
2008-05-16 08:24:21 +01:00
|
|
|
}
|
2023-04-13 16:18:27 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
TimeoutTimer<TimerWindow> reminder_timeout = {REMINDER_START, [this]() {
|
|
|
|
this->SetWidgetDirty(WID_S_MIDDLE);
|
|
|
|
}};
|
2023-04-13 12:56:00 +01:00
|
|
|
|
|
|
|
IntervalTimer<TimerGameCalendar> daily_interval = {{TimerGameCalendar::DAY, TimerGameCalendar::Priority::NONE}, [this](auto) {
|
|
|
|
this->SetWidgetDirty(WID_S_LEFT);
|
|
|
|
}};
|
2008-05-16 08:24:21 +01:00
|
|
|
};
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2009-04-03 13:39:52 +01:00
|
|
|
static const NWidgetPart _nested_main_status_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 18:21:13 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_S_LEFT), SetMinimalSize(140, 12), EndContainer(),
|
|
|
|
NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_MIDDLE), SetMinimalSize(40, 12), SetDataTip(0x0, STR_STATUSBAR_TOOLTIP_SHOW_LAST_NEWS), SetResize(1, 0),
|
|
|
|
NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_RIGHT), SetMinimalSize(140, 12),
|
2009-04-03 13:39:52 +01:00
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2010-11-13 23:40:36 +00:00
|
|
|
static WindowDesc _main_status_desc(
|
2019-04-10 22:07:06 +01:00
|
|
|
WDP_MANUAL, nullptr, 0, 0,
|
2008-04-18 16:13:45 +01:00
|
|
|
WC_STATUS_BAR, WC_NONE,
|
2012-11-11 16:10:43 +00:00
|
|
|
WDF_NO_FOCUS,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_main_status_widgets, lengthof(_nested_main_status_widgets)
|
2009-03-15 15:12:06 +00:00
|
|
|
);
|
2008-04-18 16:13:45 +01:00
|
|
|
|
2008-05-16 08:08:04 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the news ticker is currently being used.
|
|
|
|
*/
|
|
|
|
bool IsNewsTickerShown()
|
|
|
|
{
|
2008-05-16 08:24:21 +01:00
|
|
|
const StatusBarWindow *w = dynamic_cast<StatusBarWindow*>(FindWindowById(WC_STATUS_BAR, 0));
|
2019-04-10 22:07:06 +01:00
|
|
|
return w != nullptr && w->ticker_scroll < StatusBarWindow::TICKER_STOP;
|
2008-05-16 08:08:04 +01:00
|
|
|
}
|
|
|
|
|
2011-05-01 20:51:52 +01:00
|
|
|
/**
|
|
|
|
* Show our status bar.
|
|
|
|
*/
|
2008-04-18 16:13:45 +01:00
|
|
|
void ShowStatusBar()
|
|
|
|
{
|
2008-05-16 08:24:21 +01:00
|
|
|
new StatusBarWindow(&_main_status_desc);
|
2008-04-18 16:13:45 +01:00
|
|
|
}
|