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 "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 = (_current_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 (_current_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   static const int TICKER_STOP    = 1640; 
00086   static const int REMINDER_START =   91; 
00087   static const int REMINDER_STOP  =    0; 
00088   static const int COUNTER_STEP   =    2; 
00089 
00090   StatusBarWindow(const WindowDesc *desc) : Window()
00091   {
00092     CLRBITS(this->flags4, WF_WHITE_BORDER_MASK);
00093     this->ticker_scroll    =   TICKER_STOP;
00094     this->reminder_timeout = REMINDER_STOP;
00095 
00096     this->InitNested(desc);
00097     PositionStatusbar(this);
00098   }
00099 
00100   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00101   {
00102     Point pt = { 0, _screen.height - sm_height };
00103     return pt;
00104   }
00105 
00106   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00107   {
00108     Dimension d;
00109     switch (widget) {
00110       case SBW_LEFT:
00111         SetDParam(0, MAX_YEAR * DAYS_IN_YEAR);
00112         d = GetStringBoundingBox(STR_WHITE_DATE_LONG);
00113         break;
00114 
00115       case SBW_RIGHT: {
00116         int64 max_money = UINT32_MAX;
00117         const Company *c;
00118         FOR_ALL_COMPANIES(c) max_money = max<int64>(c->money, max_money);
00119         SetDParam(0, 100LL * max_money);
00120         d = GetStringBoundingBox(STR_COMPANY_MONEY);
00121         break;
00122       }
00123 
00124       default:
00125         return;
00126     }
00127 
00128     d.width += padding.width;
00129     d.height += padding.height;
00130     *size = maxdim(d, *size);
00131   }
00132 
00133   virtual void DrawWidget(const Rect &r, int widget) const
00134   {
00135     switch (widget) {
00136       case SBW_LEFT:
00137         /* Draw the date */
00138         SetDParam(0, _date);
00139         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);
00140         break;
00141 
00142       case SBW_RIGHT: {
00143         /* Draw company money, if any */
00144         const Company *c = Company::GetIfValid(_local_company);
00145         if (c != NULL) {
00146           SetDParam(0, c->money);
00147           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_COMPANY_MONEY, TC_FROMSTRING, SA_HOR_CENTER);
00148         }
00149         break;
00150       }
00151 
00152       case SBW_MIDDLE:
00153         /* Draw status bar */
00154         if (this->saving) { // true when saving is active
00155           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);
00156         } else if (_do_autosave) {
00157           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_HOR_CENTER);
00158         } else if (_pause_mode != PM_UNPAUSED) {
00159           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_PAUSED, TC_FROMSTRING, SA_HOR_CENTER);
00160         } else if (this->ticker_scroll < TICKER_STOP && FindWindowById(WC_NEWS_WINDOW, 0) == NULL && _statusbar_news_item != NULL && _statusbar_news_item->string_id != 0) {
00161           /* Draw the scrolling news text */
00162           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)) {
00163             InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
00164             if (Company::IsValidID(_local_company)) {
00165               /* This is the default text */
00166               SetDParam(0, _local_company);
00167               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);
00168             }
00169           }
00170         } else {
00171           if (Company::IsValidID(_local_company)) {
00172             /* This is the default text */
00173             SetDParam(0, _local_company);
00174             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);
00175           }
00176         }
00177 
00178         if (this->reminder_timeout > 0) {
00179           Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS);
00180           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);
00181         }
00182         break;
00183     }
00184   }
00185 
00191   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00192   {
00193     if (!gui_scope) return;
00194     switch (data) {
00195       default: NOT_REACHED();
00196       case SBI_SAVELOAD_START:  this->saving = true;  break;
00197       case SBI_SAVELOAD_FINISH: this->saving = false; break;
00198       case SBI_SHOW_TICKER:     this->ticker_scroll = 0; break;
00199       case SBI_SHOW_REMINDER:   this->reminder_timeout = REMINDER_START; break;
00200       case SBI_NEWS_DELETED:
00201         this->ticker_scroll    =   TICKER_STOP; // reset ticker ...
00202         this->reminder_timeout = REMINDER_STOP; // ... and reminder
00203         break;
00204     }
00205   }
00206 
00207   virtual void OnClick(Point pt, int widget, int click_count)
00208   {
00209     switch (widget) {
00210       case SBW_MIDDLE: ShowLastNewsMessage(); break;
00211       case SBW_RIGHT:  if (_local_company != COMPANY_SPECTATOR) ShowCompanyFinances(_local_company); break;
00212       default: ResetObjectToPlace();
00213     }
00214   }
00215 
00216   virtual void OnTick()
00217   {
00218     if (_pause_mode != PM_UNPAUSED) return;
00219 
00220     if (this->ticker_scroll < TICKER_STOP) { // Scrolling text
00221       this->ticker_scroll += COUNTER_STEP;
00222       this->SetWidgetDirty(SBW_MIDDLE);
00223     }
00224 
00225     if (this->reminder_timeout > REMINDER_STOP) { // Red blot to show there are new unread newsmessages
00226       this->reminder_timeout -= COUNTER_STEP;
00227     } else if (this->reminder_timeout < REMINDER_STOP) {
00228       this->reminder_timeout = REMINDER_STOP;
00229       this->SetWidgetDirty(SBW_MIDDLE);
00230     }
00231   }
00232 };
00233 
00234 static const NWidgetPart _nested_main_status_widgets[] = {
00235   NWidget(NWID_HORIZONTAL),
00236     NWidget(WWT_PANEL, COLOUR_GREY, SBW_LEFT), SetMinimalSize(140, 12), EndContainer(),
00237     NWidget(WWT_PUSHBTN, COLOUR_GREY, SBW_MIDDLE), SetMinimalSize(40, 12), SetDataTip(0x0, STR_STATUSBAR_TOOLTIP_SHOW_LAST_NEWS), SetResize(1, 0),
00238     NWidget(WWT_PUSHBTN, COLOUR_GREY, SBW_RIGHT), SetMinimalSize(140, 12),
00239   EndContainer(),
00240 };
00241 
00242 static WindowDesc _main_status_desc(
00243   WDP_MANUAL, 640, 12,
00244   WC_STATUS_BAR, WC_NONE,
00245   WDF_UNCLICK_BUTTONS | WDF_NO_FOCUS,
00246   _nested_main_status_widgets, lengthof(_nested_main_status_widgets)
00247 );
00248 
00252 bool IsNewsTickerShown()
00253 {
00254   const StatusBarWindow *w = dynamic_cast<StatusBarWindow*>(FindWindowById(WC_STATUS_BAR, 0));
00255   return w != NULL && w->ticker_scroll < StatusBarWindow::TICKER_STOP;
00256 }
00257 
00258 int16 *_preferred_statusbar_size = &_main_status_desc.default_width; 
00259 
00263 void ShowStatusBar()
00264 {
00265   new StatusBarWindow(&_main_status_desc);
00266 }

Generated on Fri Jun 3 05:18:58 2011 for OpenTTD by  doxygen 1.6.1