statusbar_gui.cpp

Go to the documentation of this file.
00001 /* $Id: statusbar_gui.cpp 21331 2010-11-26 15:22:18Z alberth $ */
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 "saveload/saveload.h"
00026 #include "window_func.h"
00027 #include "statusbar_gui.h"
00028 #include "core/geometry_func.hpp"
00029 
00030 #include "table/strings.h"
00031 #include "table/sprites.h"
00032 
00033 static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
00034 {
00035   CopyInDParam(0, ni->params, lengthof(ni->params));
00036   StringID str = ni->string_id;
00037 
00038   char buf[512];
00039   GetString(buf, str, lastof(buf));
00040   const char *s = buf;
00041 
00042   char buffer[256];
00043   char *d = buffer;
00044   const char *last = lastof(buffer);
00045 
00046   for (;;) {
00047     WChar c = Utf8Consume(&s);
00048     if (c == 0) {
00049       break;
00050     } else if (c == '\n') {
00051       if (d + 4 >= last) break;
00052       d[0] = d[1] = d[2] = d[3] = ' ';
00053       d += 4;
00054     } else if (IsPrintable(c)) {
00055       if (d + Utf8CharLen(c) >= last) break;
00056       d += Utf8Encode(d, c);
00057     }
00058   }
00059   *d = '\0';
00060 
00061   DrawPixelInfo tmp_dpi;
00062   if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
00063 
00064   int width = GetStringBoundingBox(buffer).width;
00065   int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
00066 
00067   DrawPixelInfo *old_dpi = _cur_dpi;
00068   _cur_dpi = &tmp_dpi;
00069   DrawString(pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
00070   _cur_dpi = old_dpi;
00071 
00072   return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
00073 }
00074 
00075 enum StatusbarWidget {
00076   SBW_LEFT,   
00077   SBW_MIDDLE, 
00078   SBW_RIGHT,  
00079 };
00080 
00081 struct StatusBarWindow : Window {
00082   bool saving;
00083   int ticker_scroll;
00084   int reminder_timeout;
00085 
00086   static const int TICKER_STOP    = 1640; 
00087   static const int REMINDER_START =   91; 
00088   static const int REMINDER_STOP  =    0; 
00089   static const int COUNTER_STEP   =    2; 
00090 
00091   StatusBarWindow(const WindowDesc *desc) : Window()
00092   {
00093     CLRBITS(this->flags4, WF_WHITE_BORDER_MASK);
00094     this->ticker_scroll    =   TICKER_STOP;
00095     this->reminder_timeout = REMINDER_STOP;
00096 
00097     this->InitNested(desc);
00098     PositionStatusbar(this);
00099   }
00100 
00101   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00102   {
00103     Point pt = { 0, _screen.height - sm_height };
00104     return pt;
00105   }
00106 
00107   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00108   {
00109     Dimension d;
00110     switch (widget) {
00111       case SBW_LEFT:
00112         SetDParam(0, MAX_YEAR * DAYS_IN_YEAR);
00113         d = GetStringBoundingBox(STR_WHITE_DATE_LONG);
00114         break;
00115 
00116       case SBW_RIGHT: {
00117         int64 max_money = UINT32_MAX;
00118         const Company *c;
00119         FOR_ALL_COMPANIES(c) max_money = max<int64>(c->money, max_money);
00120         SetDParam(0, 100LL * max_money);
00121         d = GetStringBoundingBox(STR_COMPANY_MONEY);
00122         break;
00123       }
00124 
00125       default:
00126         return;
00127     }
00128 
00129     d.width += padding.width;
00130     d.height += padding.height;
00131     *size = maxdim(d, *size);
00132   }
00133 
00134   virtual void DrawWidget(const Rect &r, int widget) const
00135   {
00136     switch (widget) {
00137       case SBW_LEFT:
00138         /* Draw the date */
00139         SetDParam(0, _date);
00140         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_HOR_CENTER);
00141         break;
00142 
00143       case SBW_RIGHT: {
00144         /* Draw company money, if any */
00145         const Company *c = Company::GetIfValid(_local_company);
00146         if (c != NULL) {
00147           SetDParam(0, c->money);
00148           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_COMPANY_MONEY, TC_FROMSTRING, SA_HOR_CENTER);
00149         }
00150         break;
00151       }
00152 
00153       case SBW_MIDDLE:
00154         /* Draw status bar */
00155         if (this->saving) { // true when saving is active
00156           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);
00157         } else if (_do_autosave) {
00158           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_HOR_CENTER);
00159         } else if (_pause_mode != PM_UNPAUSED) {
00160           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_PAUSED, TC_FROMSTRING, SA_HOR_CENTER);
00161         } else if (this->ticker_scroll < TICKER_STOP && FindWindowById(WC_NEWS_WINDOW, 0) == NULL && _statusbar_news_item != NULL && _statusbar_news_item->string_id != 0) {
00162           /* Draw the scrolling news text */
00163           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)) {
00164             InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
00165             if (Company::IsValidID(_local_company)) {
00166               /* This is the default text */
00167               SetDParam(0, _local_company);
00168               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);
00169             }
00170           }
00171         } else {
00172           if (Company::IsValidID(_local_company)) {
00173             /* This is the default text */
00174             SetDParam(0, _local_company);
00175             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);
00176           }
00177         }
00178 
00179         if (this->reminder_timeout > 0) {
00180           Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS);
00181           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);
00182         }
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, int click_count)
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 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 int16 *_preferred_statusbar_size = &_main_status_desc.default_width; 
00254 
00255 void ShowStatusBar()
00256 {
00257   new StatusBarWindow(&_main_status_desc);
00258 }

Generated on Thu Jan 20 22:57:41 2011 for OpenTTD by  doxygen 1.6.1