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