# subsidy_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: subsidy_gui.cpp 18966 2010-01-30 18:34:48Z frosch $ */
    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   SubsidyListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00035   {
    00036     this->InitNested(desc, window_number);
    00037     this->OnInvalidateData(0);
    00038   }
    00039
    00040   virtual void OnClick(Point pt, int widget, int click_count)
    00041   {
    00042     if (widget != SLW_PANEL) return;
    00043
    00044     int y = (pt.y - this->GetWidget<NWidgetBase>(SLW_PANEL)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height;
    00045     if (!IsInsideMM(y, 0, this->vscroll.GetCapacity())) return;
    00046
    00047     y += this->vscroll.GetPosition();
    00048
    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
    00109   virtual void OnPaint()
    00110   {
    00111     this->DrawWidgets();
    00112   }
    00113
    00118   uint CountLines()
    00119   {
    00120     /* Count number of (non) awarded subsidies */
    00121     uint num_awarded = 0;
    00122     uint num_not_awarded = 0;
    00123     const Subsidy *s;
    00124     FOR_ALL_SUBSIDIES(s) {
    00125       if (!s->IsAwarded()) {
    00126         num_not_awarded++;
    00127       } else {
    00128         num_awarded++;
    00129       }
    00130     }
    00131
    00132     /* Count the 'none' lines */
    00133     if (num_awarded     == 0) num_awarded = 1;
    00134     if (num_not_awarded == 0) num_not_awarded = 1;
    00135
    00136     /* Offered, accepted and an empty line before the accepted ones. */
    00137     return 3 + num_awarded + num_not_awarded;
    00138   }
    00139
    00140   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00141   {
    00142     if (widget != SLW_PANEL) return;
    00143     Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
    00144
    00145     resize->height = d.height;
    00146
    00147     d.height *= 5;
    00148     d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
    00149     d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00150     *size = maxdim(*size, d);
    00151   }
    00152
    00153   virtual void DrawWidget(const Rect &r, int widget) const
    00154   {
    00155     if (widget != SLW_PANEL) return;
    00156
    00157     YearMonthDay ymd;
    00158     ConvertDateToYMD(_date, &ymd);
    00159
    00160     int right = r.right - WD_FRAMERECT_RIGHT;
    00161     int y = r.top + WD_FRAMERECT_TOP;
    00162     int x = r.left + WD_FRAMERECT_LEFT;
    00163
    00164     int pos = -this->vscroll.GetPosition();
    00165     const int cap = this->vscroll.GetCapacity();
    00166
    00167     /* Section for drawing the offered subisidies */
    00168     if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE);
    00169     pos++;
    00170
    00171     uint num = 0;
    00172     const Subsidy *s;
    00173     FOR_ALL_SUBSIDIES(s) {
    00174       if (!s->IsAwarded()) {
    00175         if (IsInsideMM(pos, 0, cap)) {
    00176           /* Displays the two offered towns */
    00177           SetupSubsidyDecodeParam(s, 1);
    00178           SetDParam(7, _date - ymd.day + s->remaining * 32);
    00179           DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO);
    00180         }
    00181         pos++;
    00182         num++;
    00183       }
    00184     }
    00185
    00186     if (num == 0) {
    00187       if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
    00188       pos++;
    00189     }
    00190
    00191     /* Section for drawing the already granted subisidies */
    00192     pos++;
    00193     if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE);
    00194     pos++;
    00195     num = 0;
    00196
    00197     FOR_ALL_SUBSIDIES(s) {
    00198       if (s->IsAwarded()) {
    00199         if (IsInsideMM(pos, 0, cap)) {
    00200           SetupSubsidyDecodeParam(s, 1);
    00201           SetDParam(7, s->awarded);
    00202           SetDParam(8, _date - ymd.day + s->remaining * 32);
    00203
    00204           /* Displays the two connected stations */
    00205           DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
    00206         }
    00207         pos++;
    00208         num++;
    00209       }
    00210     }
    00211
    00212     if (num == 0) {
    00213       if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
    00214       pos++;
    00215     }
    00216   }
    00217
    00218   virtual void OnResize()
    00219   {
    00220     this->vscroll.SetCapacityFromWidget(this, SLW_PANEL);
    00221   }
    00222
    00223   virtual void OnInvalidateData(int data)
    00224   {
    00225     this->vscroll.SetCount(this->CountLines());
    00226   }
    00227 };
    00228
    00229 static const NWidgetPart _nested_subsidies_list_widgets[] = {
    00230   NWidget(NWID_HORIZONTAL),
    00231     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    00232     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00233     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
    00234     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
    00235   EndContainer(),
    00236   NWidget(NWID_HORIZONTAL),
    00237     NWidget(WWT_PANEL, COLOUR_BROWN, SLW_PANEL), SetDataTip(0x0, STR_SUBSIDIES_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetResize(1, 1), EndContainer(),
    00238     NWidget(NWID_VERTICAL),
    00239       NWidget(WWT_SCROLLBAR, COLOUR_BROWN, SLW_SCROLLBAR),
    00240       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
    00241     EndContainer(),
    00242   EndContainer(),
    00243 };
    00244
    00245 static const WindowDesc _subsidies_list_desc(
    00246   WDP_AUTO, 500, 127,
    00247   WC_SUBSIDIES_LIST, WC_NONE,
    00248   0,
    00249   _nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
    00250 );
    00251
    00252
    00253 void ShowSubsidiesList()
    00254 {
    00255   AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
    00256 }