OpenTTD/src/subsidy_gui.cpp

236 lines
6.3 KiB
C++
Raw Normal View History

/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file subsidy_gui.cpp GUI for subsidies. */
#include "stdafx.h"
#include "station_base.h"
#include "industry.h"
#include "town.h"
#include "economy_func.h"
#include "cargotype.h"
#include "window_gui.h"
#include "strings_func.h"
#include "date_func.h"
#include "viewport_func.h"
#include "gfx_func.h"
#include "gui.h"
#include "subsidy_func.h"
#include "subsidy_base.h"
#include "table/strings.h"
/** Widget numbers for the subsidy list window. */
enum SubsidyListWidgets {
SLW_CLOSEBOX,
SLW_CAPTION,
SLW_STICKYBOX,
SLW_PANEL,
SLW_SCROLLBAR,
SLW_RESIZEBOX,
};
struct SubsidyListWindow : Window {
SubsidyListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
{
this->InitNested(desc, window_number);
}
virtual void OnClick(Point pt, int widget)
{
if (widget != SLW_PANEL) return;
int y = pt.y - this->nested_array[SLW_PANEL]->pos_y - FONT_HEIGHT_NORMAL - WD_FRAMERECT_TOP; // Skip 'subsidies on offer' line
if (y < 0) return;
uint num = 0;
const Subsidy *s;
FOR_ALL_SUBSIDIES(s) {
if (!s->IsAwarded()) {
y -= FONT_HEIGHT_NORMAL;
if (y < 0) {
this->HandleClick(s);
return;
}
num++;
}
}
if (num == 0) {
y -= FONT_HEIGHT_NORMAL; // "None"
if (y < 0) return;
}
y -= 2 * FONT_HEIGHT_NORMAL; // "Services already subsidised:"
if (y < 0) return;
FOR_ALL_SUBSIDIES(s) {
if (s->IsAwarded()) {
y -= FONT_HEIGHT_NORMAL;
if (y < 0) {
this->HandleClick(s);
return;
}
}
}
}
void HandleClick(const Subsidy *s)
{
/* determine src coordinate for subsidy and try to scroll to it */
TileIndex xy;
switch (s->src_type) {
case ST_INDUSTRY: xy = Industry::Get(s->src)->xy; break;
case ST_TOWN: xy = Town::Get(s->src)->xy; break;
default: NOT_REACHED();
}
if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
if (_ctrl_pressed) ShowExtraViewPortWindow(xy);
/* otherwise determine dst coordinate for subsidy and scroll to it */
switch (s->dst_type) {
case ST_INDUSTRY: xy = Industry::Get(s->dst)->xy; break;
case ST_TOWN: xy = Town::Get(s->dst)->xy; break;
default: NOT_REACHED();
}
if (_ctrl_pressed) {
ShowExtraViewPortWindow(xy);
} else {
ScrollMainWindowToTile(xy);
}
}
}
virtual void OnPaint()
{
this->DrawWidgets();
}
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *resize)
{
if (widget != SLW_PANEL) return;
Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
/* Count number of (non) awarded subsidies */
uint num_awarded = 0;
uint num_not_awarded = 0;
const Subsidy *s;
FOR_ALL_SUBSIDIES(s) {
if (!s->IsAwarded()) {
num_not_awarded++;
} else {
num_awarded++;
}
}
/* Count the 'none' lines */
if (num_awarded == 0) num_awarded = 1;
if (num_not_awarded == 0) num_not_awarded = 1;
/* Number of lines to show. */
uint lines = 3; // Offered, accepted and an empty line before the accepted ones.
/* The lines with actual subsidies with a minimum of 4 */
lines += max(num_awarded + num_not_awarded, 4U);
d.height *= lines;
d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
*size = maxdim(*size, d);
}
virtual void DrawWidget(const Rect &r, int widget) const
{
if (widget != SLW_PANEL) return;
YearMonthDay ymd;
ConvertDateToYMD(_date, &ymd);
int right = r.right - WD_FRAMERECT_RIGHT;
int y = r.top + WD_FRAMERECT_TOP;
int x = r.left + WD_FRAMERECT_LEFT;
/* Section for drawing the offered subisidies */
DrawString(x, right, y, STR_SUBSIDIES_OFFERED_TITLE);
y += FONT_HEIGHT_NORMAL;
uint num = 0;
const Subsidy *s;
FOR_ALL_SUBSIDIES(s) {
if (!s->IsAwarded()) {
/* Displays the two offered towns */
SetupSubsidyDecodeParam(s, 1);
SetDParam(7, _date - ymd.day + s->remaining * 32);
DrawString(x, right, y, STR_SUBSIDIES_OFFERED_FROM_TO);
y += FONT_HEIGHT_NORMAL;
num++;
}
}
if (num == 0) {
DrawString(x, right, y, STR_SUBSIDIES_NONE);
y += FONT_HEIGHT_NORMAL;
}
/* Section for drawing the already granted subisidies */
y += FONT_HEIGHT_NORMAL;
DrawString(x, right, y, STR_SUBSIDIES_SUBSIDISED_TITLE);
y += FONT_HEIGHT_NORMAL;
num = 0;
FOR_ALL_SUBSIDIES(s) {
if (s->IsAwarded()) {
SetupSubsidyDecodeParam(s, 1);
SetDParam(7, s->awarded);
SetDParam(8, _date - ymd.day + s->remaining * 32);
/* Displays the two connected stations */
DrawString(x, right, y, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
y += FONT_HEIGHT_NORMAL;
num++;
}
}
if (num == 0) DrawString(x, right, y, STR_SUBSIDIES_NONE);
}
};
static const NWidgetPart _nested_subsidies_list_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_BROWN, SLW_CLOSEBOX),
NWidget(WWT_CAPTION, COLOUR_BROWN, SLW_CAPTION), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
NWidget(WWT_STICKYBOX, COLOUR_BROWN, SLW_STICKYBOX),
EndContainer(),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_PANEL, COLOUR_BROWN, SLW_PANEL), SetDataTip(0x0, STR_SUBSIDIES_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetResize(1, 1), EndContainer(),
NWidget(NWID_VERTICAL),
NWidget(WWT_SCROLLBAR, COLOUR_BROWN, SLW_SCROLLBAR),
NWidget(WWT_RESIZEBOX, COLOUR_BROWN, SLW_RESIZEBOX),
EndContainer(),
EndContainer(),
};
static const WindowDesc _subsidies_list_desc(
WDP_AUTO, WDP_AUTO, 320, 127, 500, 127,
WC_SUBSIDIES_LIST, WC_NONE,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON | WDF_RESIZABLE,
NULL, _nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
);
void ShowSubsidiesList()
{
AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
}