subsidy_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 "industry.h"
00014 #include "town.h"
00015 #include "window_gui.h"
00016 #include "strings_func.h"
00017 #include "date_func.h"
00018 #include "viewport_func.h"
00019 #include "gfx_func.h"
00020 #include "gui.h"
00021 #include "subsidy_func.h"
00022 #include "subsidy_base.h"
00023 #include "core/geometry_func.hpp"
00024 
00025 #include "table/strings.h"
00026 
00028 enum SubsidyListWidgets {
00029   SLW_PANEL,
00030   SLW_SCROLLBAR,
00031 };
00032 
00033 struct SubsidyListWindow : Window {
00034   Scrollbar *vscroll;
00035 
00036   SubsidyListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00037   {
00038     this->CreateNestedTree(desc);
00039     this->vscroll = this->GetScrollbar(SLW_SCROLLBAR);
00040     this->FinishInitNested(desc, window_number);
00041     this->OnInvalidateData(0);
00042   }
00043 
00044   virtual void OnClick(Point pt, int widget, int click_count)
00045   {
00046     if (widget != SLW_PANEL) return;
00047 
00048     int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SLW_PANEL, WD_FRAMERECT_TOP);
00049     int num = 0;
00050     const Subsidy *s;
00051     FOR_ALL_SUBSIDIES(s) {
00052       if (!s->IsAwarded()) {
00053         y--;
00054         if (y == 0) {
00055           this->HandleClick(s);
00056           return;
00057         }
00058         num++;
00059       }
00060     }
00061 
00062     if (num == 0) {
00063       y--; // "None"
00064       if (y < 0) return;
00065     }
00066 
00067     y -= 2; // "Services already subsidised:"
00068     if (y < 0) return;
00069 
00070     FOR_ALL_SUBSIDIES(s) {
00071       if (s->IsAwarded()) {
00072         y--;
00073         if (y == 0) {
00074           this->HandleClick(s);
00075           return;
00076         }
00077       }
00078     }
00079   }
00080 
00081   void HandleClick(const Subsidy *s)
00082   {
00083     /* determine src coordinate for subsidy and try to scroll to it */
00084     TileIndex xy;
00085     switch (s->src_type) {
00086       case ST_INDUSTRY: xy = Industry::Get(s->src)->location.tile; break;
00087       case ST_TOWN:     xy =     Town::Get(s->src)->xy; break;
00088       default: NOT_REACHED();
00089     }
00090 
00091     if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
00092       if (_ctrl_pressed) ShowExtraViewPortWindow(xy);
00093 
00094       /* otherwise determine dst coordinate for subsidy and scroll to it */
00095       switch (s->dst_type) {
00096         case ST_INDUSTRY: xy = Industry::Get(s->dst)->location.tile; break;
00097         case ST_TOWN:     xy =     Town::Get(s->dst)->xy; break;
00098         default: NOT_REACHED();
00099       }
00100 
00101       if (_ctrl_pressed) {
00102         ShowExtraViewPortWindow(xy);
00103       } else {
00104         ScrollMainWindowToTile(xy);
00105       }
00106     }
00107   }
00108 
00113   uint CountLines()
00114   {
00115     /* Count number of (non) awarded subsidies */
00116     uint num_awarded = 0;
00117     uint num_not_awarded = 0;
00118     const Subsidy *s;
00119     FOR_ALL_SUBSIDIES(s) {
00120       if (!s->IsAwarded()) {
00121         num_not_awarded++;
00122       } else {
00123         num_awarded++;
00124       }
00125     }
00126 
00127     /* Count the 'none' lines */
00128     if (num_awarded     == 0) num_awarded = 1;
00129     if (num_not_awarded == 0) num_not_awarded = 1;
00130 
00131     /* Offered, accepted and an empty line before the accepted ones. */
00132     return 3 + num_awarded + num_not_awarded;
00133   }
00134 
00135   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00136   {
00137     if (widget != SLW_PANEL) return;
00138     Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
00139 
00140     resize->height = d.height;
00141 
00142     d.height *= 5;
00143     d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
00144     d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00145     *size = maxdim(*size, d);
00146   }
00147 
00148   virtual void DrawWidget(const Rect &r, int widget) const
00149   {
00150     if (widget != SLW_PANEL) return;
00151 
00152     YearMonthDay ymd;
00153     ConvertDateToYMD(_date, &ymd);
00154 
00155     int right = r.right - WD_FRAMERECT_RIGHT;
00156     int y = r.top + WD_FRAMERECT_TOP;
00157     int x = r.left + WD_FRAMERECT_LEFT;
00158 
00159     int pos = -this->vscroll->GetPosition();
00160     const int cap = this->vscroll->GetCapacity();
00161 
00162     /* Section for drawing the offered subisidies */
00163     if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE);
00164     pos++;
00165 
00166     uint num = 0;
00167     const Subsidy *s;
00168     FOR_ALL_SUBSIDIES(s) {
00169       if (!s->IsAwarded()) {
00170         if (IsInsideMM(pos, 0, cap)) {
00171           /* Displays the two offered towns */
00172           SetupSubsidyDecodeParam(s, true);
00173           SetDParam(7, _date - ymd.day + s->remaining * 32);
00174           DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO);
00175         }
00176         pos++;
00177         num++;
00178       }
00179     }
00180 
00181     if (num == 0) {
00182       if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
00183       pos++;
00184     }
00185 
00186     /* Section for drawing the already granted subisidies */
00187     pos++;
00188     if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE);
00189     pos++;
00190     num = 0;
00191 
00192     FOR_ALL_SUBSIDIES(s) {
00193       if (s->IsAwarded()) {
00194         if (IsInsideMM(pos, 0, cap)) {
00195           SetupSubsidyDecodeParam(s, true);
00196           SetDParam(7, s->awarded);
00197           SetDParam(8, _date - ymd.day + s->remaining * 32);
00198 
00199           /* Displays the two connected stations */
00200           DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
00201         }
00202         pos++;
00203         num++;
00204       }
00205     }
00206 
00207     if (num == 0) {
00208       if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
00209       pos++;
00210     }
00211   }
00212 
00213   virtual void OnResize()
00214   {
00215     this->vscroll->SetCapacityFromWidget(this, SLW_PANEL);
00216   }
00217 
00223   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00224   {
00225     if (!gui_scope) return;
00226     this->vscroll->SetCount(this->CountLines());
00227   }
00228 };
00229 
00230 static const NWidgetPart _nested_subsidies_list_widgets[] = {
00231   NWidget(NWID_HORIZONTAL),
00232     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00233     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00234     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00235     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00236   EndContainer(),
00237   NWidget(NWID_HORIZONTAL),
00238     NWidget(WWT_PANEL, COLOUR_BROWN, SLW_PANEL), SetDataTip(0x0, STR_SUBSIDIES_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetResize(1, 1), SetScrollbar(SLW_SCROLLBAR), EndContainer(),
00239     NWidget(NWID_VERTICAL),
00240       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, SLW_SCROLLBAR),
00241       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00242     EndContainer(),
00243   EndContainer(),
00244 };
00245 
00246 static const WindowDesc _subsidies_list_desc(
00247   WDP_AUTO, 500, 127,
00248   WC_SUBSIDIES_LIST, WC_NONE,
00249   0,
00250   _nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
00251 );
00252 
00253 
00254 void ShowSubsidiesList()
00255 {
00256   AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
00257 }

Generated on Thu Apr 14 00:48:20 2011 for OpenTTD by  doxygen 1.6.1