Files
@ r28520:f9aebe299cae
Branch filter:
Location: cpp/openttd-patchpack/source/src/statusbar_gui.cpp - annotation
r28520:f9aebe299cae
8.9 KiB
text/x-c
Codechange: MacOS already has MIN/MAX macros defined
This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r8976:8660ab19d0e6 r8976:8660ab19d0e6 r26823:cb65f12c003b r8976:8660ab19d0e6 r8976:8660ab19d0e6 r10208:ef8fcc3dc4ca r8976:8660ab19d0e6 r8976:8660ab19d0e6 r10208:ef8fcc3dc4ca r9148:4c9b8a45d519 r8976:8660ab19d0e6 r10208:ef8fcc3dc4ca r8976:8660ab19d0e6 r15514:7e3c6c9e07c2 r9117:e2b5a638a7c1 r9248:afa00db99401 r21990:cedbcf015b65 r14248:a9050881acd7 r23647:2fa88e4b691a r27089:0c098eaacf35 r27090:1ea30951c0f3 r27089:0c098eaacf35 r8976:8660ab19d0e6 r18670:f122c356353c r18670:f122c356353c r8976:8660ab19d0e6 r8976:8660ab19d0e6 r8976:8660ab19d0e6 r21383:942c32fb8b0e r21383:942c32fb8b0e r11879:4ecbaf7e5a53 r8976:8660ab19d0e6 r27680:4da89a3b46b7 r8976:8660ab19d0e6 r27411:18fb446ea1a8 r27411:18fb446ea1a8 r8976:8660ab19d0e6 r8976:8660ab19d0e6 r11879:4ecbaf7e5a53 r8976:8660ab19d0e6 r27411:18fb446ea1a8 r16431:ec558deca9d7 r11881:e67abbaf98b1 r26823:cb65f12c003b r27411:18fb446ea1a8 r8976:8660ab19d0e6 r16431:ec558deca9d7 r8976:8660ab19d0e6 r8976:8660ab19d0e6 r9250:54df676211ea r9250:54df676211ea r9250:54df676211ea r8976:8660ab19d0e6 r15175:66e0817dc450 r15175:66e0817dc450 r27089:0c098eaacf35 r10646:b3b2c1ecae0d r20280:ca1fc41725ff r9250:54df676211ea r23128:57a1ceae9616 r8976:8660ab19d0e6 r20280:ca1fc41725ff r18667:3b72643a9c00 r16453:a6fa65e29fdb r9250:54df676211ea r9250:54df676211ea r27942:f7389062d120 r13428:b65c72efe5d8 r16453:a6fa65e29fdb r13428:b65c72efe5d8 r13428:b65c72efe5d8 r13428:b65c72efe5d8 r27942:f7389062d120 r21990:cedbcf015b65 r21990:cedbcf015b65 r21990:cedbcf015b65 r21990:cedbcf015b65 r28353:bfc4ab63f376 r13428:b65c72efe5d8 r13727:9069b5049dbd r13727:9069b5049dbd r18689:162d0eb61e95 r27884:803962be0328 r27243:999e213784ca r13727:9069b5049dbd r13727:9069b5049dbd r18689:162d0eb61e95 r27737:728d55b97775 r27737:728d55b97775 r13727:9069b5049dbd r27242:3ff2aff79b0e r15608:7b580ec7448a r15608:7b580ec7448a r13727:9069b5049dbd r13727:9069b5049dbd r13727:9069b5049dbd r13727:9069b5049dbd r13727:9069b5049dbd r13727:9069b5049dbd r13727:9069b5049dbd r13727:9069b5049dbd r13428:b65c72efe5d8 r13428:b65c72efe5d8 r28353:bfc4ab63f376 r13174:19a8d87f3e31 r26575:d5725bf82346 r28167:b14ea36d49e5 r13174:19a8d87f3e31 r18689:162d0eb61e95 r13174:19a8d87f3e31 r27148:4e041ae27b9d r27243:999e213784ca r13174:19a8d87f3e31 r13174:19a8d87f3e31 r18689:162d0eb61e95 r25880:b201cd186329 r26549:82dacf005b0a r25880:b201cd186329 r25880:b201cd186329 r25880:b201cd186329 r25880:b201cd186329 r25880:b201cd186329 r27242:3ff2aff79b0e r25880:b201cd186329 r13174:19a8d87f3e31 r15608:7b580ec7448a r15608:7b580ec7448a r9250:54df676211ea r18689:162d0eb61e95 r13174:19a8d87f3e31 r13174:19a8d87f3e31 r26549:82dacf005b0a r13174:19a8d87f3e31 r26549:82dacf005b0a r13174:19a8d87f3e31 r24483:c8962d3f47a6 r26549:82dacf005b0a r23791:267042c65018 r13174:19a8d87f3e31 r26575:d5725bf82346 r13174:19a8d87f3e31 r13174:19a8d87f3e31 r13174:19a8d87f3e31 r13174:19a8d87f3e31 r26549:82dacf005b0a r13174:19a8d87f3e31 r13174:19a8d87f3e31 r13174:19a8d87f3e31 r13174:19a8d87f3e31 r13174:19a8d87f3e31 r13174:19a8d87f3e31 r26549:82dacf005b0a r13174:19a8d87f3e31 r8976:8660ab19d0e6 r13174:19a8d87f3e31 r27089:0c098eaacf35 r16017:2291ca54c6cd r26549:82dacf005b0a r16016:ab5d2771109c r13174:19a8d87f3e31 r9250:54df676211ea r9250:54df676211ea r8976:8660ab19d0e6 r17476:d3b7a183536d r17476:d3b7a183536d r17476:d3b7a183536d r17476:d3b7a183536d r17476:d3b7a183536d r27942:f7389062d120 r9250:54df676211ea r17476:d3b7a183536d r9250:54df676211ea r9250:54df676211ea r9250:54df676211ea r9250:54df676211ea r11881:e67abbaf98b1 r27089:0c098eaacf35 r10640:67fddb68ce12 r10646:b3b2c1ecae0d r27089:0c098eaacf35 r10640:67fddb68ce12 r9250:54df676211ea r9250:54df676211ea r8976:8660ab19d0e6 r28353:bfc4ab63f376 r9250:54df676211ea r9250:54df676211ea r18689:162d0eb61e95 r18689:162d0eb61e95 r9250:54df676211ea r9250:54df676211ea r9250:54df676211ea r9250:54df676211ea r27089:0c098eaacf35 r27089:0c098eaacf35 r11847:235c011992a8 r8976:8660ab19d0e6 r27089:0c098eaacf35 r27089:0c098eaacf35 r18689:162d0eb61e95 r9250:54df676211ea r27089:0c098eaacf35 r27089:0c098eaacf35 r27089:0c098eaacf35 r27089:0c098eaacf35 r27089:0c098eaacf35 r27090:1ea30951c0f3 r27090:1ea30951c0f3 r27090:1ea30951c0f3 r27090:1ea30951c0f3 r9250:54df676211ea r8976:8660ab19d0e6 r28487:2130fff7270c r11558:a3c1f30208d3 r18689:162d0eb61e95 r18689:162d0eb61e95 r18689:162d0eb61e95 r11558:a3c1f30208d3 r11558:a3c1f30208d3 r11558:a3c1f30208d3 r28078:5e5d6c0447b1 r23607:36c15679007d r8976:8660ab19d0e6 r27580:953c5017a71d r27861:6269475166e0 r11368:058349c3a02c r8976:8660ab19d0e6 r9248:afa00db99401 r9248:afa00db99401 r9248:afa00db99401 r9248:afa00db99401 r9248:afa00db99401 r9250:54df676211ea r23607:36c15679007d r9248:afa00db99401 r9248:afa00db99401 r17625:366001a31c88 r17625:366001a31c88 r17625:366001a31c88 r8976:8660ab19d0e6 r8976:8660ab19d0e6 r9250:54df676211ea r8976:8660ab19d0e6 | /*
* 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/>.
*/
/** @file statusbar_gui.cpp The GUI for the bottom status bar. */
#include "stdafx.h"
#include "core/backup_type.hpp"
#include "gfx_func.h"
#include "news_func.h"
#include "company_func.h"
#include "string_func.h"
#include "strings_func.h"
#include "company_base.h"
#include "tilehighlight_func.h"
#include "news_gui.h"
#include "company_gui.h"
#include "window_gui.h"
#include "saveload/saveload.h"
#include "window_func.h"
#include "statusbar_gui.h"
#include "toolbar_gui.h"
#include "core/geometry_func.hpp"
#include "zoom_func.h"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_window.h"
#include "widgets/statusbar_widget.h"
#include "table/strings.h"
#include "table/sprites.h"
#include "safeguards.h"
static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
{
CopyInDParam(ni->params);
/* Replace newlines and the likes with spaces. */
std::string message = StrMakeValid(GetString(ni->string_id), SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
DrawPixelInfo tmp_dpi;
if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
int width = GetStringBoundingBox(message).width;
int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
DrawString(pos, INT16_MAX, 0, message, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
}
struct StatusBarWindow : Window {
bool saving;
int ticker_scroll;
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
static constexpr auto REMINDER_START = std::chrono::milliseconds(1350); ///< time in ms for reminder notification (red dot on the right) to stay
StatusBarWindow(WindowDesc *desc) : Window(desc)
{
this->ticker_scroll = TICKER_STOP;
this->InitNested();
CLRBITS(this->flags, WF_WHITE_BORDER);
PositionStatusbar(this);
}
Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
{
Point pt = { 0, _screen.height - sm_height };
return pt;
}
void FindWindowPlacementAndResize([[maybe_unused]] int def_width, [[maybe_unused]] int def_height) override
{
Window::FindWindowPlacementAndResize(_toolbar_width, def_height);
}
void UpdateWidgetSize(WidgetID widget, Dimension *size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension *fill, [[maybe_unused]] Dimension *resize) override
{
Dimension d;
switch (widget) {
case WID_S_LEFT:
SetDParamMaxValue(0, TimerGameCalendar::DateAtStartOfYear(CalendarTime::MAX_YEAR));
d = GetStringBoundingBox(STR_JUST_DATE_LONG);
break;
case WID_S_RIGHT: {
int64_t max_money = UINT32_MAX;
for (const Company *c : Company::Iterate()) max_money = std::max<int64_t>(c->money, max_money);
SetDParam(0, 100LL * max_money);
d = GetStringBoundingBox(STR_JUST_CURRENCY_LONG);
break;
}
default:
return;
}
d.width += padding.width;
d.height += padding.height;
*size = maxdim(d, *size);
}
void DrawWidget(const Rect &r, WidgetID widget) const override
{
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect, RectPadding::zero);
tr.top = CenterBounds(r.top, r.bottom, GetCharacterHeight(FS_NORMAL));
switch (widget) {
case WID_S_LEFT:
/* Draw the date */
SetDParam(0, TimerGameCalendar::date);
DrawString(tr, STR_JUST_DATE_LONG, TC_WHITE, SA_HOR_CENTER);
break;
case WID_S_RIGHT: {
if (_local_company == COMPANY_SPECTATOR) {
DrawString(tr, STR_STATUSBAR_SPECTATOR, TC_FROMSTRING, SA_HOR_CENTER);
} else {
/* Draw company money, if any */
const Company *c = Company::GetIfValid(_local_company);
if (c != nullptr) {
SetDParam(0, c->money);
DrawString(tr, STR_JUST_CURRENCY_LONG, TC_WHITE, SA_HOR_CENTER);
}
}
break;
}
case WID_S_MIDDLE:
/* Draw status bar */
if (this->saving) { // true when saving is active
DrawString(tr, STR_STATUSBAR_SAVING_GAME, TC_FROMSTRING, SA_HOR_CENTER | SA_VERT_CENTER);
} else if (_do_autosave) {
DrawString(tr, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_HOR_CENTER);
} else if (_pause_mode != PM_UNPAUSED) {
StringID msg = (_pause_mode & PM_PAUSED_LINK_GRAPH) ? STR_STATUSBAR_PAUSED_LINK_GRAPH : STR_STATUSBAR_PAUSED;
DrawString(tr, msg, TC_FROMSTRING, SA_HOR_CENTER);
} else if (this->ticker_scroll < TICKER_STOP && _statusbar_news_item != nullptr && _statusbar_news_item->string_id != 0) {
/* Draw the scrolling news text */
if (!DrawScrollingStatusText(_statusbar_news_item, ScaleGUITrad(this->ticker_scroll), tr.left, tr.right, tr.top, tr.bottom)) {
InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
if (Company::IsValidID(_local_company)) {
/* This is the default text */
SetDParam(0, _local_company);
DrawString(tr, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_HOR_CENTER);
}
}
} else {
if (Company::IsValidID(_local_company)) {
/* This is the default text */
SetDParam(0, _local_company);
DrawString(tr, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_HOR_CENTER);
}
}
if (!this->reminder_timeout.HasFired()) {
Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS);
DrawSprite(SPR_UNREAD_NEWS, PAL_NONE, tr.right - icon_size.width, CenterBounds(r.top, r.bottom, icon_size.height));
}
break;
}
}
/**
* 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.
*/
void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
{
if (!gui_scope) return;
switch (data) {
default: NOT_REACHED();
case SBI_SAVELOAD_START: this->saving = true; break;
case SBI_SAVELOAD_FINISH: this->saving = false; break;
case SBI_SHOW_TICKER: this->ticker_scroll = 0; break;
case SBI_SHOW_REMINDER: this->reminder_timeout.Reset(); break;
case SBI_NEWS_DELETED:
this->ticker_scroll = TICKER_STOP; // reset ticker ...
this->reminder_timeout.Abort(); // ... and reminder
break;
}
}
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
{
switch (widget) {
case WID_S_MIDDLE: ShowLastNewsMessage(); break;
case WID_S_RIGHT: if (_local_company != COMPANY_SPECTATOR) ShowCompanyFinances(_local_company); break;
default: ResetObjectToPlace();
}
}
/** Move information on the ticker slowly from one side to the other. */
IntervalTimer<TimerWindow> ticker_scroll_interval = {std::chrono::milliseconds(15), [this](uint count) {
if (_pause_mode != PM_UNPAUSED) return;
if (this->ticker_scroll < TICKER_STOP) {
this->ticker_scroll += count;
this->SetWidgetDirty(WID_S_MIDDLE);
}
}};
TimeoutTimer<TimerWindow> reminder_timeout = {REMINDER_START, [this]() {
this->SetWidgetDirty(WID_S_MIDDLE);
}};
IntervalTimer<TimerGameCalendar> daily_interval = {{TimerGameCalendar::DAY, TimerGameCalendar::Priority::NONE}, [this](auto) {
this->SetWidgetDirty(WID_S_LEFT);
}};
};
static constexpr NWidgetPart _nested_main_status_widgets[] = {
NWidget(NWID_HORIZONTAL),
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),
EndContainer(),
};
static WindowDesc _main_status_desc(__FILE__, __LINE__,
WDP_MANUAL, nullptr, 0, 0,
WC_STATUS_BAR, WC_NONE,
WDF_NO_FOCUS | WDF_NO_CLOSE,
std::begin(_nested_main_status_widgets), std::end(_nested_main_status_widgets)
);
/**
* Checks whether the news ticker is currently being used.
*/
bool IsNewsTickerShown()
{
const StatusBarWindow *w = dynamic_cast<StatusBarWindow*>(FindWindowById(WC_STATUS_BAR, 0));
return w != nullptr && w->ticker_scroll < StatusBarWindow::TICKER_STOP;
}
/**
* Show our status bar.
*/
void ShowStatusBar()
{
new StatusBarWindow(&_main_status_desc);
}
|