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

Generated on Sat Dec 26 20:06:05 2009 for OpenTTD by  doxygen 1.5.6