statusbar_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "date_func.h"
00014 #include "gfx_func.h"
00015 #include "news_func.h"
00016 #include "company_func.h"
00017 #include "string_func.h"
00018 #include "strings_func.h"
00019 #include "company_base.h"
00020 #include "tilehighlight_func.h"
00021 #include "news_gui.h"
00022 #include "company_gui.h"
00023 #include "window_gui.h"
00024 #include "saveload/saveload.h"
00025 #include "window_func.h"
00026 #include "statusbar_gui.h"
00027 #include "core/geometry_func.hpp"
00028 
00029 #include "widgets/statusbar_widget.h"
00030 
00031 #include "table/strings.h"
00032 #include "table/sprites.h"
00033 
00034 static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
00035 {
00036   CopyInDParam(0, ni->params, lengthof(ni->params));
00037   StringID str = ni->string_id;
00038 
00039   char buf[512];
00040   GetString(buf, str, lastof(buf));
00041   const char *s = buf;
00042 
00043   char buffer[256];
00044   char *d = buffer;
00045   const char *last = lastof(buffer);
00046 
00047   for (;;) {
00048     WChar c = Utf8Consume(&s);
00049     if (c == 0) {
00050       break;
00051     } else if (c == '\n') {
00052       if (d + 4 >= last) break;
00053       d[0] = d[1] = d[2] = d[3] = ' ';
00054       d += 4;
00055     } else if (IsPrintable(c)) {
00056       if (d + Utf8CharLen(c) >= last) break;
00057       d += Utf8Encode(d, c);
00058     }
00059   }
00060   *d = '\0';
00061 
00062   DrawPixelInfo tmp_dpi;
00063   if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
00064 
00065   int width = GetStringBoundingBox(buffer).width;
00066   int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
00067 
00068   DrawPixelInfo *old_dpi = _cur_dpi;
00069   _cur_dpi = &tmp_dpi;
00070   DrawString(pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
00071   _cur_dpi = old_dpi;
00072 
00073   return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
00074 }
00075 
00076 struct StatusBarWindow : Window {
00077   bool saving;
00078   int ticker_scroll;
00079   int reminder_timeout;
00080 
00081   static const int TICKER_STOP    = 1640; 
00082   static const int REMINDER_START =   91; 
00083   static const int REMINDER_STOP  =    0; 
00084   static const int COUNTER_STEP   =    2; 
00085 
00086   StatusBarWindow(const WindowDesc *desc) : Window()
00087   {
00088     this->ticker_scroll    =   TICKER_STOP;
00089     this->reminder_timeout = REMINDER_STOP;
00090 
00091     this->InitNested(desc);
00092     CLRBITS(this->flags, WF_WHITE_BORDER);
00093     PositionStatusbar(this);
00094   }
00095 
00096   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00097   {
00098     Point pt = { 0, _screen.height - sm_height };
00099     return pt;
00100   }
00101 
00102   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00103   {
00104     Dimension d;
00105     switch (widget) {
00106       case WID_S_LEFT:
00107         SetDParamMaxValue(0, MAX_YEAR * DAYS_IN_YEAR);
00108         d = GetStringBoundingBox(STR_WHITE_DATE_LONG);
00109         break;
00110 
00111       case WID_S_RIGHT: {
00112         int64 max_money = UINT32_MAX;
00113         const Company *c;
00114         FOR_ALL_COMPANIES(c) max_money = max<int64>(c->money, max_money);
00115         SetDParam(0, 100LL * max_money);
00116         d = GetStringBoundingBox(STR_COMPANY_MONEY);
00117         break;
00118       }
00119 
00120       default:
00121         return;
00122     }
00123 
00124     d.width += padding.width;
00125     d.height += padding.height;
00126     *size = maxdim(d, *size);
00127   }
00128 
00129   virtual void DrawWidget(const Rect &r, int widget) const
00130   {
00131     switch (widget) {
00132       case WID_S_LEFT:
00133         /* Draw the date */
00134         SetDParam(0, _date);
00135         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);
00136         break;
00137 
00138       case WID_S_RIGHT: {
00139         /* Draw company money, if any */
00140         const Company *c = Company::GetIfValid(_local_company);
00141         if (c != NULL) {
00142           SetDParam(0, c->money);
00143           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_COMPANY_MONEY, TC_FROMSTRING, SA_HOR_CENTER);
00144         }
00145         break;
00146       }
00147 
00148       case WID_S_MIDDLE:
00149         /* Draw status bar */
00150         if (this->saving) { // true when saving is active
00151           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);
00152         } else if (_do_autosave) {
00153           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_HOR_CENTER);
00154         } else if (_pause_mode != PM_UNPAUSED) {
00155           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_PAUSED, TC_FROMSTRING, SA_HOR_CENTER);
00156         } else if (this->ticker_scroll < TICKER_STOP && FindWindowById(WC_NEWS_WINDOW, 0) == NULL && _statusbar_news_item != NULL && _statusbar_news_item->string_id != 0) {
00157           /* Draw the scrolling news text */
00158           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)) {
00159             InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
00160             if (Company::IsValidID(_local_company)) {
00161               /* This is the default text */
00162               SetDParam(0, _local_company);
00163               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);
00164             }
00165           }
00166         } else {
00167           if (Company::IsValidID(_local_company)) {
00168             /* This is the default text */
00169             SetDParam(0, _local_company);
00170             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);
00171           }
00172         }
00173 
00174         if (this->reminder_timeout > 0) {
00175           Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS);
00176           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);
00177         }
00178         break;
00179     }
00180   }
00181 
00187   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00188   {
00189     if (!gui_scope) return;
00190     switch (data) {
00191       default: NOT_REACHED();
00192       case SBI_SAVELOAD_START:  this->saving = true;  break;
00193       case SBI_SAVELOAD_FINISH: this->saving = false; break;
00194       case SBI_SHOW_TICKER:     this->ticker_scroll = 0; break;
00195       case SBI_SHOW_REMINDER:   this->reminder_timeout = REMINDER_START; break;
00196       case SBI_NEWS_DELETED:
00197         this->ticker_scroll    =   TICKER_STOP; // reset ticker ...
00198         this->reminder_timeout = REMINDER_STOP; // ... and reminder
00199         break;
00200     }
00201   }
00202 
00203   virtual void OnClick(Point pt, int widget, int click_count)
00204   {
00205     switch (widget) {
00206       case WID_S_MIDDLE: ShowLastNewsMessage(); break;
00207       case WID_S_RIGHT:  if (_local_company != COMPANY_SPECTATOR) ShowCompanyFinances(_local_company); break;
00208       default: ResetObjectToPlace();
00209     }
00210   }
00211 
00212   virtual void OnTick()
00213   {
00214     if (_pause_mode != PM_UNPAUSED) return;
00215 
00216     if (this->ticker_scroll < TICKER_STOP) { // Scrolling text
00217       this->ticker_scroll += COUNTER_STEP;
00218       this->SetWidgetDirty(WID_S_MIDDLE);
00219     }
00220 
00221     if (this->reminder_timeout > REMINDER_STOP) { // Red blot to show there are new unread newsmessages
00222       this->reminder_timeout -= COUNTER_STEP;
00223     } else if (this->reminder_timeout < REMINDER_STOP) {
00224       this->reminder_timeout = REMINDER_STOP;
00225       this->SetWidgetDirty(WID_S_MIDDLE);
00226     }
00227   }
00228 };
00229 
00230 static const NWidgetPart _nested_main_status_widgets[] = {
00231   NWidget(NWID_HORIZONTAL),
00232     NWidget(WWT_PANEL, COLOUR_GREY, WID_S_LEFT), SetMinimalSize(140, 12), EndContainer(),
00233     NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_MIDDLE), SetMinimalSize(40, 12), SetDataTip(0x0, STR_STATUSBAR_TOOLTIP_SHOW_LAST_NEWS), SetResize(1, 0),
00234     NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_S_RIGHT), SetMinimalSize(140, 12),
00235   EndContainer(),
00236 };
00237 
00238 static WindowDesc _main_status_desc(
00239   WDP_MANUAL, 640, 12,
00240   WC_STATUS_BAR, WC_NONE,
00241   WDF_NO_FOCUS,
00242   _nested_main_status_widgets, lengthof(_nested_main_status_widgets)
00243 );
00244 
00248 bool IsNewsTickerShown()
00249 {
00250   const StatusBarWindow *w = dynamic_cast<StatusBarWindow*>(FindWindowById(WC_STATUS_BAR, 0));
00251   return w != NULL && w->ticker_scroll < StatusBarWindow::TICKER_STOP;
00252 }
00253 
00254 int16 *_preferred_statusbar_size = &_main_status_desc.default_width; 
00255 
00259 void ShowStatusBar()
00260 {
00261   new StatusBarWindow(&_main_status_desc);
00262 }